在iOS AudioQueue中用Speex進行編碼和解碼

來源:互聯網
上載者:User

n 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.


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;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.