C++ 录制音频并压缩到 GSM

标签 c++ audio 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);

关于C++ 录制音频并压缩到 GSM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13088795/

相关文章:

c++ - 抽象类类型指针编译成功?

android - 在Android中录制几个音频

java - Java System.arraycopy数组范围

javascript - HTML5 音频元素在移动设备上不起作用

embedded - 在不输入 PIN 的情况下,可以从受 PIN 保护的 SIM 中读取哪些数据/元数据?

java - C++11、C#和Java内存模型的区别

c++ - 在 C++ 上包含文件以避免循环依赖的 final方法

c++ - C 风格的转换是否与函数风格的转换相同?

linux - USSD 网关实现

android - 信号强度中的负dbm是什么意思?