Python Forum

Full Version: Find frequencies of an audio file at a specific time via librosa
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am writing a program that will add to a dictionary the exact time, and frequencies (at that time) when a new note is played, in an audio file. I have figured out how to get the exact time a new note is being played. However, I can't figure out or find anywhere that shows how to get the frequencies at the exact time.

I followed a YT video.. (linked at the bottom of my question), and during that lecture, they spoke of onsets. I followed the code and from that, I have managed to get the times of the new notes, as seen in the last line of my code. However, I also want to have the frequencies of that specific time in a list too, as I plan to make a dictionary of the time as a key and the frequencies as the value.

Below is my code so far...
y, sr = librosa.load("audio.wav")

onset_envelope = librosa.onset.onset_strength(y, sr)
onsets = librosa.onset.onset_detect(onset_envelope=onset_envelope)

librosa.onset.onset_detect(y=y, sr=sr, units='time')

D = np.abs(librosa.stft(y))
fig, ax = plt.subplots(nrows=2, sharex=True)
librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), x_axis='time', y_axis='log', ax=ax[0])

o_env = librosa.onset.onset_strength(y, sr=sr)
times = librosa.times_like(o_env, sr=sr)
onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)


ax[1].plot(times, o_env, label='Onset strength')
ax[1].vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9,
           linestyle='--', label='Onsets')

ax[1].legend()
plt.xlabel("Time in seconds")
plt.show()
new_note_times = [times[onset_frames]]          # a list of the exact time a new note is being played
YT link -> https://www.youtube.com/watch?v=MhOdbtPh...=Enthought

Any and all help will be much appreciated! Thanks