import re from telnetlib import Telnet class Controller(object): TIMEOUT = 30 SUCCESS_RESPONSE = {"success": True} FONT_MATCHER = re.compile("(?P[0-9]+)\\s(?P.*)", re.MULTILINE) INSTRUMENT_MATCHER = re.compile( "(?P[0-9]+)-(?P[0-9]+)\\s(?P.*)", re.MULTILINE ) def connect(self, host: str = "0.0.0.0", port: int = 5800): self.connection = Telnet(host=host, port=port, timeout=Controller.TIMEOUT) return Controller.SUCCESS_RESPONSE def execute(self, command: str): self.connection.write(command.encode("ascii") + b"\r\n") output = self.connection.read_until(match=b"OK", timeout=1) return output.decode("ascii") def get_soundfonts(self): output = self.execute("fonts") fonts = Controller.FONT_MATCHER.findall(output) print(fonts) return [{"id": int(f[0]), "name": f[1]} for f in fonts] def get_instruments(self, font: int): output = self.execute(f"inst {font}") instruments = Controller.INSTRUMENT_MATCHER.findall(output) print(instruments) return [ {"bank": int(i[0]), "program": int(i[1]), "name": i[2]} for i in instruments ] def select(self, channel: int, instrument: int, bank: int, program: int): self.execute(f"select {channel} {instrument} {bank} {program}") return Controller.SUCCESS_RESPONSE def set_gain(self, amount: int): self.execute(f"gain {amount}") return Controller.SUCCESS_RESPONSE