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?

Cristian
  • 42,563
  • 25
  • 88
  • 99
  • Related: http://stackoverflow.com/questions/507227/high-level-programming-language-for-music-composition – S.Lott Feb 20 '09 at 12:21
  • Why don't you mark your question as answered!? – Shimmy Weitzhandler Jul 17 '10 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 – Chris Arndt Jan 04 '12 at 17:47

7 Answers7

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

Mark
  • 346
  • 3
  • 2
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

dbr
  • 165,801
  • 69
  • 278
  • 343
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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 4
    For people who find this page via Google: note that pyPortMidi was last updated on March 15, 2005. I suspect it has been abandoned. – larsks Jan 09 '12 at 03:18
  • 2
    There is a slightly more up to date version of pyPortMidi included in pyGame, which I have used successfully. – rjmunro Mar 20 '12 at 16:22
  • can't even send pitch bends without writing them in raw hex form? `player.write_short(0xE0, 0x00, 0x60)` :/ – endolith Oct 04 '14 at 04:02
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.

fccoelho
  • 6,012
  • 10
  • 55
  • 67
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)

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
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.

Aaron
  • 566
  • 2
  • 4
  • 13
  • 5
    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](https://bitbucket.org/pygame/pygame) 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](https://portmedia.svn.sourceforge.net/svnroot/portmedia/portmidi/), where the pyPortMidi code is living now. There are several other pyPortMidi repos on the net which are outdated. – Chris Arndt Jan 04 '12 at 18:52
-3

Look at cSounds.

S.Lott
  • 384,516
  • 81
  • 508
  • 779