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

http://echo-nest-remix.googlecode.com/ · Python · 135 lines · 74 code · 6 blank · 55 comment · 4 complexity · 697eccf16779cfe975f0f3f5ff160502 MD5 · raw file

  1. class MidiOutStreamBase:
  2. """
  3. MidiOutStreamBase is Basically an eventhandler. It is the most central
  4. class in the Midi library. You use it both for writing events to
  5. an output stream, and as an event handler for an input stream.
  6. This makes it extremely easy to take input from one stream and
  7. send it to another. Ie. if you want to read a Midi file, do some
  8. processing, and send it to a midiport.
  9. All time values are in absolute values from the opening of a
  10. stream. To calculate time values, please use the MidiTime and
  11. MidiDeltaTime classes.
  12. """
  13. def __init__(self):
  14. # the time is rather global, so it needs to be stored
  15. # here. Otherwise there would be no really simple way to
  16. # calculate it. The alternative would be to have each event
  17. # handler do it. That sucks even worse!
  18. self._absolute_time = 0
  19. self._relative_time = 0
  20. self._current_track = 0
  21. # time handling event handlers. They should overwritten with care
  22. def update_time(self, new_time=0, relative=1):
  23. """
  24. Updates the time, if relative is true, new_time is relative,
  25. else it's absolute.
  26. """
  27. if relative:
  28. self._relative_time = new_time
  29. self._absolute_time += new_time
  30. else:
  31. self._absolute_time = new_time
  32. self._relative_time = new_time - self._absolute_time
  33. def rel_time(self):
  34. "Returns the relative time"
  35. return self._relative_time
  36. def abs_time(self):
  37. "Returns the absolute time"
  38. return self._absolute_time
  39. # track handling event handlers
  40. def set_current_track(self, new_track):
  41. "Sets the current track number"
  42. self._current_track = new_track
  43. def get_current_track(self):
  44. "Returns the current track number"
  45. return self._current_track
  46. #####################
  47. ## Midi events
  48. def channel_message(self, message_type, channel, data):
  49. """The default event handler for channel messages"""
  50. pass
  51. #####################
  52. ## Common events
  53. def system_exclusive(self, data):
  54. """The default event handler for system_exclusive messages"""
  55. pass
  56. def system_common(self, common_type, common_data):
  57. """The default event handler for system common messages"""
  58. pass
  59. #########################
  60. # header does not really belong here. But anyhoo!!!
  61. def header(self, format, nTracks, division):
  62. """
  63. format: type of midi file in [1,2]
  64. nTracks: number of tracks
  65. division: timing division
  66. """
  67. pass
  68. def start_of_track(self, n_track=0):
  69. """
  70. n_track: number of track
  71. """
  72. pass
  73. def eof(self):
  74. """
  75. End of file. No more events to be processed.
  76. """
  77. pass
  78. #####################
  79. ## meta events
  80. def meta_event(self, meta_type, data, time):
  81. """The default event handler for meta_events"""
  82. pass
  83. if __name__ == '__main__':
  84. midiOut = MidiOutStreamBase()
  85. midiOut.update_time(0,0)
  86. midiOut.note_on(0, 63, 127)
  87. midiOut.note_off(0, 63, 127)