Finished the binding/mapping, tests to follow then real midi bindings

This commit is contained in:
Maximilien Rigaut 2019-05-04 19:04:37 +02:00
parent 185ce923e3
commit bc247794cf
3 changed files with 35 additions and 16 deletions

View File

@ -1,4 +1,4 @@
from mapping import BasicMessageParse,BasicAction from mapping import BasicMessageParse,BasicAction,MatchError
import mido import mido
import re,configparser import re,configparser
@ -36,13 +36,20 @@ class Action(BasicAction):
self.config=configparser.ConfigParser() self.config=configparser.ConfigParser()
self.config.read(config) self.config.read(config)
self.sections={} self.sections={}
self.outputs={} #Have every output in a dict self.outputs={} #Have every output in a dict THIS PART IS SUPPOSEDLY ALREADY TAKEN CARE OF
# Very cheesy way to figure out notes from control, I should find a better way, but I can't be bothered to
self.dec10=[str(x) for x in range(10)] self.dec10=[str(x) for x in range(10)]
# Parse config & check sanity # Parse config & check sanity
if "interface" not in self.config: if "interface" not in self.config:
raise IOError #You must define an interface to connect to raise IOError #You must define an interface to connect to
#TODO WARNING; THIS IS SUPPOSEED TO BE TAKEN CARE BY THE FUNCTION CALLING Action()
self.outputs={1:self.config["interface"]["output1"], 2:self.config["interface"]["output2"]}
self.populateSections() self.populateSections()
print self.sections #self.prettyprint()
def populateSections(self): def populateSections(self):
"Create all the call setups" "Create all the call setups"
@ -86,12 +93,10 @@ class Action(BasicAction):
def makeAllKeys(self,key,action,startkey=0,stopkey=0): def makeAllKeys(self,key,action,startkey=0,stopkey=0):
"""Make all the actions associated with the key """Make all the actions associated with the key
startkey and stopkey indicate if an interpolation over the values is to be made""" startkey and stopkey indicate if an interpolation over the values is to be made"""
#print key,action,startkey,stopkey
#Check if there are multiple actions to do #Check if there are multiple actions to do
if ';' in action: if ';' in action:
allactions=[] allactions=[]
for a in action.split(";"): for a in action.split(";"):
#print a
allactions+=self.makeAllKeys(key,a,startkey,stopkey) allactions+=self.makeAllKeys(key,a,startkey,stopkey)
return allactions return allactions
try: try:
@ -110,15 +115,17 @@ class Action(BasicAction):
intensity=127 intensity=127
else: else:
raise ValueError raise ValueError
#print "io:{} act:{} note:{} int:{}".format(interfaceout,midiaction,note,intensity)
intensity,interfaceout=int(intensity),int(interfaceout) intensity,interfaceout=int(intensity),int(interfaceout)
except: except:
print("Config key {} is incorrect, values {} can't be unpacked".format(key,action)) print("Config key {} is incorrect, values {} can't be unpacked".format(key,action))
try:
return [MidiTrigger(self.outputs[interfaceout],midiaction,self.findNote(key,startkey,stopkey,note),intensity)]
return [MidiTrigger(interfaceout,midiaction,note,self.findIntensity(key,startkey,stopkey,note))] except KeyError:
print("Can't find interface {} for note {} action {}, legal interfaces are {}".format(interfaceout,note,action,self.outputs.keys()))
def findIntensity(self,key,startkey,stopkey,note): pass #This choice is not necessary the best, but I figure it's easier that forcing the config to be perfect
return []
def findNote(self,key,startkey,stopkey,note):
"Find the note to bind to the received one"
if startkey!=stopkey: #do an interpolation over the values if startkey!=stopkey: #do an interpolation over the values
if "-" in note: #there is a range to interpolate over to if "-" in note: #there is a range to interpolate over to
nmin,nmax=note.split("-") nmin,nmax=note.split("-")
@ -132,6 +139,7 @@ class Action(BasicAction):
pad=1 pad=1
return nmin+(key-startkey)*pad return nmin+(key-startkey)*pad
return int(note) return int(note)
def format(self,command,number,page): def format(self,command,number,page):
"Format a match to call the correct action" "Format a match to call the correct action"
try: try:
@ -145,6 +153,17 @@ class Action(BasicAction):
# Try a predefined action # Try a predefined action
pass pass
def __repr__(self):
return(self.sections)
def prettyprint(self):
for sect in self.sections:
print(sect)
for subsect in self.sections[sect]:
print(" > "+subsect)
for elt in self.sections[sect][subsect]:
print(" {} : {}".format(elt,self.sections[sect][subsect][elt]))
FTrue=lambda x: True FTrue=lambda x: True
class MidiTrigger(object): class MidiTrigger(object):
@ -165,7 +184,4 @@ class MidiTrigger(object):
pass pass
def __repr__(self): def __repr__(self):
return "MidiTrigger({}/{}/{})@{}".format(self.messagetype,self.value,self.intensity,self.output) return "<MT>({}/{}/{})@{}".format(self.messagetype,self.value,self.intensity,self.output)
class MatchError(Exception):
pass

View File

@ -23,3 +23,6 @@ class BasicAction(object):
def __call__(self,config): def __call__(self,config):
"""Resolve the current action, all actions will be performed depending on what's inside the mapping and the config""" """Resolve the current action, all actions will be performed depending on what's inside the mapping and the config"""
pass pass
class MatchError(Exception):
pass

View File

@ -17,7 +17,7 @@
# Will map linearly 57 to 12; 58 to 14 etc on output 1 # Will map linearly 57 to 12; 58 to 14 etc on output 1
57-67 = 1/cc/12-32 57-67 = 1/cc/12-32
#Map fader 56 to note 152 on output 2 #Map fader 56 to note 152 on output 2
56 = 2/note/152 56 = 7/note/152
# Function choosing the value to send, depending on the intensity received # Function choosing the value to send, depending on the intensity received
56/val = lambda x: min(x,50) 56/val = lambda x: min(x,50)