AudioQueue callback in simulator but not on device

1.1k views Asked by At

I am currently working on an audio processing app on iPhone. it is based on Apple's SpeakHere sample code, aims at real-time audio processing and playback. The code works well in simulator, but not callback when tested on device.

The callback function is like this:

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, inCompleteAQBuffer->mPacketDescriptions, THIS->GetCurrentPacket(), &nPackets, 
                                       inCompleteAQBuffer->mAudioData);
if (result)
    printf("AudioFileReadPackets failed: %d", (int)result);
if (nPackets > 0) {
    inCompleteAQBuffer->mAudioDataByteSize = numBytes;      
    inCompleteAQBuffer->mPacketDescriptionCount = nPackets;     

    //Buffer Modification

    SInt16 *testBuffer = (SInt16*)inCompleteAQBuffer->mAudioData;       

    for (int i = inCompleteAQBuffer->mAudioDataByteSize/sizeof(SInt16); i > 0; i --)
    {           
        //printf("before modification %d", (int)*testBuffer);                                               
        *testBuffer = *testBuffer/2;   //Simplest processing
        //printf("after modification %d", (int)*testBuffer);
        testBuffer++;           
    }               
    //Enqueue Buffer
    AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, NULL);

    THIS->mCurrentPacket = (THIS->GetCurrentPacket() + nPackets);
} 

Other parts remains same as in SpeakHere sample code.

So what is the problem related to this strange behavior? Any insight will be appreciated. And kindly let me know if more information is needed.

Thank you very much.

Cheers,

Manca

1

There are 1 answers

0
ldiqual On

You have to initialize the AudioSession and set its mode to Play&Record:

AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_PlayAndRecord;  
int error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
if (error) printf("Couldn't set audio category!");

See awakeFromNib in SpeakHereController.mm from the famous SpeakHere example.