celtera / libremidi

A modern C++ MIDI 1 / MIDI 2 real-time & file I/O library. Supports Windows, macOS, Linux and WebMIDI.
Other
463 stars 52 forks source link

Support finding a compiled api with API::UNSPECIFIED #116

Open ejconlon opened 3 months ago

ejconlon commented 3 months ago

Hello, I'm working on Haskell FFI bindings for this library and found that using API::UNSPECIFIED doesn't quite behave as I expect. Creating an observer simply fails, rather than picking a valid API. Here's a diff that works for me:

diff --git a/libremidi/vendor/libremidi/backends.hpp b/libremidi/vendor/libremidi/backends.hpp
index a101f46..bbf7161 100644
--- a/libremidi/vendor/libremidi/backends.hpp
+++ b/libremidi/vendor/libremidi/backends.hpp
@@ -156,7 +156,7 @@ template <typename F>
 auto for_backend(libremidi::API api, F&& f)
 {
   static constexpr auto is_api
-      = [](auto& backend, libremidi::API api) { return backend.API == api; };
+      = [](auto& backend, libremidi::API api) { return backend.API == api || api == libremidi::API::UNSPECIFIED; };
   std::apply([&](auto&&... b) { ((is_api(b, api) && (f(b), true)) || ...); }, available_backends);
 }
 }

Is this the solution?

Thanks!

jcelerier commented 3 months ago

~hello ! I actually pushed some commits that change this behaviour this very morning, could you try on the latest commit if that was not the case?~

nevermind, I didn't push the change for observers

jcelerier commented 3 months ago

Patch looks sound nevertheless, will apply

jcelerier commented 3 months ago

btw, are you ok with me linking the bindings in the README ? :)

ejconlon commented 3 months ago

Thanks for addressing this!

Linking is fine with me.

Would you be open to exposing more methods in the C API? For example, finding which APIs are available, and what their names are. At the moment I’m trying to but the new bindings to use, but I might have time later to send a patch for those new methods.

jcelerier commented 3 months ago

yes definitely, the C api is very provisional right now and not stable yet. For instance all the names aren't homogenous. Stable version will be part of the 5.0 release.

jcelerier commented 2 months ago

heya! I just added some of the requested methods. there will still be a bit of breakage before stable release..

ejconlon commented 2 months ago

Thank you. I am still wrangling foreign pointers and the Haskell side! Will pull and try the new methods soon.