pythonaudiolibrosarms

How to read a range of time in Librosa?


I am wondering if there's a way to read a range between specific timing. Currently, I am using librosa to calculate each note of the rms. Here is the following code.

import librosa
import matplotlib.pyplot as plt
y, sr = librosa.load(librosa.ex('trumpet'))
librosa.feature.rms(y=y)
S, phase = librosa.magphase(librosa.stft(y))
rms = librosa.feature.rms(S=S)
times = librosa.times_like(rms)
plt.semilogy(times, rms[0], label='RMS Energy')

Then, it shows enter image description here

But Let's say we just want the timing between 1.8 to 2.4. Then, how to do so?


Solution

  • Librosa loads samples to, looking at your code, to y. The sampling rate, sr, tells you how many samples you have in each second. Knowing the two:

    time_start_seconds = 1.8 
    time_end_seconds = 2.4
    
    time_start_samples = int(time_start_seconds * sr)
    time_end_samples = int(time_end_seconds * sr)
    
    new_y = y[time_start_samples: time_end_samples]
    

    From there you can proceed with rest of your analysis.