[Speex-dev] encoding -> decoding doesnt work
Jürgen Ladstätter
info at innova-studios.com
Mon Apr 13 11:43:05 PDT 2009
hi all,
i'm new to speex and i'm stuck on a problem. I have a buffer that holds 5
seconds of audio data, sampled at 16000 hz and quantitized with 16 bits pcm.
I only want to encode it with speex and then decode it and write it back in
the same buffer.
no compiling errors and no errors during runtime, but from the sample of 5
seconds only a short noise is all I get.
I hope you have any ideas!
kind regards juergen
SpeexBits bits;
void *enc_state;
speex_bits_init(&bits);
const SpeexMode *mode;
mode = speex_lib_get_mode (SPEEX_MODEID_WB);
enc_state = speex_encoder_init(mode);
int frame_size = 0;
speex_encoder_ctl(enc_state,SPEEX_GET_FRAME_SIZE,&frame_size);
int tmp = 16000;
speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &tmp);
tmp = 4;
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
tmp = 1;
speex_encoder_ctl(enc_state, SPEEX_SET_VBR, &tmp);
tmp = 4;
speex_encoder_ctl(enc_state, SPEEX_SET_VBR_QUALITY, &tmp);
tmp = 2;
speex_encoder_ctl( enc_state, SPEEX_SET_COMPLEXITY, &tmp );
tmp = 1;
speex_encoder_ctl( enc_state, SPEEX_SET_DTX, &tmp );
tmp = 1;
speex_encoder_ctl( enc_state, SPEEX_SET_VAD, &tmp );
int numBytesEncoded = 0;
int amountSamples = defaultfrequency * numchannels * 5; // frequency
is 16000, channels = 1, 5 is for 5 seconds
unsigned int total = 0;
char *encoded = NULL;
for( int j = 0; j < amountSamples/frame_size; j++ )
{
// Encode the voice data
speex_bits_reset(&bits);
speex_encode_int(enc_state, (short*)((char*)p_raw1 +
j*frame_size*sizeof(short)), &bits);
int nBytes = speex_bits_nbytes(&bits);
char *encodedArray = new char[nBytes];
int encodedBytes =
speex_bits_write(&bits,encodedArray,nBytes);
printf("Encoded: %d \t",encodedBytes);
total += encodedBytes;
encoded = (char*)realloc( encoded, total );
memcpy( (encoded+total-encodedBytes), encodedArray,
encodedBytes );
}
printf("\n\nTotally: %u\nper Item: %f\n\n", total,
(float)total/(amountSamples/frame_size) );
speex_bits_destroy(&bits);
speex_encoder_destroy(enc_state);
// ###########################
// decoding
// ###########################
SpeexBits dec_bits;
void *dec_state;
const SpeexMode *dec_mode;
dec_mode = speex_lib_get_mode (SPEEX_MODEID_WB);
speex_bits_init(&dec_bits);
// speex_nb_mode = 8kHz
// speex_wb_mode = 16 kHz
// speex_uwb_mode = 32 kHz
dec_state = speex_decoder_init(dec_mode);
frame_size = 0;
speex_decoder_ctl(dec_state,SPEEX_GET_FRAME_SIZE,&frame_size);
amountSamples = defaultfrequency * numchannels * 5;
speex_bits_read_from(&dec_bits, encoded, total );
speex_decode_int( dec_state, &dec_bits, (short*)p_raw1 );
speex_bits_destroy(&dec_bits);
speex_decoder_destroy(dec_state);
More information about the Speex-dev
mailing list