music with stepper motors
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.

64 lines
1.9 KiB

5 years ago
#!/usr/bin/env python3
5 years ago
import sys, argparse
5 years ago
import time
import threading
5 years ago
import mido
5 years ago
import serial
5 years ago
drum_channels = [9]
5 years ago
def serialreceive(ser):
print("Serial running")
5 years ago
while ser:
5 years ago
try:
line = ser.readline().decode()
print(line)
except Exception:
pass
def main(argv):
inputfile = ''
outputport = ''
5 years ago
parser = argparse.ArgumentParser()
5 years ago
parser.add_argument('midifilename', help="the midi file you want to send. Set 'ALSA' for virtual midi port, see 'aconnect -o'")
5 years ago
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")
5 years ago
parser.add_argument('--delay', help="add delay before startup", type=int, default=1)
5 years ago
args = parser.parse_args()
with serial.Serial(args.serialport, 115200, timeout=12) as ser:
5 years ago
receivethread = threading.Thread(target=serialreceive, args=(ser,))
5 years ago
receivethread.daemon = True
5 years ago
receivethread.start()
5 years ago
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:
5 years ago
for msg in mid:
time.sleep(msg.time)
if not msg.is_meta:
5 years ago
if msg.type == 'note_on' or msg.type == 'note_off' or msg.type == 'pitchwheel':
5 years ago
if (not (msg.channel in drum_channels)) or args.with_drums:
print(msg)
ser.write(msg.bin())
else:
print("skipped percussion: " + str(msg))
5 years ago
else:
print("not sent: " + str(msg))
5 years ago
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")
5 years ago
if __name__ == "__main__":
main(sys.argv[1:])