From b31a20c9081afc40b540357a9816264b3b19fd20 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Mon, 6 May 2019 06:27:37 +0200 Subject: [PATCH] Refined the base interfaces and squished all bugs, only interface is left to do --- mapping/GrandMA2.py | 41 ++++++++++++++++++++++++----------------- mapping/base.py | 10 +++++++--- midiRebind.py | 11 +++++------ patch/default.ini | 24 ++++++------------------ patch/test.ini | 2 +- test.py | 43 +++++++++++++++++++++++-------------------- 6 files changed, 66 insertions(+), 65 deletions(-) diff --git a/mapping/GrandMA2.py b/mapping/GrandMA2.py index cd869db..cc18a26 100644 --- a/mapping/GrandMA2.py +++ b/mapping/GrandMA2.py @@ -8,25 +8,33 @@ class MessageParse(BasicMessageParse): """Parsing of a gma SyEx message this parser is used once and will return a different Action object every time it is called""" def __init__(self): - #The init here is irrelevant #The data is converted from hex to decimal for readability and usability by mido - self._mapping = {"deviceid":127, #Will usually be 127 for ALL - "commandformat":127, #1 is general lighting format, 2 is moving light format and 127 is ALL - "command":None, #from 1 to 7 plus 11 (off) - "data":None #The actual data - } - self.pattern=re.compile("240 127 (?P\d+) 0?2 (?P\d+) (?P\d+) (?P[\d+ ]+) 247") #The regex that will map the SysEx message + self.deviceid=127 #Will usually be 127 for ALL + self.commandformat=127 #1 is general lighting format, 2 is moving light format and 127 is ALL + self.command=None #from 1 to 7 plus 11 (off) + self.data=None #The actual data + #very pretty solution that is actually not needed... + #self.pattern=re.compile("240 127 (?P\d+) 0?2 (?P\d+) (?P\d+) (?P[\d+ ]+) 247") #The regex that will map the SysEx message + self._commandlist=commandlist def __call__(self,message): "Parse the message and return either a match or raise an error" - match=re.match(self.pattern,message) - if match: - return match + if message.type=="sysex": + message=message.data else: - raise MatchError - + raise MatchError("Message is not sysex") + try: + self.deviceid=message[1] + self.commandformat=message[3] + self.command=message[4] + self.data=message[5:] + return self + except: + raise MatchError("Can't parse the data {}".format(message)) + def __str__(self): + return "[id:{}/df:{}/cmd:{}/data:{}]".format(self.deviceid,self.commandformat,self.command,self.data) # An Action object MUST be included in the file. class Actions(BasicActions): """The list of action to perform when a message is read""" @@ -36,19 +44,18 @@ class Actions(BasicActions): # 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" - action=commandlist[int(match.group('command'))] + action=commandlist[int(match.command)] id,value="1","1" if action=="set": - data=match.group("data") - data=data.split(" ") + data=match.data page=int(data[1]) id=int(data[0]) # value=int(data[2])/128+int(data[3])/1.28 the percent value value=int(data[3]) # Just mapped from 0 to 127 elif action=="fire": - id=int(match.group("data")) # Can only be from 0 to 255 ? + id=int(match.data) # Can only be from 0 to 255 ? value="" - #print(action,id,page) + page="" try: listact=self.findAction(action,id,page) for act in listact: diff --git a/mapping/base.py b/mapping/base.py index 85a77ad..b65ea48 100644 --- a/mapping/base.py +++ b/mapping/base.py @@ -59,7 +59,7 @@ class BasicMidiInterface(object): self.input.close except: pass - + class BasicMessageParse(object): """Classic System Specific Midi Message as Hex (BasicSyHexParse). @@ -132,9 +132,13 @@ class BasicActions(object): nmin,nmax=key.split("-") nmin,nmax=int(nmin),int(nmax) for i in range(nmin,nmax): - 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/ + 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 - keys[int(key)]=self.makeAllKeys(int(key),conf[key]) + if int(key) not in keys: + keys[int(key)]=[] + keys[int(key)]+=self.makeAllKeys(int(key),conf[key]) return keys def makeAllKeys(self,key,action,startkey=0,stopkey=0): diff --git a/midiRebind.py b/midiRebind.py index c3573a0..11a467b 100644 --- a/midiRebind.py +++ b/midiRebind.py @@ -3,22 +3,21 @@ from mapping import GrandMA2 from test import toDec import sys if __name__ == '__main__': - Inter=GrandMA2.MidiInterface("patch/test.ini") + Inter=GrandMA2.MidiInterface("patch/default.ini") Par=GrandMA2.MessageParse() Acts=GrandMA2.Actions(Inter) - #message=toDec("F0 7F 7F 02 7F 01 33 37 2E 32 30 30 00 35 20 31 F7") - #message=toDec("F0 7F 7F 02 7F 06 02 02 4C 39 F7") try: for msg in Inter.input: - print("Received {}".format(msg)) + #print("Received {}".format(msg)) try: - match=Par(message) + match=Par(msg) + print(match) res=Acts(match) except GrandMA2.MatchError: pass except: - print("[Unexpected error] {}".format(sys.exc_info()[1])) + print("[Unexpected error] {}".format(sys.exc_info())) except KeyboardInterrupt: print("[Keyboard Interrupt] : Exiting...") diff --git a/patch/default.ini b/patch/default.ini index 3f6c65b..b80ad1e 100644 --- a/patch/default.ini +++ b/patch/default.ini @@ -1,39 +1,27 @@ [interface] - input="Midi Through Port-0" - output1="Midi Through Port-0" - output2="Akai Mix" + input=Midi Through Port-0 + output1=Midi Through Port-0 + output2=KMidimon inputchanel = 1 outputchannel1 = 1 outputchannel2 = 1 - # Will fetch GrandMA2.py in the mappings mapping = GrandMA2 -#Patching faders: Patches layer 1 patch:2 would patch layer 2 and so on [set/1] -#Send (midi) control change (10) with value mapped to the fader (1) value (fader 1 => cc 10) on output 1 - 1 = 1/cc/10 -#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 + 0-99 = 1/cc/10 + 0-99/toggle = 22/42 + 0-50 = 2/cc/11 57-67 = 1/cc/12-32 -#Map fader 56 to note 152 on output 2 56 = 1/note/127 -# Function choosing the value to send, depending on the intensity received 56/val = lambda x: min(x,50)*2 - [set/5] 1 = 1/cc/2;2/note/19/127 -#No layer specified [fire] 13 = 1/note/10 -#Send control change with value 127 15 = 1/cc/44/127 -# Send control change 45-55 with value 127 17-27 = cc/45/127 -# Will map 29 to 37 to 46 29-37 = cc/46-46/127 -#(Stack the action to link to) 38 = 1/cc/65/124;2/cc/79/100;note/25 38/toggle = 0/127 [fire/5] diff --git a/patch/test.ini b/patch/test.ini index 5da65aa..13f9dff 100644 --- a/patch/test.ini +++ b/patch/test.ini @@ -11,7 +11,7 @@ #Patching faders: Patches layer 1 patch:2 would patch layer 2 and so on [set/1] #Send (midi) control change (10) with value mapped to the fader (1) value (fader 1 => cc 10) on output 1 - 1 = 1/cc/10 + 0-4 = 1/cc/10 #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 diff --git a/test.py b/test.py index 3bcbce2..56b87c4 100644 --- a/test.py +++ b/test.py @@ -31,7 +31,7 @@ class mappingTest(minitest.simpleTestUnit): self.currentTest("Testing hex2dec") testX=["22","19 22","3 4 5 6 22 FF"] - testD=["34","25 34","3 4 5 6 34 255"] + testD=[[34],[25,34],[3, 4, 5, 6, 34, 255]] sucess=1 for X,D in zip(testX,testD): #print("{} / {} : {} [{}]".format(X,D,self.toDec(X),D==self.toDec(X))) @@ -44,29 +44,29 @@ class mappingTest(minitest.simpleTestUnit): #I'm ifnoring the None case on purpose because I can't be bothered with, if it fails, it might as well fail big self.currentTest("Test with ill-formed message") - if self.testParse("22 12"): + if self.testParse([22, 12]): self.addFailure("The message is not supposed to be parsed") else: self.addSuccess() self.currentTest("Test with well-formed message") - msg=toDec("F0 7F 7F 02 7F 01 32 31 2E 35 30 30 F7") + msg=toDec("7F 7F 02 7F 01 32 31 2E 35 30 30") if self.testParse(msg): self.addSuccess() else: self.addFailure("Can't parse the message {}".format(msg)) self.currentTest("Checking sanity of parse") - di=(self.parser(msg).groupdict()) + di=self.parser(mido.Message("sysex",data=msg)) inc="" - if di["command"]!="1": - inc="command " - if di["data"]!="50 49 46 53 48 48": - inc+="data " - if di["deviceid"]!="127": - inc+="deviceid " - if di["commandformat"]!="127": - inc+="commandformat" + if di.command!=1: + inc="command {} ".format(di.command) + if list(di.data)!=[50, 49, 46, 53, 48, 48]: + inc+="data {} ".format(di.data) + if di.deviceid!=127: + inc+="deviceid {} ".format(di.deviceid) + if di.commandformat!=127: + inc+="commandformat {}".format(di.commandformat) if inc=="": self.addSuccess() else: @@ -162,21 +162,24 @@ class mappingTest(minitest.simpleTestUnit): def testParse(self,message): try: + message=mido.Message("sysex",data=message) parsed=self.parser(message) return True except self.gm.MatchError: return False except: + import sys + print(sys.exc_info()) self.addCritical("Can't parse (not supposed to happen)") def toDec(message): "Taken care of by mido, but testing needs to be done otherwise" - res=None + res=[] for hx in message.split(" "): - if res==None: - res=str(int(hx,16)) + if res==[]: + res=[int(hx,16)] else: - res+=" "+str(int(hx,16)) + res+=[int(hx,16)] return res class ConfigTest(minitest.simpleTestUnit): @@ -210,13 +213,13 @@ class LiveTest(minitest.simpleTestUnit): self.currentTest("Trying match") - message=toDec("F0 7F 7F 02 7F 01 33 37 2E 32 30 30 00 35 20 31 F7") - message=toDec("F0 7F 7F 02 7F 06 02 02 4C 39 F7") + message=toDec("7F 7F 02 7F 01 33 37 2E 32 30 30 00 35 20 31") + message=toDec("7F 7F 02 7F 06 02 02 4C 39") try: - match=Par(message) + match=Par(mido.Message("sysex",data=message)) self.addSuccess() except GrandMA2.MatchError: - self.addFailure("Can't match '{}'".format(message)) + self.addFailure("Can't match '{}'".format(mido.Message(message))) except: self.addFailure("Error while matching")