instrument
The Instrument class holds all events for a single instrument and contains functions for extracting information from the events it contains.
- class pretty_midi.instrument.Instrument(program, is_drum=False, name='')[source]
Object to hold event information for a single instrument.
- Parameters:
- programint
MIDI program number (instrument index), in
[0, 127]
.- is_drumbool
Is the instrument a drum instrument (channel 9)?
- namestr
Name of the instrument.
- Attributes:
- programint
The program number of this instrument.
- is_drumbool
Is the instrument a drum instrument (channel 9)?
- namestr
Name of the instrument.
- noteslist
List of
pretty_midi.Note
objects.- pitch_bendslist
List of of
pretty_midi.PitchBend
objects.- control_changeslist
List of
pretty_midi.ControlChange
objects.
- get_onsets()[source]
Get all onsets of all notes played by this instrument. May contain duplicates.
- Returns:
- onsetsnp.ndarray
List of all note onsets.
- get_piano_roll(fs=100, times=None, pedal_threshold=64)[source]
Compute a piano roll matrix of this instrument.
- Parameters:
- fsint
Sampling frequency of the columns, i.e. each column is spaced apart by
1./fs
seconds.- timesnp.ndarray
Times of the start of each column in the piano roll. Default
None
which isnp.arange(0, get_end_time(), 1./fs)
.- pedal_thresholdint
Value of control change 64 (sustain pedal) message that is less than this value is reflected as pedal-off. Pedals will be reflected as elongation of notes in the piano roll. If None, then CC64 message is ignored. Default is 64.
- Returns:
- piano_rollnp.ndarray, shape=(128,times.shape[0])
Piano roll of this instrument.
- get_chroma(fs=100, times=None, pedal_threshold=64)[source]
Get a sequence of chroma vectors from this instrument.
- Parameters:
- fsint
Sampling frequency of the columns, i.e. each column is spaced apart by
1./fs
seconds.- timesnp.ndarray
Times of the start of each column in the piano roll. Default
None
which isnp.arange(0, get_end_time(), 1./fs)
.- pedal_thresholdint
Value of control change 64 (sustain pedal) message that is less than this value is reflected as pedal-off. Pedals will be reflected as elongation of notes in the piano roll. If None, then CC64 message is ignored. Default is 64.
- Returns:
- piano_rollnp.ndarray, shape=(12,times.shape[0])
Chromagram of this instrument.
- get_end_time()[source]
Returns the time of the end of the events in this instrument.
- Returns:
- end_timefloat
Time, in seconds, of the last event.
- get_pitch_class_histogram(use_duration=False, use_velocity=False, normalize=False)[source]
Computes the frequency of pitch classes of this instrument, optionally weighted by their durations or velocities.
- Parameters:
- use_durationbool
Weight frequency by note duration.
- use_velocitybool
Weight frequency by note velocity.
- normalizebool
Normalizes the histogram such that the sum of bin values is 1.
- Returns:
- histogramnp.ndarray, shape=(12,)
Histogram of pitch classes given current instrument, optionally weighted by their durations or velocities.
- get_pitch_class_transition_matrix(normalize=False, time_thresh=0.05)[source]
Computes the pitch class transition matrix of this instrument. Transitions are added whenever the end of a note is within
time_tresh
from the start of any other note.- Parameters:
- normalizebool
Normalize transition matrix such that matrix sum equals to 1.
- time_threshfloat
Maximum temporal threshold, in seconds, between the start of a note and end time of any other note for a transition to be added.
- Returns:
- transition_matrixnp.ndarray, shape=(12,12)
Pitch class transition matrix.
- synthesize(fs=44100, wave=<ufunc 'sin'>)[source]
Synthesize the instrument’s notes using some waveshape. For drum instruments, returns zeros.
- Parameters:
- fsint
Sampling rate of the synthesized audio signal.
- wavefunction
Function which returns a periodic waveform, e.g.
np.sin
,scipy.signal.square
, etc.
- Returns:
- synthesizednp.ndarray
Waveform of the instrument’s notes, synthesized at
fs
.
- fluidsynth(fs=None, synthesizer=None, sfid=0, sf2_path=None)[source]
Synthesize using fluidsynth.
- Parameters:
- fsint
Sampling rate to synthesize at. Default
None
, which takes the sampling rate fromsynthesizer
, or usespretty_midi.fluidsynth.DEFAULT_SAMPLE_RATE
= 44100 if a synthesizer needs to be created.- synthesizerfluidsynth.Synth or str
fluidsynth.Synth instance to use or a string with the path to a .sf2 file. Default
None
, which creates a new instance using the TimGM6mb.sf2 file included withpretty_midi
.- sfidint
Soundfont ID to use if an instance of fluidsynth.Synth is provided. Default
0
, which uses the first soundfont.- sf2_pathstr
Path to a .sf2 file. Default
None
, which uses the TimGM6mb.sf2 file included withpretty_midi
. .. deprecated:: 0.2.11Use :param:`synthesizer` instead.
- Returns:
- synthesizednp.ndarray
Waveform of the MIDI data, synthesized at
fs
.