How to know the port of a message in a callback ? #389
-
Hello, I listen to several MIDI controllers with a single callback function (the number of controllers is not fixed). Thank you very much for this very good project! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Callbacks are a per-port setting. Just pass a different function for each port. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer. I'll keep looking, I'll post my solution when I find it. |
Beta Was this translation helpful? Give feedback.
-
I think I found a solution by creating callback functions dynamically (thanks to stackoverflow.com). Here is a sample script as proof of concept (interested in your opinions) : import mido
def function_builder(port):
def scan(msg):
print(port, msg)
return scan
inports = list(set(mido.get_input_names()))
scan_functions = {}
for port in inports:
print(port)
inport = mido.open_input(port)
scan_functions[port] = function_builder(port)
inport.callback = scan_functions[port]
while True:
pass |
Beta Was this translation helpful? Give feedback.
-
@rdoursenaud's code (see #389 (reply in thread)) does the job. Thanks! |
Beta Was this translation helpful? Give feedback.
@rdoursenaud's code (see #389 (reply in thread)) does the job. Thanks!