pretty_midi contains utility function/classes for handling MIDI data, so that it's in a format which is easy to modify and extract information from.
Documentation is available here. You can also find a Jupyter notebook tutorial here (click here to load in Colab).
pretty_midi is available via pip or via the setup.py script. In order to synthesize MIDI data using fluidsynth, you need the fluidsynth program and pyfluidsynth.
If you end up using pretty_midi in a published research project, please cite the following report:
Colin Raffel and Daniel P. W. Ellis. Intuitive Analysis, Creation and Manipulation of MIDI Data with pretty_midi. In Proceedings of the 15th International Conference on Music Information Retrieval Late Breaking and Demo Papers, 2014.
Example usage for analyzing, manipulating and synthesizing a MIDI file:
importpretty_midi# Load MIDI file into PrettyMIDI objectmidi_data=pretty_midi.PrettyMIDI('example.mid')
# Print an empirical estimate of its global tempoprint(midi_data.estimate_tempo())
# Compute the relative amount of each semitone across the entire song, a proxy for keytotal_velocity=sum(sum(midi_data.get_chroma()))
print([sum(semitone)/total_velocityforsemitoneinmidi_data.get_chroma()])
# Shift all notes up by 5 semitonesforinstrumentinmidi_data.instruments:
# Don't want to shift drum notesifnotinstrument.is_drum:
fornoteininstrument.notes:
note.pitch+=5# Synthesize the resulting MIDI data using sine wavesaudio_data=midi_data.synthesize()Example usage for creating a simple MIDI file:
importpretty_midi# Create a PrettyMIDI objectcello_c_chord=pretty_midi.PrettyMIDI()
# Create an Instrument instance for a cello instrumentcello_program=pretty_midi.instrument_name_to_program('Cello')
cello=pretty_midi.Instrument(program=cello_program)
# Iterate over note names, which will be converted to note number laterfornote_namein ['C5', 'E5', 'G5']:
# Retrieve the MIDI note number for this note namenote_number=pretty_midi.note_name_to_number(note_name)
# Create a Note instance for this note, starting at 0s and ending at .5snote=pretty_midi.Note(velocity=100, pitch=note_number, start=0, end=.5)
# Add it to our cello instrumentcello.notes.append(note)
# Add the cello instrument to the PrettyMIDI objectcello_c_chord.instruments.append(cello)
# Write out the MIDI datacello_c_chord.write('cello-C-chord.mid')