25

Referring to this link: https://aqibsaeed.github.io/2016-09-03-urban-sound-classification-part-1/, I am trying to make the same waveplot figure, however,i run the code through .py, there is the error:

(tensorflow) yyydeMacBook-Pro:~ yyy$ python /Users/yyy/Desktop/1.py 
Traceback (most recent call last):
  File "/Users/yyy/Desktop/1.py", line 82, in <module>
    plot_waves(sound_names,raw_sounds)
  File "/Users/yyy/Desktop/1.py", line 42, in plot_waves
    librosa.display.waveplot(np.array(f),sr=22050)
AttributeError: 'module' object has no attribute 'display'
mrry
  • 125,488
  • 26
  • 399
  • 400
Yiin
  • 251
  • 1
  • 3
  • 3

6 Answers6

61

From this github issue I read that it is now necessary to import librosa.display.

[Update 2022] Apparently now also waveplot needs to be changed to waveshow, like below: (thanks to Hammad Hassan in comments!)

import librosa.display
plt.figure(figsize=(12, 4))
librosa.display.waveshow(data, sr=sampling_rate)
Herman
  • 2,738
  • 19
  • 32
  • 6
    Now latest error is, `module 'librosa.display' has no attribute 'waveplot'`. Its fix is to use `waveshow` instead of `waveplot`. – Hammad Hassan Feb 10 '22 at 12:56
  • Thank God they changed the API from waveplot to waveshow. I'm not sure how I would've survived without that backwards-incompatible change. – legel Jul 19 '22 at 21:10
12

If you are running librosa 0.9.0 you will run into following Error using waveplot:

AttributeError: module 'librosa.display' has no attribute 'waveplot'

instead: import librosa.display and use waveshow

path = "sample.wav"
data, sampling_rate = librosa.load(path)
librosa.display.waveshow(data, sr=sampling_rate)
Joe Web
  • 558
  • 5
  • 11
6

just import the display

import librosa.display
plt.figure(figsize=(12, 4))
librosa.display.waveplot(data, sr=sampling_rate)

output

Rahul Verma
  • 2,988
  • 2
  • 11
  • 26
3

Because of the changes in version 0.6.0 of librosa you are getting those errors. I have fixed all the issues in http://aqibsaeed.github.io/2016-09-03-urban-sound-classification-part-1/ and made it work on Python 3, librosa=0.6.0

import glob
import os
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from matplotlib.pyplot import specgram
%matplotlib inline

def load_sound_files(file_paths):
    raw_sounds = []
    for fp in file_paths:
        X,sr = librosa.load(fp)
        raw_sounds.append(X)
    return raw_sounds

def plot_waves(sound_names,raw_sounds):
    i = 1
    #fig = plt.figure(figsize=(25,60), dpi = 900)
    fig = plt.figure(figsize=(25,60))
    for n,f in zip(sound_names,raw_sounds):
        plt.subplot(10,1,i)
        librosa.display.waveplot(np.array(f),sr=22050)
        plt.title(n.title())
        i += 1
    plt.suptitle("Figure 1: Waveplot",x=0.5, y=0.915,fontsize=18)
    plt.show()

def plot_specgram(sound_names,raw_sounds):
    i = 1
    #fig = plt.figure(figsize=(25,60), dpi = 900)
    fig = plt.figure(figsize=(25,60))
    for n,f in zip(sound_names,raw_sounds):
        plt.subplot(10,1,i)
        specgram(np.array(f), Fs=22050)
        plt.title(n.title())
        i += 1
    plt.suptitle("Figure 2: Spectrogram",x=0.5, y=0.915,fontsize=18)
    plt.show()

def plot_log_power_specgram(sound_names,raw_sounds):
    i = 1
    #fig = plt.figure(figsize=(25,60), dpi = 900)
    fig = plt.figure(figsize=(25,60))
    for n,f in zip(sound_names,raw_sounds):
        plt.subplot(10,1,i)
        #D = librosa.logamplitude(np.abs(librosa.stft(f))**2, ref_power=np.max)
        D = librosa.core.amplitude_to_db(np.abs(librosa.stft(f))**2, ref=np.max)
        librosa.display.specshow(D,x_axis='time' ,y_axis='log')
        plt.title(n.title())
        i += 1
    plt.suptitle("Figure 3: Log power spectrogram",x=0.5, y=0.915,fontsize=18)
    plt.show()


sound_file_paths = ["57320-0-0-7.wav","24074-1-0-3.wav","15564-2-0-1.wav","31323-3-0-1.wav",
"46669-4-0-35.wav","89948-5-0-0.wav","40722-8-0-4.wav",
"103074-7-3-2.wav","106905-8-0-0.wav","108041-9-0-4.wav"]

sound_names = ["air conditioner","car horn","children playing",
"dog bark","drilling","engine idling", "gun shot",
"jackhammer","siren","street music"]

raw_sounds = load_sound_files(sound_file_paths)

plot_waves(sound_names,raw_sounds)
plot_specgram(sound_names,raw_sounds)
plot_log_power_specgram(sound_names,raw_sounds)
Beginner
  • 1,202
  • 2
  • 20
  • 29
0
import os
import numpy as np
%pylab inline
import glob
import librosa.display
import matplotlib.pyplot as plt
import tensorflow as tf
plt.figure(figsize=(12,4))
librosa.display.waveplot(data,sr=sampling_rate)
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
  • 1
    Welcome to Stack Overflow, answering questions here is good. But a good answer is more than just a few lines of code. Please use the [edit] link below your answer to add an explanation of why this answers the question. – AdrianHHH Aug 11 '19 at 08:02
0

Old version of librosa has removed waveplot. Use waveshow to get same result.

import librosa
import matplotlib.pyplot as plt

signal, sr = librosa.load('filename.wav')
plt.figure(figsize=(14, 5))
librosa.display.waveshow(signal, sr=sr)
plt.show()
lun
  • 1
  • 1