Objective-C、CoreAudio:播放的声音有额外噪音、嘶嘶声和爆裂声的可能原因是什么?
我正在使用 CoreAudio 播放一些连续的声音。我设法让它工作,但是现在我遇到了一个无法克服的问题。它正在播放的声音,更重要的是它是我需要的实际声音,不仅仅是噪音,而且我还得到噪音、嘶嘶声和爆裂声。
我验证了采样率,将所有静音缓冲区清零,检查通道(我确信我只有 1 个),并仔细检查了提供播放方法的算法。(但我将其添加到此处只是为了当然)。我对声音的经验很薄弱,所以可能我做错了一些非常可怕的事情。我想知道是否还有其他事情需要检查,或者最好的方法是什么,首先看哪里?
//init
playedBufferSize=audioFilesSize[audioFilesIndex];
startPointForPlayedBuffer=0;
//feed the audio
static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
AudioBuffer buffer = ioData->mBuffers[0];
if (playedBufferSize>=buffer.mDataByteSize) {
memcpy(buffer.mData , audioFiles[audioFilesIndex]+startPointForPlayedBuffer, buffer.mDataByteSize);
playedBufferSize-=buffer.mDataByteSize;
startPointForPlayedBuffer+=buffer.mDataByteSize;
}else {
memcpy(buffer.mData , audioFiles[audioFilesIndex]+startPointForPlayedBuffer, playedBufferSize);
nextAudioFileIndex();
memcpy(buffer.mData+playedBufferSize, audioFiles[audioFilesIndex], playedBufferSize);
playedBufferSize = audioFilesSize[audioFilesIndex]-(buffer.mDataByteSize-playedBufferSize);
startPointForPlayedBuffer = (buffer.mDataByteSize-playedBufferSize);
}
return noErr;
}
编辑:我知道上面的代码不会连续播放声音,因为它在某个时刻用一堆 0 填充缓冲区,但是,如果声音播放并短暂停止,我会听到许多奇怪的声音同时重新开始我会很高兴,一个好的开始:)
编辑2:我编辑了代码,这样它就不会再输出静音了,不幸的是我仍然听到嘶嘶声和爆裂声......
谢谢!
I'm using CoreAudio to play some continuous sound. I managed to get it work, however I have a problem now that I can't overcome. The sound it's playing, more that that it's the actual sound I need, not just noise, but together with it I get noise, hiss, pops as well.
I verified the sample rate, zero-ed out all the silence buffers, checked the channels (I'm positive I only have 1) and double checked the algorithm that feeds the playback method.(but I'll add it here just to be sure). My experience with sound it's slim, so probably I'm doing something very terrible wrong. I would like to know if there are other things to check or what's the best approach on this, where to look first?
//init
playedBufferSize=audioFilesSize[audioFilesIndex];
startPointForPlayedBuffer=0;
//feed the audio
static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
AudioBuffer buffer = ioData->mBuffers[0];
if (playedBufferSize>=buffer.mDataByteSize) {
memcpy(buffer.mData , audioFiles[audioFilesIndex]+startPointForPlayedBuffer, buffer.mDataByteSize);
playedBufferSize-=buffer.mDataByteSize;
startPointForPlayedBuffer+=buffer.mDataByteSize;
}else {
memcpy(buffer.mData , audioFiles[audioFilesIndex]+startPointForPlayedBuffer, playedBufferSize);
nextAudioFileIndex();
memcpy(buffer.mData+playedBufferSize, audioFiles[audioFilesIndex], playedBufferSize);
playedBufferSize = audioFilesSize[audioFilesIndex]-(buffer.mDataByteSize-playedBufferSize);
startPointForPlayedBuffer = (buffer.mDataByteSize-playedBufferSize);
}
return noErr;
}
EDIT: I know that this code above won't play the sound continously because it fills the buffer with a bunch of 0's at some point, however, I get many strange sounds along with that, if the sound would play and stop for a short while and start again I would be happy, a good start :)
EDIT2: I edited the code so that it won't output silence anymore, still I get the hiss and pops unfortunately...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我并不完全熟悉你在做什么,但我在 OSX 上使用 Core Graphics 时遇到了类似的问题 - 在某些情况下,我的图像上出现了明显的“噪音”。问题出在我的缓冲区上,我实际上必须将它们归零,否则我会在它们上收到噪音。您可以在使用 buffer.mData[] 之前尝试对其进行 memset 吗?
出现的问题以及为什么我认为您可能会看到相同类型的事情是,当您在 OSX 中分配大块内存时,出于安全原因,它通常会被清零,但对于小块内存,它会赢不要被归零。这可能会导致奇怪的错误 - 即,一开始您可能会分配一块足够大的内存,以便为您清除,但当您继续流式传输时,您可能会分配未清除的较小内存。
I'm not completely familiar with what you're doing but I had a similar issue using Core Graphics stuff on OSX - where I was getting visible "noise" on my images in certain situations. The issue there was with my buffers, I had to actually zero them out or else I would get noise on them. Can you try doing a memset on buffer.mData[] before using it?
The issue that came in to play, and why I think you may be seeing the same type of thing, is that when you allocate large chunks of memory in OSX it's typically zero'd for security reasons, but for small pieces of memory it won't be zero'd out. That can lead to strange bugs - i.e. at first you may be allocating a large enough piece of memory that it's cleared for you, but as you continue your streaming you may be allocating smaller pieces that aren't cleared.