how to encode/decode speex with AudioQueue in ios
Asked Answered
C

3

6

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.

Corrie answered 2/8, 2011 at 15:29 Comment(0)
H
0

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.

Helotism answered 19/3, 2012 at 5:59 Comment(0)
O
0

You can achieve all that with FFMPEG and then play it as PCM with AudioQueue. The building of the FFMPEG library is not so painless but the whole decode/encode process is not that hard :)

FFMPEG official site SPEEX official site

You will have to download the libs and build them yourself and then you will have to include them into FFMPEG and build it.

Octosyllable answered 8/8, 2012 at 15:6 Comment(1)
I have tried all the suggestions that you have given. I have compiled FFMPEG for speex. Please refer to this link #22936287.Godevil
J
0

Below is the Code For Capturing Audio using audioqueue and encode (wide-band) using speex (For Better Quality of Audio You can encode data in separate Thread , Change your sample size according to your capture format).

Audio format

    mSampleRate = 16000;
    mFormatID = kAudioFormatLinearPCM;
    mFramesPerPacket = 1;
    mChannelsPerFrame = 1;
    mBytesPerFrame = 2;
    mBytesPerPacket = 2;
    mBitsPerChannel = 16;
    mReserved = 0;
    mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; 

Capture callback

    void CAudioCapturer::AudioInputCallback(void *inUserData, 
                           AudioQueueRef inAQ, 
                           AudioQueueBufferRef inBuffer, 
                           const AudioTimeStamp *inStartTime, 
                           UInt32 inNumberPacketDescriptions, 
                           const AudioStreamPacketDescription *inPacketDescs)
    {
    CAudioCapturer *This = (CMacAudioCapturer *)inUserData;

int len = 640;
char data[640];
char *pSrc = (char *)inBuffer->mAudioData;

while (len <= inBuffer->mAudioDataByteSize) 
{
    memcpy(data,pSrc,640);
    int enclen = encode(buffer,encBuffer);
    len=len+640;

    pSrc+=640; // 640 is the frame size for WB in speex (320 short)
}

AudioQueueEnqueueBuffer(This->m_audioQueue, inBuffer, 0, NULL);
    }

speex encoder

    int encode(char *buffer,char *pDest)
    {
int nbBytes=0;
speex_bits_reset(&encbits);

speex_encode_int(encstate, (short*)(buffer)  , &encbits);

nbBytes = speex_bits_write(&encbits, pDest ,640/(sizeof(short))); 

return nbBytes;
    }
Judyjudye answered 31/8, 2012 at 6:20 Comment(1)
Thanks for the information. You mention that 640 is the frame size in Speex (320 short). What does the "320 short" mean? That since a short is two bytes long then we have 320 pairs of bytes?Nemertean

© 2022 - 2024 — McMap. All rights reserved.