操作 AudioQueueBuffer 音频数据会产生噪音
我尝试使用 SpeakHere 示例应用程序作为起点来实现 iPhone 声音处理应用程序。该应用程序涉及在播放期间操作缓冲的音频样本。当我将样本乘以一个小数(例如 0.9)时,我会得到噪声。最奇怪的是,当将样本乘以整数(如 1.0、2.0)或对其进行加减时,播放声音会按预期播放。
我使用以下代码片段在 AudioPlayer.m 的playbackCallback 函数中执行此操作:
//------------------
//If buffer samples are multiplied by 0.9 we get noise. If we multiply it by 1.0 or add to it everything's fine.
short *buffer = (short *)calloc(1, numPackets * 2);
memmove(buffer, bufferReference->mAudioData, numPackets * 2);
for (int i = 0; i < numPackets; i++)
{
buffer[i] *= 0.9;
//buffer[i] *= 1.0;
//buffer[i] += 10.0;
}
memmove(bufferReference->mAudioData, buffer, numPackets * 2);
free(buffer);
buffer = NULL;
//-------------------
可以在此处下载完整的项目: http://depositfiles.com/files/lmnkq68n8
有人能指出我做错了什么吗?我已经为此苦苦挣扎了几天,我完全迷失了。
谢谢你!
I try to implement an iPhone sound processing app using SpeakHere sample app as a starting point. The app involves manipulating buffered audio samples during playback. When I multiply the samples by a fractional number (0.9 for instance) I get noise as a result. The strangest thing about it is that when multiplying samples by whole numbers (like 1.0, 2.0) or adding or subtracting from them the playback sounds as expected.
I'm doing it in the playbackCallback function of AudioPlayer.m with the following code snippet:
//------------------
//If buffer samples are multiplied by 0.9 we get noise. If we multiply it by 1.0 or add to it everything's fine.
short *buffer = (short *)calloc(1, numPackets * 2);
memmove(buffer, bufferReference->mAudioData, numPackets * 2);
for (int i = 0; i < numPackets; i++)
{
buffer[i] *= 0.9;
//buffer[i] *= 1.0;
//buffer[i] += 10.0;
}
memmove(bufferReference->mAudioData, buffer, numPackets * 2);
free(buffer);
buffer = NULL;
//-------------------
The full project could be downloaded here: http://depositfiles.com/files/lmnkq68n8
Could anyone point me to what am I doing wrong? I've been struggling with it for a couple of days already and I'm completely lost.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法下载您的项目(文件托管程序总是告诉我所有插槽都已满),但是通过阅读您发布的代码片段,您似乎将带符号的 16 位整数(短)值乘以 0.9(浮点值) )。这会导致隐式强制转换为short。因此,您会丢失小数点后面的所有内容,从而导致信号中出现混叠效应。
更新:
我检查了你的代码,但无法弄清楚噪音来自哪里。
为什么使用 memmove 来操作缓冲区?
您可以直接访问缓冲区:
我也很想知道是什么原因导致了噪音。因此,如果您发现了这一点,请在此处发布您的解决方案。(也许 Core Audio 邮件列表中的某个人 可以提供帮助 - 他们真的很有帮助)
如果您只需要改变增益,您可以简单地使用 AudioQueueSetParameter。
另一种选择是使用 RemoteIO 音频单元 而不是 AudioQueueServices。
I couldn't download your project (the file hoster always tells me that all slots are full), but by reading over the snippet you posted, it seems that you are multiplying signed 16bit integer (short) values with 0.9 (a floating point value). This leads to an implicit cast to short. So you loose everything behind the decimal point, which in turn leads to aliasing effects in your signal.
Update:
I checked your code but couldn't figure out where that noise comes from.
Why are you using memmove to manipulate the buffer?
You can directly access the buffer with:
I'd be curious to know what causes that noise too. So if you find that out, please post your solution here.(Maybe someone on the Core Audio mailing list can help - they are really helpful there)
If you just have to alter the gain, you could simply use AudioQueueSetParameter.
Another option would be to use the RemoteIO audio unit instead of AudioQueueServices.
这可能只是将缓冲区大端格式转换为系统小端格式以进行乘法并将其再次作为大端存储在缓冲区中。
This is probably just a matter of converting the buffers big-endian format to the systems little-endian format for the multiplication and storing it in the buffer as big-endian again.