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_out.hpp>
|
2026-06-23 00:05:41 +02:00
|
|
|
#include <libremidi/detail/ump_stream.hpp>
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2026-06-23 00:05:41 +02:00
|
|
|
NAMESPACE_LIBREMIDI::winmidi
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class midi_out_impl final
|
|
|
|
|
: public midi2::out_api
|
|
|
|
|
, public error_handler
|
2026-06-23 00:05:41 +02:00
|
|
|
, public winmidi_shared_data
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: libremidi::output_configuration
|
|
|
|
|
, winmidi::output_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
midi_out_impl(libremidi::output_configuration&& conf, winmidi::output_configuration&& apiconf)
|
|
|
|
|
: configuration{std::move(conf), std::move(apiconf)}
|
2026-06-23 00:05:41 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~midi_out_impl() override { close_port(); }
|
|
|
|
|
|
|
|
|
|
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 output_port& port, std::string_view) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2026-06-23 00:05:41 +02:00
|
|
|
auto device_id = 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;
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2026-06-23 00:05:41 +02:00
|
|
|
m_endpoint = m_session.CreateEndpointConnection(ep.EndpointDeviceId());
|
|
|
|
|
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
|
|
|
|
m_endpoint.as(libremidi::IID_IMidiEndpointConnectionRaw, m_raw_endpoint.put_void());
|
|
|
|
|
#endif
|
2024-10-28 19:24:18 +01:00
|
|
|
m_endpoint.Open();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2026-06-23 00:05:41 +02:00
|
|
|
if(!m_endpoint)
|
|
|
|
|
return std::errc::not_connected;
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
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
|
|
|
stdx::error send_ump(const uint32_t* message, size_t size) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
auto write_func = [this](const uint32_t* ump, int64_t bytes) -> std::errc {
|
2026-06-23 00:05:41 +02:00
|
|
|
|
|
|
|
|
#if !LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
2024-10-28 19:24:18 +01:00
|
|
|
MidiSendMessageResults ret{};
|
2026-06-23 00:05:41 +02:00
|
|
|
switch (bytes / 4)
|
2024-10-28 19:24:18 +01:00
|
|
|
{
|
|
|
|
|
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:
|
2026-06-23 00:05:41 +02:00
|
|
|
ret = m_endpoint.SendSingleMessagePacket(
|
|
|
|
|
MidiMessage128(0, ump[0], ump[1], ump[2], ump[3]));
|
2024-10-28 19:24:18 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return std::errc::bad_message;
|
|
|
|
|
}
|
2026-06-23 00:05:41 +02:00
|
|
|
if (ret != MidiSendMessageResults::Succeeded)
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc::bad_message;
|
2026-06-23 00:05:41 +02:00
|
|
|
#else
|
|
|
|
|
HRESULT ret{};
|
|
|
|
|
switch (bytes / 4)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(1, (UINT32*)ump));
|
|
|
|
|
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 1, (UINT32*)ump);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(2, (UINT32*)ump));
|
|
|
|
|
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 2, (UINT32*)ump);
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(3, (UINT32*)ump));
|
|
|
|
|
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 3, (UINT32*)ump);
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
assert( m_raw_endpoint->ValidateBufferHasOnlyCompleteUmps(4, (UINT32*)ump));
|
|
|
|
|
ret = m_raw_endpoint->SendMidiMessagesRaw(0, 4, (UINT32*)ump);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return std::errc::bad_message;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if(ret < 0)
|
|
|
|
|
return std::errc::io_error;
|
2024-10-28 19:24:18 +01:00
|
|
|
return std::errc{0};
|
|
|
|
|
};
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2026-06-23 00:05:41 +02:00
|
|
|
return segment_ump_stream(message, size, write_func, []() { });
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2024-10-28 19:24:18 +01:00
|
|
|
MidiSession m_session;
|
2026-06-23 00:05:41 +02:00
|
|
|
winrt::Microsoft::Windows::Devices::Midi2::MidiEndpointConnection m_endpoint{nullptr};
|
|
|
|
|
#if LIBREMIDI_WINMIDI_HAS_COM_EXTENSIONS
|
|
|
|
|
winrt::impl::com_ref<IMidiEndpointConnectionRaw> m_raw_endpoint{};
|
|
|
|
|
#endif
|
2024-10-09 02:01:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|