From 42c0be18c2426a45e3f0b627aa6cf10648e2cb7d Mon Sep 17 00:00:00 2001 From: dandaedre Date: Tue, 21 Sep 2021 12:56:16 +0200 Subject: [PATCH] Cleaned an overdue sectiono --- mapping/base.py | 80 ++++++++++++++++++++++++------------------- mapping/keystrokes.py | 57 +----------------------------- midiRebind.py | 4 +-- patch/pulse.ini | 12 +++---- patch/test.ini | 1 + 5 files changed, 52 insertions(+), 102 deletions(-) diff --git a/mapping/base.py b/mapping/base.py index f454852..e8625d5 100644 --- a/mapping/base.py +++ b/mapping/base.py @@ -123,40 +123,43 @@ class BasicActions(object): else: self.sections[elt]={"*":self.makeTriggers(self.config[elt])} + def makeOneTrigger(self,keys,key_i,special,actions_t,startkey,stopkey): + "Update or make one trigger and add it to the dict of triggers" + if key_i not in keys: # Must now initialise + # print(type(key_i)) + keys[key_i]=[] + if special: + trigg=EmptyTrigger(self) + trigg.addspecial(special,actions_t) + keys[key_i]+=[trigg] + else: + keys[key_i]+=self.makeAllKeys(key_i,actions_t,startkey=startkey,stopkey=stopkey) + + for trigger in keys[key_i]: + if special: + trigger.addspecial(special,actions_t) + else: + keys[key_i]=self.makeAllKeys(key_i,actions_t,startkey=startkey,stopkey=stopkey) #Do a different trigger for every ; and link to a different interface for every n/ + def makeTriggers(self,conf): "Take a section of a config and put all triggers in a dict" keys={} + # TODO : THIS WHOLE SECTION IS A BLODDY MESS for key in conf: # Reading every mapping and linking it to the appropriate Trigger - if "/" in key: # There is a condition to an action - note,atype=key.split("/") - if "-" in note: - nmin,nmax=note.split("-") - nmin,nmax=trunc(float(nmin)),trunc(float(nmax)) - for i in range(nmin,nmax+1): # TODO : If there are exactly as many actions as the keys inside the dict, add one action per key - for trigger in (keys[int(i)]): # TODO : If the key doesn't exist, throw a # WARNING: instead of an error - trigger.addspecial(atype,conf[key]) - else: - # Special function can be executed with no triggers associated - try: - for trigger in keys[trunc(float(note))]: - trigger.addspecial(atype,conf[key]) - except KeyError: - trigg=EmptyTrigger(self) - trigg.addspecial(atype,conf[key]) - keys[trunc(float(note))]=[trigg] - # print("No el key") - elif "-" in key: # There is a range of values to map - nmin,nmax=key.split("-") - nmin,nmax=trunc(float(nmin)),trunc(float(nmax)) - for i in range(nmin,nmax+1): - if i not in keys: - keys[i]=[] - keys[i]+=self.makeAllKeys(i,conf[key],startkey=nmin,stopkey=nmax) #Do a different trigger for every ; and link to a different interface for every n/ - else: # Nothing fancy - key_i=trunc(float(key)) - if key_i not in keys: - keys[key_i]=[] - keys[key_i]+=self.makeAllKeys(key_i,conf[key],key_i,key_i) + if "/" in key: + base,special=key.split("/") + else: + base,special=key,"" + + if "-" in base: + key_start,key_stop=base.split("-") + key_start,key_stop=trunc(float(key_start)),trunc(float(key_stop)) + for key_i in range(key_start,key_stop+1): + self.makeOneTrigger(keys,key_i,special,conf[key],startkey=key_start,stopkey=key_stop) + else: + key_i=trunc(float(base)) + self.makeOneTrigger(keys,key_i,special,conf[key],startkey=key_i,stopkey=key_i) + return keys def makeAllKeys(self,key,action,startkey=0,stopkey=0): @@ -166,7 +169,7 @@ class BasicActions(object): if ';' in action: allactions=[] for a in action.split(";"): - allactions+=self.makeAllKeys(key,a,startkey,stopkey) + allactions+=self.makeAllKeys(key,a,startkey=startkey,stopkey=stopkey) return allactions binded=False try: @@ -197,11 +200,11 @@ class BasicActions(object): except (KeyError): print("[WARNING] Can't find interface {} for note {} action {}, legal interfaces are {}".format(interfaceout,note,action,[i for i in self.interface.outputs.keys()])) return [] #This choice is not necessary the best, but I figure it's easier that forcing the config to be perfect - except ValueError: - import sys - print(sys.exc_info()) - print("[WARNING] Incorrect values given to the midi message {}".format(action)) - return [] + # except ValueError: + # import sys + # print("[WARNING] Incorrect values given to the midi message {} {} {}:{}".format(action,key,startkey,stopkey)) + # print("[Exec Info]", sys.exc_info()) + # return [] def findNote(self,key,startkey,stopkey,note): "Find the note to bind to the received one" @@ -217,6 +220,11 @@ class BasicActions(object): else: pad=1 return nmin+(key-startkey)*pad + else: + if "-" in note: # Bind ALL notes to a single one, this is not doable this way + print(startkey,stopkey) + print("[WARNING] As of now it is not possible to bind one note to severall output notes, ({})->({})".format(note,key)) + return int(note.split("-")[0]) return int(note) def findAction(self,command,number,page): diff --git a/mapping/keystrokes.py b/mapping/keystrokes.py index 0554076..a9ba15b 100644 --- a/mapping/keystrokes.py +++ b/mapping/keystrokes.py @@ -5,63 +5,8 @@ from mapping.base import BasicMessageParse,BasicActions,MatchError,BasicMidiInte import mido import re,configparser - -#A MessageParse object MUST be included in the file, the rest is implementation Specific from mapping.midi import MessageParse -# class MessageParse(BasicMessageParse): -# """Parsing of a midi message this parser is used once and will return a different Action object every time it is called""" -# -# def __init__(self): -# #The data is converted from hex to decimal for readability and usability by mido -# self.type=None #note_off note_on control_change program_change sysex (technically all the others but we don't care much for it) -# self.channel=0 #0 by default, but can be anything -# self.note=None #0 - 127 (it will be the program in program_change etc...) -# self.value=None #The actual value, sometimes None -# -# def __call__(self,message): -# "Parse the message and return either a match or raise an error" -# self.type=message.type -# try: -# if message.type=="note_off" or message.type=="note_on": -# self.channel=message.channel -# self.note=message.note -# self.value=message.velocity -# elif message.type=="control_change": -# self.channel=message.channel -# self.note=message.control -# self.value=message.value -# elif message.type=="program_change": -# self.channel=message.channel -# self.note=message.program -# self.value=None -# else: -# raise MatchError("Message is not either note_off, note_on, control_change or program_change") -# return self -# except: -# raise MatchError("Can't parse the data {}".format(message)) -# -# def __str__(self): -# return "[ty:{}/note:{}/value:{}]@{}".format(self.type,self.note,self.value,self.channel) -# An Action object MUST be included in the file. from mapping.midi import Actions -# class Actions(BasicActions): -# """The list of action to perform when a message is read""" -# def __init__(self, interface): -# super(Actions, self).__init__(interface) -# self.listfunctions["presskey"]=presskey -# # self.listfunctions[""]= -# -# # This funtion is MANDATORY, it makes the link between MessageParse and Action -# def __call__(self,match): -# "Make the link between the regex and the action" -# id,value,page="1","1","*" -# try: -# listact=self.findAction(match.type,match.note,"*") -# for act in listact: -# act(match.value) -# except: -# # No action linked to the trigger -# pass def presskey(val,*params): "Press the given keys" @@ -74,4 +19,4 @@ class MidiInterface(BasicMidiInterface): def __init__(self,configfile): super(MidiInterface,self).__init__(configfile) - self.functionlist["testfunction"]=testfunction + self.functionlist["presskey"]=presskey diff --git a/midiRebind.py b/midiRebind.py index 58ef20d..19811dd 100755 --- a/midiRebind.py +++ b/midiRebind.py @@ -45,8 +45,8 @@ if __name__ == '__main__': print("[Unexpected error@midibind.py] {}".format(sys.exc_info())) except ImportError as e: print("[Error] There was an error loading module {} ({})".format(interpath,e)) - except KeyError: - print("[Error] Patch {} doesn't exist".format(path)) + except KeyError as e: + print("[Error] There was an error while loading patch {} ({})".format(path,e)) except KeyboardInterrupt: print("[Keyboard Interrupt] : Exiting...") del Inter diff --git a/patch/pulse.ini b/patch/pulse.ini index 50d1585..82150d2 100644 --- a/patch/pulse.ini +++ b/patch/pulse.ini @@ -1,19 +1,15 @@ [interface] input=APC MINI MIDI 1 - output=APC MINI MIDI 1 + output1=APC MINI MIDI 1 inputchanel = 1 outputchannel1 = 1 #outputchannel2 = 1 mapping = keystrokes [note_on] - 1-99 = 1/on/1 - 1-99/toggle = 127/0 - 1 = 2/cc/1/45;2/cc/2/56;2/cc/3/87;2/cc/4/12;2/cc/5/50;2/cc/6/100 - 3 = 2/cc/1/89;2/cc/2/13;2/cc/3/100;2/cc/4/50;2/cc/5/80;2/cc/6/30 - 4 = 2/cc/7;2/cc/8;2/cc/9;2/cc/10;2/cc/11;2/cc/12 - 4/toggle = 0/127 - + #1-99 = 1/on/1 + #1-99/toggle = 127/0 + 1-99/funct = presskey("a","z") #[control_change] # 16-18 = 2/cc/1 diff --git a/patch/test.ini b/patch/test.ini index 4d8c0a5..a41ac44 100644 --- a/patch/test.ini +++ b/patch/test.ini @@ -12,6 +12,7 @@ [set/1] #Send (midi) control change (10) with value mapped to the fader (1) value (fader 1 => cc 10) on output 1 0-4 = 1/cc/10 +# 122 = 1/cc/22-55 #Map the faders 5-50 to the control change 11-45 (Straight patch) on output 1 5-50 = 1/cc/11 # Will map linearly 57 to 12; 58 to 14 etc on output 1