[Speex-dev] Another newbie question on encoding

Daniele Barzotti
2010-04-13T14:57:11Z

Hi,

I'm very sorry if those questions are repeated over and over, but I
cannot find a solution on the net.

I try to use speex to encode/decode voice to send over the network.
My doubts are:

1. The Bits_Per_Sample I use, are independent from the speex
encoding/decoding? (So...can I use 8, 16, 24..and so on?)

2. If I have this situation:

SAMPLE RATE.....: 8000
BITS PER SAMPLE.: 16
CHANNELS........: 1
DATA TYPE.......: float

And I need to encode a buffer of 640 PCM float frames, the following
Encode method is correct?

Thanks in advance for the help!
Daniele.



class CSpeexCodec
{
public:

  enum ESpeexCodecType
  {
    SC_ENCODER,
    SC_DECODER
  };

  enum ESpeexCodecMode
  {
    SC_NARROWBAND,    //  8 kHz
    SC_WIDEBAND,      // 16 kHz
    SC_ULTRAWIDEBAND  // 32 kHz
  };

  CSpeexCodec();
  ~CSpeexCodec();

  BOOL  Init();
  UINT  Encode( sample_type *inBuff, char **outBuff, UINT BufferFrames );

private:
  SpeexBits       spx_bits_;
  void*           spx_state_;
  UINT            spx_quality_;
  UINT            frameSize_;
  char*           outBuffer_;
};


BOOL CSpeexCodec::Init()
{
  spx_state_ = speex_encoder_init(&speex_nb_mode);
  speex_encoder_ctl(spx_state_, SPEEX_GET_FRAME_SIZE, &frameSize_);
  // Create the output buffer
  outBuffer_ = new char[frameSize_];
  speex_bits_init(&spx_bits_);
}


UINT CSpeexCodec::Encode( float *inBuff,
                          char **outBuff,
                          UINT BufferFrames )
{
  INT nBytes = 0;
  float *pBuff = inBuff;

  if (!spx_state_) return 0;

  while (BufferFrames>0)
  {
    speex_bits_reset(&spx_bits_);
    speex_encode(spx_state_, pBuff, &spx_bits_);
    nBytes += speex_bits_write(&spx_bits_, outBuffer_, frameSize_);
    pBuff  += frameSize_;
    BufferFrames -= frameSize_;
  }
  // Point the outBuff to the internal buffer
  *outBuff = outBuffer_;
  return nBytes;
};