/src/echonest/support/midi/experimental/EventDispatcherBase.py

http://echo-nest-remix.googlecode.com/ · Python · 76 lines · 37 code · 27 blank · 12 comment · 1 complexity · 64ea7f519085b09e60dac050bedb6d44 MD5 · raw file

  1. class EventDispatcherBase:
  2. def __init__(self, outstream):
  3. """
  4. The event dispatcher generates events on the outstream. This
  5. is the base implementation. It is more like an interface for
  6. how the EventDispatcher. It has the methods that are used by
  7. the Midi Parser.
  8. """
  9. # internal values, don't mess with 'em directly
  10. self.outstream = outstream
  11. def eof(self):
  12. "End of file!"
  13. self.outstream.eof()
  14. def update_time(self, new_time=0, relative=1):
  15. "Updates relative/absolute time."
  16. self.outstream.update_time(new_time, relative)
  17. # 'official' midi events
  18. def header(self, format, nTracks, division):
  19. "Triggers the header event"
  20. self.outstream.header(format, nTracks, division)
  21. def start_of_track(self, current_track):
  22. "Triggers the start of track event"
  23. # I do this twice so that users can overwrite the
  24. # start_of_track event handler without worrying whether the
  25. # track number is updated correctly.
  26. self.outstream.set_current_track(current_track)
  27. self.outstream.start_of_track(current_track)
  28. # Event dispatchers for midi events
  29. def channel_messages(self, hi_nible, channel, data):
  30. "Dispatches channel messages"
  31. self.outstream.channel_message(hi_nible, channel, data)
  32. def continuous_controllers(self, channel, controller, value):
  33. "Dispatches channel messages"
  34. self.outstream.continuous_controller(channel, controller, value)
  35. def system_commons(self, common_type, common_data):
  36. "Dispatches system common messages"
  37. self.outstream.system_common(common_type, common_data)
  38. def meta_event(self, meta_type, data):
  39. "Dispatches meta events"
  40. self.outstream.meta_event(meta_type, data)
  41. def sysex_events(self, data):
  42. "Dispatcher for sysex events"
  43. self.outstream.sysex_event(data)
  44. if __name__ == '__main__':
  45. from MidiToText import MidiToText
  46. from constants import NOTE_ON
  47. outstream = MidiToText()
  48. dispatcher = EventDispatcherBase(outstream)
  49. dispatcher.channel_messages(NOTE_ON, 0x00, '\x40\x40')