I am working on a C++ program that records audio and compresses it to GSM. I am able to record audio and write the raw data to a file. But I cannot get the GSM compression to work. I am trying to use a source code for compression that I found at this website ftp://ftp.cs.cmu.edu/project/fgdata/speech-compression/GSM/gsm_13200bps.
I think my problem is when using the gsm_encode() function. After encoding and saving the compressed data to the file when you play this file it is inaudible. I know that the raw audio data is correct but the compressed audio data is incorrect.
gsm_encode() encodes an array of 160 13-bit samples (given as
gsm_signal’s, signed integral values of at least 16 bits) into
a gsm_frame of 33 bytes.
Here is my function am I sending the data into gsm_encode() incorrectly? Or is there another problem with my function? Thank you for your help 🙂
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
}
nevermind I figured it out it the pRawBuffer never gets incremented it should be