pavlos163
pavlos163

Reputation: 2890

Plotting audio from librosa in matplotlib

I am trying to plot the waveform of an audio file in Python.

This is my code (I am using the Librosa library):

import plot as plt

def save_plot(filename):
    y, sr = librosa.load(filename)        
    plt.plot(y, 'audio', 'time', 'amplitude')

Where the plot.py file is:

import matplotlib.pylab as plt

def plot(vector, name, xlabel=None, ylabel=None):
    plt.figure()
    plt.plot(vector)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.plot()
    plt.savefig('static/plots/' + name)

The weird thing is that, even though I get a plot that seems like a valid waveform: waveform

The audio file is only 5 seconds long. Therefore, I don't understand what the x axis is talking about; it seems to go up to 90000?

Thanks

Upvotes: 3

Views: 8044

Answers (2)

Luan Souza
Luan Souza

Reputation: 175

This is why you're using matplotlib.pyplot to plot your vector, which contains many terms as it (probably) samples 22050 data points per second. If you got an audio file with 5 seconds then you get 5 * 22050 = 110250 data points, which will be plotted in the figure. Instead of using matplotlib.pyplot you can just use the proper way to do this with librosa:

import librosa
import librosa.display

y, sr = librosa.load(<path_audio_file>, sr=<sample_rate>)
fig, ax = librosa.display.waveplot(y, sr=sr)

As it retains the sample rate as information, then it will normalize the time series at the right time length!

Note that for using librosa.display you need to explicitly import it.

If you're interested in more details check librosa.display.waveplot.

Upvotes: 1

user7977281
user7977281

Reputation: 66

The waveform will have a data point at every time your audio file is sampled, they can be sampled from 8000 Hz to 48 kHz. 90,000/5 = 18000 Hz.

Look at the variable you're currently ignoring from librosa.load, that is the sampling rate, which will let you figure out the timescale.

Upvotes: 5

Related Questions