Skip to content

mxm, IT's mad science

Sections
Personal tools
You are here: Home » Products » Open Source » Python Midi Package » example for multi track MIDI file writing
Downloads
You can download mxm products here.

Due to it's technical and international nature, this section is in english.

Max M Has a blog too.

og er glad for mad

 

Comment

Above in this comment thread: Python Midi Package

example for multi track MIDI file writing

Posted by Anonymous User at 2011-11-03 07:55 AM
Hi

A couple of messages on here seem to have had difficulties writing multitrack MIDI files as output.

The very short & sweet answer is that you must instantiate a type 1 (or 2 i guess) MIDI file to output to a multitrack MIDI file..

anyway, type 0 (the default) will only create a 1 track output file

here is a brief example of how this is achieved (simply by passing appropriate arguments on instantiation)

>>>


from MidiOutFile import MidiOutFile




out_file = 'test.mid'

m = MidiOutFile(out_file)



# non optional midi framework

m.header(1,3)
#(default params are format=0, nTracks=1, division=96)
# refer MIdiOutFile module for details on the functions & the parameter definition



'''

not sure of necessity

but it may be good practice to put timesig & tempo messages etc on track 0
& start the notes on track 1

'''



# musical events

m.start_of_track(0)

m.update_time(0)

m.time_signature(4,2,24,8)

m.tempo(750000)

m.end_of_track()



m.start_of_track(1)

m.sequence_name('Piano')

m.instrument_name('Piano')



m.update_time(0)

m.note_on(channel=1, note=0x3C)

m.update_time(0)

m.note_on(channel=1, note=0x3E)

m.update_time(0)

m.note_on(channel=1, note=0x43)



# advance duration relative 1/4 note

m.update_time(96)

m.note_off(channel=1, note=0x3C)

m.update_time(0)

m.note_off(channel=1, note=0x3E)

m.update_time(0)

m.note_off(channel=1, note=0x43)

m.update_time(0)



# advance duration relative 1/2 note

m.update_time(192)

m.note_on(channel=1, note=0x3C)

m.update_time(0)

m.note_on(channel=1, note=0x3E)

m.update_time(0)

m.note_on(channel=1, note=0x43)



# advance duration relative 1/4 note

m.update_time(96)

m.note_off(channel=1, note=0x3C)

m.update_time(0)

m.note_off(channel=1, note=0x3E)

m.update_time(0)

m.note_off(channel=1, note=0x43)

m.update_time(0)



m.end_of_track()



# track 2



m.start_of_track(2)

m.sequence_name('bleep')

m.instrument_name('bloop')



m.update_time(0)

m.note_on(channel=1, note=0x3C)

m.note_on(channel=1, note=0x3E)



m.note_on(channel=1, note=0x43)



m.update_time(96)

m.note_off(channel=1, note=0x3C)



m.update_time(0)

m.note_off(channel=1, note=0x3E)

m.note_off(channel=1, note=0x43)

m.end_of_track()



# non optional midi framework

m.update_time(0)

m.end_of_track()



m.eof()



Tim in Adelaide