6 scale_steps =[0,2,4,5,7,9,11]
8 def split_channels (track):
16 e = (e[0], tuple ([data[0] & 0xf0] + data[1:]))
24 for v in chs.values ():
25 ns = notes_on_channel(v)
26 ns = unthread_notes (ns)
33 def __init__(self, duration, pitch, velocity):
34 self.velocity = velocity
36 self.duration = duration
38 def duration_compare(a,b):
39 if a.duration < b.duration :
41 elif a.duration > b.duration:
46 def notes_on_channel (channel):
53 if e[1][0] == midi.NOTE_ON:
54 if not pitches.has_key (e[1][1]):
55 pitches[e[1][1]] = (t, e[1][2])
56 elif e[1][0] == midi.NOTE_OFF:
58 (lt, vel) = pitches[e[1][1]]
61 nch.append ((t, Note (t-lt, e[1][1], vel)))
70 def unthread_notes (channel):
79 if (t == start_busy_t and e[1].duration + t == end_busy_t) \
83 end_busy_t = t + e[1].duration
86 threads.append (thread)
102 wholes = dt / (4*384)
108 print 's1*%d' % wholes
110 print 's4*%d' % quarters
114 def dump_duration (dur):
116 sys.stdout.write ('4')
117 (p,q) = (dur / g, 384 / g)
118 if (p == 1 and q == 1) :
122 sys.stdout.write ('*%d'% p)
124 sys.stdout.write ('*%d'% q)
127 def dump_note (note):
133 while i < len (scale_steps):
134 if scale_steps[i] > step:
139 str = chr (i + ord ('a'))
140 if scale_steps[i] <> step:
143 sys.stdout.write (' %s' % str);
144 dump_duration (note.duration)
150 sys.stdout.write ("<")
152 sys.stdout.write ('>')
154 def dump_thread (thread):
160 if last_e and last_e[0] == e[0]:
164 chs.append ( (last_e[0], ch))
170 if ch: chs.append ((last_e[0] ,ch))
180 last_t = t + ch[1][0].duration
182 def dump_notes (channel):
195 def convert_midi (f):
196 str = open (f).read ()
197 midi_dump = midi.parse (str)
201 for t in midi_dump[1]:
202 channels.append (split_channels (t))
205 for f in sys.argv[1:]: