rb3652
rb3652

Reputation: 445

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:

Would appreciate any help. Thanks.

Upvotes: 3

Views: 256

Answers (1)

Scene
Scene

Reputation: 507

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)

Upvotes: 1

Related Questions