Beispiel #1
0
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)