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

David Stubbs stubbsd at gmail.com
Wed Feb 9 11:47:20 PST 2005


Hi Tom,

Thanks for your reply, I'm having a few difficulty's following you
advise. You mention that I should be calling 'speex_encoder_int' &
'speex_decoder_int', I'm having problems finding these functions in
the speex header. I am currenlty calling speex_encoder_init &
speex_decoder_init in my test code, I have repeated the relevent bits
of the example bellow.  My version of speex is 1.1.6.

Thanks once again for your help,

   David.


       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
      speex_bits_init(&dec_bits);
      dec_state = speex_decoder_init(&speex_wb_mode);


On Wed, 9 Feb 2005 09:08:47 -0500, Tom Newbern <tnewbern at comcast.net> wrote:
> 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.
> 
> Tom
> 
> 
> -----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
> 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. :)
> 
> Thanks,
> 
>        David.
> 
>        ==== 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
>        speex_bits_init(&enc_bits);
>        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
>        speex_bits_init(&dec_bits);
>        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)
> {
>                perror("test.spx.debug");
>                exit(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.
>        speex_bits_reset(&enc_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
>        speex_bits_destroy(&enc_bits);
>        speex_encoder_destroy(&enc_state);
> 
>        // clean up decoder
>        speex_bits_destroy(&dec_bits);
>        speex_encoder_destroy(&dec_state);
> 
>        // close file
>        fsync(file);
>        close(file);
> _______________________________________________
> Speex-dev mailing list
> Speex-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/speex-dev
> 
>


More information about the Speex-dev mailing list