title: Python Voice Communication
body: <p>Hello I am trying to figure out some code which is suppose to send voice over the network. I am having problems with the audio it sends but its just a series of loud beeps and not the audio I input</p>
  
  <p>After the beeps are finished I get an  <code>EOFError</code></p>
  
  <p>I have spent the last 48 hours trying to figure this out any ideas are greatly appreciated</p>
  
  <p>The relevant code</p>
  
  <pre>
  import pyaudio
  import speex
  import sys
  
  chunk = 320
  FORMAT = pyaudio.paInt16
  CHANNELS = 1
  RATE = 44100
  RECORD_SECONDS = 5
  
  ### Server function ###
  def server():
      ### Initialize socket 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((HOST, PORT))
      s.listen(5)
      ### Start recieve loop
      while True:
  ...
          elif cmd == CMD_AUDIO:
              d = speex.Decoder()
              d.initialize(speex.SPEEX_MODEID_WB)
              p = pyaudio.PyAudio()
              stream = p.open(format = FORMAT,
                              channels = CHANNELS,
                              rate = RATE,
                              input = True,
                              output = True,
                              frames_per_buffer = chunk)
  
              #voice = cPickle.loads(decrypt_my_message(msg))
              voice = cPickle.loads(msg)
              print voice
              for i in range(len(voice)):
                  decdata = d.decode(voice[i])#DECODE my data. (YaY)#DECODE my data. (YaY)
                  stream.write(str(voice), chunk)    #Write the data back out to the speakers
              stream.stop_stream()
              stream.close()
              p.terminate()
              d.destroy()
          if not msg: break
  
      conn.close()
  
  
  ### READ DATA FROM THE MIC ###
  
  def sendAudio():
      chunklist = []
      init_my_audio = speex.Encoder()
      init_my_audio.initialize(speex.SPEEX_MODEID_WB)
      p = pyaudio.PyAudio()
      stream = p.open(format = FORMAT,
                      channels = CHANNELS,
                      rate = RATE,
                      input = True,
                      output = True,
                      frames_per_buffer = chunk)
  
      for i in range(0, 44100 / chunk * RECORD_SECONDS):
          try:
              data = stream.read(chunk)
          except IOError:
              pass
          encdata = init_my_audio.encode(data)
          chunklist.append(encdata)
          client(chr(CMD_AUDIO), cPickle.dumps((chunklist), 1))
      stream.stop_stream()
      stream.close()
      p.terminate()
      init_my_audio.destroy()
  
  </pre>