用 Python 制作音乐

Making Music with Python

尝试通过组合和声系列来创建一些音符。非常简单的代码,但音频变成空白。有什么想法吗?

from IPython.display import Audio
import numpy as np
import matplotlib.pyplot as plt

def Harmonic(i,linComb):
    x=np.linspace(0,3,24000)
    y = [0 for _ in x] 
    weights = linComb
    for n in range(0,i):
        y += np.sin((2*n+1)*(2*np.pi*weights[n])*(x))/(2*n+1)
    plt.plot(x,y)
    plt.show()
    return y

out = Harmonic(3,[0,2,3])
Audio(data=out, rate=8000)

我尝试过的东西:

非常感谢任何帮助。谢谢

代码生成的声音可以听到但微弱。

我没有音频编程方面的经验,但是一些类类似于响亮哔哔声的噪音可以通过以下方式产生:

from IPython.display import Audio
import numpy as np
import matplotlib.pyplot as plt

def Harmonic(i, weights):
    x=np.linspace(0,3,24000)
    y = [0 for _ in x] 
    for n in range(0,i):
        y += np.sin((2*n+1)*(2*np.pi*weights[n])*(x))/(2*n+1)
    plt.plot(x,y)
    plt.show()
    return y

i = 1000
weights = [1000] * 1000 # Length equal to i

out = Harmonic(i, weights)
Audio(data=out, rate=8000)