craffel / pretty-midi

Utility functions for handling MIDI data in a nice/intuitive way.
MIT License
873 stars 151 forks source link

Plotting piano roll not working #170

Closed PeterGardas closed 5 years ago

PeterGardas commented 5 years ago

So, I want to use pretty-midi for my ML project, but I am having trouble with the examples at the Jupyter notebook. My code:

import pretty_midi
import numpy as np
# For plotting
import mir_eval.display
import librosa.display
import matplotlib.pyplot as plt

pm = pretty_midi.PrettyMIDI('/home/peter/Downloads/1080-c01.mid')

def plot_piano_roll(pm, start_pitch, end_pitch, fs=4100):
    # Use librosa's specshow function for displaying the piano roll
    librosa.display.specshow(pm.get_piano_roll(fs)[start_pitch:end_pitch],
                             hop_length=1, sr=fs, x_axis='time', y_axis='cqt_note',
                             fmin=pretty_midi.note_number_to_hz(start_pitch))
plt.figure(figsize=(12, 4))
plot_piano_roll(pm, 56, 70)

But, for some unknown reason it doesn't show the plot. Also the piano roll array seems to be all 0 which means according to the code that it didn't detected any notes when in fact the midi is valid and playable.

I am completely lost. Thanks for help.

Peter Gardas

craffel commented 5 years ago

The code you are posting is plotting notes between MIDI note 56 and 70. If the MIDI file you are trying to plot has no notes in that range, you will not see anything in the piano note diagram. You can change this name via the start_pitch and end_pitch arguments to plot_piano_roll.

PeterGardas commented 5 years ago

Checked it, has notes. My problem is that the plot is not showing (not even empty). Is there any problem with Ubuntu 18.04 LTS?

craffel commented 5 years ago

Oh, maybe you don't have the notebook backend for matplotlib installed or something. Does a notebook which just has one cell which contains

import matplotlib.pyplot as plt
%matplotlib inline
plt.plot()

show anything? If not you need to sort out your matplotlib installation, and I'm afraid I can't help you with that.