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; ? ? ? ? ? ? ? ?}
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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.xiph.org/pipermail/speex-dev/attachments/20111116/6ae3c61c/attachment.htm
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 > >