title: Python : PortAudio + Opus encoding/decoding
body: <p>I'm capturing audio from my mic with Pyaudio and trying to encode/decode it with the opus codec. I'm using the bindings to libopus made by SvartalF ( <a href="https://github.com/svartalf/python-opus" rel="nofollow">https://github.com/svartalf/python-opus</a> ).</p>
  
  <p>Here is my code :</p>
  
  <pre><code>import pyaudio
  from opus import encoder, decoder
  
  def streaming(p):
      chunk = 960
      FORMAT = pyaudio.paInt16
      CHANNELS = 1
      RATE = 48000
      streamin = p.open(format = FORMAT,
              channels = CHANNELS, 
              rate = RATE, 
              input = True,
              input_device_index = 7,
              frames_per_buffer = chunk)
      streamout = p.open(format = FORMAT,
              channels = CHANNELS, 
              rate = 48000, 
              output = True,
              output_device_index = p.get_default_input_device_info()["index"],
              frames_per_buffer = chunk)
      enc = encoder.Encoder(RATE,CHANNELS,'voip')
      dec = decoder.Decoder(RATE,CHANNELS)
      data = []
      for i in xrange(100):
          data.append(streamin.read(chunk*2))
      streamout.write(''.join(data))
      encdata = []
      for x in data:
          encdata.append(enc.encode(x,chunk))
      print "DATA LENGTH :", len(''.join(data))
      print "ENCDATA LENGTH :", len(''.join(encdata))
      decdata = ''
      for x in encdata:
          decdata += dec.decode(x,chunk)
      print "DECDATA LENGTH :", len(decdata)
      streamout.write(decdata)
      streamin.close()
      streamout.close()
  
  
  p = pyaudio.PyAudio()
  streaming(p)
  p.terminate()
  </code></pre>
  
  <p>I must put <code>chunk*2</code> instead of <code>chunk</code> in <code>data.append(streamin.read(chunk*2))</code> or <code>DECDATA LENGTH == DATA LENGTH*2</code> and I don't know why.</p>
  
  <p>Outputs :</p>
  
  <pre><code>DATA LENGTH :    384000  
  ENCDATA LENGTH : 12865  
  DECDATA LENGTH : 384000
  </code></pre>
  
  <p>Without encoding/decoding, the first <code>streamout.write(''.join(data))</code> works perfectly. With encoding/decoding, the <code>streamout.write(decdata)</code> kinda works but has a lot of cracklings mixed in.</p>
  
  <p>What am I doing wrong here?</p>