Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Last active September 3, 2022 02:10
Show Gist options
  • Save victormurcia/797fe458e5a5415eee844a072d5d7bc5 to your computer and use it in GitHub Desktop.
Save victormurcia/797fe458e5a5415eee844a072d5d7bc5 to your computer and use it in GitHub Desktop.
Convert pandas dataframe to numpy array and make a playable audiofile
frequencies = pixels_df['notes'].to_numpy()
song = np.array([])
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):
val = frequencies[i]
note = 0.5*np.sin(2*np.pi*val*t) #Represent each note as a sign wave
song = np.concatenate([song, note]) #Add notes into song array to make song
ipd.Audio(song, rate=sr) # load a NumPy array as audio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment