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

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