将字节写入波形文件?
Writing bytes to wave file?
我在 Python 中有一些数据,我想将其输出到 WAV 文件。现在我正在生成所有样本作为短裤并将它们放入缓冲区。然后每当该缓冲区达到一定长度时,我打包数据并将其发送到 writeframes(在写入每个样本之间进行妥协,这很慢,并且在写入之前将整个数据保存在内存中,这很昂贵)。
但它总是抛出类型错误。
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
,但其中 none 似乎有效.
同上
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。
您需要这样做:
output.writeframes(b''.join(cooked))
方法的第一个参数是self
。如果您正常调用该方法,则会自动传递此参数。但是,如果您通过 Class.method()
调用它,则必须手动传递它。因为你传递了一个列表而不是 bytes
对象作为你的第一个参数,所以你得到了第一个 TypeError。
为了完整起见,以下是剩余的错误:
output.writeframes(bytes(cooked)) # Write to the wave file
TypeError: 'bytes' object cannot be interpreted as an integer
bytes()
接受整数序列;而且它们一次是一个字节。您需要使用按位运算而不是 struct.pack()
(例如 cooked.extend((ldata & 0xFF, ldata >> 8, rdata & 0xFF, rdata >> 8))
用于 little-endian 16 位整数,假设没有大于 0xFFFF 且不考虑负数)。
cooked.append(struct.pack('<hh',ldata,rdata)) # Pack it as two signed shorts, little endian
TypeError: an integer is required
同样,bytearray.append()
接受整数。
output.writeframes(cooked)
TypeError: memoryview: list object does not have the buffer interface
一个list
不是bytes-like对象,所以writeframes()
是不能接受的。
output.writeframes(''.join(cooked)) # Write to the wave file
TypeError: sequence item 0: expected str instance, bytes found
不能混合使用文本和二进制字符串。
我在 Python 中有一些数据,我想将其输出到 WAV 文件。现在我正在生成所有样本作为短裤并将它们放入缓冲区。然后每当该缓冲区达到一定长度时,我打包数据并将其发送到 writeframes(在写入每个样本之间进行妥协,这很慢,并且在写入之前将整个数据保存在内存中,这很昂贵)。
但它总是抛出类型错误。
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
,但其中 none 似乎有效.
同上
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。
您需要这样做:
output.writeframes(b''.join(cooked))
方法的第一个参数是self
。如果您正常调用该方法,则会自动传递此参数。但是,如果您通过 Class.method()
调用它,则必须手动传递它。因为你传递了一个列表而不是 bytes
对象作为你的第一个参数,所以你得到了第一个 TypeError。
为了完整起见,以下是剩余的错误:
output.writeframes(bytes(cooked)) # Write to the wave file
TypeError: 'bytes' object cannot be interpreted as an integer
bytes()
接受整数序列;而且它们一次是一个字节。您需要使用按位运算而不是 struct.pack()
(例如 cooked.extend((ldata & 0xFF, ldata >> 8, rdata & 0xFF, rdata >> 8))
用于 little-endian 16 位整数,假设没有大于 0xFFFF 且不考虑负数)。
cooked.append(struct.pack('<hh',ldata,rdata)) # Pack it as two signed shorts, little endian
TypeError: an integer is required
同样,bytearray.append()
接受整数。
output.writeframes(cooked)
TypeError: memoryview: list object does not have the buffer interface
一个list
不是bytes-like对象,所以writeframes()
是不能接受的。
output.writeframes(''.join(cooked)) # Write to the wave file
TypeError: sequence item 0: expected str instance, bytes found
不能混合使用文本和二进制字符串。