开发者

AudioQueue screws up output after modification

I am currently working on an audio DSP App development. The project requires direct access and modification of audio data. Right now I can successfully access and modify the raw audio data using AudioQueue but encounters error during playback. The output audio after any modification turns out be noise.

In short, the code is something like this:

(Modified from Speakhere sample code. The rest remains unchanged.)

void AQPlayer::AQBufferCallback(void *                  inUserData,
                            AudioQueueRef           inAQ,
                            AudioQueueBufferRef     inCompleteAQBuffer) 
{
AQPlayer *THIS = (AQPlayer *)inUserData;

if (THIS->mIsDone) return;

UInt32 numBytes;
UInt32 nPackets = THIS->GetNumPacketsToRead();
OSStatus result = AudioFileReadPackets(THIS->GetAudioFileID(), 
                                       false, 
                                       &numBytes, 
                  开发者_C百科                     inCompleteAQBuffer->mPacketDescriptions, 
                                       THIS->GetCurrentPacket(), 
                                       &nPackets, 
                                       inCompleteAQBuffer->mAudioData);
if (result)
    printf("AudioFileReadPackets failed: %d", (int)result);
if (nPackets > 0) {
    inCompleteAQBuffer->mAudioDataByteSize = numBytes;      
    inCompleteAQBuffer->mPacketDescriptionCount = nPackets;     

//My modification starts from here
//Modifying audio data

SInt16 *testBuffer = (SInt16*)inCompleteAQBuffer->mAudioData;   
for (int i = 0; i < (inCompleteAQBuffer->mAudioDataByteSize)/sizeof(SInt16); i++)
    {           
        //printf("before modification %d", (int)*testBuffer);   
        *testBuffer = (SInt16) *testBuffer/2; //Say some simple modification
        //printf("after modification %d", (int)*testBuffer);    

        testBuffer++;
        }

AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, NULL);

}

During debugging, the data in buffer is displayed as expected, but the actual output is nothing but noise.

Here are some other strange behaviors of the code that makes both the whole team crazy:

  1. If there is no change to the data (add/sub by 0, multiply by 1) or the whole buffer is assigned to a constant (say 0, then the audio will be muted), the playback behaves normally (Of course!) But if I perform anything more than it, it still turns out to be noise.

  2. In the case I hardcode a single tone as test audio, the output noise spreads into another channel also.

So where is the bug in this code? Or if I am on the wrong track, what is the correct approach to modify the audio data and perform playback CORRECTLY? Any insight will be sincerely appreciated.

Thank you very much :-)

Cheers,

Manca


are you SURE the sample format is SInt16? And how many channels are there? You seem to treat the audio as a single channel short stream, but suppose the format is actually dual channel Float32 or so, and you do the modifications there, than the effect would be exactly as you describe, including the noise on other channels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜