7a35ff30c6cbf3d583b719c018976a8cd7175aa3
1 # -*- coding: ISO-8859-1 -*-
3 from MidiOutStream
import MidiOutStream
4 class MidiToText(MidiOutStream
):
8 This class renders a midi file as text. It is mostly used for debugging
11 #############################
14 def channel_message(self
, message_type
, channel
, data
):
15 """The default event handler for channel messages"""
16 print 'message_type:%X, channel:%X, data size:%X' % (message_type
, channel
, len(data
))
19 def note_on(self
, channel
=0, note
=0x40, velocity
=0x40):
20 print 'note_on - ch:%02X, note:%02X, vel:%02X time:%s' % (channel
, note
, velocity
, self
.timeInMs())
22 def note_off(self
, channel
=0, note
=0x40, velocity
=0x40):
23 print 'note_off - ch:%02X, note:%02X, vel:%02X time:%s' % (channel
, note
, velocity
, self
.abs_time())
25 def aftertouch(self
, channel
=0, note
=0x40, velocity
=0x40):
26 print 'aftertouch', channel
, note
, velocity
29 def continuous_controller(self
, channel
, controller
, value
):
30 print 'controller - ch: %02X, cont: #%02X, value: %02X' % (channel
, controller
, value
)
33 def patch_change(self
, channel
, patch
):
34 print 'patch_change - ch:%02X, patch:%02X' % (channel
, patch
)
37 def channel_pressure(self
, channel
, pressure
):
38 print 'channel_pressure', channel
, pressure
41 def pitch_bend(self
, channel
, value
):
42 print 'pitch_bend ch:%s, value:%s' % (channel
, value
)
50 def system_exclusive(self
, data
):
51 print 'system_exclusive - data size: %s' % len(data
)
54 def song_position_pointer(self
, value
):
55 print 'song_position_pointer: %s' % value
58 def song_select(self
, songNumber
):
59 print 'song_select: %s' % songNumber
62 def tuning_request(self
):
63 print 'tuning_request'
66 def midi_time_code(self
, msg_type
, values
):
67 print 'midi_time_code - msg_type: %s, values: %s' % (msg_type
, values
)
71 #########################
72 # header does not really belong here. But anyhoo!!!
74 def header(self
, format
=0, nTracks
=1, division
=96):
75 print 'format: %s, nTracks: %s, division: %s' % (format
, nTracks
, division
)
76 print '----------------------------------'
79 self
.division
= division
85 def start_of_track(self
, n_track
=0):
86 print 'Start - track #%s' % n_track
89 def end_of_track(self
):
98 def sysex_event(self
, data
):
99 print 'sysex_event - datasize: %X' % len(data
)
102 #####################
105 def meta_event(self
, meta_type
, data
):
106 print 'undefined_meta_event:', meta_type
, len(data
)
109 def sequence_number(self
, value
):
110 print 'sequence_number', value
113 def text(self
, text
):
117 def copyright(self
, text
):
118 print 'copyright', text
121 def sequence_name(self
, text
):
122 print 'sequence_name:', text
125 def instrument_name(self
, text
):
126 print 'instrument_name:', text
129 def lyric(self
, text
):
133 def marker(self
, text
):
137 def cuepoint(self
, text
):
138 print 'cuepoint', text
141 def midi_ch_prefix(self
, channel
):
142 print 'midi_ch_prefix', channel
145 def midi_port(self
, value
):
146 print 'midi_port:', value
149 def tempo(self
, value
):
150 print 'tempo:', value
154 def smtp_offset(self
, hour
, minute
, second
, frame
, framePart
):
155 print 'smtp_offset', hour
, minute
, second
, frame
, framePart
158 def time_signature(self
, nn
, dd
, cc
, bb
):
159 print 'time_signature:', nn
, dd
, cc
, bb
162 def key_signature(self
, sf
, mi
):
163 print 'key_signature', sf
, mi
166 def sequencer_specific(self
, data
):
167 print 'sequencer_specific', len(data
)
170 return(long(self
.abs_time())*1000000000/(long(self
.tempo
)*long(self
.division
)))
174 if __name__
== '__main__':
177 test_file
= '../songs/midis/test.mid'
178 f
= open(test_file
, 'rb')
181 from MidiInFile
import MidiInFile
182 midiIn
= MidiInFile(MidiToText(), f
)