AutoSyncHost/ashost.py

164 lines
4.6 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:
2024-06-26 21:55:14 +02:00
if input_name == device:
2024-06-26 14:15:53 +02:00
return device
2024-06-26 21:55:14 +02:00
#If name differs
elif any(substring in device for substring in input_name.split()):
return device
2024-06-26 14:15:53 +02:00
return None
2024-06-26 22:15:29 +02:00
def remove_through_ports(ports):
2024-06-26 22:12:37 +02:00
# Filter out ports containing "Through" in their names
2024-06-26 22:15:29 +02:00
filtered_ports = [port for port in ports if 'through' not in port.lower()]
2024-06-26 22:12:37 +02:00
return filtered_ports
2024-06-26 14:15:53 +02:00
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:
2024-06-26 21:55:14 +02:00
print("Inport closing", port)
2024-06-26 14:15:53 +02:00
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
for message in input_port.iter_pending():
read_message = str(message)
2024-06-27 19:03:03 +02:00
#print(read_message)
2024-06-26 14:15:53 +02:00
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']:
2024-06-27 19:03:03 +02:00
print("Exiting", recent_messages)
2024-06-26 14:15:53 +02:00
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()
2024-06-26 22:15:29 +02:00
devices_in = remove_through_ports(devices_in)
devices_out = remove_through_ports(devices_out)
2024-06-27 19:03:03 +02:00
print("Available MIDI input devices:", devices_in)
2024-06-26 22:15:29 +02:00
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
2024-06-26 22:15:29 +02:00
matching_output_device = match_device_name(midi_host_in, devices_out)
2024-06-26 22:12:37 +02:00
print("Filtering", matching_output_device)
2024-06-26 14:15:53 +02:00
if matching_output_device:
2024-06-26 22:15:29 +02:00
devices_out.remove(matching_output_device)
2024-06-26 14:15:53 +02:00
if midi_host_in:
2024-06-26 22:15:29 +02:00
relay_midi_host_to_out(midi_host_in, devices_out)
2024-06-26 14:15:53 +02:00
main_run()