title: Writing bytes to wave file?
body: <p>I have some data in Python which I want to output to a WAV file. Right now I'm generating all of the samples as shorts and putting them into a buffer. Then whenever that buffer hits a certain length, I pack the data and send it to writeframes (compromising between writing every sample, which is slow, and holding the whole thing in memory before writing, which is expensive).</p>
  
  <p>But it always throws a TypeError.</p>
  
  <pre><code>output = wave.open(fname, 'wb')
  output.setparams((channels, sample_width, sample_rate, 0, 'NONE', 'not compressed'))
  # ...generate the data, write to the buffer, then if the buffer is full...
  cooked = []
  for (ldata, rdata) in rawdata:
      cooked.append(struct.pack('&lt;hh',ldata,rdata)) # Pack it as two signed shorts, little endian
  output.writeframes(bytes.join(cooked)) # Write to the wave file
  </code></pre>
  
  <p>I've also tried <code>''.join(cooked)</code>, <code>bytes(cooked)</code>, and making <code>cooked</code> a <code>bytearray</code> from the start, but none of these seem to work.</p>
  
  <p>As above</p>
  
  <pre><code>output.writeframes(bytes.join(cooked)) # Write to the wave file
  TypeError: descriptor 'join' requires a 'bytes' object but received a 'list'
  </code></pre>
  
  <p>Using <code>bytes()</code></p>
  
  <pre><code>output.writeframes(bytes(cooked)) # Write to the wave file
  TypeError: 'bytes' object cannot be interpreted as an integer
  </code></pre>
  
  <p>Making <code>cooked</code> a bytearray</p>
  
  <pre><code>cooked.append(struct.pack('&lt;hh',ldata,rdata)) # Pack it as two signed shorts, little endian
  TypeError: an integer is required
  </code></pre>
  
  <p>Sending <code>cooked</code> in directly</p>
  
  <pre><code>TypeError: memoryview: list object does not have the buffer interface
  </code></pre>
  
  <p>Using <code>''.join()</code></p>
  
  <pre><code>output.writeframes(''.join(cooked)) # Write to the wave file
  TypeError: sequence item 0: expected str instance, bytes found
  </code></pre>
  
  <p>What is the proper way to do this? I can't figure out what exactly Python wants.</p>
  
  <p>EDIT: Using Python 3.4.1 if that affects anything.</p>