AutoSyncHost/ashost.py

149 lines
4.2 KiB
Python
Raw Normal View History

2024-06-26 14:40:38 +02:00
#!/usr/bin/env python3
2024-06-26 14:15:53 +02:00
2024-06-26 15:02:12 +02:00
### Copyright 2024 Thomas Herrmann
### Released under GPL v3
2024-06-26 14:40:38 +02:00
import time
2024-06-26 14:15:53 +02:00
import mido
def match_device_name(input_name, output_list):
# Function to match device names flexibly
for device in output_list:
if any(substring in device for substring in input_name.split()):
return device
return None
2024-06-26 15:37:31 +02:00
def determine_host(devices_in: list) -> str:
2024-06-26 14:15:53 +02:00
master = None
"""
Determine the host by listening for 'start' messages on input ports.
Args:
- devices_in (list of str): The names of the MIDI input ports to listen to.
Returns:
- str: The name of the input port that received the 'start' message.
"""
# Open all the input ports
inports = [mido.open_input(device) for device in devices_in]
print("Listening for messages...")
try:
while not master:
for port in inports:
for message in port.iter_pending():
#print(f"Received message: ({port.name}, {message})")
if 'start' in str(message):
master = port.name
break
except KeyboardInterrupt:
print("Interrupted by user.")
finally:
2024-06-26 15:37:31 +02:00
# Ensure all ports are closed, important to not get a stuck script without errors
2024-06-26 14:15:53 +02:00
for port in inports:
port.close()
print("Closed all input ports.")
if master:
return master
2024-06-26 14:44:05 +02:00
2024-06-26 14:15:53 +02:00
def relay_midi_host_to_out(host_name, devices_out):
"""
Relay MIDI messages from a specific input port to all specified output ports.
Args:
- host_name (str): The name of the MIDI input port to listen to.
- devices_out (list of str): The names of the MIDI output ports to send messages to.
"""
#host_name = f'{host_name}'
reset = False
# Open the input port
2024-06-26 15:37:31 +02:00
print("Opening Input", host_name)
2024-06-26 14:15:53 +02:00
input_port = mido.open_input(host_name)
output_ports = [mido.open_output(device) for device in devices_out]
print(f"Listening for messages on {host_name} and relaying to {devices_out}")
recent_messages = []
try:
print(reset)
while not reset:
# Process all pending messages
#print("Entering loop", input_port.iter_pending() )
for message in input_port.iter_pending():
read_message = str(message)
if 'start' in read_message or 'stop' in read_message:
for dev in output_ports:
dev.send(message)
recent_messages.append(read_message)
elif 'clock' in read_message:
for dev in output_ports:
dev.send(message)
# Keep only the last 3 messages
if len(recent_messages) > 3:
recent_messages.pop(0)
# Check if the last 3 messages are all 'stop'
if recent_messages[-3:] == ['stop time=0', 'stop time=0', 'stop time=0']:
recent_messages.clear()
reset = True
break
except KeyboardInterrupt:
# Handle user interrupt (e.g., Ctrl+C)
print("MIDI relay interrupted by user.")
finally:
# Close all ports
input_port.close()
print(input_port)
for port in output_ports:
port.close()
print(port)
print("Closed all MIDI ports.")
#Repeat host detection a nd main loop
time.sleep(1)
main_run()
2024-06-26 14:44:05 +02:00
def main_run():
2024-06-26 14:15:53 +02:00
# Retrieve the list of available input devices
devices_in = mido.get_input_names()
devices_out = mido.get_output_names()
print("Available MIDI input devices:", devices_in)
2024-06-26 15:37:31 +02:00
# Listen for devices that send start and return the host name
2024-06-26 14:15:53 +02:00
midi_host_in = determine_host(devices_in)
print(midi_host_in)
# Find and remove the matching output device to avoid feedback
matching_output_device = match_device_name(midi_host_in, devices_out)
if matching_output_device:
devices_out.remove(matching_output_device)
print(devices_out)
#time.sleep(3)
if midi_host_in:
relay_midi_host_to_out(midi_host_in, devices_out)
main_run()