Python Notes

Friday, August 20, 2004

Implementing real time MIDI playback

After some thought, I decided to give it a try and implement real time playback using the MIDI Streams API. I installed ctypes, an outstanding Python library to support native DLL calls from Python. After hacking with it a little bit, I managed to implement the basic midiStreamOpen and midiStreamClose calls. The library is actually fairly easy to use, but there were a few gotchas:
  • I mistakenly assumed that I would have to load the mmsystem.dll library. In fact, I had to load winmm.dll. What made this problem worse is that the error message when trying to load mmsystem.dll is totally misleading, and I had to count on the help of the ctypes-users mailing list to solve it.

  • The winmm.dll library is 16 bit - weird, but understandable, given Microsoft tradition. This was not an issue up to this point, but I'm better be prepared for surprises.

  • It takes some time to get to speed to translate C protoype definitions (with all those ugly hungary notation names) in the correspondent ctypes for Python. It's not hard, but a few mistakes were met with segfaults and stuff like that.

In the end, everything ran fine. I'm now working on the hardest part - the midiStreamOut call. The call itself is easy, bu I have first to set up the MIDIHDR and MIDIEVENT buffers. Both structures form simple linked lists with real C pointers, and that's something that I haven't done for a long time (being first a Pascal/Delphi nut, and now a Python lover). I fear that a few segfaults expect me there.

There is another thing that I absolutely have to do, that is to understand better the MIDI tempo stuff. Some of the information on the MIDI header is tempo related. My current model for the guitar is time-based -- the argument for the right hand movement defines how long does it take for each beat, in seconds. Now I have to change everything to expressed in terms of quarter notes and beats per minute. It's a small paradigm change, but clearly for better, and I'm still on the early stages of design so it's welcome.


Post a Comment

<< Home