+ if (e[1][0] == midi.NOTE_OFF
+ or (e[1][0] == midi.NOTE_ON and e[1][2] == 0)):
+ debug ('%d: NOTE OFF: %s' % (t, e[1][1]))
+ if not e[1][2]:
+ debug (' ...treated as OFF')
+ end_note (pitches, notes, t, e[1][1])
+
+ elif e[1][0] == midi.NOTE_ON:
+ if not pitches.has_key (e[1][1]):
+ debug ('%d: NOTE ON: %s' % (t, e[1][1]))
+ pitches[e[1][1]] = (t, e[1][2])
+ else:
+ debug ('...ignored')
+
+ # all include ALL_NOTES_OFF
+ elif (e[1][0] >= midi.ALL_SOUND_OFF
+ and e[1][0] <= midi.POLY_MODE_ON):
+ for i in pitches:
+ end_note (pitches, notes, t, i)
+
+ elif e[1][0] == midi.META_EVENT:
+ if e[1][1] == midi.END_OF_TRACK:
+ for i in pitches:
+ end_note (pitches, notes, t, i)
+ break
+
+ elif e[1][1] == midi.SET_TEMPO:
+ (u0, u1, u2) = map (ord, e[1][2])
+ us_per_4 = u2 + 256 * (u1 + 256 * u0)
+ seconds_per_1 = us_per_4 * 4 / 1e6
+ music.append ((t, Tempo (seconds_per_1)))
+ elif e[1][1] == midi.TIME_SIGNATURE:
+ (num, dur, clocks4, count32) = map (ord, e[1][2])
+ den = 2 ** dur
+ music.append ((t, Time (num, den)))
+ elif e[1][1] == midi.KEY_SIGNATURE:
+ (alterations, minor) = map (ord, e[1][2])
+ sharps = 0
+ flats = 0
+ if alterations < 127:
+ sharps = alterations
+ else:
+ flats = 256 - alterations
+
+ k = Key (sharps, flats, minor)
+ if not t and global_options.key:
+ # At t == 0, a set --key overrides us
+ k = global_options.key
+ music.append ((t, k))
+
+ # ugh, must set key while parsing
+ # because Note init uses key
+ # Better do Note.calc () at dump time?
+ global_options.key = k
+
+ elif (e[1][1] == midi.LYRIC
+ or (global_options.text_lyrics
+ and e[1][1] == midi.TEXT_EVENT)):
+ self.lyrics_p_ = True
+ if last_lyric:
+ last_lyric.clocks = t - last_time
+ music.append ((last_time, last_lyric))
+ last_time = t
+ last_lyric = Text (midi.LYRIC, e[1][2])
+
+ elif (e[1][1] >= midi.SEQUENCE_NUMBER
+ and e[1][1] <= midi.CUE_POINT):
+ text = Text (e[1][1], e[1][2])
+ text.track = self
+ music.append ((t, text))
+ if (text.type == midi.SEQUENCE_TRACK_NAME):
+ self.name = text.text
+ else:
+ if global_options.verbose:
+ sys.stderr.write ("SKIP: %s\n" % `e`)
+ else:
+ if global_options.verbose:
+ sys.stderr.write ("SKIP: %s\n" % `e`)
+
+ if last_lyric:
+ # last_lyric.clocks = t - last_time
+ # hmm
+ last_lyric.clocks = clocks_per_4
+ music.append ((last_time, last_lyric))
+ last_lyric = 0
+
+ i = 0
+ while len (notes):
+ if i < len (music) and notes[0][0] >= music[i][0]:
+ i = i + 1
+ else:
+ music.insert (i, notes[0])
+ del notes[0]
+ return music
+
+class Track (Channel):
+ def __init__ (self):
+ Channel.__init__ (self, None)
+ self.name = None
+ self.channels = {}
+ self.lyrics_p_ = False
+ def _add (self, event):
+ self.events.append (event)
+ def add (self, event, channel=None):
+ if channel == None:
+ self._add (event)
+ else:
+ self.channels[channel] = self.channels.get (channel, Channel (channel))
+ self.channels[channel].add (event)
+ def get_voices (self):
+ return ([self.get_voice ()]
+ + [self.channels[k].get_voice ()
+ for k in sorted (self.channels.keys ())])
+
+def create_track (events):
+ track = Track ()
+ for e in events: