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_out.hpp>
|
|
|
|
|
|
|
|
|
|
namespace libremidi
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class midi_out_winuwp final
|
|
|
|
|
: public midi1::out_api
|
|
|
|
|
, public error_handler
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: output_configuration
|
|
|
|
|
, winuwp_output_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
midi_out_winuwp(output_configuration&& conf, winuwp_output_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_out_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 output_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(MidiOutPort::FromIdAsync(id));
|
2024-10-28 19:24:18 +01:00
|
|
|
if (!bool(port_))
|
|
|
|
|
return std::errc::io_error;
|
|
|
|
|
|
|
|
|
|
return stdx::error{};
|
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
|
|
|
{
|
|
|
|
|
if (port_)
|
|
|
|
|
{
|
|
|
|
|
port_.Close();
|
|
|
|
|
port_ = {};
|
|
|
|
|
}
|
2024-10-28 19:24:18 +01:00
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error send_message(const unsigned char* message, size_t size) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
if (!port_)
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc::not_connected;
|
2024-10-09 02:01:23 +02:00
|
|
|
|
|
|
|
|
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());
|
2024-10-28 19:24:18 +01:00
|
|
|
|
|
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
winrt::Windows::Devices::Midi::IMidiOutPort port_{nullptr};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|