2012-10-26 85 views
1

我正在致力於一個C++程序,記錄音頻並將其壓縮到GSM。我能夠錄製音頻並將原始數據寫入文件。但我無法使GSM壓縮工作。我正在嘗試使用我在此網站ftp://ftp.cs.cmu.edu/project/fgdata/speech-compression/GSM/gsm_13200bps發現的壓縮源代碼。C++錄製音頻和壓縮到GSM

我想我的問題是當使用gsm_encode()函數。在播放此文件時,編碼並將壓縮數據保存到文件後,該文件聽不見。我知道原始音頻數據是正確的,但壓縮音頻數據不正確。 gsm_encode()將包含160個13位樣本的數組(編號爲 gsm_signal's,至少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 
} 
+0

格式代碼 – sashoalm

+0

對不起,我嘗試了第一次,但現在被格式化 – Megan

+0

您可以將其作爲答案發布。有一個「回答你的問題」按鈕 – sashoalm

回答

0

沒關係我想通了它的pRawBuffer永遠不會增加它應該是

gsm_encode(&m_GSM_EncodeStruture,(long *)&pRawBuffer[uiBytesCompressed],m_Buffer);