title: Portaudio + Opus: Horrible sound quality
body: <p>I'm currently developing a VOIP application. To achieve this, I use the PortAudio library to retrieve and play sound, and the Opus library to encode and decode sound packets.</p>
  
  <p>For now, I successfully managed to use PortAudio. My program simply do:</p>
  
  <ol>
  <li>Get Sound from the microphone</li>
  <li>Play back the sound</li>
  </ol>
  
  <p>The sound quality is absolutely good.</p>
  
  <p>I'm now trying to encode and decode the sound packet. I've coded an EncodeManagerClass to do it and my program now do:</p>
  
  <ol>
  <li>Get sound from the microphone</li>
  <li>Encode the sound</li>
  <li>Decode it</li>
  <li>Play it back</li>
  </ol>
  
  <p>But now, the sound quality is absolutely horrible (and it's obviouly problematic in a VOIP application).</p>
  
  <p>Here is my <code>EncodeManager</code> class:</p>
  
  <pre><code>class EncodeManager {
  
      // ctor - dtor
      public:
          EncodeManager(void);
          ~EncodeManager(void);
  
      // coplien form
      private:
          EncodeManager(const EncodeManager &amp;) {}
          const EncodeManager &amp;operator=(const EncodeManager &amp;) { return *this; }
  
      // encode - decode
      public:
          Sound::Encoded  encode(const Sound::Decoded &amp;sound);
          Sound::Decoded  decode(const Sound::Encoded &amp;sound);
  
      // attributes
      private:
          OpusEncoder *mEncoder;
          OpusDecoder *mDecoder;
  
  };
  </code></pre>
  
  <p>Here is the source file:</p>
  
  <pre><code>EncodeManager::EncodeManager(void) {
      int error;
  
      mEncoder = opus_encoder_create(Sound::SAMPLE_RATE, Sound::NB_CHANNELS, OPUS_APPLICATION_VOIP, &amp;error);
      if (error != OPUS_OK)
          throw new SoundException("fail opus_encoder_create");
  
      mDecoder = opus_decoder_create(Sound::SAMPLE_RATE, Sound::NB_CHANNELS, &amp;error);
      if (error != OPUS_OK)
          throw new SoundException("fail opus_decoder_create");
  }
  
  EncodeManager::~EncodeManager(void) {
      if (mEncoder)
          opus_encoder_destroy(mEncoder);
  
      if (mDecoder)
          opus_decoder_destroy(mDecoder);
  }
  
  Sound::Encoded  EncodeManager::encode(const Sound::Decoded &amp;sound) {
      Sound::Encoded encoded;
  
      encoded.buffer = new unsigned char[sound.size];
      encoded.size = opus_encode_float(mEncoder, sound.buffer, Sound::FRAMES_PER_BUFFER, encoded.buffer, sound.size);
  
      if (encoded.size &lt; 0)
          throw new SoundException("fail opus_encode_float");
  
      return encoded;
  }
  
  Sound::Decoded  EncodeManager::decode(const Sound::Encoded &amp;sound) {
      Sound::Decoded decoded;
  
      decoded.buffer = new float[Sound::FRAMES_PER_BUFFER * Sound::NB_CHANNELS];
      decoded.size = opus_decode_float(mDecoder, sound.buffer, sound.size, decoded.buffer, Sound::FRAMES_PER_BUFFER, 0);
  
      if (decoded.size &lt; 0)
          throw new SoundException("fail opus_decode_float");
  
      return decoded;
  }
  </code></pre>
  
  <p>Here is my main:</p>
  
  <pre><code>int main(void) {
      SoundInputDevice input;
      SoundOutputDevice output;
      EncodeManager encodeManager;
  
      input.startStream();
      output.startStream();
  
      while (true) {
          Sound::Decoded *sound;
  
          input &gt;&gt; sound;
          if (sound) {
              Sound::Encoded encodedSound = encodeManager.encode(*sound);
              Sound::Decoded decodedSound = encodeManager.decode(encodedSound);
              output &lt;&lt; &amp;decodedSound;
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Additional informations:</p>
  
  <pre><code>const int   SAMPLE_RATE = 48000;
  const int   NB_CHANNELS = 2;
  const int   FRAMES_PER_BUFFER = 480;
  </code></pre>
  
  <p>I've tried to configure the opus encoder with <code>opus_encode_ctl</code> (bandwidth, bitrate, VBR), but it doesn't work at all: sound quality is still shitty.</p>
  
  <p>Even if I change the SAMPLE_RATE or the FRAME_PER_BUFFER, sound quality doesn't improve...</p>
  
  <p>Have I missed something concerning PortAudio/Opus?</p>