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

Async constructors for MidiInput and MidiOutput to support WebMIDI #164

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jhartquist
Copy link

@jhartquist jhartquist commented Feb 2, 2025

Relates a bit to #47 and #48

Motivation

When using the webmidi backend, the browser requires async initialization for the MidiInput and MidiOutput objects. When using MidiInput::new and MidiOutput::new and then immediately querying the ports, none will be available until the call to requestMidiAccess resolves.

In the existing browser example, a loop is set up to poll until devices are available, but this makes it difficult to distinguish between the case where there are actually no devices, or if they just haven't resolved yet.

Changes

  • Added new_async methods to both MidiInput and MidiOutput
    • they fall back to new method in all backends except for webmidi
  • Added browser examples for the async cases and one similar to the test_play example to test output
  • Simplified the loading of WASM in index.html due to browser support of JS modules
  • Added instructions for building & running the browser examples
  • Updated the azure WASM test to point to the read_input_async example

Notes

  • Opening ports is also asynchronous, but the current implementation works fine for my use case, so I didn't make any changes for that in this PR.
  • The new_async methods could be put behind a feature flag (there's an added dependency in the webmidi case for wasm_bindgen_futures, however I believe most people would find the async version more usable than the sync version when targeting the web. Would love to hear what others think about this.
  • May want to create tests for each of the 3 WASM examples (currently only one)

Let me know if you have any questions or suggested changes. Thanks for the awesome work on this crate!

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

Successfully merging this pull request may close these issues.

1 participant