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.
63 lines
1.9 KiB
63 lines
1.9 KiB
#!/usr/bin/env python3
|
|
|
|
import sys, argparse
|
|
import time
|
|
import threading
|
|
|
|
import mido
|
|
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. Set 'ALSA' for virtual midi port, see 'aconnect -o'")
|
|
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")
|
|
parser.add_argument('--delay', help="add delay before startup", type=int, default=1)
|
|
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(args.delay)
|
|
if args.midifilename == 'ALSA':
|
|
# portmidi = mido.Backend('mido.backends.portmidi')
|
|
midi_in = mido.open_input()
|
|
else:
|
|
midi_in = mido.MidiFile(args.midifilename)
|
|
with midi_in 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' or msg.type == 'pitchwheel':
|
|
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:])
|
|
|