Objective-C, CoreAudio: Possible reasons for which played sound has additional noise, hiss and pops?
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 experi开发者_C百科ence 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!
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.
精彩评论