Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MidiInput is missing a method for removeMessageReceivedListener() #90

Open
FDelporte opened this issue Sep 9, 2024 · 4 comments
Open

Comments

@FDelporte
Copy link

Because of race conditions, it seems it is possible to assign a listener twice. It would probably be nice to have a way to remove a listener before assigning a new one, e.g. when change midi inputs.

public interface MidiInput : dev.atsushieno.ktmidi.MidiPort {
public abstract fun setMessageReceivedListener(listener: dev.atsushieno.ktmidi.OnMidiReceivedEventListener): kotlin.Unit
}

@atsushieno
Copy link
Owner

atsushieno commented Sep 10, 2024

Adding the same event handler twice itself is not strictly harmful, but it indeed seems error-prone. And having no way to reset is indeed problematic, as the listener argument is not nullable.

We should really switch to add/remove listener style. I kind of avoided that as it is a breaking API change to the entire MidiAccess, but probably earlier to execute is better...

@FDelporte
Copy link
Author

FDelporte commented Sep 14, 2024

This is strange... I checked the ktmidi code and the listener is never a list, it's a single object being assigned. But still when switching between MIDI inputs I need to recreate the listener, otherwise it doesn't work, but I get the input events 2, 3, 4 times... Even tried making it null before creating a new one, but still get multiple input events

runBlocking {
            midiInput = JvmMidiAccess().openInput(portDetails.id)
            logger.info("Adding midi input handler for {}", midiInput!!.details.name)
            if (midiInputHandler != null) {
                midiInputHandler = null
            }
            midiInputHandler = MidiInputHandler()
            midiInput!!.setMessageReceivedListener(midiInputHandler!!)
            logger.info("Added midi input handler for {}", midiInput!!.details.name)
        }

@atsushieno
Copy link
Owner

Because you didn't close() the old MidiInput ?

@FDelporte
Copy link
Author

Thanks, indeed closing en open midiinput before opening a new one, solves the double events!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants