1

I am working on an audio project where I am using Librosa and have the following code from an example online. Rather than opening up an image with a graph of the amplitude versus time, I want to be able to store the coordinates that make up the graph in an array. I have tried a lot of different examples found on stackoverflow as well as other websites with no luck. I am relatively new to python and this is my first question on stackoverflow so please be kind.

import librosa.display
import matplotlib.pyplot as plt
from IPython.display import display, Audio
filename = 'queen2.mp3'
samples, sampleRate = librosa.load(filename)
display(Audio(filename))
plt.figure(figsize=(12, 4))
librosa.display.waveplot(y, sr=None, max_points=200)
plt.show()
Alan Esses
  • 11
  • 1

1 Answers1

1

librosa is open-source (under the ISC license), so you can look at the code to see how it does this. The documentation for functions has a handy [source] link which takes you do the code. For librosa.display.waveplot you will see that it calls a function __envelope() to compute the envelope. Presumably it is these coordinates you are after.

hop_length = 1
y = __envelope(y, hop_length)

y_top = y[0]
y_bottom = -y[-1]
import numpy as np

def __envelope(x, hop):
    '''Compute the max-envelope of non-overlapping frames of x at length hop

    x is assumed to be multi-channel, of shape (n_channels, n_samples).
    '''
    x_frame = np.abs(util.frame(x, frame_length=hop, hop_length=hop))
    return x_frame.max(axis=1)
Jon Nordby
  • 5,494
  • 1
  • 21
  • 50
  • Hi, thank you for your response. Sorry if this is a dumb question but how would I go about changing the source code in librosa so that my program now uses the edited source code in order to make my program store the coordinates in a list that I am able to access later in the program? – Alan Esses Jul 09 '20 at 18:31
  • Don't modify librosa, just copy the relevant code into your application – Jon Nordby Jul 09 '20 at 21:28