#!/usr/bin/env python3 import sys, argparse import time import threading from mido import MidiFile import serial drum_channels = [9] def serialreceive(ser): print("Serial running") while ser: try: line = ser.readline().decode() print(line) except Exception: pass def main(argv): inputfile = '' outputport = '' parser = argparse.ArgumentParser() parser.add_argument('midifilename', help="the midi file you want to send") parser.add_argument('-P', '--serialport', help="serial port to open", default="/dev/ttyUSB0") parser.add_argument('--with-drums', action="store_true", help="send percussion track 10") args = parser.parse_args() with serial.Serial(args.serialport, 115200, timeout=12) as ser: receivethread = threading.Thread(target=serialreceive, args=(ser,)) receivethread.daemon = True receivethread.start() time.sleep(1) with MidiFile(args.midifilename) as mid: for msg in mid: time.sleep(msg.time) if not msg.is_meta: if msg.type == 'note_on' or msg.type == 'note_off': if (not (msg.channel in drum_channels)) or args.with_drums: print(msg) ser.write(msg.bin()) else: print("skipped percussion: " + str(msg)) else: print("not sent: " + str(msg)) if msg.type == 'program_change' and msg.program in [50,]: drum_channels.append(msg.channel) else: print("meta message: " + str(msg)) print("EOF") print("done") print("serial closed") if __name__ == "__main__": main(sys.argv[1:])