working on win32 uwp! and libremidi update
This commit is contained in:
@@ -5,15 +5,23 @@
|
||||
#include <libremidi/detail/midi_api.hpp>
|
||||
|
||||
#include <mutex>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <guiddef.h>
|
||||
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Foundation.Collections.h>
|
||||
#include <winrt/Windows.Devices.Enumeration.h>
|
||||
#include <winrt/Windows.Devices.Midi2.h>
|
||||
|
||||
|
||||
namespace midi2 = winrt::Windows::Devices::Midi2;
|
||||
namespace foundation = winrt::Windows::Foundation;
|
||||
namespace collections = winrt::Windows::Foundation::Collections;
|
||||
|
||||
|
||||
#include <winrt/Microsoft.Devices.Midi2.h>
|
||||
// clang-format on
|
||||
|
||||
namespace libremidi::winmidi
|
||||
@@ -22,6 +30,31 @@ using namespace winrt;
|
||||
using namespace winrt::Windows::Foundation;
|
||||
using namespace winrt::Windows::Devices::Enumeration;
|
||||
using namespace winrt::Windows::Storage::Streams;
|
||||
using namespace Microsoft::Devices::Midi2;
|
||||
using namespace winrt::Windows::Devices::Midi2;
|
||||
using namespace Windows::Devices::Enumeration;
|
||||
|
||||
inline bool ichar_equals(char a, 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) {
|
||||
auto eps = MidiEndpointDeviceInformation::FindAll();
|
||||
for (const auto& ep : eps)
|
||||
{
|
||||
auto str = to_string(ep.Id());
|
||||
if (str.empty())
|
||||
continue;
|
||||
|
||||
if (iequals(str, port))
|
||||
return ep;
|
||||
}
|
||||
return {nullptr};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <libremidi/backends/winmidi/helpers.hpp>
|
||||
#include <libremidi/backends/winmidi/observer.hpp>
|
||||
#include <libremidi/detail/midi_in.hpp>
|
||||
#include <libremidi/detail/midi_stream_decoder.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace libremidi::winmidi
|
||||
{
|
||||
@@ -21,23 +24,15 @@ public:
|
||||
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")}
|
||||
{
|
||||
this->client_open_ = stdx::error{};
|
||||
}
|
||||
|
||||
~midi_in_impl() override { close_port(); }
|
||||
|
||||
bool open_virtual_port(std::string_view) override
|
||||
~midi_in_impl() override
|
||||
{
|
||||
warning(configuration, "midi_in_winmidi: open_virtual_port unsupported");
|
||||
return false;
|
||||
}
|
||||
void set_client_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_in_winmidi: set_client_name unsupported");
|
||||
}
|
||||
void set_port_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_in_winmidi: set_port_name unsupported");
|
||||
close_port();
|
||||
this->client_open_ = std::errc::not_connected;
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override
|
||||
@@ -45,50 +40,58 @@ public:
|
||||
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
||||
}
|
||||
|
||||
bool open_port(const input_port& port, std::string_view) override
|
||||
stdx::error open_port(const input_port& port, std::string_view) override
|
||||
{
|
||||
#if 0
|
||||
const auto id = winrt::to_hstring(port.port_name);
|
||||
if (id.empty())
|
||||
return false;
|
||||
auto ep = get_port_by_name(port.port_name);
|
||||
if (!ep)
|
||||
return std::errc::address_not_available;
|
||||
|
||||
port_ = get(MidiInPort::FromIdAsync(id));
|
||||
if (!port_)
|
||||
return false;
|
||||
port_.MessageReceived(
|
||||
[=](const winrt::Windows::Devices::Midi::IMidiInPort& inputPort,
|
||||
const winrt::Windows::Devices::Midi::MidiMessageReceivedEventArgs& args) {
|
||||
this->process_message(args.Message());
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.Id());
|
||||
|
||||
m_revoke_token = m_endpoint.MessageReceived(
|
||||
[&](const foundation::IInspectable& sender,
|
||||
const winrt::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& args) {
|
||||
process_message(args);
|
||||
});
|
||||
|
||||
#endif
|
||||
return true;
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
#if 0
|
||||
void process_message(const winrt::Windows::Devices::Midi::IMidiMessage& msg)
|
||||
void process_message(const winrt::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
||||
{
|
||||
auto reader = DataReader::FromBuffer(msg.RawData());
|
||||
auto begin = msg.RawData().data();
|
||||
auto end = begin + msg.RawData().Length();
|
||||
static constexpr timestamp_backend_info timestamp_info{
|
||||
.has_absolute_timestamps = true,
|
||||
.absolute_is_monotonic = false,
|
||||
.has_samples = false,
|
||||
};
|
||||
|
||||
auto t = msg.Timestamp().count();
|
||||
this->configuration.on_message(libremidi::message{{begin, end}, t});
|
||||
const auto& ump = msg.GetMessagePacket();
|
||||
const auto& b = ump.GetAllWords();
|
||||
|
||||
uint32_t ump_space[64];
|
||||
array_view<uint32_t> ref{ump_space};
|
||||
b.GetMany(0, ref);
|
||||
|
||||
auto to_ns = [t = ump.Timestamp()] { return t; };
|
||||
m_processing.on_bytes(
|
||||
{ump_space, ump_space + b.Size()}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
void close_port() override
|
||||
stdx::error close_port() override
|
||||
{
|
||||
#if 0
|
||||
if (port_)
|
||||
{
|
||||
port_.Close();
|
||||
port_ = nullptr;
|
||||
}
|
||||
#endif
|
||||
m_endpoint.MessageReceived(m_revoke_token);
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
virtual timestamp absolute_timestamp() const noexcept override { return {}; }
|
||||
|
||||
private:
|
||||
// winrt::Microsoft::Devices::Midi2::IMidiInPort port_{nullptr};
|
||||
MidiSession m_session;
|
||||
winrt::event_token m_revoke_token{};
|
||||
winrt::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
midi2::input_state_machine m_processing{this->configuration};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#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>
|
||||
@@ -20,72 +21,69 @@ 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")}
|
||||
{
|
||||
this->client_open_ = stdx::error{};
|
||||
}
|
||||
|
||||
~midi_out_impl() override { close_port(); }
|
||||
|
||||
bool open_virtual_port(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winmidi: open_virtual_port unsupported");
|
||||
return false;
|
||||
}
|
||||
void set_client_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winmidi: set_client_name unsupported");
|
||||
}
|
||||
void set_port_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winmidi: set_port_name unsupported");
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override
|
||||
{
|
||||
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
||||
}
|
||||
|
||||
bool open_port(const output_port& port, std::string_view) override
|
||||
stdx::error open_port(const output_port& port, std::string_view) override
|
||||
{
|
||||
#if 0
|
||||
const auto id = winrt::to_hstring(port.port_name);
|
||||
if (id.empty())
|
||||
return false;
|
||||
auto ep = get_port_by_name(port.port_name);
|
||||
if (!ep)
|
||||
return std::errc::address_not_available;
|
||||
|
||||
port_ = get(MidiOutPort::FromIdAsync(id));
|
||||
return bool(port_);
|
||||
#endif
|
||||
return true;
|
||||
m_endpoint = m_session.CreateEndpointConnection(ep.Id());
|
||||
m_endpoint.Open();
|
||||
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
void close_port() override
|
||||
stdx::error close_port() override
|
||||
{
|
||||
#if 0
|
||||
if (port_)
|
||||
{
|
||||
port_.Close();
|
||||
port_ = {};
|
||||
}
|
||||
#endif
|
||||
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
||||
return stdx::error{};
|
||||
}
|
||||
|
||||
void send_ump(const uint32_t* message, size_t size) override
|
||||
stdx::error send_ump(const uint32_t* message, size_t size) override
|
||||
{
|
||||
#if 0
|
||||
if (!port_)
|
||||
return;
|
||||
auto write_func = [this](const uint32_t* ump, int64_t bytes) -> std::errc {
|
||||
MidiSendMessageResults ret{};
|
||||
switch(bytes / 4)
|
||||
{
|
||||
case 1:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage32(0, ump[0]));
|
||||
break;
|
||||
case 2:
|
||||
ret = m_endpoint.SendSingleMessagePacket(MidiMessage64(0, ump[0], ump[1]));
|
||||
break;
|
||||
case 3:
|
||||
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]));
|
||||
break;
|
||||
default:
|
||||
return std::errc::bad_message;
|
||||
}
|
||||
|
||||
InMemoryRandomAccessStream str;
|
||||
DataWriter rb(str);
|
||||
rb.WriteBytes(
|
||||
winrt::array_view<const uint8_t>{(const uint8_t*)message, (const uint8_t*)message + size});
|
||||
port_.SendBuffer(rb.DetachBuffer());
|
||||
#endif
|
||||
if(ret != MidiSendMessageResults::Succeeded)
|
||||
return std::errc::bad_message;
|
||||
return std::errc{0};
|
||||
};
|
||||
|
||||
return segment_ump_stream(message, size, write_func, []() {});
|
||||
}
|
||||
|
||||
private:
|
||||
#if 0
|
||||
winrt::Windows::Devices::Midi2::IMidiOutPort port_{nullptr};
|
||||
#endif
|
||||
MidiSession m_session;
|
||||
winrt::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -20,13 +20,11 @@ public:
|
||||
{
|
||||
} configuration;
|
||||
|
||||
MidiSession session;
|
||||
|
||||
explicit observer_impl(
|
||||
libremidi::observer_configuration&& conf, winmidi::observer_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
, session{
|
||||
MidiSession::CreateNewSession(L"libremidi session", MidiSessionSettings::Default())}
|
||||
, session{MidiSession::CreateSession(L"libremidi session")}
|
||||
{
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
@@ -36,6 +34,7 @@ public:
|
||||
if (configuration.input_added)
|
||||
for (const auto& p : get_input_ports())
|
||||
configuration.input_added(p);
|
||||
|
||||
if (configuration.output_added)
|
||||
for (const auto& p : get_output_ports())
|
||||
configuration.output_added(p);
|
||||
@@ -87,10 +86,14 @@ public:
|
||||
|
||||
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
||||
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
||||
for (const auto& selectedEndpointInformation : endpointDevices)
|
||||
for (const auto& ep : endpointDevices)
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
continue;
|
||||
}
|
||||
// FIXME if(has input...)
|
||||
ret.emplace_back(to_port_info(selectedEndpointInformation));
|
||||
|
||||
ret.emplace_back(to_port_info<true>(ep));
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -102,10 +105,13 @@ public:
|
||||
|
||||
auto deviceSelector = MidiEndpointConnection::GetDeviceSelector();
|
||||
auto endpointDevices = DeviceInformation::FindAllAsync(deviceSelector).get();
|
||||
for (const auto& selectedEndpointInformation : endpointDevices)
|
||||
for (const auto& ep : endpointDevices)
|
||||
{
|
||||
if(ep.Name().starts_with(L"Diagnostics")) {
|
||||
continue;
|
||||
}
|
||||
// FIXME if(has output...)
|
||||
ret.emplace_back(to_port_info(selectedEndpointInformation));
|
||||
ret.emplace_back(to_port_info<false>(ep));
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -114,38 +120,29 @@ public:
|
||||
void on_input_added(const DeviceInformation& name)
|
||||
{
|
||||
if (configuration.input_added)
|
||||
configuration.input_added(to_port_info(name));
|
||||
configuration.input_added(to_port_info<true>(name));
|
||||
}
|
||||
|
||||
void on_input_removed(const DeviceInformation& name)
|
||||
{
|
||||
if (configuration.input_removed)
|
||||
configuration.input_removed(to_port_info(name));
|
||||
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(name));
|
||||
configuration.output_added(to_port_info<false>(name));
|
||||
}
|
||||
|
||||
void on_output_removed(const DeviceInformation& name)
|
||||
{
|
||||
if (configuration.output_removed)
|
||||
configuration.output_removed(to_port_info(name));
|
||||
configuration.output_removed(to_port_info<false>(name));
|
||||
}
|
||||
|
||||
private:
|
||||
#if 0
|
||||
static inline observer_winmidi_internal internalInPortObserver_{MidiInPort::GetDeviceSelector()};
|
||||
static inline observer_winmidi_internal internalOutPortObserver_{
|
||||
MidiOutPort::GetDeviceSelector()};
|
||||
|
||||
int evTokenOnInputAdded_{-1};
|
||||
int evTokenOnInputRemoved_{-1};
|
||||
int evTokenOnOutputAdded_{-1};
|
||||
int evTokenOnOutputRemoved_{-1};
|
||||
#endif
|
||||
MidiSession session;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user