#macos #midi #core-midi #hotplug

coremidi-hotplug-notification

Simple helper providing a way to receive device updates notifications on MacOS

3 releases

0.1.3 Jan 13, 2024
0.1.1 Jan 1, 2024
0.1.0 Jan 1, 2024

#218 in Audio

Download history 10/week @ 2023-12-27 6/week @ 2024-01-10 2/week @ 2024-02-14 24/week @ 2024-02-21 16/week @ 2024-02-28 5/week @ 2024-03-06 8/week @ 2024-03-13 3/week @ 2024-03-20 29/week @ 2024-03-27 113/week @ 2024-04-03

158 downloads per month
Used in tauri-plugin-midi

MIT license

11KB
116 lines

coremidi-hotplug-notification

This crate is a simple helper providing a way to receive device updates notifications. It also allows users of crates such as midir to refresh the list of devices without having to restart the program, the notification is optional.

Prerequisites

[target.'cfg(target_os = "macos")'.dependencies]
coremidi-hotplug-notification = "0.1.1"

Usage

fn main() {
    #[cfg(target_os = "macos")]
    // Register for device update notifications
    coremidi_hotplug_notification::receive_device_updates(|| {
        println!("A MIDI device was connected or disconnected.");
        // Insert your handling logic here
    }).expect("Failed to register for MIDI device updates");
}

In practice, you'll most likely want to use a channel to receive updates. The closure does not receive any parameter, just re-read the list of devices from there.

If you are not interested in notifications and just intend to refresh manually, you can pass an empty closure ( || () ).

Caveats

MacOS will set the thread on which notifications are sent at the first call to create a coremidi client. Due to that, receive_device_updates will fail if you use any MIDI functionality before calling it.

This crate will spawn a thread dedicated to the runloop, which is necessary in order to receive device updates. If you are not willing to run a dedicated thread, you'll probably want to directly use the coremidi crate and call CFRunLoop::run_current or run_in_mode yourself.

Dependencies

~635KB
~13K SLoC