Making Music with Python

Trying to create some musical notes by combining harmonic series. Very simple code, but the audio turns up blank. Any thoughts?

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)

enter image description here

Stuff I've tried:

  • Changing the rate
  • Manipulating the y-values
  • Ensuring the harmonic function does indeed work
  • Looking at this answer (same function, but still doesn't work)

Would appreciate any help. Thanks.


The sound generated by the code is audible but weak.

I have no experience in audio programming, but some type of noise resembling a loud beep can be generated by the following:

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)