def from_Track(track): """Process a Track object and return the LilyPond equivalent in a string.""" # Throw exception if not hasattr(track, 'bars'): return False lastkey = Key('C') lasttime = (4, 4) # Handle the Bars: result = '' for bar in track.bars: if lastkey != bar.key: showkey = True else: showkey = False if lasttime != bar.meter: showtime = True else: showtime = False result += from_Bar(bar, showkey, showtime) + ' ' lastkey = bar.key lasttime = bar.meter return '{ %s}' % result
def test_key(self): self.assertEqual(self.b.key, Key("C")) self.assertEqual(self.c.key, Key("E"))
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) c = Composition() if header[2]["fps"]: print("Don't know how to parse this yet") return c ticks_per_beat = header[2]["ticks_per_beat"] for track in track_data: t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4, 4) key = "C" for e in track: (deltatime, event) = e duration = float(deltatime) / (ticks_per_beat * 4.0) if duration != 0.0: duration = 1.0 / duration if len(b.bar) > 0: current_length = b.bar[-1][1] b.bar[-1][1] = duration if current_length - duration != 0: b.current_beat -= 1.0 / current_length b.current_beat += 1.0 / duration if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) if event["event"] == 8: if deltatime == 0: pass elif event["event"] == 9: # note on n = Note( notes.int_to_note(event["param1"] % 12), event["param1"] // 12 - 1, ) n.channel = event["channel"] n.velocity = event["param2"] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n elif event["event"] == 10: # note aftertouch pass elif event["event"] == 11: # controller select pass elif event["event"] == 12: # program change i = MidiInstrument() i.instrument_nr = event["param1"] t.instrument = i elif event["event"] == 0x0F: # meta event Text if event["meta_event"] == 1: pass elif event["meta_event"] == 3: # Track name t.name = event["data"].decode("ascii") elif event["meta_event"] == 6: # Marker pass elif event["meta_event"] == 7: # Cue Point pass elif event["meta_event"] == 47: # End of Track pass elif event["meta_event"] == 81: # Set tempo warning Only the last change in bpm will get # saved currently mpqn = self.bytes_to_int(event["data"]) bpm = 60000000 // mpqn elif event["meta_event"] == 88: # Time Signature d = event["data"] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2**self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event["meta_event"] == 89: # Key Signature d = event["data"] sharps = self.bytes_to_int(d[0]) minor = self.bytes_to_int(d[0]) if minor: key = "A" else: key = "C" for i in range(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Key(key) else: print("Unsupported META event", event["meta_event"]) else: print("Unsupported MIDI event", event) t + b c.tracks.append(t) return (c, bpm)
def test_key(self): self.assertEqual(self.b.key, Key('C')) self.assertEqual(self.c.key, Key('E'))