Here's a rather simple example of a mapper implemented in Python just for fun. It lacks round robin and key switches, but that is easily added.
Code: Select all
#!/usr/bin/env python
"""Simple mapper example."""
class Any(object):
"""A wildcard type."""
def __lt__(self, other): return True
def __le__(self, other): return True
def __eq__(self, other): return True
def __ne__(self, other): return True
def __gt__(self, other): return True
def __ge__(self, other): return True
def note_event(key=None, vel=None, ch=None, trigger='attack'):
"""Note event factory."""
type = "note"
return locals()
def control_event(control=None, value=None):
"""Control event factory."""
type = "control"
return locals()
def region(out,
keylo = Any(), keyhi = Any(),
vello = Any(), velhi = Any(),
chlo = Any(), chhi = Any(),
bendlo = Any(), bendhi = Any(),
chaftlo = Any(), chafthi = Any(),
polyaftlo = Any(), polyafthi = Any(),
bmplo = Any(), bmphi = Any(),
trigger = 'attack'
):
"""Region factory."""
return locals()
def status(key=None,
vel=None,
ch=None,
bend=None,
chaft=None,
polyaft=None,
bmp=None,
trigger='attack'):
"""Status matching closure factory."""
def _match(r):
return key >= r['keylo'] and key <= r['keyhi'] and \
vel >= r['vello'] and vel <= r['velhi'] and \
ch >= r['chlo'] and ch <= r['chhi'] and \
bend >= r['bendlo'] and bend <= r['bendhi'] and \
chaft >= r['chaftlo'] and chaft <= r['chafthi'] and \
polyaft >= r['polyaftlo'] and polyaft <= r['polyafthi'] and \
bmp >= r['bmplo'] and bmp <= r['bmphi'] and \
trigger == r['trigger']
return _match
class Mapper(object):
def __init__(self, regions):
self.regions = regions
self.bend = 0
self.chaft = 0
self.polyaft = 0
self.bmp = 0
def map(self, event):
return getattr(self, event['type']+'_event')(event)
def note_event(self, event):
match = status(key=event['key'], vel=event['vel'], ch=event['ch'],
bend=self.bend, chaft=self.chaft, polyaft=self.polyaft,
bmp=self.bmp, trigger=event['trigger'])
return [note_event(event['key'], event['vel'], event['ch'], event['trigger'], r['out'])
for r in self.regions if match(r)]
def control_event(self, event):
getattr(self, 'update_'+event['control'])()
return [event]
def update_bend(self, event):
self.bend = event['value']
def update_chaft(self, event):
self.chaft = event['value']
def update_polyaft(self, event):
self.polyaft = event['value']
def update_bmp(self, event):
self.bmp = event['value']
if __name__ == '__main__':
mapper = Mapper([
region(out=0, keylo=20, keyhi=25),
region(out=1, keylo=26, keyhi=27)
])
print mapper.map(note_event(key=23))
print mapper.map(note_event(key=26))