Ejemplo n.º 1
0
class MusicPlayer(EventDispatcher):
    def __init__(self, midiFile, **kw):
        self.tempo = 80
        self.metronome = self._set_up_metronome()
        self.watchers = set()
        self.file_player = MidiFilePlayer(midiFile)
        self.beat_length = self.file_player.beat_length

        self.register_event_type('on_bar')
        # Can add a flag for repeating
        self.file_player.bind(status=self.schedule_music)
        self.file_player.bind(bar_number=self.on_bar)
        super(MusicPlayer, self).__init__(**kw)

    def _set_up_metronome(self):
        metronome = Track()
        bar = Bar('C', (4, 4))
        metronome.add_bar(bar)
        kick = MidiPercussion.BassDrum1
        kick.velocity = 120
        for i in range(4):
            bar.place_notes(kick, value.quarter)
        return metronome

    def on_bar(self, inst, bar_number):
        if bar_number:
            print('bar {}'.format(bar_number))
            for watcher in self.watchers:
                watcher.dispatch('on_bar', bar_number)

    def start(self):
        self.schedule_music(None, PlayStatus.Start)

    def schedule_music(self, inst, status):
        if status == PlayStatus.Start:
            Clock.schedule_once(self.file_player.play)

    def stop(self):
        # Unfortunately, this throws a bunch of assertions and does nothing.
        self.file_player.stop()