Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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.

>Solution :

The sound generated by the code is audible but weak.

I have no experience in audio programming, but some chaos of noise can be generated by the following:

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

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

out = Harmonic(1000)
Audio(data=out, rate=8000)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading