1
0
Fork 0

add midi fnord

This commit is contained in:
entropia 2010-03-31 21:20:08 +02:00
parent 8ed4510a4e
commit 5d93efa09c
45 changed files with 3061 additions and 0 deletions

View file

@ -0,0 +1,76 @@
class EventDispatcherBase:
def __init__(self, outstream):
"""
The event dispatcher generates events on the outstream. This
is the base implementation. It is more like an interface for
how the EventDispatcher. It has the methods that are used by
the Midi Parser.
"""
# internal values, don't mess with 'em directly
self.outstream = outstream
def eof(self):
"End of file!"
self.outstream.eof()
def update_time(self, new_time=0, relative=1):
"Updates relative/absolute time."
self.outstream.update_time(new_time, relative)
# 'official' midi events
def header(self, format, nTracks, division):
"Triggers the header event"
self.outstream.header(format, nTracks, division)
def start_of_track(self, current_track):
"Triggers the start of track event"
# I do this twice so that users can overwrite the
# start_of_track event handler without worrying whether the
# track number is updated correctly.
self.outstream.set_current_track(current_track)
self.outstream.start_of_track(current_track)
# Event dispatchers for midi events
def channel_messages(self, hi_nible, channel, data):
"Dispatches channel messages"
self.outstream.channel_message(hi_nible, channel, data)
def continuous_controllers(self, channel, controller, value):
"Dispatches channel messages"
self.outstream.continuous_controller(channel, controller, value)
def system_commons(self, common_type, common_data):
"Dispatches system common messages"
self.outstream.system_common(common_type, common_data)
def meta_event(self, meta_type, data):
"Dispatches meta events"
self.outstream.meta_event(meta_type, data)
def sysex_events(self, data):
"Dispatcher for sysex events"
self.outstream.sysex_event(data)
if __name__ == '__main__':
from MidiToText import MidiToText
from constants import NOTE_ON
outstream = MidiToText()
dispatcher = EventDispatcherBase(outstream)
dispatcher.channel_messages(NOTE_ON, 0x00, '\x40\x40')

View file

@ -0,0 +1,182 @@
from MidiOutStream import MidiOutStream
class MidiOutPassThrough(MidiOutStream):
"""
This class i mainly used for testing the event dispatcher. The
methods just returns the passed parameters as a tupple.
"""
#####################
## Midi channel events
def note_on(self, channel, note, velocity, time=None):
return channel, note, velocity, time
def note_off(self, channel, note, velocity, time=None):
return channel, note, velocity, time
def aftertouch(self, channel, note, velocity, time=None):
return channel, note, velocity, time
def continuous_controller(self, channel, controller, value, time=None):
return channel, controller, value, time
def patch_change(self, channel, patch, time=None):
return channel, patch, time
def channel_pressure(self, channel, pressure, time=None):
return channel, pressure, time
#####################
## defined continuous controller events
# def cc_
#####################
## Common events
def system_exclusive(self, data, time=None):
return data, time
def song_position_pointer(self, hiPos, loPos, time=None):
return hiPos, loPos, time
def song_select(self, songNumber, time=None):
return songNumber, time
def tuning_request(self, time=None):
return time
#########################
# header does not really belong here. But anyhoo!!!
def header(self, format, nTracks, division):
return format, nTracks, division
def eof(self):
return 'eof'
#####################
## meta events
def start_of_track(self, n_track=0):
return n_track
def end_of_track(self, n_track=0, time=None):
return n_track, time
def sequence_number(self, hiVal, loVal, time=None):
return hiVal, loVal, time
def text(self, text, time=None):
return text, time
def copyright(self, text, time=None):
return text, time
def sequence_name(self, text, time=None):
return text, time
def instrument_name(self, text, time=None):
return text, time
def lyric(self, text, time=None):
return text, time
def marker(self, text, time=None):
return text, time
def cuepoint(self, text, time=None):
return text, time
def midi_port(self, value, time=None):
return value, time
def tempo(self, value, time=None):
return value, time
def smtp_offset(self, hour, minute, second, frame, framePart, time=None):
return hour, minute, second, frame, framePart, time
def time_signature(self, nn, dd, cc, bb, time=None):
return nn, dd, cc, bb, time
def key_signature(self, sf, mi, time=None):
return sf, mi, time
def sequencer_specific(self, data, time=None):
return data, time
#####################
## realtime events
def timing_clock(self, time=None):
return time
def song_start(self, time=None):
return time
def song_stop(self, time=None):
return time
def song_continue(self, time=None):
return time
def active_sensing(self, time=None):
return time
def system_reset(self, time=None):
return time
if __name__ == '__main__':
midiOut = MidiOutStream()
midiOut.note_on(0, 63, 127, 0)
midiOut.note_off(0, 63, 127, 384)

View file

@ -0,0 +1,135 @@
class MidiOutStreamBase:
"""
MidiOutStreamBase is Basically an eventhandler. It is the most central
class in the Midi library. You use it both for writing events to
an output stream, and as an event handler for an input stream.
This makes it extremely easy to take input from one stream and
send it to another. Ie. if you want to read a Midi file, do some
processing, and send it to a midiport.
All time values are in absolute values from the opening of a
stream. To calculate time values, please use the MidiTime and
MidiDeltaTime classes.
"""
def __init__(self):
# the time is rather global, so it needs to be stored
# here. Otherwise there would be no really simple way to
# calculate it. The alternative would be to have each event
# handler do it. That sucks even worse!
self._absolute_time = 0
self._relative_time = 0
self._current_track = 0
# time handling event handlers. They should overwritten with care
def update_time(self, new_time=0, relative=1):
"""
Updates the time, if relative is true, new_time is relative,
else it's absolute.
"""
if relative:
self._relative_time = new_time
self._absolute_time += new_time
else:
self._absolute_time = new_time
self._relative_time = new_time - self._absolute_time
def rel_time(self):
"Returns the relative time"
return self._relative_time
def abs_time(self):
"Returns the absolute time"
return self._absolute_time
# track handling event handlers
def set_current_track(self, new_track):
"Sets the current track number"
self._current_track = new_track
def get_current_track(self):
"Returns the current track number"
return self._current_track
#####################
## Midi events
def channel_message(self, message_type, channel, data):
"""The default event handler for channel messages"""
pass
#####################
## Common events
def system_exclusive(self, data):
"""The default event handler for system_exclusive messages"""
pass
def system_common(self, common_type, common_data):
"""The default event handler for system common messages"""
pass
#########################
# header does not really belong here. But anyhoo!!!
def header(self, format, nTracks, division):
"""
format: type of midi file in [1,2]
nTracks: number of tracks
division: timing division
"""
pass
def start_of_track(self, n_track=0):
"""
n_track: number of track
"""
pass
def eof(self):
"""
End of file. No more events to be processed.
"""
pass
#####################
## meta events
def meta_event(self, meta_type, data, time):
"""The default event handler for meta_events"""
pass
if __name__ == '__main__':
midiOut = MidiOutStreamBase()
midiOut.update_time(0,0)
midiOut.note_on(0, 63, 127)
midiOut.note_off(0, 63, 127)

View file

@ -0,0 +1 @@
Stuff that I am just playing around with