[英]C++ Recording Audio and Compressing to GSM
我正在开发一个C ++程序,该程序可以记录音频并将其压缩为GSM。 我能够录制音频并将原始数据写入文件。 但是我无法使GSM压缩正常工作。 我正在尝试使用在此网站ftp://ftp.cs.cmu.edu/project/fgdata/speech-compression/GSM/gsm_13200bps上找到的压缩源代码。
我认为我的问题是使用gsm_encode()函数时。 播放此文件时,将压缩数据编码并保存到文件后,听不见。 我知道原始音频数据是正确的,但是压缩音频数据是不正确的。
gsm_encode()将160个13位样本(作为gsm_signal的符号,至少16位的带符号整数值)的数组编码为33字节的gsm_frame。
这是我的功能,我将数据错误地发送到gsm_encode()吗? 还是我的功能还有其他问题? 谢谢您的帮助 :)
int CAudioGSM::CompressAudio(unsigned char * pRawBuffer, _int32 uiRawBufferSize, unsigned char * pCompressedBuffer, _int32 uiCompressedBufferSize)
{
// Note: uiRawBufferSize must be a multiple of 640 (AUDIO_DMA_DESCRITOR_LEN)
if(!pRawBuffer || uiRawBufferSize == 0 || !pCompressedBuffer || uiCompressedBufferSize == 0 || uiRawBufferSize % AUDIO_DMA_DESCRITOR_LEN != 0)
{
return -1; //invalid parameters
}
_int32 uiBytesCompressed = 0; // Number of bytes that have been compressed. At the end of the function this should be equal to iRawBufferSize meaning we have compressed the whole raw buffer
_int32 uiCompBuffOffset = 0; // Offset into the compressed buffer
while(uiBytesCompressed < uiRawBufferSize)
{
if(uiCompressedBufferSize - uiCompBuffOffset < GSM_OUTPUT_SIZE || uiCompBuffOffset >= uiCompressedBufferSize)
{
return -2; // Compressed buffer is too small
}
gsm_encode(&m_GSM_EncodeStruture,(long *)pRawBuffer,m_Buffer);
//Now we need to move the data to compressed buffer
if(m_bFirstHalfOfBlockRecord)
{
//Just copy the data over
memcpy(&pCompressedBuffer[uiCompBuffOffset],m_Buffer,GSM_OUTPUT_SIZE_FIRST_HALF);
m_bFirstHalfOfBlockRecord = false;
uiCompBuffOffset += GSM_OUTPUT_SIZE_FIRST_HALF;
}
else
{
memcpy(&pCompressedBuffer[uiCompBuffOffset],m_Buffer,GSM_OUTPUT_SIZE);
m_bFirstHalfOfBlockRecord = true;
uiCompBuffOffset += GSM_OUTPUT_SIZE;
}
uiBytesCompressed += AUDIO_DMA_DESCRITOR_LEN;
}
return uiCompBuffOffset; // Success, we have compressed the buffer return compressed data size
}
没关系,我想通了,pRawBuffer永远不会增加,应该是
gsm_encode(&m_GSM_EncodeStruture,(long *)&pRawBuffer[uiBytesCompressed],m_Buffer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.