【发布时间】:2015-10-08 17:18:05
【问题描述】:
我有一些 Python 数据,我想输出到 WAV 文件。现在我将所有样本生成为短裤并将它们放入缓冲区。然后,每当该缓冲区达到一定长度时,我就会打包数据并将其发送到写入帧(在写入每个样本之间进行妥协,这很慢,并且在写入之前将整个内容保存在内存中,这很昂贵)。
但它总是抛出 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
我也尝试过''.join(cooked)、bytes(cooked),并从一开始就将cooked 设为bytearray,但这些似乎都不起作用。
如上
output.writeframes(bytes.join(cooked)) # Write to the wave file
TypeError: descriptor 'join' requires a 'bytes' object but received a 'list'
使用bytes()
output.writeframes(bytes(cooked)) # Write to the wave file
TypeError: 'bytes' object cannot be interpreted as an integer
使cooked 成为一个字节数组
cooked.append(struct.pack('<hh',ldata,rdata)) # Pack it as two signed shorts, little endian
TypeError: an integer is required
直接发送cooked
TypeError: memoryview: list object does not have the buffer interface
使用''.join()
output.writeframes(''.join(cooked)) # Write to the wave file
TypeError: sequence item 0: expected str instance, bytes found
这样做的正确方法是什么?我无法弄清楚 Python 到底想要什么。
编辑:如果有任何影响,请使用 Python 3.4.1。
【问题讨论】:
标签: python python-3.x byte wav wave