libremidi update to v5.4.3 and added virtual midi out as default
This commit is contained in:
@@ -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