2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
#include <libremidi/backends/winmidi/config.hpp>
|
|
|
|
|
#include <libremidi/backends/winmidi/helpers.hpp>
|
|
|
|
|
#include <libremidi/backends/winmidi/observer.hpp>
|
|
|
|
|
#include <libremidi/detail/midi_in.hpp>
|
2024-10-28 19:24:18 +01:00
|
|
|
#include <libremidi/detail/midi_stream_decoder.hpp>
|
|
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
namespace libremidi::winmidi
|
|
|
|
|
{
|
|
|
|
|
class midi_in_impl final
|
|
|
|
|
: public midi2::in_api
|
|
|
|
|
, public error_handler
|
2025-06-13 19:06:09 +02:00
|
|
|
, public winmidi_shared_data
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: libremidi::ump_input_configuration
|
|
|
|
|
, winmidi::input_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
explicit midi_in_impl(
|
|
|
|
|
libremidi::ump_input_configuration&& conf, winmidi::input_configuration&& apiconf)
|
|
|
|
|
: configuration{std::move(conf), std::move(apiconf)}
|
2025-06-13 19:06:09 +02:00
|
|
|
, m_session{
|
|
|
|
|
configuration.context ? *configuration.context
|
|
|
|
|
: MidiSession::Create(to_hstring(configuration.client_name))}
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
this->client_open_ = stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
~midi_in_impl() override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
close_port();
|
|
|
|
|
this->client_open_ = std::errc::not_connected;
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
libremidi::API get_current_api() const noexcept override
|
|
|
|
|
{
|
|
|
|
|
return libremidi::API::WINDOWS_MIDI_SERVICES;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error open_port(const input_port& port, std::string_view) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2025-06-13 19:06:09 +02:00
|
|
|
auto device_id = std::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)
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc::address_not_available;
|
|
|
|
|
|
2025-06-13 19:06:09 +02:00
|
|
|
m_group_filter = port.port - 1;
|
|
|
|
|
|
|
|
|
|
// TODO use a MidiGroupEndpointListener for the filtering
|
|
|
|
|
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
2024-10-28 19:24:18 +01:00
|
|
|
|
|
|
|
|
m_revoke_token = m_endpoint.MessageReceived(
|
2025-06-13 19:06:09 +02:00
|
|
|
[this](
|
|
|
|
|
const winrt::Microsoft::Windows::Devices::Midi2::IMidiMessageReceivedEventSource&,
|
|
|
|
|
const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& args) {
|
2024-10-28 19:24:18 +01:00
|
|
|
process_message(args);
|
2024-10-09 02:01:23 +02:00
|
|
|
});
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
m_endpoint.Open();
|
|
|
|
|
|
|
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 19:06:09 +02:00
|
|
|
void process_message(const winrt::Microsoft::Windows::Devices::Midi2::MidiMessageReceivedEventArgs& msg)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
static constexpr timestamp_backend_info timestamp_info{
|
|
|
|
|
.has_absolute_timestamps = true,
|
|
|
|
|
.absolute_is_monotonic = false,
|
|
|
|
|
.has_samples = false,
|
|
|
|
|
};
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
const auto& ump = msg.GetMessagePacket();
|
2025-06-13 19:06:09 +02:00
|
|
|
auto pk = msg.PeekFirstWord();
|
|
|
|
|
if (m_group_filter >= 0)
|
|
|
|
|
{
|
|
|
|
|
int group = cmidi2_ump_get_group(&pk);
|
|
|
|
|
if (group != m_group_filter)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
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));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error close_port() override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
m_endpoint.MessageReceived(m_revoke_token);
|
|
|
|
|
m_session.DisconnectEndpointConnection(m_endpoint.ConnectionId());
|
|
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
virtual timestamp absolute_timestamp() const noexcept override { return {}; }
|
|
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
private:
|
2024-10-28 19:24:18 +01:00
|
|
|
MidiSession m_session;
|
|
|
|
|
winrt::event_token m_revoke_token{};
|
2025-06-13 19:06:09 +02:00
|
|
|
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
2024-10-28 19:24:18 +01:00
|
|
|
midi2::input_state_machine m_processing{this->configuration};
|
2025-06-13 19:06:09 +02:00
|
|
|
int m_group_filter = -1;
|
2024-10-09 02:01:23 +02:00
|
|
|
};
|
|
|
|
|
}
|