How to encode / decode speex using AudioQueue in ios

If anyone has experience that encodes / decodes speex audio format using AudioQueue?

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

From the Apple API document, AudioQueue may support the codec, but I cannot find a sample. Can someone give me some suggestion? I already compiled the speex codec in my project in Xcode 4.

+6
source share
3 answers

in the apple SpeakHere code example, you can do something like this:

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

you can do something in the function "MyInputBufferHandler", for example

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

encoder function, for example:

 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 a general idea. Of course, this is difficult, but you can see some kind of open source VOIP, maybe it can help you. good luck.

0
source

You can achieve all this with FFMPEG and then play it as PCM with AudioQueue. Building the FFMPEG library is not so painless, but the whole decoding / coding process is not so difficult :)

Official site FFMPEG Official site SPEEX

You will have to download the libraries and create them yourself, and then you will have to include them in FFMPEG and create them.

0
source

Below is the code for recording sound using an audio signal and encoding (broadband) using speex (For better sound quality, you can encode data in a separate stream, resize the sample 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; 

Callback capture

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

Source: https://habr.com/ru/post/894115/


All Articles