- cleanup

This commit is contained in:
bklronin 2024-06-26 15:37:31 +02:00
parent 809cd8a46d
commit d8e837963b
1 changed files with 4 additions and 17 deletions

View File

@ -15,7 +15,7 @@ def match_device_name(input_name, output_list):
return None return None
def determine_host(devices_in): def determine_host(devices_in: list) -> str:
master = None master = None
""" """
Determine the host by listening for 'start' messages on input ports. Determine the host by listening for 'start' messages on input ports.
@ -29,9 +29,6 @@ def determine_host(devices_in):
# Open all the input ports # Open all the input ports
inports = [mido.open_input(device) for device in devices_in] inports = [mido.open_input(device) for device in devices_in]
# Create a MultiPort to listen on all input ports
#ports = MultiPort(inports, True)
print("Listening for messages...") print("Listening for messages...")
try: try:
while not master: while not master:
@ -46,7 +43,7 @@ def determine_host(devices_in):
print("Interrupted by user.") print("Interrupted by user.")
finally: finally:
# Ensure all ports are closed # Ensure all ports are closed, important to not get a stuck script without errors
for port in inports: for port in inports:
port.close() port.close()
print("Closed all input ports.") print("Closed all input ports.")
@ -65,7 +62,7 @@ def relay_midi_host_to_out(host_name, devices_out):
#host_name = f'{host_name}' #host_name = f'{host_name}'
reset = False reset = False
# Open the input port # Open the input port
print("Opening Input ffsk", host_name) print("Opening Input", host_name)
input_port = mido.open_input(host_name) input_port = mido.open_input(host_name)
output_ports = [mido.open_output(device) for device in devices_out] output_ports = [mido.open_output(device) for device in devices_out]
@ -80,19 +77,15 @@ def relay_midi_host_to_out(host_name, devices_out):
#print("Entering loop", input_port.iter_pending() ) #print("Entering loop", input_port.iter_pending() )
for message in input_port.iter_pending(): for message in input_port.iter_pending():
read_message = str(message) read_message = str(message)
#spread_the_word = MultiPort(output_ports)
#print(read_message)
if 'start' in read_message or 'stop' in read_message: if 'start' in read_message or 'stop' in read_message:
for dev in output_ports: for dev in output_ports:
dev.send(message) dev.send(message)
recent_messages.append(read_message) recent_messages.append(read_message)
#print("SS")
elif 'clock' in read_message: elif 'clock' in read_message:
for dev in output_ports: for dev in output_ports:
dev.send(message) dev.send(message)
#print("clock")
# Keep only the last 3 messages # Keep only the last 3 messages
if len(recent_messages) > 3: if len(recent_messages) > 3:
@ -100,9 +93,6 @@ def relay_midi_host_to_out(host_name, devices_out):
# Check if the last 3 messages are all 'stop' # Check if the last 3 messages are all 'stop'
if recent_messages[-3:] == ['stop time=0', 'stop time=0', 'stop time=0']: if recent_messages[-3:] == ['stop time=0', 'stop time=0', 'stop time=0']:
#print("Received 'stop' three times in a row")
# Perform the desired action when 'stop' is received three times in a row
# For example, resetting the list
recent_messages.clear() recent_messages.clear()
reset = True reset = True
@ -118,7 +108,6 @@ def relay_midi_host_to_out(host_name, devices_out):
print(input_port) print(input_port)
for port in output_ports: for port in output_ports:
#port.reset()
port.close() port.close()
print(port) print(port)
print("Closed all MIDI ports.") print("Closed all MIDI ports.")
@ -134,7 +123,7 @@ def main_run():
devices_out = mido.get_output_names() devices_out = mido.get_output_names()
print("Available MIDI input devices:", devices_in) print("Available MIDI input devices:", devices_in)
# Call the function to start receiving messages from all devices # Listen for devices that send start and return the host name
midi_host_in = determine_host(devices_in) midi_host_in = determine_host(devices_in)
print(midi_host_in) print(midi_host_in)
@ -149,8 +138,6 @@ def main_run():
if midi_host_in: if midi_host_in:
relay_midi_host_to_out(midi_host_in, devices_out) relay_midi_host_to_out(midi_host_in, devices_out)
print("mido died")
main_run() main_run()