Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created September 3, 2022 02:47
Show Gist options
  • Save victormurcia/f23da41fc5fadca6d8b0260ef895dc5c to your computer and use it in GitHub Desktop.
Save victormurcia/f23da41fc5fadca6d8b0260ef895dc5c to your computer and use it in GitHub Desktop.
Numpy to song with octaves and random pixel choice
song = np.array([])
octaves = np.array([1/2,1,2])
sr = 22050 # sample rate
T = 0.1 # 0.1 second duration
t = np.linspace(0, T, int(T*sr), endpoint=False) # time variable
#Make a song with numpy array :]
#nPixels = int(len(frequencies))#All pixels in image
nPixels = 60
for i in range(nPixels):
octave = random.choice(octaves)
val = octave * random.choice(frequencies)
note = 0.5*np.sin(2*np.pi*val*t)
song = np.concatenate([song, note])
ipd.Audio(song, rate=sr) # load a NumPy array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment