You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
52 lines
1.2 KiB
5 years ago
|
#!/usr/bin/env python3
|
||
|
|
||
|
import sys, getopt
|
||
|
import time
|
||
|
import threading
|
||
|
|
||
|
from mido import MidiFile
|
||
|
import serial
|
||
|
|
||
|
def serialreceive(ser):
|
||
|
print("Serial running")
|
||
|
while True:
|
||
|
try:
|
||
|
line = ser.readline().decode()
|
||
|
print(line)
|
||
|
except Exception:
|
||
|
pass
|
||
|
|
||
|
|
||
|
def main(argv):
|
||
|
inputfile = ''
|
||
|
outputport = ''
|
||
|
try:
|
||
|
opts, args = getopt.getopt(argv, "hi:o:",["ifile=","oport="])
|
||
|
except getopt.GetoptError:
|
||
|
print('send.py -i <inputfile> -o <outputport>')
|
||
|
sys.exit(2)
|
||
|
for opt, arg in opts:
|
||
|
if opt == '-h':
|
||
|
print('send.py -i <inputfile> -o <outputport>')
|
||
|
sys.exit()
|
||
|
elif opt in ("-i", "--ifile"):
|
||
|
inputfile = arg
|
||
|
elif opt in ("-o", "--oport"):
|
||
|
outputport = arg
|
||
|
with serial.Serial(outputport, 115200, timeout=12) as ser:
|
||
|
receivethread = threading.Thread(target=serialreceive, args=(ser,))
|
||
|
receivethread.start()
|
||
|
with MidiFile(inputfile) 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':
|
||
|
print(msg)
|
||
|
ser.write(msg.bin())
|
||
|
else:
|
||
|
print("not sent: " + str(msg))
|
||
|
#time.sleep(1000)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main(sys.argv[1:])
|