我正在为客户做一些音频编程,我遇到了一个我不明白的问题。
我有一个由 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);
}
这一次,信号出现了奇怪的音频失真。
我只是看不出我做错了什么!任何建议将不胜感激。
最佳答案
看来你所做的是无意中引入的phaser影响您的音频。
这是因为您只延迟了音频的一个 channel ,所以结果是左 channel 比右 channel 延迟了一帧。这将导致一些奇怪的频率取消/放大,这符合您对“奇怪的音频失真”的描述。
尝试将效果应用于两个 channel :
AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData;
AudioSampleType *outB = (AudioSampleType *)ioData->mBuffers[1].mData;
// apply the same effect to outB as you did to outA
这假设您正在使用立体声音频(即 ioData->mNumberBuffers == 2
)
作为风格问题,在渲染回调中使用像您的 last
变量这样的全局变量是 (IMO) 一个坏主意。使用 inRefCon
传递适当的上下文(作为单个变量或必要时作为结构)。不过,这可能与您遇到的问题无关。
关于objective-c - 未保留 Objective C 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096588/