libremidi update to v5.4.3 and added virtual midi out as default
This commit is contained in:
@@ -1,19 +1,36 @@
|
||||
#pragma once
|
||||
#include <libremidi/config.hpp>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
#include <string>
|
||||
|
||||
#if !defined(LIBREMIDI_MODULE_BUILD) || !defined(_WIN32)
|
||||
namespace winrt::Microsoft::Windows::Devices::Midi2
|
||||
{
|
||||
struct MidiSession;
|
||||
struct MidiEndpointDeviceInformation;
|
||||
struct MidiEndpointConnection;
|
||||
}
|
||||
#endif
|
||||
|
||||
// TODO allow to share midi session and endpoints
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
|
||||
struct input_configuration
|
||||
{
|
||||
std::string client_name = "libremidi input";
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiSession* context{};
|
||||
};
|
||||
|
||||
struct output_configuration
|
||||
{
|
||||
std::string client_name = "libremidi output";
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiSession* context{};
|
||||
};
|
||||
|
||||
struct observer_configuration
|
||||
{
|
||||
std::string client_name = "libremidi observer";
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -3,58 +3,216 @@
|
||||
#define NOMINMAX 1
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <libremidi/detail/midi_api.hpp>
|
||||
#include <libremidi/detail/memory.hpp>
|
||||
|
||||
#include <mutex>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <guiddef.h>
|
||||
#include <unknwn.h>
|
||||
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Foundation.Collections.h>
|
||||
#include <winrt/Windows.Devices.Enumeration.h>
|
||||
#include <winrt/Windows.Devices.Midi2.h>
|
||||
#include <winrt/Microsoft.Windows.Devices.Midi2.h>
|
||||
#include <libremidi/cmidi2.hpp>
|
||||
|
||||
#if __has_include(<WindowsMidiServicesAppSdkComExtensions.h>)
|
||||
#include <WindowsMidiServicesAppSdkComExtensions.h>
|
||||
#define LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS 1
|
||||
|
||||
namespace midi2 = winrt::Windows::Devices::Midi2;
|
||||
namespace foundation = winrt::Windows::Foundation;
|
||||
namespace collections = winrt::Windows::Foundation::Collections;
|
||||
#define LIBREMIDI_DEFINE_GUID_CONSTEXPR(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
|
||||
LIBREMIDI_STATIC constexpr const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
|
||||
|
||||
NAMESPACE_LIBREMIDI {
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiEndpointConnectionMessagesReceivedCallback, 0x8087b303, 0x0519, 0x31d1, 0x31, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiEndpointConnectionRaw, 0x8087b303, 0x0519, 0x31d1, 0x31, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_IMidiClientInitializer, 0x8087b303, 0xd551, 0xbce2, 0x1e, 0xad, 0xa2, 0x50, 0x0d, 0x50, 0xc5, 0x80);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_MidiClientInitializerUuid, 0xc3263827, 0xc3b0, 0xbdbd, 0x25, 0x00, 0xce, 0x63, 0xa3, 0xf3, 0xf2, 0xc3);
|
||||
LIBREMIDI_DEFINE_GUID_CONSTEXPR(IID, IID_MidiSrvTransportUuid, 0x2ba15e4e, 0x5417, 0x4a66, 0x85, 0xb8, 0x2b, 0x22, 0x60, 0xef, 0xbc, 0x84);
|
||||
}
|
||||
#endif
|
||||
|
||||
// clang-format on
|
||||
|
||||
namespace libremidi::winmidi
|
||||
namespace midi2 = winrt::Microsoft::Windows::Devices::Midi2;
|
||||
namespace foundation = winrt::Windows::Foundation;
|
||||
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
using namespace winrt;
|
||||
using namespace winrt::Windows::Foundation;
|
||||
using namespace winrt::Windows::Devices::Enumeration;
|
||||
using namespace winrt::Windows::Storage::Streams;
|
||||
using namespace winrt::Windows::Devices::Midi2;
|
||||
using namespace Windows::Devices::Enumeration;
|
||||
using namespace winrt::Microsoft::Windows::Devices::Midi2;
|
||||
|
||||
inline bool ichar_equals(char a, char b)
|
||||
{
|
||||
return std::tolower(static_cast<unsigned char>(a)) ==
|
||||
std::tolower(static_cast<unsigned char>(b));
|
||||
return std::tolower(static_cast<unsigned char>(a))
|
||||
== std::tolower(static_cast<unsigned char>(b));
|
||||
}
|
||||
|
||||
inline bool iequals(std::string_view lhs, std::string_view rhs)
|
||||
{
|
||||
return std::ranges::equal(lhs, rhs, ichar_equals);
|
||||
}
|
||||
|
||||
inline winrt::Windows::Devices::Midi2::MidiEndpointDeviceInformation
|
||||
get_port_by_name(const std::string& port) {
|
||||
inline std::pair<
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointDeviceInformation,
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiGroupTerminalBlock>
|
||||
get_port(const std::string& device_name, int group_terminal_block)
|
||||
{
|
||||
auto eps = MidiEndpointDeviceInformation::FindAll();
|
||||
for (const auto& ep : eps)
|
||||
{
|
||||
auto str = to_string(ep.Id());
|
||||
auto str = to_string(ep.EndpointDeviceId());
|
||||
if (str.empty())
|
||||
continue;
|
||||
|
||||
if (iequals(str, port))
|
||||
return ep;
|
||||
if (iequals(str, device_name))
|
||||
{
|
||||
for (const auto& gp : ep.GetGroupTerminalBlocks())
|
||||
{
|
||||
if (gp.Number() == group_terminal_block)
|
||||
{
|
||||
return std::make_pair(ep, gp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {nullptr};
|
||||
return {nullptr, nullptr};
|
||||
}
|
||||
|
||||
// From Microsoft.Windows.Devices.Midi2.Initialization.hpp
|
||||
typedef enum
|
||||
{
|
||||
Platform_x64 = 1,
|
||||
// Platform_Arm64 = 2,
|
||||
// Platform_Arm64EC = 3,
|
||||
Platform_Arm64X = 4,
|
||||
} MidiAppSDKPlatform;
|
||||
|
||||
struct IMidiClientInitializer : ::IUnknown
|
||||
{
|
||||
// returns the SDK version info. Supply nullptr for arguments you don't care about
|
||||
STDMETHOD(GetInstalledWindowsMidiServicesSdkVersion)(
|
||||
MidiAppSDKPlatform* buildPlatform,
|
||||
USHORT* versionMajor,
|
||||
USHORT* versionMinor,
|
||||
USHORT* versionPatch,
|
||||
|
||||
LPWSTR* buildSource,
|
||||
LPWSTR* versionName,
|
||||
LPWSTR* versionFullString
|
||||
) = 0;
|
||||
|
||||
// demand-starts the service if present
|
||||
STDMETHOD(EnsureServiceAvailable)() = 0;
|
||||
};
|
||||
struct winmidi_shared_data_instance
|
||||
{
|
||||
IMidiClientInitializer* initializer{nullptr};
|
||||
bool ready{false};
|
||||
winmidi_shared_data_instance()
|
||||
{
|
||||
// 1. Check if MIDI Services are available
|
||||
{
|
||||
::IUnknown* servicePointer{ nullptr };
|
||||
auto hr = CoCreateInstance(
|
||||
libremidi::IID_MidiSrvTransportUuid,
|
||||
NULL,
|
||||
CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(&servicePointer)
|
||||
);
|
||||
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
if (servicePointer == nullptr)
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Here is the good case:
|
||||
servicePointer->Release();
|
||||
servicePointer = nullptr;
|
||||
ready = true;
|
||||
}
|
||||
else if (hr == REGDB_E_CLASSNOTREG)
|
||||
{
|
||||
servicePointer = nullptr;
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
servicePointer = nullptr;
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Check if MIDI services can be created
|
||||
{
|
||||
if (SUCCEEDED(CoCreateInstance(
|
||||
libremidi::IID_MidiClientInitializerUuid,
|
||||
NULL,
|
||||
CLSCTX::CLSCTX_INPROC_SERVER | CLSCTX::CLSCTX_FROM_DEFAULT_CONTEXT,
|
||||
libremidi::IID_IMidiClientInitializer,
|
||||
reinterpret_cast<void**>(&initializer)
|
||||
)))
|
||||
{
|
||||
if (initializer != nullptr)
|
||||
{
|
||||
ready = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Check if MIDI services can be used
|
||||
if (SUCCEEDED(initializer->EnsureServiceAvailable()))
|
||||
{
|
||||
ready = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ready = false;
|
||||
initializer->Release();
|
||||
initializer = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
~winmidi_shared_data_instance()
|
||||
{
|
||||
if (initializer != nullptr)
|
||||
{
|
||||
initializer->Release();
|
||||
initializer = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct winmidi_shared_data
|
||||
{
|
||||
std::shared_ptr<winmidi_shared_data_instance> self = libremidi::instance<winmidi_shared_data_instance>();
|
||||
|
||||
winmidi_shared_data()
|
||||
{
|
||||
}
|
||||
|
||||
~winmidi_shared_data()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -5,14 +5,12 @@
|
||||
#include <libremidi/detail/midi_in.hpp>
|
||||
#include <libremidi/detail/midi_stream_decoder.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
|
||||
class midi_in_impl final
|
||||
: public midi2::in_api
|
||||
, public error_handler
|
||||
, public winmidi_shared_data
|
||||
{
|
||||
public:
|
||||
struct
|
||||
@@ -21,10 +19,62 @@ public:
|
||||
{
|
||||
} configuration;
|
||||
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
struct raw_callback_type final : IMidiEndpointConnectionMessagesReceivedCallback
|
||||
{
|
||||
explicit raw_callback_type(midi_in_impl& self)
|
||||
: self{self}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
midi_in_impl& self;
|
||||
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override
|
||||
{
|
||||
if (!ppvObject)
|
||||
return E_POINTER;
|
||||
|
||||
if (riid == __uuidof(IUnknown) ||
|
||||
riid == libremidi::IID_IMidiEndpointConnectionMessagesReceivedCallback)
|
||||
{
|
||||
*ppvObject = static_cast<IMidiEndpointConnectionMessagesReceivedCallback*>(this);
|
||||
AddRef();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
*ppvObject = nullptr;
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
ULONG STDMETHODCALLTYPE AddRef() override
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
ULONG STDMETHODCALLTYPE Release() override
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE MessagesReceived(
|
||||
GUID sessionId,
|
||||
GUID connectionId,
|
||||
UINT64 timestamp,
|
||||
UINT32 wordCount,
|
||||
UINT32 *messages) override {
|
||||
HRESULT res{};
|
||||
self.process_message(sessionId, connectionId, timestamp, wordCount, messages);
|
||||
return res;
|
||||
}
|
||||
} raw_callback{*this};
|
||||
#endif
|
||||
|
||||
explicit midi_in_impl(
|
||||
libremidi::ump_input_configuration&& conf, winmidi::input_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
, m_session{MidiSession::CreateSession(L"libremidi session")}
|
||||
, m_session{
|
||||
configuration.context ? *configuration.context
|
||||
: MidiSession::Create(to_hstring(configuration.client_name))}
|
||||
{
|
||||
this->client_open_ = stdx::error{};
|
||||
}
|
||||
@@ -42,24 +92,41 @@ public:
|
||||
|
||||
stdx::error open_port(const input_port& port, std::string_view) override
|
||||
{
|
||||
auto ep = get_port_by_name(port.port_name);
|
||||
if (!ep)
|
||||
auto device_id = get_if<std::string>(&port.device);
|
||||
if (!device_id)
|
||||
return std::errc::invalid_argument;
|
||||
|
||||
auto [ep, gp] = get_port(*device_id, port.port);
|
||||
if (!ep || !gp)
|
||||
return std::errc::address_not_available;
|
||||
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.Id());
|
||||
m_group_filter = port.port - 1;
|
||||
|
||||
// TODO use a MidiGroupEndpointListener for the filtering
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_revoke_token = m_endpoint.MessageReceived(
|
||||
[&](const foundation::IInspectable& sender,
|
||||
const winrt::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& args) {
|
||||
[this](
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::IMidiMessageReceivedEventSource&,
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& args) {
|
||||
process_message(args);
|
||||
});
|
||||
#else
|
||||
m_endpoint.as(libremidi::IID_IMidiEndpointConnectionRaw, m_raw_endpoint.put_void());
|
||||
|
||||
m_raw_endpoint->SetMessagesReceivedCallback(
|
||||
&raw_callback
|
||||
);
|
||||
#endif
|
||||
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
void process_message(const winrt::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
||||
void process_message(
|
||||
const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
||||
{
|
||||
static constexpr timestamp_backend_info timestamp_info{
|
||||
.has_absolute_timestamps = true,
|
||||
@@ -68,6 +135,14 @@ public:
|
||||
};
|
||||
|
||||
const auto& ump = msg.GetMessagePacket();
|
||||
auto pk = msg.PeekFirstWord();
|
||||
if (m_group_filter >= 0)
|
||||
{
|
||||
int group = cmidi2_ump_get_group(&pk);
|
||||
if (group != m_group_filter)
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& b = ump.GetAllWords();
|
||||
|
||||
uint32_t ump_space[64];
|
||||
@@ -79,9 +154,49 @@ public:
|
||||
{ump_space, ump_space + b.Size()}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
||||
}
|
||||
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
void process_message(
|
||||
const GUID& /* sessionId */,
|
||||
const GUID& /* connectionId */,
|
||||
UINT64 timestamp,
|
||||
UINT32 wordCount,
|
||||
UINT32 *ump)
|
||||
{
|
||||
static constexpr timestamp_backend_info timestamp_info{
|
||||
.has_absolute_timestamps = true,
|
||||
.absolute_is_monotonic = false,
|
||||
.has_samples = false,
|
||||
};
|
||||
|
||||
if(wordCount == 0)
|
||||
return;
|
||||
|
||||
if (m_group_filter >= 0)
|
||||
{
|
||||
int group = cmidi2_ump_get_group(ump);
|
||||
if (group != m_group_filter)
|
||||
return;
|
||||
}
|
||||
|
||||
auto to_ns = [t = timestamp] { return t; };
|
||||
m_processing.on_bytes(
|
||||
{ump, ump + wordCount}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
stdx::error close_port() override
|
||||
{
|
||||
if(!m_endpoint)
|
||||
return std::errc::not_connected;
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_endpoint.MessageReceived(m_revoke_token);
|
||||
#else
|
||||
if(m_raw_endpoint) {
|
||||
m_raw_endpoint->RemoveMessagesReceivedCallback();
|
||||
m_raw_endpoint = nullptr;
|
||||
}
|
||||
#endif
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
@@ -91,7 +206,11 @@ public:
|
||||
private:
|
||||
MidiSession m_session;
|
||||
winrt::event_token m_revoke_token{};
|
||||
winrt::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
winrt::impl::com_ref<IMidiEndpointConnectionRaw> m_raw_endpoint{};
|
||||
#endif
|
||||
midi2::input_state_machine m_processing{this->configuration};
|
||||
int m_group_filter = -1;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#pragma once
|
||||
#include <libremidi/detail/ump_stream.hpp>
|
||||
#include <libremidi/backends/winmidi/config.hpp>
|
||||
#include <libremidi/backends/winmidi/helpers.hpp>
|
||||
#include <libremidi/backends/winmidi/observer.hpp>
|
||||
#include <libremidi/detail/midi_out.hpp>
|
||||
#include <libremidi/detail/ump_stream.hpp>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
|
||||
class midi_out_impl final
|
||||
: public midi2::out_api
|
||||
, public error_handler
|
||||
, public winmidi_shared_data
|
||||
{
|
||||
public:
|
||||
struct
|
||||
@@ -21,7 +22,9 @@ public:
|
||||
|
||||
midi_out_impl(libremidi::output_configuration&& conf, winmidi::output_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
, m_session{MidiSession::CreateSession(L"libremidi session")}
|
||||
, m_session{
|
||||
configuration.context ? *configuration.context
|
||||
: MidiSession::Create(to_hstring(configuration.client_name))}
|
||||
{
|
||||
this->client_open_ = stdx::error{};
|
||||
}
|
||||
@@ -35,11 +38,18 @@ public:
|
||||
|
||||
stdx::error open_port(const output_port& port, std::string_view) override
|
||||
{
|
||||
auto ep = get_port_by_name(port.port_name);
|
||||
if (!ep)
|
||||
auto device_id = get_if<std::string>(&port.device);
|
||||
if (!device_id)
|
||||
return std::errc::invalid_argument;
|
||||
|
||||
auto [ep, gp] = get_port(*device_id, port.port);
|
||||
if (!ep || !gp)
|
||||
return std::errc::address_not_available;
|
||||
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.Id());
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
m_endpoint.as(libremidi::IID_IMidiEndpointConnectionRaw, m_raw_endpoint.put_void());
|
||||
#endif
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
@@ -47,6 +57,9 @@ public:
|
||||
|
||||
stdx::error close_port() override
|
||||
{
|
||||
if(!m_endpoint)
|
||||
return std::errc::not_connected;
|
||||
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
@@ -54,8 +67,10 @@ public:
|
||||
stdx::error send_ump(const uint32_t* message, size_t size) override
|
||||
{
|
||||
auto write_func = [this](const uint32_t* ump, int64_t bytes) -> std::errc {
|
||||
|
||||
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
MidiSendMessageResults ret{};
|
||||
switch(bytes / 4)
|
||||
switch (bytes / 4)
|
||||
{
|
||||
case 1:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage32(0, ump[0]));
|
||||
@@ -67,23 +82,52 @@ public:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage96(0, ump[0], ump[1], ump[2]));
|
||||
break;
|
||||
case 4:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage128(0, ump[0], ump[1], ump[2], ump[3]));
|
||||
ret = m_endpoint.SendSingleMessagePacket(
|
||||
MidiMessage128(0, ump[0], ump[1], ump[2], ump[3]));
|
||||
break;
|
||||
default:
|
||||
return std::errc::bad_message;
|
||||
}
|
||||
|
||||
if(ret != MidiSendMessageResults::Succeeded)
|
||||
if (ret != MidiSendMessageResults::Succeeded)
|
||||
return std::errc::bad_message;
|
||||
#else
|
||||
HRESULT ret{};
|
||||
switch (bytes / 4)
|
||||
{
|
||||
case 1:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(1, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 1, (UINT32*)ump);
|
||||
break;
|
||||
case 2:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(2, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 2, (UINT32*)ump);
|
||||
break;
|
||||
case 3:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(3, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 3, (UINT32*)ump);
|
||||
break;
|
||||
case 4:
|
||||
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(4, (UINT32*)ump));
|
||||
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 4, (UINT32*)ump);
|
||||
break;
|
||||
default:
|
||||
return std::errc::bad_message;
|
||||
}
|
||||
#endif
|
||||
if(ret < 0)
|
||||
return std::errc::io_error;
|
||||
return std::errc{0};
|
||||
};
|
||||
|
||||
return segment_ump_stream(message, size, write_func, []() {});
|
||||
return segment_ump_stream(message, size, write_func, []() { });
|
||||
}
|
||||
|
||||
private:
|
||||
MidiSession m_session;
|
||||
winrt::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
||||
winrt::impl::com_ref<IMidiEndpointConnectionRaw> m_raw_endpoint{};
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@
|
||||
#include <libremidi/backends/winmidi/config.hpp>
|
||||
#include <libremidi/backends/winmidi/helpers.hpp>
|
||||
#include <libremidi/detail/observer.hpp>
|
||||
;
|
||||
namespace libremidi::winmidi
|
||||
|
||||
NAMESPACE_LIBREMIDI::winmidi
|
||||
{
|
||||
struct port_info
|
||||
{
|
||||
hstring id;
|
||||
hstring name;
|
||||
winrt::hstring id;
|
||||
winrt::hstring name;
|
||||
};
|
||||
|
||||
class observer_impl final : public observer_api
|
||||
class observer_impl final
|
||||
: public observer_api
|
||||
, public winmidi_shared_data
|
||||
{
|
||||
public:
|
||||
struct
|
||||
@@ -20,46 +22,55 @@ public:
|
||||
{
|
||||
} configuration;
|
||||
|
||||
MidiEndpointDeviceWatcher watcher = nullptr;
|
||||
MidiEndpointDeviceWatcher::Added_revoker m_addHandler;
|
||||
MidiEndpointDeviceWatcher::Updated_revoker m_updHandler;
|
||||
MidiEndpointDeviceWatcher::Removed_revoker m_delHandler;
|
||||
std::map<winrt::hstring, std::vector<input_port>> m_known_input_devices;
|
||||
std::map<winrt::hstring, std::vector<output_port>> m_known_output_devices;
|
||||
std::mutex m_devices_mtx;
|
||||
std::atomic_bool m_in_constructor{true};
|
||||
|
||||
explicit observer_impl(
|
||||
libremidi::observer_configuration&& conf, winmidi::observer_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
, session{MidiSession::CreateSession(L"libremidi session")}
|
||||
{
|
||||
struct on_delete { observer_impl& self; ~on_delete() { self.m_in_constructor = false; } } on_delete{*this};
|
||||
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
|
||||
if (configuration.notify_in_constructor)
|
||||
// Note: winmidi also notifies for existing devices, so the "notify in constructor"
|
||||
// is handled in the callbacks
|
||||
if ((watcher = MidiEndpointDeviceWatcher::Create()))
|
||||
{
|
||||
if (configuration.input_added)
|
||||
for (const auto& p : get_input_ports())
|
||||
configuration.input_added(p);
|
||||
namespace enumeration = winrt::Windows::Devices::Enumeration;
|
||||
auto addHandler = foundation::TypedEventHandler<
|
||||
MidiEndpointDeviceWatcher, MidiEndpointDeviceInformationAddedEventArgs>(
|
||||
this, &observer_impl::on_device_added);
|
||||
auto updHandler = foundation::TypedEventHandler<
|
||||
MidiEndpointDeviceWatcher, MidiEndpointDeviceInformationUpdatedEventArgs>(
|
||||
this, &observer_impl::on_device_updated);
|
||||
auto delHandler = foundation::TypedEventHandler<
|
||||
MidiEndpointDeviceWatcher, MidiEndpointDeviceInformationRemovedEventArgs>(
|
||||
this, &observer_impl::on_device_removed);
|
||||
|
||||
if (configuration.output_added)
|
||||
for (const auto& p : get_output_ports())
|
||||
configuration.output_added(p);
|
||||
m_addHandler = watcher.Added(winrt::auto_revoke, addHandler);
|
||||
m_updHandler = watcher.Updated(winrt::auto_revoke, updHandler);
|
||||
m_delHandler = watcher.Removed(winrt::auto_revoke, delHandler);
|
||||
|
||||
watcher.Start();
|
||||
}
|
||||
|
||||
/*
|
||||
evTokenOnInputAdded_
|
||||
= internalInPortObserver_.PortAdded([this](const port_info& p) { on_input_added(p); });
|
||||
evTokenOnInputRemoved_
|
||||
= internalInPortObserver_.PortRemoved([this](const port_info& p) { on_input_removed(p); });
|
||||
evTokenOnOutputAdded_
|
||||
= internalOutPortObserver_.PortAdded([this](const port_info& p) { on_output_added(p); });
|
||||
evTokenOnOutputRemoved_ = internalOutPortObserver_.PortRemoved(
|
||||
[this](const port_info& p) { on_output_removed(p); });
|
||||
*/
|
||||
}
|
||||
|
||||
~observer_impl()
|
||||
{
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
// internalInPortObserver_.PortAdded(evTokenOnInputAdded_);
|
||||
// internalInPortObserver_.PortRemoved(evTokenOnInputRemoved_);
|
||||
// internalOutPortObserver_.PortAdded(evTokenOnOutputAdded_);
|
||||
// internalOutPortObserver_.PortRemoved(evTokenOnOutputRemoved_);
|
||||
|
||||
m_addHandler.revoke();
|
||||
m_updHandler.revoke();
|
||||
m_delHandler.revoke();
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override
|
||||
@@ -67,33 +78,60 @@ public:
|
||||
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
||||
}
|
||||
|
||||
template <bool Input>
|
||||
auto to_port_info(const DeviceInformation& p) const noexcept
|
||||
-> std::conditional_t<Input, input_port, output_port>
|
||||
static transport_type code_to_type(std::string_view str) noexcept
|
||||
{
|
||||
using enum transport_type;
|
||||
|
||||
if (str.starts_with("KS"))
|
||||
return hardware;
|
||||
if (str == "BLE")
|
||||
return transport_type(hardware | bluetooth);
|
||||
if (str == "VPB" || str == "APP")
|
||||
return software;
|
||||
if (str == "LOOP")
|
||||
return transport_type(software | loopback);
|
||||
if (str.starts_with("NET"))
|
||||
return network;
|
||||
return unknown;
|
||||
}
|
||||
|
||||
template <bool Input>
|
||||
auto to_port_info(const MidiEndpointDeviceInformation& p, const MidiGroupTerminalBlock& gp)
|
||||
const noexcept -> std::conditional_t<Input, input_port, output_port>
|
||||
{
|
||||
const auto& tinfo = p.GetTransportSuppliedInfo();
|
||||
|
||||
return {
|
||||
{.client = 0,
|
||||
.port = 0,
|
||||
.manufacturer = "",
|
||||
.device_name = "",
|
||||
.port_name = to_string(p.Id()),
|
||||
.display_name = to_string(p.Name())}};
|
||||
{.api = libremidi::API::WINDOWS_MIDI_SERVICES,
|
||||
.client = 0,
|
||||
.container = std::bit_cast<libremidi::uuid>(p.ContainerId()),
|
||||
.device = to_string(p.EndpointDeviceId()),
|
||||
.port = gp.Number(),
|
||||
.manufacturer = to_string(tinfo.ManufacturerName),
|
||||
.product = to_string(tinfo.Name),
|
||||
.serial = to_string(tinfo.SerialNumber),
|
||||
.device_name = to_string(p.Name()),
|
||||
.port_name = to_string(gp.Name()),
|
||||
.display_name = to_string(gp.Name()) + " " + std::to_string(gp.Number()),
|
||||
.type = code_to_type(to_string(p.GetTransportSuppliedInfo().TransportCode))}};
|
||||
}
|
||||
|
||||
std::vector<libremidi::input_port> get_input_ports() const noexcept override
|
||||
{
|
||||
std::vector<libremidi::input_port> ret;
|
||||
|
||||
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
||||
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
||||
for (const auto& ep : endpointDevices)
|
||||
for (const auto& ep : MidiEndpointDeviceInformation::FindAll())
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
if (ep.Name().starts_with(L"Diagnostics"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// FIXME if(has input...)
|
||||
|
||||
ret.emplace_back(to_port_info<true>(ep));
|
||||
for (const auto& gp : ep.GetGroupTerminalBlocks())
|
||||
{
|
||||
if (gp.Direction() != MidiGroupTerminalBlockDirection::BlockOutput)
|
||||
ret.emplace_back(to_port_info<true>(ep, gp));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -103,46 +141,142 @@ public:
|
||||
{
|
||||
std::vector<libremidi::output_port> ret;
|
||||
|
||||
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
||||
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
||||
for (const auto& ep : endpointDevices)
|
||||
for (const auto& ep : MidiEndpointDeviceInformation::FindAll())
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
if (ep.Name().starts_with(L"Diagnostics"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// FIXME if(has output...)
|
||||
ret.emplace_back(to_port_info<false>(ep));
|
||||
|
||||
for (const auto& gp : ep.GetGroupTerminalBlocks())
|
||||
{
|
||||
if (gp.Direction() != MidiGroupTerminalBlockDirection::BlockInput)
|
||||
ret.emplace_back(to_port_info<false>(ep, gp));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void on_input_added(const DeviceInformation& name)
|
||||
// Note: these callbacks are called from some random thread!
|
||||
void on_device_added(
|
||||
const MidiEndpointDeviceWatcher&, const MidiEndpointDeviceInformationAddedEventArgs& result)
|
||||
{
|
||||
if (configuration.input_added)
|
||||
configuration.input_added(to_port_info<true>(name));
|
||||
add_device(result.AddedDevice());
|
||||
}
|
||||
|
||||
void on_input_removed(const DeviceInformation& name)
|
||||
void on_device_updated(
|
||||
const MidiEndpointDeviceWatcher&,
|
||||
const MidiEndpointDeviceInformationUpdatedEventArgs& result)
|
||||
{
|
||||
// OPTIMIZEME
|
||||
remove_device(result.EndpointDeviceId());
|
||||
|
||||
add_device(
|
||||
MidiEndpointDeviceInformation::CreateFromEndpointDeviceId(result.EndpointDeviceId()));
|
||||
}
|
||||
|
||||
void on_device_removed(
|
||||
const MidiEndpointDeviceWatcher&,
|
||||
const MidiEndpointDeviceInformationRemovedEventArgs& result)
|
||||
{
|
||||
remove_device(result.EndpointDeviceId());
|
||||
}
|
||||
|
||||
void add_device(const MidiEndpointDeviceInformation& ep)
|
||||
{
|
||||
for (const auto& gp : ep.GetGroupTerminalBlocks())
|
||||
{
|
||||
MidiGroupTerminalBlockDirection direction = gp.Direction();
|
||||
switch (direction)
|
||||
{
|
||||
case MidiGroupTerminalBlockDirection::Bidirectional: {
|
||||
if (configuration.input_added)
|
||||
{
|
||||
auto ip = to_port_info<true>(ep, gp);
|
||||
{
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_input_devices[ep.EndpointDeviceId()].push_back(ip);
|
||||
}
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.input_added(std::move(ip));
|
||||
}
|
||||
if (configuration.output_added)
|
||||
{
|
||||
auto op = to_port_info<false>(ep, gp);
|
||||
{
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_output_devices[ep.EndpointDeviceId()].push_back(op);
|
||||
}
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.output_added(std::move(op));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MidiGroupTerminalBlockDirection::BlockInput:
|
||||
if (configuration.input_added)
|
||||
{
|
||||
auto ip = to_port_info<true>(ep, gp);
|
||||
{
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_input_devices[ep.EndpointDeviceId()].push_back(ip);
|
||||
}
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.input_added(std::move(ip));
|
||||
}
|
||||
break;
|
||||
case MidiGroupTerminalBlockDirection::BlockOutput:
|
||||
if (configuration.output_added)
|
||||
{
|
||||
auto op = to_port_info<false>(ep, gp);
|
||||
{
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
m_known_output_devices[ep.EndpointDeviceId()].push_back(op);
|
||||
}
|
||||
|
||||
if(!m_in_constructor || configuration.notify_in_constructor)
|
||||
configuration.output_added(std::move(op));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void remove_device(winrt::hstring eid)
|
||||
{
|
||||
std::vector<input_port> to_remove_in;
|
||||
std::vector<output_port> to_remove_out;
|
||||
|
||||
{
|
||||
std::lock_guard _{m_devices_mtx};
|
||||
if (auto it = m_known_input_devices.find(eid); it != m_known_input_devices.end())
|
||||
{
|
||||
for (auto& ip : it->second)
|
||||
{
|
||||
to_remove_in.push_back(ip);
|
||||
}
|
||||
m_known_input_devices.erase(it);
|
||||
}
|
||||
if (auto it = m_known_output_devices.find(eid); it != m_known_output_devices.end())
|
||||
{
|
||||
for (auto& op : it->second)
|
||||
{
|
||||
to_remove_out.push_back(op);
|
||||
}
|
||||
m_known_output_devices.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
if (configuration.input_removed)
|
||||
configuration.input_removed(to_port_info<true>(name));
|
||||
}
|
||||
|
||||
void on_output_added(const DeviceInformation& name)
|
||||
{
|
||||
if (configuration.output_added)
|
||||
configuration.output_added(to_port_info<false>(name));
|
||||
}
|
||||
|
||||
void on_output_removed(const DeviceInformation& name)
|
||||
{
|
||||
for (auto& port : to_remove_in)
|
||||
configuration.input_removed(port);
|
||||
if (configuration.output_removed)
|
||||
configuration.output_removed(to_port_info<false>(name));
|
||||
for (auto& port : to_remove_out)
|
||||
configuration.output_removed(port);
|
||||
}
|
||||
|
||||
private:
|
||||
MidiSession session;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user