【问题标题】:python waveform to WAV file converterpython波形到WAV文件转换器
【发布时间】:2018-11-24 17:09:24
【问题描述】:

我正在寻找一种将由 x 轴上的时间和 y 轴上的幅度组成的波形转换为 wav 或任何其他音频文件的方法。非常感谢代码或 python 库

Here is the waveform that I want to convert

【问题讨论】:

    标签: python wav waveform


    【解决方案1】:

    您可以使用标准的wave 库。这是我使用的一个功能。如果您需要更多通道或不同的样本宽度,可以进一步修改它。

    import wave
    import struct
    
    def signal_to_wav(signal, fname, Fs):
        """Convert a numpy array into a wav file.
    
         Args
         ----
         signal : 1-D numpy array
             An array containing the audio signal.
         fname : str
             Name of the audio file where the signal will be saved.
         Fs: int
            Sampling rate of the signal.
    
        """
        data = struct.pack('<' + ('h'*len(signal)), *signal)
        wav_file = wave.open(fname, 'wb')
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(Fs)
        wav_file.writeframes(data)
        wav_file.close()
    

    文档的一些链接:

    https://docs.python.org/3/library/wave.html

    https://docs.python.org/2/library/wave.html

    【讨论】:

    • (str(len(signal)) + 'h') 也可以工作+我认为那更节省内存。 + 我认为你应该提到我的振幅,他的意思是位移,因为固定位移的声音听起来不像任何东西
    猜你喜欢
    • 1970-01-01
    • 2023-01-12
    • 2021-02-01
    • 1970-01-01
    • 2012-11-17
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多