Simple, Cross Platform MIDI Library for Python

Question:

I want to do build a small app that creates MIDI sounds. I’ve never dealt with sound in programming so I’d like to start with something that’s basic and has good documentation. I want to stick with Python since I’m the most comfortable with it and don’t want to overwhelm myself, initially.

My time is split about 50/50 between Windows and Ubuntu so something that “just works” on both platforms would be really helpful.

Any suggestions?

Asked By: Cristian

||

Answers:

pyPortMidi is a Python wrapper of PortMidi, which is described as a “a cross-platform C library for realtime MIDI control”. I haven’t used it myself, but it looks very promising. Explicit mention of being able to send MIDI data in realtime.

Answered By: unwind

Look at cSounds.

Answered By: S.Lott

Midi support (in and out) has been added to pyGame 1.9, though it’s mainly in the development stage and isn’t very well documented yet, but it works.

Midi support is also being developed in the pyGame successor, pgreloaded (or pygame2).

Also note that even though pyGame has ‘game’ in the title, its uses stretch far beyond just game design.

Answered By: Aaron

I was looking for a pure-Python library to generate a MIDI file, mxm’s Python MIDI library is exactly that.

From this dzone snippet, there is a single-file version of the above library, smidi.py (gist’d here for posterity)

Usage is quite simple:

>>> import smidi
>>> m = smidi.MidiOutFile('out.mid')
>>> m.header()
>>> m.start_of_track()
>>> m.update_time(0)
>>> m.note_on(note=0x40)  # single note
>>> m.update_time(192)
>>> m.note_off(note=0x40) # stop it after 192
>>> m.update_time(0)
>>> m.end_of_track()
>>> m.eof()

Presumably works on Windows (as the original example uses C:out.mid as the output filename), and I’ve tested it on OS X

Answered By: dbr

The MIDIUtil Library (hosted here at Google Code) does what you want: write MIDI Files from a pure Python library. Once nice thing about it (and full disclosure: I’m the author) is that you don’t have to keep track of lower-level MID events such as note-on and note-off: it handles them for you.

As an example to write a note, you would do something like:

MyMIDI = MIDIFile(1)
track = 0
channel = 0
pitch = 60
time = 0
duration = 1
volume = 100
MyMIDI.addNote(track,channel,pitch,time,duration,volume)

Hope this helps

Answered By: Mark

If you only need to generate Midi or process midi files, try mingus,
It’s a great package and it also allows much higher abstractions such as chords, chord progressions, scales and so on.

Answered By: fccoelho

I tried eight packages listed on the wiki http://wiki.python.org/moin/PythonInMusic.
I found that the one that music21 (http://web.mit.edu/music21/) was

  • the most comprehensive
  • the best tutorial
  • easiest to install on windows

but as to your request for simplicity, I think it’s not the simplest one. But I couldn’t get any of the other programs to read midi files in a robust way (I have a variety of weird and wonderful midi file formats hanging around)

Answered By: Sanjay Manohar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.