Exemple #1
0
The smallest possible type 0 midi file. 1 note for 1 bar duration.
"""

from mxm.midifile import MidiOutFile
from mxm.midifile import exampledir

QUARTER = 480
BAR = QUARTER*4
NOTE = 0x3C # middle C
GRAND_PIANO = 0

# in type 0 midi files there is only one track.
# there can be different midi channels in the same track, mo matter
# the type of midi file

out_file = open(exampledir('midi-out/mxm_midifile_type_0_minimal.mid'), 'wb')
midi = MidiOutFile(out_file)
midi.header(format=0, nTracks=1, division=QUARTER)

#####################
# track 0 begin

midi.start_of_track()

midi.tempo_bpm(135)
midi.time_signature(nn=4, dd=2, cc=24, bb=8)
midi.patch_change(0, GRAND_PIANO)

# note on
midi.update_time(0)
midi.note_on(0, NOTE, 0x64)
"""
This is an example that uses the MidiToText eventhandler. When an 
event is triggered on it, it prints the event to the console.

It gets the events from the MidiInFile.

So it prints all the events from the infile to the console. great for 
debugging :-s
"""

from mxm.midifile import exampledir
from mxm.midifile import MidiInFile
from mxm.midifile import MidiToCode

# test_file = testdir('midifiles/cubase-minimal-type1.mid')
# test_file = testdir('midifiles-out/mxm_midifile_type_1_minimal.mid')

# advanced Creative Commons midi file example found at:
# http://www.piano-midi.de/bach.htm
# Copyright © 1996-2016 by Bernd Krueger

test_file = exampledir('midi-in/bach_847.mid')
midiIn = MidiInFile(MidiToCode(), test_file)
midiIn.read()
        'type': 'note_on',
        'pitch': pitch,
        'channel': channel,
        'velocity': 0x40,
        'time': time
    })
    notes.append({
        'type': 'note_off',
        'pitch': pitch,
        'channel': channel,
        'velocity': 0x40,
        'time': time + SIXTENTH
    })
notes.sort(key=lambda x: x['time'])

out_file = open(exampledir('midi-out/mxm_midifile_type_0_8ch_random.mid'),
                'wb')
midi = MidiOutFile(out_file)
midi.header(format=0, nTracks=1, division=QUARTER)

#####################
# track 0 begin

midi.start_of_track()

midi.tempo_bpm(135)
midi.time_signature(nn=4, dd=2, cc=24, bb=8)
for ch in range(8):
    midi.patch_change(ch, GRAND_PIANO)

for note in notes:
        delta = 24
        if ch != 9:  # not the drums!
            if 0 <= (note + delta) <= 127:  # dont transpose out of midi range
                note += delta
        return note

    def note_on(self,
                channel=0,
                note=0x40,
                velocity=0x40,
                use_running_status=False):
        note = self._transp(channel, note)
        MidiOutFile.note_on(self, channel, note, velocity, use_running_status)

    def note_off(self,
                 channel=0,
                 note=0x40,
                 velocity=0x40,
                 use_running_status=False):
        note = self._transp(channel, note)
        MidiOutFile.note_off(self, channel, note, velocity, use_running_status)


FNAME = 'cubase-minimal-type1'
in_file = exampledir('midi-in/%s.mid' % FNAME)
out_file = exampledir('midi-out/%s-transposed.mid' % FNAME)

midi_out = Transposer(out_file)
midi_in = MidiInFile(midi_out, in_file)
midi_in.read()