As part of the work that I've done with generative music, a few years ago I developed a Python library for writing multi-track MIDI files and released it under an open-source (MIT) license. The code was hosted on Google Code, and when that service went away the code kind of languished.
I've recently ported the code-base to github, updated the documentation (and placed it on Read The Docs), cleaned up the code, and uploaded it to the Python Package Index.
Some of the highlights of the new code include:
- Unified support for Python 2.6+ and Python 3.0.
- Bug fixes.
- Code clean-up.
- Updated documentation.
- Improved sorting of events in the file stream.
- Added functions useful in using micro-tonalities (selection of tuning program and tuning bank).
- Added functions to create arbitrary Non-Registered Parameter calls, as well as Registered Parameter calls.
The code is indexed on the Python Package Index (MIDIUtil) and can be installed in the usual way:
pip install MIDIUtil
The code-base is hosted on GitHub (MIDIUtil), and the documentation can be found at Read The Docs (either the latest stable version or the development version).
The library is intended to make it possible to write MIDI files from python in an easy and straight-forward way. As an example, the following code creates a single-track file, writes a C-major scale at 60 BPM, and writes the file to disk:
#!/usr/bin/env python from midiutil.MidiFile import MIDIFile degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number track = 0 channel = 0 time = 0 # In beats duration = 1 # In beats tempo = 60 # In BPM volume = 100 # 0-127, as per the MIDI standard MyMIDI = MIDIFile(1) # One track MyMIDI.addTempo(track, time, tempo) for pitch in degrees: MyMIDI.addNote(track, channel, pitch, time, duration, volume) time = time + 1 with open("major-scale.mid", "wb") as output_file: MyMIDI.writeFile(output_file)