Dưới đây là mã để chụp âm thanh sử dụng âm thanh và mã hóa (băng rộng) sử dụng dấu phẩy (để có chất lượng âm thanh tốt hơn, bạn có thể mã hóa dữ liệu trong chủ đề riêng biệt, thay đổi kích thước mẫu theo định dạng chụp).
âm thanh định dạng
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;
}
Tôi đã thử tất cả những đề nghị mà bạn đã đưa ra. Tôi đã biên soạn FFMPEG cho speex. Vui lòng tham khảo liên kết này http://stackoverflow.com/questions/22935787/compiling-ffmpeg-to-support-speex-decoding. – user2955351