Playing MIDI files

François Pinard pinard at IRO.UMontreal.CA
Thu Oct 7 07:47:48 EDT 1999


mdoering at comlineag.de (Martin Döring) writes:

> Is there a module to play midi-files from python?

If not, and unless someone else feels like taking this duty, I might try
to contribute.  It would be a good exercise for me to learn handling bits
and bytes within Python.

Not so long ago, someone in my family asked me to produce a cassette for some
special event, and despite I had only a few days to prepare it, I decided to
try the MIDI and sequencer route, instead of the perform and record approach,
a bit more usual to me.  This meant rushing, for things to be ready on time.

To verify the music, I needed a simple MIDI sequencer.  As I was much worried
about achieving real-time despite the Linux multi-processing environment, I
quickly studied what provisions were available in the kernel for guaranteed
services, and such.  I found many things, but loosing portability, not to
say the need of acquiring root privileges, did not please me much.

Maybe that for a quick test, perfection could just wait.  So, from the MIDI
specifications, I wrote an humble sequencer in C, to be run as an ordinary
user program.  I just added a bit of code so any delays induced by the system
were exactly compensated by playing subsequent notes a bit faster.  To my
own surprise and astonishment, it went admirably well.  Unless the system
gets very heavily loaded, you do not hear any stumbling in the music flow.
This told me that even if the ear is very sensitive to slight frequency
deviations, it seems surprisingly tolerant detecting asynchronicities at
the detail level, as long as the overall rythm is preserved.

Rewriting from C to Python surely means an appreciable slowdown, but since
the MIDI rendering from the C code was so bearable, even without unusual
system measures, I'm a bit curious to see how much we could torture the code
efficiency, speedwise, before starting to notice appreciable lags.  Once on
this track (sic!), why not even consider the `sched' module?  With some
care, we might get some unusually legible code for a MIDI sequencer! :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list