By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,536 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

Python Midi package 0.1

P: n/a
If anybody is interrested in Midi, but are not on the Python Midi list,
I will just notify that I have released the first version of a high
level midi package for Python.

It is fully functional. It reads and writes midi files! It is *very*
easy to write your own event handlers to make midi files do whatever you
want them to.

It uses the same general ideas as the Sax parser for xml.
#########################################
# example #1

"""
This prints all note_on events on midi channel 0. It's a short example
of creating your own event handler by subclassing the MidiOutStream.
"""

from MidiOutStream import MidiOutStream
from MidiInFile import MidiInFile
class NoteOnPrinter(MidiOutStream):

"Prints all note_on events on channel 0"

def note_on(self, channel=0, note=0x40, velocity=0x40):
if channel == 0:
print channel, note, velocity, self.rel_time()
event_handler = NoteOnPrinter()

in_file = 'midiout/minimal_type0.mid'
midi_in = MidiInFile(event_handler, in_file)
midi_in.read()

#########################################
#########################################
# example #2

"""
This is an example of how to create the smallest possible type 0 midi
file, where all the midi events are in the same track.
"""

from MidiOutFile import MidiOutFile

out_file = 'midiout/minimal_type0.mid'
midi = MidiOutFile(out_file)

# non optional midi framework
midi.header()
midi.start_of_track()

# musical events

midi.update_time(0)
midi.note_on(channel=0, note=0x40)

midi.update_time(192)
midi.note_off(channel=0, note=0x40)

# midi framework

midi.update_time(0)
midi.end_of_track() # not optional!

midi.eof()
#########################################
It cannot read/write midi ports at this point. So no realtime stuff yet.
Get it at:
http://www.mxm.dk/technologies/pythonmidi/
regards Max M

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.