Python Midi package 0.1

M

Max M

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top