美文网首页
Python生成音频

Python生成音频

作者: 多问Why | 来源:发表于2019-03-22 11:02 被阅读0次
    1. 生成一段正弦波音频
    import wave
    import numpy as np
    import struct
    import matplotlib.pyplot as plt
    
    # sample/every second
    framerate = 44100
    # bytes needed every sample
    sample_width = 2
    duration = 5
    frequency = 2000
    volume = 1000
    x = np.linspace(0, duration, num=duration*framerate)
    y = np.sin(2 * np.pi * frequency * x) * volume
    # 将波形数据转换成数组
    sine_wave = y
    #save wav file
    wf = wave.open("sine.wav", 'wb')
    wf.setnchannels(1)
    wf.setframerate(framerate)
    wf.setsampwidth(sample_width)
    for i in sine_wave:
        data = struct.pack('<h', int(i))
        wf.writeframesraw(data)
    wf.close()
    
    

    相关文章

      网友评论

          本文标题:Python生成音频

          本文链接:https://www.haomeiwen.com/subject/xellvqtx.html