28

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?

3
  • Related: stackoverflow.com/questions/507227/… Commented Feb 20, 2009 at 12:21
  • Why don't you mark your question as answered!? Commented Jul 17, 2010 at 20:33
  • 4
    Your question is not really clear. You can't "create MIDI sounds". MIDI does not store or transmit sound, only control-data for MIDI-compatible equipment/software. I suppose you want to either a) talk to other MIDI devices/software in real-time using the MIDI interface of your computer or b) read and write standard MIDI files (SMF). These are two different requirements (though many applications will have both) and there are different libraries to suggest for both tasks Commented Jan 4, 2012 at 17:47

7 Answers 7

33

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

Sign up to request clarification or add additional context in comments.

3 Comments

Really late to post this, but can it handle chords? I might be implementing this in my software if it can...
you can easily define chords, right? for pitch in pitches: midi.addNote(.., pitch, ...)
Can you read midi files with this?
10

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

Comments

8

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.

3 Comments

For people who find this page via Google: note that pyPortMidi was last updated on March 15, 2005. I suspect it has been abandoned.
There is a slightly more up to date version of pyPortMidi included in pyGame, which I have used successfully.
can't even send pitch bends without writing them in raw hex form? player.write_short(0xE0, 0x00, 0x60) :/
7

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.

Comments

5

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)

Comments

4

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.

1 Comment

MIDI support in PyGame is based on the same code base as pyPortMidi (see @unwind's answer) and the pyPortMidi code in the PyGame repository is currently the most up-to-date and usable version. Disclaimer: I'm the author of a patch to PyGame's pyPortMidi version, which cleans up the code a lot and fixes grave errors. The patch is not yet applied in the portmidi repository, where the pyPortMidi code is living now. There are several other pyPortMidi repos on the net which are outdated.
-3

Look at cSounds.

2 Comments

Could you expand this answer? The cSounds page doesn't mention MIDI or Python.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.