Search code examples
iphoneioscodecaudioqueuespeex

how to encode/decode speex with AudioQueue in ios


If anyone have some experience that encode/decode speex audio format with AudioQueue?

I have tried to implement it by editing the SpeakHere sample. But not success!

From the apple API document, AudioQueue can support codec, but I can't found any sample. Could anyone give me some suggestion? I already compiled speex codec successfully in my project in XCode 4.


Solution

  • in the apple sample code "SpeakHere" you can do some thing like this:

    AudioQueueNewInput(
                                         &mRecordFormat,
                                         MyInputBufferHandler,
                                         this /* userData */,
                                         NULL /* run loop */,
                                         NULL /* run loop mode */,
                                         0 /* flags */, &mQueue)
    

    you can do some thing in function "MyInputBufferHandler" like

    [self encoder:(short *)buffer->mAudioData count:buffer->mAudioDataByteSize/sizeof(short)];
    

    the encoder function like:

    while ( count >= samplesPerFrame )
        {
            speex_bits_reset( &bits );
    
            speex_encode_int( enc_state, samples, &bits ); 
    
            static const unsigned maxSize = 256;
            char data[maxSize];
            unsigned size = (unsigned)speex_bits_write( &bits, data, maxSize );
            /*
                        do some thing... for example :send to server
            */
    
            samples += samplesPerFrame;
            count -= samplesPerFrame;
        }
    

    This is the general idea.Of course fact is hard, but you can see some open source of VOIP, maybe can help you. good luck.