我正在为客户做一些音频编程,但遇到了一个我不明白的问题。
我有一个由 CoreAudio 反复调用的渲染回调。在这个回调中,我有以下内容:
// Get the audio sample data
AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData;
Float32 data;
// Loop over the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++) {
// Convert from SInt16 to Float32 just to prove it's possible
data = (Float32) outA[frame] / (Float32) 32768;
// Convert back to SInt16 to show that everything works as expected
outA[frame] = (SInt16) round(next * 32768);
}
这按预期工作,表明没有任何意外的舍入错误。
我想做的下一件事是添加一个小的延迟。我在类中添加了一个全局变量:
即在@implementation 行下方
Float32 last = 0;
然后我使用这个变量来获得一帧延迟:
// Get the audio sample data
AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData;
Float32 data;
Float32 next;
// Loop over the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++) {
// Convert from SInt16 to Float32 just to prove it's possible
data = (Float32) outA[frame] / (Float32) 32768;
next = last;
last = data;
// Convert back to SInt16 to show that everything works as expected
outA[frame] = (SInt16) round(next * 32768);
}
这一次,信号上出现了奇怪的音频失真。
我只是看不出我做错了什么!任何建议将不胜感激。