繁体   English   中英

Python波形到WAV文件转换器

[英]python waveform to WAV file converter

我正在寻找一种将x轴上的时间和y轴上的振幅组成的波形转换为wav或任何其他音频文件的方法。 代码或python库备受赞赏

这是我要转换的波形

您可以使用标准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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM