def to_midi(self): f = MIDIFile(len(self.melodies), ticksPerBeat=self.ticks_per_beat) time = 0 track = 0 f.addTrackName(track, time, "Lye") f.addTempo(track, time, self.tempo) for channel, melody in self.melodies.iteritems(): # Pan. f.addControllerEvent(track, channel, time, 0x0a, melody.pan) if melody.instrument in numbered_instruments: f.addProgramChange(track, channel, time, numbered_instruments[melody.instrument]) melody.to_midi(f, channel) sio = StringIO() f.writeFile(sio) return sio.getvalue()
class FileExporter(object): """ Export MIDI data to a file. """ def __init__(self, channels, tempo, tpb): self.f = MIDIFile(channels, ticksPerBeat=tpb) self.f.addTrackName(0, 0, "Lye") self.f.addTempo(0, 0, tempo) self.tpb = tpb def commit(self): sio = StringIO() self.f.writeFile(sio) return sio.getvalue() def note(self, channel, time, duration, pitch, velocity): time /= self.tpb duration /= self.tpb self.f.addNote(0, channel, pitch, time, duration, velocity) def bend(self, channel, time, value): pass def pan(self, channel, time, amount): time /= self.tpb self.f.addControllerEvent(0, channel, time, 0x0a, amount) def pc(self, channel, time, program): time /= self.tpb self.f.addProgramChange(0, channel, time, program) def volume(self, channel, time, amount): time /= self.tpb self.f.addControllerEvent(0, channel, time, 0x07, amount)