我正在开发一个记录音频并将其压缩为 GSM 的 C++ 程序。我能够录制音频并将原始数据写入文件。但我无法让 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
}