为什么会引发“未指定样本宽度”错误?

时间:2019-09-08 12:45:09

标签: python wave

我正在尝试使用wave模块在Python 3中写入wav文件,但出现“未指定样本宽度”异常。为什么会这样?

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)

1 个答案:

答案 0 :(得分:0)

已解决,将.setsampwidth方法误读为n是位而不是字节