#!/bin/python3 # import pyautogui from mapping.base import BasicMessageParse,BasicActions,MatchError,BasicMidiInterface 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" print("Pressing key {},params {}".format(val,params)) #pyautogui.press(params) # MidiInterface(config) class MidiInterface(BasicMidiInterface): "Adding a couple of custom functions" def __init__(self,configfile): super(MidiInterface,self).__init__(configfile) self.functionlist["testfunction"]=testfunction