-def events_on_channel (channel):
- pitches = {}
-
- notes = []
- events = []
- last_lyric = 0
- last_time = 0
- for e in channel:
- t = e[0]
-
- if start_quant_clocks:
- t = quantise_clocks (t, start_quant_clocks)
-
-
- if e[1][0] == midi.NOTE_OFF \
- or (e[1][0] == midi.NOTE_ON and e[1][2] == 0):
- end_note (pitches, notes, t, e[1][1])
-
- elif e[1][0] == midi.NOTE_ON:
- if not pitches.has_key (e[1][1]):
- pitches[e[1][1]] = (t, e[1][2])
-
- # 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
- events.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
- events.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)
- events.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):
- if last_lyric:
- last_lyric.clocks = t - last_time
- events.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:
- events.append ((t, Text (e[1][1], e[1][2])))
- else:
- if global_options.verbose:
- sys.stderr.write ("SKIP: %s\n" % `e`)
- pass
- else:
- if global_options.verbose:
- sys.stderr.write ("SKIP: %s\n" % `e`)
- pass
-
- if last_lyric:
- # last_lyric.clocks = t - last_time
- # hmm
- last_lyric.clocks = clocks_per_4
- events.append ((last_time, last_lyric))
- last_lyric = 0
-
- i = 0
- while len (notes):
- if i < len (events) and notes[0][0] >= events[i][0]:
- i = i + 1
- else:
- events.insert (i, notes[0])
- del notes[0]
- return events
-