Fixed all issues

All future will likely be readability & cosmetic
This commit is contained in:
Maximilien Rigaut 2019-05-06 18:22:56 +02:00
parent b31a20c908
commit fc5e1d9e5c
6 changed files with 114 additions and 17 deletions

View File

@ -64,7 +64,6 @@ class Actions(BasicActions):
# No action linked to the trigger
pass
# Same as above, import BasicMidiInterface as MidiInterface could be used instead
# MidiInterface(config)
MidiInterface=BasicMidiInterface

View File

@ -90,9 +90,6 @@ class BasicActions(object):
if "interface" not in self.config:
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.prettyprint()
@ -276,8 +273,8 @@ class BasicMidiTrigger(object):
def sendmessage(self):
if self.toggle!=None:
self.toggle=not self.toggle
#print("SENDING:",self.message)
self.interface.send(self.message) #Needs interface implementation
#print("SENDING:",self.message) #TODO Add Verbose option
self.interface.send(self.message)
def addspecial(self,typ,val):
"""Affect a specific action (usually a condition) to the execution of the event"""

59
mapping/midi.py Normal file
View File

@ -0,0 +1,59 @@
from mapping.base import BasicMessageParse,BasicActions,MatchError,BasicMidiInterface
import mido
import configparser
#A MessageParse object MUST be included in the file, the rest is implementation Specific
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 "<MessageParse>[ty:{}/note:{}/value:{}]@{}".format(self.type,self.note,self.value,self.channel)
# An Action object MUST be included in the file.
class Actions(BasicActions):
"""The list of action to perform when a message is read"""
def __init__(self, interface):
super(Actions, self).__init__(interface)
# 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(value)
except:
# No action linked to the trigger
pass
# MidiInterface(config)
MidiInterface=BasicMidiInterface

View File

@ -1,25 +1,43 @@
import mido
from mapping import GrandMA2
from test import toDec
import sys
import mido, argparse, sys
import configparser,importlib
if __name__ == '__main__':
Inter=GrandMA2.MidiInterface("patch/default.ini")
Par=GrandMA2.MessageParse()
Acts=GrandMA2.Actions(Inter)
parser = argparse.ArgumentParser(description='Listen to a midi interface for incomming messages and send messages to different midi interfaces according to a patch file')
parser.add_argument('patch', type=str,help='The patch file, this file is mandatory')
parser.add_argument('--verbose', action='store_true',default=False,help='Make the program verbose)')
args = parser.parse_args()
try:
path="patch/{}.ini".format(args.patch)
conf=configparser.ConfigParser()
conf.read(path)
interpath="mapping.{}".format(conf["interface"]["mapping"])
Module=importlib.import_module(interpath)
#from mapping import GrandMA2
Inter=Module.MidiInterface(path) # TODO, take directly a config object, will save some time
Par=Module.MessageParse()
Acts=Module.Actions(Inter)
for msg in Inter.input:
#print("Received {}".format(msg))
if args.verbose:
print("[Received] {}".format(msg))
try:
match=Par(msg)
print(match)
res=Acts(match)
except GrandMA2.MatchError:
except Module.MatchError:
pass
except:
print("[Unexpected error] {}".format(sys.exc_info()))
except ImportError:
print("[Error] Module {} doesn't exist".format(interpath))
except KeyboardInterrupt:
print("[Keyboard Interrupt] : Exiting...")
del Inter
sys.exit()
except OSError:
print('[Error] The patch "patch/{}.ini" is ill-formed or non-existent'.format(args.patch))

24
patch/midi.ini Normal file
View File

@ -0,0 +1,24 @@
[interface]
input=MIDI Mix MIDI 1
output1=KMidimon
output2=MIDI Mix MIDI 1
inputchanel = 1
outputchannel1 = 1
outputchannel2 = 1
mapping = midi
[note_on]
0-99 = 2/note/0
0-99/toggle = 127/0
0-50 = 1/cc/11
57-67 = 1/cc/12-32
56 = 1/note/127
56/val = lambda x: min(x,50)*2
[control_change]
13 = 1/note/10
15 = 1/cc/44/127
17-27 = cc/45/127
29-37 = cc/46-46/127
38 = 1/cc/65/124;2/cc/79/100;note/25
38/toggle = 0/127