MIDI drumloop

To play a drum loop you need a scheduler that places events in time. In the following example this is realised using the time() function from the Python time library.

Sending MIDI events to a MIDI output is done using the midi section of the pygame library. In the function showMidiDevices() all known MIDI devices are queried and their capabilities (input/ouput etc) are shown to the user so they can select the input or output device.

The scheduler uses a list of events. Each event has a timestamp and a MIDI note number. The timestamps are specified in milliseconds.

Why? How is this specified in the code?

In the loop, the actual time (now) is taken from the computer's real-time clock and converted to the relative time in the program. This relative time is compared with the timestamp of the event at the top of the queue and if it's time to play it, a note-on message will be sent to the MIDI output and the event is removed from the queue.

drumloop.py