[Speex-dev] encoding speex, (insanity looming)

David Stubbs stubbsd at gmail.com
Wed Feb 9 04:43:30 PST 2005

Hi All,

I'm very new to speex and in fact handling audio at all, it seems I have run
in to a problem I seem unable to fix. I'm trying to take audio from a microphone
using alsa, then encode it as speex and save to disk. I have been
wondering if it has something to do with endian type, but speexenc and
speexdec works fine.

Currently I have the following setup:

       Platform:         PowerPC (powerbook) running Linux (2.6)
       Input stream:  Signed 16bit Little endian, mono, PCM @ 16000Hz
                               if saved out 'as is' can be played with "aplay"
                               using the following command:
                               "aplay -c1 -r16000 -f S16_LE -t raw <fileame>"

Bellow is part of some test code that I have been trying to get to work,
I have removed some of the code to make it easyer to see. Any help would
be wonderful, think of it as an heroic act by saving me from a mental
break down. :)



       ==== sample code ====
       // encoder
       void      *enc_state;
       SpeexBits  enc_bits;

       // decoder
       void      *dec_state;
       SpeexBits  dec_bits

       // quaility and rate
       int quality = 8, rate = 16000;

       // setup encoder
       enc_state = speex_encoder_init(&speex_wb_mode);
       speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &quality);
       speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &rate);

       // setup decoder
       dec_state = speex_decoder_init(&speex_wb_mode);
       speex_encoder_ctl(dec_state, SPEEX_SET_QUALITY, &quality);
       speex_encoder_ctl(dec_state, SPEEX_SET_SAMPLING_RATE, &rate);

       int file; // for the file to write.

       // setup file for writing the encoded => decoded audio out to
       if ((file= open64("test.spx.debug", O_WRONLY | O_CREAT, 0644)) == -1) {
       // Note: This area is repeated in a for loop with "packet"
filled with 320
       //       pcm samples (320 shorts)
       //       short packet[320]; // packet size for wideband.

       int nbytes, err;
       char encoded_audio[320]; // buffer to store encoded audio from speex
       short pcm_out[640]; // buffer of short to write decoded audio to disk.
                           // Note: Please see the note bellow about this as
                           //       there seems something wrong, thought
                           //       I could have set this to 320 not 640

       // reset the encoder bits.

       // encode a frame
       // Note: packet is a buffer of shorts containing 320 samples
       //           of pcm data (320 shorts).
       speex_encode(enc_state, packet, enc_bits);

       // write the chars from encoding to encoded_audio, read to write to disk
       // althought we will not do so here,
       nbytes = speex_bits_write(&enc_bits, encoded_audio, 320) ;

       // read in the chars from the encoding process ready to decode
       speex_bits_read_from(&dec_bits, encoded_audio, nbytes);

       // decode the bits in to the array for short "pcm_out" ready for writing
       // to disk.
       // Note: I have noticed that if the array "pcm_out" is not 640 long then
       //           the app segfaults, this isn't what I would have expected as
       //           I thought that, using wideband you put in 320
samples (shorts)
       //           and that at the other end (this end) you got 320
back so I don't
       //           understand why it would segfault?
       speex_decode(dec_state, &dec_bits, pcm_out);

       // save the encoded data to a file

       write(file, pcm_out, sizeof(short) * frame_size);

       // clean up encoder

       // clean up decoder

       // close file

More information about the Speex-dev mailing list