python-rtmidi icon indicating copy to clipboard operation
python-rtmidi copied to clipboard

does not detect ports that go away

Open folkertvanheusden opened this issue 3 years ago • 2 comments

Situation: I switch on my yamaha keyboard, a midi port (via usb) appears. I connect through python-rtmidi but after a bit I switch off the keyboard: this is not detected by python-rtmidi altough e.g. "amidi -l" no longer detects it.

root@host:~/# ./test3.py 
['Midi Through:Midi Through Port-0 14:0', 'Digital Keyboard:Digital Keyboard MIDI 1 20:0']
1
Fri Aug  6 19:06:06 2021 ([144, 58, 80], 0.0) None
Fri Aug  6 19:06:06 2021 ([144, 58, 0], 0.017002723) None
Fri Aug  6 19:06:06 2021 ([144, 59, 80], 0.023001593) None
Fri Aug  6 19:06:06 2021 ([144, 59, 0], 0.078011351) None

Here I switch off the keyboard: nothing happens. I expected an exception or so.

In dmesg:

[Fri Aug  6 19:06:08 2021] usb 1-1.4: urb status -32
...
[Fri Aug  6 19:06:08 2021] usb 1-1.4: urb status -32
[Fri Aug  6 19:06:08 2021] usb 1-1.4: USB disconnect, device number 6
[Fri Aug  6 19:06:08 2021] usb 1-1.4: urb status -32
[Fri Aug  6 19:06:08 2021] usb 1-1.4: urb status -32

test-code:

#! /usr/bin/python3

import rtmidi
import time

def printer(a, b):
    print(time.ctime(), a, b)

midiin = rtmidi.MidiIn()

ports = midiin.get_ports()
print(ports)

nr = ports.index('Digital Keyboard:Digital Keyboard MIDI 1 20:0')
print(nr)
midiin.open_port(nr)

midiin.set_callback(printer)

while True:
    time.sleep(1)

folkertvanheusden avatar Aug 06 '21 17:08 folkertvanheusden

In src/rtmidi/RtMidi.cpp, SND_SEQ_EVENT_PORT_UNSUBSCRIBED needs to be handled.

See https://github.com/SpotlightKid/rtmidi/pull/2 Works for both callback and non-callback scripts.

folkertvanheusden avatar Aug 06 '21 18:08 folkertvanheusden

Thanks for your report and sorry for the slow response. I currently have only little time for FLOSS development but I'll look at the issue as soon as I can.

SpotlightKid avatar Aug 16 '21 09:08 SpotlightKid

That's it, the 1.5.0 release with binary wheels for recent cpython has been released on pypi! https://pypi.org/manage/project/python-rtmidi/release/1.5.0/ It includes this fix.

Please let me know if this fixes your issue.

brunoenten avatar May 23 '23 16:05 brunoenten

PR was merged, let's assume this issue's fixed.

brunoenten avatar Jun 13 '23 15:06 brunoenten