【发布时间】:2012-10-27 01:56:15
【问题描述】:
我正在为客户进行一些音频编程,但遇到了一个我不明白的问题。
我有一个由 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);
}
这一次,信号出现了奇怪的音频失真。
我只是看不出我做错了什么!任何建议将不胜感激。
【问题讨论】:
-
我认为这是音频问题,而不是 C 问题。尝试记录所有变量的值,看看是否有帮助。总是在下结论之前先登录。
-
这可能是线程问题。检查您的代码是否一直在同一个线程上运行。即,NSLog(@"thread: %@", [NSThread currentThread]);。如果没有,您需要使用锁/互斥锁来保护您的全局。
标签: objective-c ios c audio core-audio