2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
#include <libremidi/backends/alsa_raw_ump/config.hpp>
|
|
|
|
|
#include <libremidi/backends/alsa_raw_ump/helpers.hpp>
|
|
|
|
|
#include <libremidi/detail/midi_out.hpp>
|
|
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
namespace libremidi::alsa_raw_ump
|
|
|
|
|
{
|
|
|
|
|
class midi_out_impl final
|
|
|
|
|
: public midi2::out_api
|
|
|
|
|
, public error_handler
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct
|
|
|
|
|
: libremidi::output_configuration
|
|
|
|
|
, libremidi::alsa_raw_ump::output_configuration
|
|
|
|
|
{
|
|
|
|
|
} configuration;
|
|
|
|
|
|
|
|
|
|
const libasound& snd = libasound::instance();
|
|
|
|
|
|
|
|
|
|
midi_out_impl(
|
|
|
|
|
libremidi::output_configuration&& conf,
|
|
|
|
|
libremidi::alsa_raw_ump::output_configuration&& apiconf)
|
|
|
|
|
: configuration{std::move(conf), std::move(apiconf)}
|
|
|
|
|
{
|
|
|
|
|
assert(snd.ump.available);
|
2024-10-28 19:24:18 +01:00
|
|
|
client_open_ = stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~midi_out_impl() override
|
|
|
|
|
{
|
|
|
|
|
// Close a connection if it exists.
|
|
|
|
|
midi_out_impl::close_port();
|
2024-10-28 19:24:18 +01:00
|
|
|
client_open_ = std::errc::not_connected;
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
libremidi::API get_current_api() const noexcept override { return libremidi::API::ALSA_RAW_UMP; }
|
2024-10-09 02:01:23 +02:00
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error connect_port(const char* portname)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
constexpr int mode = SND_RAWMIDI_SYNC;
|
2024-10-28 19:24:18 +01:00
|
|
|
int ret = snd.ump.open(NULL, &midiport_, portname, mode);
|
|
|
|
|
if (ret < 0)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
libremidi_handle_error(this->configuration, "cannot open device.");
|
|
|
|
|
return from_errc(ret);
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
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 open_port(const output_port& p, std::string_view) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
return connect_port(raw_from_port_handle(p.port).to_string().c_str());
|
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 (midiport_)
|
|
|
|
|
snd.ump.close(midiport_);
|
|
|
|
|
midiport_ = nullptr;
|
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_ump(const uint32_t* ump_stream, std::size_t count) override
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
if (!midiport_)
|
2024-10-28 19:24:18 +01:00
|
|
|
libremidi_handle_error(
|
2024-10-09 02:01:23 +02:00
|
|
|
this->configuration,
|
2024-10-28 19:24:18 +01:00
|
|
|
"trying to send a message without an open "
|
2024-10-09 02:01:23 +02:00
|
|
|
"port.");
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
return write(ump_stream, count * sizeof(uint32_t));
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
stdx::error write(const uint32_t* ump_stream, size_t bytes)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
if (auto err = snd.ump.write(midiport_, ump_stream, bytes); err < 0)
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
2024-10-28 19:24:18 +01:00
|
|
|
libremidi_handle_error(this->configuration, "cannot write message.");
|
|
|
|
|
return from_errc(err);
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 19:24:18 +01:00
|
|
|
return stdx::error{};
|
2024-10-09 02:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snd_ump_t* midiport_{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|