为什么会抛出 'sample width not specified' 错误?
Why is the 'sample width not specified' error being thrown?
我正在尝试使用 wave 模块写入 Python 3 中的 wav 文件,但出现 'sample width not specified' 异常。这是为什么?
def write_sample(path, sample):
"""Writes a sample to a file at path location
Arguments:
path -- file path
sample -- sample to be written
"""
with wave.open(path, 'w') as wave_file:
# set up (make generic later)
wave_file.setnchannels(1)
wave_file.setsampwidth(16)
wave_file.setframerate(44100)
sample = struct.pack(f"<{len(sample)}h", sample)
wave_file.writeframesraw(sample)
已解决,将 .setsampwidth 方法误读为 n 是位,而不是字节
我正在尝试使用 wave 模块写入 Python 3 中的 wav 文件,但出现 'sample width not specified' 异常。这是为什么?
def write_sample(path, sample):
"""Writes a sample to a file at path location
Arguments:
path -- file path
sample -- sample to be written
"""
with wave.open(path, 'w') as wave_file:
# set up (make generic later)
wave_file.setnchannels(1)
wave_file.setsampwidth(16)
wave_file.setframerate(44100)
sample = struct.pack(f"<{len(sample)}h", sample)
wave_file.writeframesraw(sample)
已解决,将 .setsampwidth 方法误读为 n 是位,而不是字节