[Speex-dev] Just getting noise

Steve Checkoway s at pahtak.org
Wed Nov 16 15:49:12 PST 2011


The way I do this is to compute the number of frames (by dividing the  
size of my input by the number of bytes per frame and then calling  
speex_encode_int() that many times. Something like

                 speex_bits_reset( &bits );
                 for( n = 0; n < num_frames; ++n )
                 {
                         speex_encode_int( enc_state,  
(int16_t*)input_tail, &bits );
                         input_tail += BYTES_PER_FRAME;
                 }
                 speex_bits_insert_terminator( &bits );
                 n = speex_bits_nbytes( &bits ) + RTP_HEADER_SIZE;

Of course, I was sending RTP packets.

On the decoding side, I was using the jitter buffer, but I never  
managed to figure out exactly how that was supposed to work (and  
messages to the list got no replies). I think I ended up only ever  
encoding a single frame at a time as a result.

At any rate, you can work out how many frames are in a SpeexBits (I  
adapted code from the list for that, if it's of any interest).

So to decode, work out how many frames you want and then for each  
frame, call speex_decode_int(). My code looks like

                                 while( frames > 0 && (ret =  
speex_decode_int(dec_state, &bits, (int16_t *)((char*)data+length)))  
== 0 )
                                 {
                                         length += BYTES_PER_FRAME;
                                         samples -= SAMPLES_PER_FRAME;
                                         --frames;
                                         jitter_buffer_tick( jitter );
                                         lost_frames = 0;
                                 }

You can ignore the lost_frames. That just gets incremented if there  
isn't data in the jitter buffer and once enough frames have been lost,  
it starts buffering data (and so playing silence).

On Nov 16, 2011, at 15:25 , Christopher Schaefer wrote:

> Alright noted, I changed me code so that the state is created in the
> constructor and destroyed in the destructor of the object. However I'm
> still getting the same issue although I'm sure that would have bit me
> sooner or later.
> The new code is as follows.
>
> 		virtual Enigma::u8* Encode(Enigma::u8* inputBuffer,size_t inputSize,
> size_t& outputSize)
> 		{
> 			short *in=(short*)inputBuffer;
> 			float *input = new float[(inputSize/2)]();
> 			int nbBytes;
> 			int i;
>
> 			for (i=0;i<(inputSize/2);i++)
> 			{
> 				 input[i]=in[i];
> 			}
> 			
> 			/*Flush all the bits in the struct so we can encode a new frame*/
> 			speex_bits_reset(&mBits);
>
> 			/*Encode the frame*/
> 			speex_encode(mState, input, &mBits);
>
> 			nbBytes = speex_bits_nbytes(&mBits);
>
> 			char* cbits = new char[nbBytes]();
>
> 			/*Copy the bits to an array of char that can be written*/
> 			nbBytes = speex_bits_write(&mBits, cbits, nbBytes);
>
> 			outputSize=nbBytes;
>
> 			delete[] input;
>
> 			return (Enigma::u8*)cbits;			
> 		}
>
> 		virtual Enigma::u8* Decode(Enigma::u8* inputBuffer,size_t inputSize,
> size_t& outputSize)
> 		{
> 		   int nbBytes;
> 		   int i;
>
> 		   speex_bits_read_from(&mBits, (char*)inputBuffer, inputSize);
>
> 		   outputSize = speex_bits_nbytes(&mBits);
>
> 		   float *output = new float[(outputSize/2)]();
> 		   short *out = new short[(outputSize/2)]();
>
> 		   /*Decode the data*/
> 		   speex_decode(mState, &mBits, output);
>
> 		   for (i=0;i<(inputSize/2);i++)
> 		   {
> 				 out[i]=output[i];
> 		   }
>
> 		   return (Enigma::u8*)out;
> 		}
>
> On Wed, Nov 16, 2011 at 5:58 PM, Ken Smith <ken at alanta.com> wrote:
>> At just a first quick glance, I'm pretty sure that you don't want  
>> to be
>> creating and destroying the speex encoder/decoder with each frame.  
>> That's
>> supposed to stay the same for more-or-less the entire session.
>>
>> Ken Smith
>> Cell: 425-443-2359
>> Email: ken at alanta.com
>> Blog: http://blog.wouldbetheologian.com/
>>
>>
>> On Wed, Nov 16, 2011 at 2:52 PM, Christopher Schaefer <disks86 at gmail.com 
>> >
>> wrote:
>>>
>>> I'm completely new to speex and I'm having issues adding it to my
>>> application. I can pass raw PCM and that works fine so I know my
>>> transmission code is fine however when I try to encode/decode using
>>> speex I get noise almost like a whining/buzzing sound. I'm sure it's
>>> and issue in my code but I'm not sure where to start looking other
>>> then my gut tells me I'm not sizing or reading the buffer right.  
>>> Below
>>> are the functions I'm using. The audio data is from OpenAL and is
>>> 16000 frequency, 16bits, and the frames being passed to the included
>>> functions are 640 bytes which if my math is right (very well may not
>>> be) that is 20ms.
>>>
>>>                virtual Enigma::u8* Encode(Enigma::u8*
>>> inputBuffer,size_t inputSize,
>>> size_t& outputSize)
>>>                {
>>>                        short *in=(short*)inputBuffer;
>>>                        float *input = new float[(inputSize/2)]();
>>>                        int nbBytes;
>>>                        /*Holds the state of the encoder*/
>>>                        void *state;
>>>                        /*Holds bits so they can be read and  
>>> written to
>>> by the Speex routines*/
>>>                        SpeexBits bits;
>>>                        int i, tmp;
>>>
>>>                        const SpeexMode* mode;
>>>                        mode = speex_lib_get_mode (SPEEX_MODEID_NB);
>>>
>>>                        /*Create a new encoder state in narrowband  
>>> mode*/
>>>                        state = speex_encoder_init(mode);
>>>
>>>                        /*Set the quality to 8 (15 kbps)*/
>>>                        tmp=8;
>>>                        speex_encoder_ctl(state, SPEEX_SET_QUALITY,  
>>> &tmp);
>>>
>>>                        /*Initialization of the structure that  
>>> holds the
>>> bits*/
>>>                        speex_bits_init(&bits);
>>>
>>>                        for (i=0;i<(inputSize/2);i++)
>>>                        {
>>>                                 input[i]=in[i];
>>>                        }
>>>
>>>                        /*Flush all the bits in the struct so we can
>>> encode a new frame*/
>>>                        speex_bits_reset(&bits);
>>>
>>>                        /*Encode the frame*/
>>>                        speex_encode(state, input, &bits);
>>>
>>>                        nbBytes = speex_bits_nbytes(&bits);
>>>
>>>                        char* cbits = new char[nbBytes]();
>>>
>>>                        /*Copy the bits to an array of char that  
>>> can be
>>> written*/
>>>                        nbBytes = speex_bits_write(&bits, cbits,  
>>> nbBytes);
>>>
>>>                        /*Destroy the encoder state*/
>>>                        speex_encoder_destroy(state);
>>>                        /*Destroy the bit-packing struct*/
>>>                        speex_bits_destroy(&bits);
>>>
>>>                        outputSize=nbBytes;
>>>
>>>
>>>                        delete[] input;
>>>
>>>                        return (Enigma::u8*)cbits;
>>>                }
>>>
>>>                virtual Enigma::u8* Decode(Enigma::u8*
>>> inputBuffer,size_t inputSize,
>>> size_t& outputSize)
>>>                {
>>>                   int nbBytes;
>>>                   /*Holds the state of the decoder*/
>>>                   void *state;
>>>                   /*Holds bits so they can be read and written to by
>>> the Speex routines*/
>>>                   SpeexBits bits;
>>>                   int i, tmp;
>>>
>>>                        const SpeexMode* mode;
>>>                        mode = speex_lib_get_mode (SPEEX_MODEID_NB);
>>>
>>>                   /*Create a new decoder state in narrowband mode*/
>>>                   state = speex_decoder_init(mode);
>>>
>>>                   /*Set the perceptual enhancement on*/
>>>                   tmp=1;
>>>                   speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
>>>
>>>
>>>                   /*Initialization of the structure that holds the  
>>> bits*/
>>>                   speex_bits_init(&bits);
>>>
>>>                   speex_bits_read_from(&bits, (char*)inputBuffer,
>>> inputSize);
>>>
>>>                   outputSize = speex_bits_nbytes(&bits);
>>>
>>>                   float *output = new float[(outputSize/2)]();
>>>                   short *out = new short[(outputSize/2)]();
>>>
>>>                   /*Decode the data*/
>>>                   speex_decode(state, &bits, output);
>>>
>>>                   for (i=0;i<(inputSize/2);i++)
>>>                   {
>>>                                 out[i]=output[i];
>>>                   }
>>>
>>>                   /*Destroy the decoder state*/
>>>                   speex_decoder_destroy(state);
>>>                   /*Destroy the bit-stream truct*/
>>>                   speex_bits_destroy(&bits);
>>>
>>>                   return (Enigma::u8*)out;
>>>                }
>>> _______________________________________________
>>> Speex-dev mailing list
>>> Speex-dev at xiph.org
>>> http://lists.xiph.org/mailman/listinfo/speex-dev
>>
>>
> _______________________________________________
> Speex-dev mailing list
> Speex-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/speex-dev
>

-- 
Steve Checkoway



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4745 bytes
Desc: not available
Url : http://lists.xiph.org/pipermail/speex-dev/attachments/20111116/402a3528/attachment.bin 


More information about the Speex-dev mailing list