README.md
1`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.
2
3Documentation is available [here](http://craffel.github.io/pretty-midi/). You can also find a Jupyter notebook tutorial [here](http://nbviewer.jupyter.org/github/craffel/pretty-midi/blob/master/Tutorial.ipynb).
4
5`pretty_midi` is available via [pip](https://pypi.python.org/pypi/pretty_midi) or via the [setup.py](https://github.com/craffel/pretty-midi/blob/master/setup.py) script. In order to synthesize MIDI data using fluidsynth, you need the [fluidsynth](http://www.fluidsynth.org/) program and [pyfluidsynth](https://pypi.python.org/pypi/pyfluidsynth).
6
7If you end up using `pretty_midi` in a published research project, please cite the following report:
8
9Colin Raffel and Daniel P. W. Ellis. [_Intuitive Analysis, Creation and Manipulation of MIDI Data with pretty_midi_](http://colinraffel.com/publications/ismir2014intuitive.pdf). In Proceedings of the 15th International Conference on Music Information Retrieval Late Breaking and Demo Papers, 2014.
10
11
12Example usage for analyzing, manipulating and synthesizing a MIDI file:
13
14```python
15import pretty_midi
16# Load MIDI file into PrettyMIDI object
17midi_data = pretty_midi.PrettyMIDI('example.mid')
18# Print an empirical estimate of its global tempo
19print(midi_data.estimate_tempo())
20# Compute the relative amount of each semitone across the entire song, a proxy for key
21total_velocity = sum(sum(midi_data.get_chroma()))
22print([sum(semitone)/total_velocity for semitone in midi_data.get_chroma()])
23# Shift all notes up by 5 semitones
24for instrument in midi_data.instruments:
25 # Don't want to shift drum notes
26 if not instrument.is_drum:
27 for note in instrument.notes:
28 note.pitch += 5
29# Synthesize the resulting MIDI data using sine waves
30audio_data = midi_data.synthesize()
31```
32
33Example usage for creating a simple MIDI file:
34
35```python
36import pretty_midi
37# Create a PrettyMIDI object
38cello_c_chord = pretty_midi.PrettyMIDI()
39# Create an Instrument instance for a cello instrument
40cello_program = pretty_midi.instrument_name_to_program('Cello')
41cello = pretty_midi.Instrument(program=cello_program)
42# Iterate over note names, which will be converted to note number later
43for note_name in ['C5', 'E5', 'G5']:
44 # Retrieve the MIDI note number for this note name
45 note_number = pretty_midi.note_name_to_number(note_name)
46 # Create a Note instance for this note, starting at 0s and ending at .5s
47 note = pretty_midi.Note(velocity=100, pitch=note_number, start=0, end=.5)
48 # Add it to our cello instrument
49 cello.notes.append(note)
50# Add the cello instrument to the PrettyMIDI object
51cello_c_chord.instruments.append(cello)
52# Write out the MIDI data
53cello_c_chord.write('cello-C-chord.mid')
54```
55