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

Tom Newbern tnewbern at comcast.net
Wed Feb 9 06:08:47 PST 2005

In short try calling speex_encode_int and speex_decode_int.

The calls to speex_encode and speex_decode expects the data to be a floating
point values.  The data is passed by pointer and the compiler does not do
the conversion.  This is why you are getting the segfault on decode as the
short is 16-bits and a standard float is 32-bits.


-----Original Message-----
From: speex-dev-bounces at xiph.org [mailto:speex-dev-bounces at xiph.org]On
Behalf Of David Stubbs
Sent: Wednesday, February 09, 2005 7:44 AM
To: speex-dev at xiph.org
Subject: [Speex-dev] encoding speex, (insanity looming)

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
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
                               using the following command:
                               "aplay -c1 -r16000 -f S16_LE -t raw

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
                           // 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
       // 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
       // to disk.
       // Note: I have noticed that if the array "pcm_out" is not 640 long
       //           the app segfaults, this isn't what I would have expected
       //           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
Speex-dev mailing list
Speex-dev at xiph.org

More information about the Speex-dev mailing list