如何在ios中使用AudioQueue对speex进行编码/解码
如果任何人有使用AudioQueue编码/解码Speex音频格式的经验?如何在ios中使用AudioQueue对speex进行编码/解码
我试图通过编辑SpeakHere示例来实现它。但不成功!
从apple API文档中,AudioQueue可以支持编解码器,但我找不到任何示例。任何人都可以给我一些建议吗?我已经在苹果的示例代码的XCode 4
编译Speex编解码器成功地在我的项目“SpeakHere”你可以做一些这样的事:
AudioQueueNewInput(
&mRecordFormat,
MyInputBufferHandler,
this /* userData */,
NULL /* run loop */,
NULL /* run loop mode */,
0 /* flags */, &mQueue)
你可以做一些事情在功能“MyInputBufferHandler”像
[self encoder:(short *)buffer->mAudioData count:buffer->mAudioDataByteSize/sizeof(short)];
编码器功能,如:
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;
}
这是一般的想法。当然事实很难,但你可以看到一些VOIP的开源,也许可以帮助你。 祝你好运。
您可以使用FFMPEG实现所有功能,然后将其作为带有AudioQueue的PCM播放。 的FFMPEG库的建设是不那么痛苦,但整个解码/编码过程并不难:)
FFMPEG official site SPEEX official site
您必须下载库和自己构建它们,然后你会必须将它们包含到FFMPEG中并构建它。
下面是一个使用使用Speex语音 audioqueue和编码(宽带)的代码捕获音频(音频质量越好,您可以编码在单独的线程中的数据,根据您的拍摄格式更改样本大小)。
音频格式
mSampleRate = 16000;
mFormatID = kAudioFormatLinearPCM;
mFramesPerPacket = 1;
mChannelsPerFrame = 1;
mBytesPerFrame = 2;
mBytesPerPacket = 2;
mBitsPerChannel = 16;
mReserved = 0;
mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
捕捉回调
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编码
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;
}
感谢您的信息。您提到640是Speex中的帧大小(320短)。 “320短”是什么意思?既然短是两个字节长,那么我们有320对字节? – csotiriou
我已经尝试了所有的建议你已经给了。我已经为speex编译了FFMPEG。请参阅此链接http://stackoverflow.com/questions/22935787/compiling-ffmpeg-to-support-speex-decoding。 – user2955351