Encoding and decoding problem in speex 1.0.4

Source: Internet
Author: User

I am using the speex 1.0.4 library from windows.
I have posted my problem before but didn't get a solution. I am doing an VoIP project
In which I am recording sound and streaming it to the peer. I wanted to encode and decode
Wav files that brought me to this site.

I am recording sound in the following format :-

M_waveformatex.wformattag = wave_format_pcm;
M_waveformatex.nchannels = 1;
M_waveformatex.wbitspersample = 8;
M_waveformatex.cbsize = 0;
M_waveformatex.nsamplespersec = 8000;
M_waveformatex.nblockalign = 1;
M_waveformatex.navgbytespersec = 8000;

The recording is as follows :-

When the buffer (size = 2000 bytes) gets filled with sound data
Function with the body shown
Below is called.

Lpwavehdr lphdr = (lpwavehdr) lparam;
If (lphdr-> dwbytesrecorded = 0 | lphdr = NULL)
Return error_success;
: Waveinunprepareheader (m_hrecord, lphdr, sizeof (wavehdr ));

Here lphdr-> lpdata contains the audio data in a character array.

Now here I want to use speex codec for encoding the data so the Encoding
Function is
Called (I am thankful to Tay yueweng for the function ).

Char * encode (char * buffer, Int & encodesize)
{
Char * encodedbuffer = new char [recbuffer/2];/* recbuffer = 2000 */
Short speexshort;
Float speexfloat [recbuffer/2];
Void * mencode = speex_encoder_init (& speex_nb_mode );

/* Initialization of the structure that holds the bits */
Speex_bits_init (& Mbits );

// Convert the audio to a short then to a float Buffer
Int halfbuffersize = recbuffer/2;

For (INT I = 0; I {
Memcpy (& speexshort, & buffer [I * 2], sizeof (short ));
Speexfloat [I] = speexshort;
}

// Encode the sound data using the float Buffer
Speex_bits_reset (& Mbits );
Speex_encode (mencode, speexfloat, & Mbits );
Encodesize = speex_bits_write (& Mbits, encodedbuffer, recbuffer/2 );

/* Destroy the encoder state */
Speex_encoder_destroy (mencode );
/* Destroy the bit-stream struct */
Speex_bits_destroy (& Mbits );

// Return the encoded Buffer
Return encodedbuffer;
}

Here I noticed that though my captured audio data is 2000 bytes
Compressed form is
Always 38 bytes. In the speexfloat array above I get values in the range
-32767 to + 32767.
Is it correct. Also after calling the 'speex _ encode' function the first
160 values in
Input float array I. e. speexfloat is changed (Why does it happen? Is
Anything abnormal ).

Further after calling the above function for testing I decode
Returned encoded data
Immediately by calling the decoding function shown bellow :-

Char * decode (char * buffer, int encodesize)
{
Char * decodedbuffer = new char [recbuffer];
Short speexshort;
Float speexfloat [recbuffer/2];
// Decode the sound data into a float Buffer
Void * mdecode = speex_decoder_init (& speex_nb_mode );

/* Initialization of the structure that holds the bits */
Speex_bits_init (& Mbits );

Int halfbuffersize = recbuffer/2;
Speex_bits_reset (& Mbits );
Speex_bits_read_from (& Mbits, buffer, encodesize );
Speex_decode (mdecode, & Mbits, speexfloat );
// Convert from float to short to Char

For (INT I = 0; I {
Speexshort = speexfloat [I];
Memcpy (& decodedbuffer [I * 2], & speexshort, sizeof (short ));
}

/* Destroy the decoder state */
Speex_encoder_destroy (mdecode );
/* Destroy the bit-stream truct */
Speex_bits_destroy (& Mbits );

// Return the buffer
Return decodedbuffer;
}

After decoding using the above function only the first 160 values in the decodedbuffer array is changed. i. e I encoded an 2000 byte audio data to get a 38 byte encoded audio data. on Decoding the 38 byte audio data I get an decompressed 160 byte data. I don't understand whats going wrong. I checked all the messages posted in this newsgroup and did' nt find an answer so I am posting this code hoping that it gets solved soon. thanks in advance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.