Speex Programming

Encoding

In order to encode speech using Speex, you first need to:
#include <speex.h>
You then need to declare a Speex bit-packing struct
SpeexBits bits;
and a Speex encoder state
void *enc_state;
The two are initialized by:
speex_bits_init(&bits);
enc_state = speex_encoder_init(&speex_nb_mode);
For wideband coding, speex_nb_mode will be replaced by speex_wb_mode . In most cases, you will need to know the frame size used by the mode you are using. You can get that value in the frame_size variable with:
speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size);
For every input frame:
speex_bits_reset(&bits);
speex_encode(enc_state, input_frame, &bits);
nbBytes = speex_bits_write(&bits, byte_ptr, MAX_NB_BYTES);
where input_frame is a (float *) pointing to the beginning of a speech frame, byte_ptr is a (char *) where the encoded frame will be written, MAX_NB_BYTES is the maximum number of bytes that can be written to byte_ptr without causing an overflow and nbBytes is the number of bytes actually written to byte_ptr (the encoded size in bytes).

After you're done with the encoding, free all resources with:
speex_bits_destroy(&bits);
speex_encoder_destroy(&enc_state);
That's about it for the encoder.

Decoding

In order to encode speech using Speex, you first need to:
#include <speex.h>
You then need to declare a Speex bit-packing struct
SpeexBits bits;
and a Speex encoder state
void *dec_state;
The two are initialized by:
speex_bits_init(&bits);
dec_state = speex_decoder_init(&speex_nb_mode);
For wideband decoding, speex_nb_mode will be replaced by speex_wb_mode . You can get that value in the frame_size variable with:
speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size);
There is also a parameter that can be set for the decoder: whether or not to use a perceptual post-filter. This can be set by:
speex_decoder_ctl(dec_state, SPEEX_SET_PF, &pf);
where pf is an int that with value 0 to have the post-filter disabled and 1 to have it enabled.

For every input frame:
speex_bits_read_from(&bits, input_bytes, nbBytes);
speex_decode(st, &bits, output_frame, 0);
where input_bytes is a (char *) containing the bit-stream data received for a frame, nbBytes is the size (in bytes) of that bit-stream, and output_frame is a (float *) and points to the area where the decoded speech frame will be written. The last argument indicates whether the frame we'd like to decode was lost. A value of 0 indicates the normal case where bits points to the bit of the current frame. A value of 1 indicates that we don't have the bits for the current frame, in which case the bits argument should be the same as the bits for the last correctly received frame. When a frame is lost, the Speex decoder will do its best to "guess" the sorrect signal.