2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
#include <libremidi/backends/winuwp/config.hpp>
|
|
|
|
|
#include <libremidi/backends/winuwp/helpers.hpp>
|
|
|
|
|
#include <libremidi/backends/winuwp/observer.hpp>
|
|
|
|
|
#include <libremidi/detail/midi_in.hpp>
|
|
|
|
|
#include <libremidi/detail/midi_stream_decoder.hpp>
|
|
|
|
|
|
|
|
|
|
namespace libremidi
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class midi_in_winuwp final
|
|
|
|
|
: public midi1::in_api
|
|
|
|
|
, public error_handler
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: input_configuration
|
|
|
|
|
, winuwp_input_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
explicit midi_in_winuwp(input_configuration&& conf, winuwp_input_configuration&& apiconf)
|
|
|
|
|
: configuration{std::move(conf), std::move(apiconf)}
|
|
|
|
|
{
|
|
|
|
|
winrt_init();
|
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_winuwp() 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_UWP; }
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
const auto id = winrt::to_hstring(port.port_name);
|
|
|
|
|
if (id.empty())
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc::invalid_argument;
|
2024-10-09 02:01:23 +02:00
|
|
|
|
|
|
|
|
port_ = get(MidiInPort::FromIdAsync(id));
|
|
|
|
|
if (!port_)
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc::io_error;
|
2024-10-09 02:01:23 +02:00
|
|
|
|
|
|
|
|
midi_start_timestamp = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
port_.MessageReceived(
|
|
|
|
|
[=](const winrt::Windows::Devices::Midi::IMidiInPort& inputPort,
|
|
|
|
|
const winrt::Windows::Devices::Midi::MidiMessageReceivedEventArgs& args) {
|
|
|
|
|
this->process_message(args.Message());
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void process_message(const winrt::Windows::Devices::Midi::IMidiMessage& msg)
|
|
|
|
|
{
|
|
|
|
|
static constexpr timestamp_backend_info timestamp_info{
|
|
|
|
|
.has_absolute_timestamps = true,
|
|
|
|
|
.absolute_is_monotonic = false,
|
|
|
|
|
.has_samples = false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto reader = DataReader::FromBuffer(msg.RawData());
|
|
|
|
|
auto begin = msg.RawData().data();
|
|
|
|
|
auto end = begin + msg.RawData().Length();
|
|
|
|
|
|
|
|
|
|
const auto to_ns = [&msg] { return msg.Timestamp().count() * 1'000'000; };
|
|
|
|
|
m_processing.on_bytes({begin, end}, m_processing.timestamp<timestamp_info>(to_ns, 0));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error close_port() override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
if (port_)
|
|
|
|
|
{
|
|
|
|
|
port_.Close();
|
|
|
|
|
port_ = nullptr;
|
|
|
|
|
}
|
2024-10-28 19:24:18 +01:00
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timestamp absolute_timestamp() const noexcept override
|
|
|
|
|
{
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
|
std::chrono::steady_clock::now() - midi_start_timestamp)
|
|
|
|
|
.count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
winrt::Windows::Devices::Midi::IMidiInPort port_{nullptr};
|
|
|
|
|
std::chrono::steady_clock::time_point midi_start_timestamp;
|
|
|
|
|
|
|
|
|
|
midi1::input_state_machine m_processing{this->configuration};
|
|
|
|
|
};
|
|
|
|
|
}
|