Question

I'd like to encode data with libspeex. However, speex_encode_int() always returns a SpeexBits of length 70. It doesn't matter what I provide (music, silence, voice).

Input specification: 16bit, PCM, Mono, 44100Hz sampling rate, LE

int tmp;
short *input = ...; // input data (PCM, 16bit)
int inputFrames = ...; // number of frames
void *state;
SpeexBits bits;
int frameSize;
int bufferLength;
void* output = NULL;
int outputLength = 0;
int i;

state = speex_encoder_init(&speex_wb_mode);
tmp = 8;
speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &frameSize);
tmp = 44100;
speex_encoder_ctl(state, SPEEX_SET_SAMPLING_RATE, &tmp);
speex_bits_init(&bits);


for(i = 0; i < (inputFrames / frameSize); i++){
    speex_bits_reset(&bits);
    speex_encode_int(state, &input[i * frameSize], &bits);
    bufferLength = speex_bits_nbytes(&bits);
    output = realloc(output, outputLength + bufferLength);
    speex_bits_write(&bits, (char*)(output + outputLength), bufferLength);
    outputLength += bufferLength;
}

// process output data    

free(output);
free(input);

Did I make an error in my encoding routine? Or is there anything wrong with my initialization?

Was it helpful?

Solution

Well, you're using quality 8, which is 27.8 kb/s for wideband, which rounded up is 70 bytes/frame. I'm not sure what you expect. Changing the quality parameter should give you more bytes or fewer bytes (depending on the value you choose). Also, using 44.1 kHz is not recommended. You should use wideband (16 kHz). Alternatively, you can have a look at the Opus codec.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top