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).
But it always throws a TypeError.
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('<hh',ldata,rdata)) # Pack it as two signed shorts, little endian
output.writeframes(bytes.join(cooked)) # Write to the wave file
I've also tried ''.join(cooked)
, bytes(cooked)
, and making cooked
a bytearray
from the start, but none of these seem to work.
As above
output.writeframes(bytes.join(cooked)) # Write to the wave file
TypeError: descriptor 'join' requires a 'bytes' object but received a 'list'
Using bytes()
output.writeframes(bytes(cooked)) # Write to the wave file
TypeError: 'bytes' object cannot be interpreted as an integer
Making cooked
a bytearray
cooked.append(struct.pack('<hh',ldata,rdata)) # Pack it as two signed shorts, little endian
TypeError: an integer is required
Sending cooked
in directly
TypeError: memoryview: list object does not have the buffer interface
Using ''.join()
output.writeframes(''.join(cooked)) # Write to the wave file
TypeError: sequence item 0: expected str instance, bytes found
What is the proper way to do this? I can't figure out what exactly Python wants.
EDIT: Using Python 3.4.1 if that affects anything.