At just a first quick glance, I&#39;m pretty sure that you don&#39;t want to be creating and destroying the speex encoder/decoder with each frame. That&#39;s supposed to stay the same for more-or-less the entire session.<br clear="all">
<br>Ken Smith<br>Cell: 425-443-2359<br>Email: <a href="mailto:ken@alanta.com" target="_blank">ken@alanta.com</a><br>Blog: <a href="http://blog.wouldbetheologian.com/" target="_blank">http://blog.wouldbetheologian.com/</a><br>

<br><br><div class="gmail_quote">On Wed, Nov 16, 2011 at 2:52 PM, Christopher Schaefer <span dir="ltr">&lt;<a href="mailto:disks86@gmail.com">disks86@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I&#39;m completely new to speex and I&#39;m having issues adding it to my<br>
application. I can pass raw PCM and that works fine so I know my<br>
transmission code is fine however when I try to encode/decode using<br>
speex I get noise almost like a whining/buzzing sound. I&#39;m sure it&#39;s<br>
and issue in my code but I&#39;m not sure where to start looking other<br>
then my gut tells me I&#39;m not sizing or reading the buffer right. Below<br>
are the functions I&#39;m using. The audio data is from OpenAL and is<br>
16000 frequency, 16bits, and the frames being passed to the included<br>
functions are 640 bytes which if my math is right (very well may not<br>
be) that is 20ms.<br>
<br>
               virtual Enigma::u8* Encode(Enigma::u8*<br>
inputBuffer,size_t inputSize,<br>
size_t&amp; outputSize)<br>
               {<br>
                       short *in=(short*)inputBuffer;<br>
                       float *input = new float[(inputSize/2)]();<br>
                       int nbBytes;<br>
                       /*Holds the state of the encoder*/<br>
                       void *state;<br>
                       /*Holds bits so they can be read and written to<br>
by the Speex routines*/<br>
                       SpeexBits bits;<br>
                       int i, tmp;<br>
<br>
                       const SpeexMode* mode;<br>
                       mode = speex_lib_get_mode (SPEEX_MODEID_NB);<br>
<br>
                       /*Create a new encoder state in narrowband mode*/<br>
                       state = speex_encoder_init(mode);<br>
<br>
                       /*Set the quality to 8 (15 kbps)*/<br>
                       tmp=8;<br>
                       speex_encoder_ctl(state, SPEEX_SET_QUALITY, &amp;tmp);<br>
<br>
                       /*Initialization of the structure that holds the bits*/<br>
                       speex_bits_init(&amp;bits);<br>
<br>
                       for (i=0;i&lt;(inputSize/2);i++)<br>
                       {<br>
                                input[i]=in[i];<br>
                       }<br>
<br>
                       /*Flush all the bits in the struct so we can<br>
encode a new frame*/<br>
                       speex_bits_reset(&amp;bits);<br>
<br>
                       /*Encode the frame*/<br>
                       speex_encode(state, input, &amp;bits);<br>
<br>
                       nbBytes = speex_bits_nbytes(&amp;bits);<br>
<br>
                       char* cbits = new char[nbBytes]();<br>
<br>
                       /*Copy the bits to an array of char that can be written*/<br>
                       nbBytes = speex_bits_write(&amp;bits, cbits, nbBytes);<br>
<br>
                       /*Destroy the encoder state*/<br>
                       speex_encoder_destroy(state);<br>
                       /*Destroy the bit-packing struct*/<br>
                       speex_bits_destroy(&amp;bits);<br>
<br>
                       outputSize=nbBytes;<br>
<br>
<br>
                       delete[] input;<br>
<br>
                       return (Enigma::u8*)cbits;<br>
               }<br>
<br>
               virtual Enigma::u8* Decode(Enigma::u8*<br>
inputBuffer,size_t inputSize,<br>
size_t&amp; outputSize)<br>
               {<br>
                  int nbBytes;<br>
                  /*Holds the state of the decoder*/<br>
                  void *state;<br>
                  /*Holds bits so they can be read and written to by<br>
the Speex routines*/<br>
                  SpeexBits bits;<br>
                  int i, tmp;<br>
<br>
                       const SpeexMode* mode;<br>
                       mode = speex_lib_get_mode (SPEEX_MODEID_NB);<br>
<br>
                  /*Create a new decoder state in narrowband mode*/<br>
                  state = speex_decoder_init(mode);<br>
<br>
                  /*Set the perceptual enhancement on*/<br>
                  tmp=1;<br>
                  speex_decoder_ctl(state, SPEEX_SET_ENH, &amp;tmp);<br>
<br>
<br>
                  /*Initialization of the structure that holds the bits*/<br>
                  speex_bits_init(&amp;bits);<br>
<br>
                  speex_bits_read_from(&amp;bits, (char*)inputBuffer, inputSize);<br>
<br>
                  outputSize = speex_bits_nbytes(&amp;bits);<br>
<br>
                  float *output = new float[(outputSize/2)]();<br>
                  short *out = new short[(outputSize/2)]();<br>
<br>
                  /*Decode the data*/<br>
                  speex_decode(state, &amp;bits, output);<br>
<br>
                  for (i=0;i&lt;(inputSize/2);i++)<br>
                  {<br>
                                out[i]=output[i];<br>
                  }<br>
<br>
                  /*Destroy the decoder state*/<br>
                  speex_decoder_destroy(state);<br>
                  /*Destroy the bit-stream truct*/<br>
                  speex_bits_destroy(&amp;bits);<br>
<br>
                  return (Enigma::u8*)out;<br>
               }<br>
_______________________________________________<br>
Speex-dev mailing list<br>
<a href="mailto:Speex-dev@xiph.org">Speex-dev@xiph.org</a><br>
<a href="http://lists.xiph.org/mailman/listinfo/speex-dev" target="_blank">http://lists.xiph.org/mailman/listinfo/speex-dev</a><br>
</blockquote></div><br>