very, very simple WP with midi CC out
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <libremidi/config.hpp>
|
||||
|
||||
namespace libremidi
|
||||
{
|
||||
|
||||
struct winuwp_input_configuration
|
||||
{
|
||||
};
|
||||
|
||||
struct winuwp_output_configuration
|
||||
{
|
||||
};
|
||||
|
||||
struct winuwp_observer_configuration
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
#define NOMINMAX 1
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <libremidi/detail/midi_api.hpp>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <winrt/Windows.Devices.Enumeration.h>
|
||||
#include <winrt/Windows.Devices.Midi.h>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Storage.Streams.h>
|
||||
|
||||
namespace libremidi
|
||||
{
|
||||
inline void winrt_init()
|
||||
{
|
||||
// init_apartment should only be called on the threads we own.
|
||||
// Since we're the library we don't own the threads we are called from,
|
||||
// so we should not perform this initialization ourselves.
|
||||
// winrt::init_apartment();
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace winrt;
|
||||
using namespace winrt::Windows::Foundation;
|
||||
using namespace winrt::Windows::Devices::Midi;
|
||||
using namespace winrt::Windows::Devices::Enumeration;
|
||||
using namespace winrt::Windows::Storage::Streams;
|
||||
|
||||
// Helper function to allow waiting for aynchronous operation completion
|
||||
// from the thread in STA. The only benefit from it compared to the
|
||||
// get() function from winrt is that we avoid an assertion if waiting
|
||||
// from the STA thread.
|
||||
template <typename T>
|
||||
auto get(T const& async)
|
||||
{
|
||||
if (async.Status() != AsyncStatus::Completed)
|
||||
{
|
||||
slim_mutex m;
|
||||
slim_condition_variable cv;
|
||||
bool completed = false;
|
||||
|
||||
async.Completed([&](auto&&, auto&&) {
|
||||
{
|
||||
slim_lock_guard const guard(m);
|
||||
completed = true;
|
||||
}
|
||||
|
||||
cv.notify_one();
|
||||
});
|
||||
|
||||
slim_lock_guard guard(m);
|
||||
cv.wait(m, [&] { return completed; });
|
||||
}
|
||||
|
||||
return async.GetResults();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
~midi_in_winuwp() override { close_port(); }
|
||||
|
||||
bool open_virtual_port(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_in_winuwp: open_virtual_port unsupported");
|
||||
return false;
|
||||
}
|
||||
void set_client_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_in_winuwp: set_client_name unsupported");
|
||||
}
|
||||
void set_port_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_in_winuwp: set_port_name unsupported");
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override { return libremidi::API::WINDOWS_UWP; }
|
||||
|
||||
bool open_port(const input_port& port, std::string_view) override
|
||||
{
|
||||
const auto id = winrt::to_hstring(port.port_name);
|
||||
if (id.empty())
|
||||
return false;
|
||||
|
||||
port_ = get(MidiInPort::FromIdAsync(id));
|
||||
if (!port_)
|
||||
return false;
|
||||
|
||||
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());
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void close_port() override
|
||||
{
|
||||
if (port_)
|
||||
{
|
||||
port_.Close();
|
||||
port_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
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};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
~midi_out_winuwp() override { close_port(); }
|
||||
|
||||
bool open_virtual_port(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winuwp: open_virtual_port unsupported");
|
||||
return false;
|
||||
}
|
||||
void set_client_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winuwp: set_client_name unsupported");
|
||||
}
|
||||
void set_port_name(std::string_view) override
|
||||
{
|
||||
warning(configuration, "midi_out_winuwp: set_port_name unsupported");
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override { return libremidi::API::WINDOWS_UWP; }
|
||||
|
||||
bool open_port(const output_port& port, std::string_view) override
|
||||
{
|
||||
const auto id = winrt::to_hstring(port.port_name);
|
||||
if (id.empty())
|
||||
return false;
|
||||
|
||||
port_ = get(MidiOutPort::FromIdAsync(id));
|
||||
return bool(port_);
|
||||
}
|
||||
|
||||
void close_port() override
|
||||
{
|
||||
if (port_)
|
||||
{
|
||||
port_.Close();
|
||||
port_ = {};
|
||||
}
|
||||
}
|
||||
|
||||
void send_message(const unsigned char* message, size_t size) override
|
||||
{
|
||||
if (!port_)
|
||||
return;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
private:
|
||||
winrt::Windows::Devices::Midi::IMidiOutPort port_{nullptr};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
#pragma once
|
||||
#include <libremidi/backends/winuwp/config.hpp>
|
||||
#include <libremidi/detail/observer.hpp>
|
||||
|
||||
namespace libremidi
|
||||
{
|
||||
class observer_winuwp_internal
|
||||
{
|
||||
public:
|
||||
struct port_info
|
||||
{
|
||||
hstring id;
|
||||
hstring name;
|
||||
};
|
||||
struct callback
|
||||
{
|
||||
int token{};
|
||||
std::function<void(const port_info&)> function;
|
||||
};
|
||||
struct callbacks
|
||||
{
|
||||
std::vector<callback> cbs;
|
||||
int current_token{};
|
||||
void operator()(const port_info& p)
|
||||
{
|
||||
for (auto& cb : cbs)
|
||||
cb.function(p);
|
||||
}
|
||||
|
||||
int add(std::function<void(const port_info&)> f)
|
||||
{
|
||||
int tk = current_token++;
|
||||
cbs.emplace_back(tk, f);
|
||||
return tk;
|
||||
}
|
||||
void remove(int tk)
|
||||
{
|
||||
auto it = std::remove_if(
|
||||
cbs.begin(), cbs.end(), [tk](const callback& c) { return c.token == tk; });
|
||||
auto r = std::distance(it, cbs.end());
|
||||
cbs.erase(it, cbs.end());
|
||||
}
|
||||
};
|
||||
|
||||
explicit observer_winuwp_internal(hstring deviceSelector) { initialize(deviceSelector); }
|
||||
~observer_winuwp_internal() { terminate(); }
|
||||
|
||||
std::vector<port_info> get_ports() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
return portList_;
|
||||
}
|
||||
|
||||
unsigned int get_port_count() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
return static_cast<unsigned int>(portList_.size());
|
||||
}
|
||||
|
||||
bool get_port_info(unsigned int portNumber, port_info& portInfo) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
if (portNumber >= portList_.size())
|
||||
return false;
|
||||
portInfo = portList_[portNumber];
|
||||
return true;
|
||||
}
|
||||
|
||||
hstring get_port_id(unsigned int portNumber) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
return portNumber < portList_.size() ? portList_[portNumber].id : hstring{};
|
||||
}
|
||||
|
||||
std::string get_port_name(unsigned int portNumber) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
return portNumber < portList_.size() ? to_string(portList_[portNumber].name) : std::string{};
|
||||
}
|
||||
|
||||
int PortAdded(const std::function<void(port_info)>& handler)
|
||||
{
|
||||
return portAddedEvent_.add(handler);
|
||||
}
|
||||
|
||||
void PortAdded(int token) noexcept { portAddedEvent_.remove(token); }
|
||||
|
||||
int PortRemoved(const std::function<void(port_info)>& handler)
|
||||
{
|
||||
return portRemovedEvent_.add(handler);
|
||||
}
|
||||
|
||||
void PortRemoved(int token) noexcept { portRemovedEvent_.remove(token); }
|
||||
|
||||
private:
|
||||
observer_winuwp_internal(const observer_winuwp_internal&) = delete;
|
||||
observer_winuwp_internal& operator=(const observer_winuwp_internal&) = delete;
|
||||
|
||||
private:
|
||||
void initialize(hstring deviceSelector)
|
||||
{
|
||||
deviceWatcher_ = DeviceInformation::CreateWatcher(deviceSelector);
|
||||
|
||||
evTokenOnDeviceAdded_
|
||||
= deviceWatcher_.Added({this, &observer_winuwp_internal::on_device_added});
|
||||
evTokenOnDeviceRemoved_
|
||||
= deviceWatcher_.Removed({this, &observer_winuwp_internal::on_device_removed});
|
||||
evTokenOnDeviceUpdated_
|
||||
= deviceWatcher_.Updated({this, &observer_winuwp_internal::on_device_updated});
|
||||
evTokenOnDeviceEnumerationCompleted_ = deviceWatcher_.EnumerationCompleted(
|
||||
{this, &observer_winuwp_internal::on_device_enumeration_completed});
|
||||
|
||||
deviceWatcher_.Start();
|
||||
}
|
||||
|
||||
void terminate()
|
||||
{
|
||||
deviceWatcher_.Stop();
|
||||
deviceWatcher_.EnumerationCompleted(evTokenOnDeviceEnumerationCompleted_);
|
||||
deviceWatcher_.Updated(evTokenOnDeviceUpdated_);
|
||||
deviceWatcher_.Removed(evTokenOnDeviceRemoved_);
|
||||
deviceWatcher_.Added(evTokenOnDeviceAdded_);
|
||||
}
|
||||
|
||||
void on_device_added(DeviceWatcher sender, DeviceInformation deviceInfo)
|
||||
{
|
||||
port_info p;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
p = port_info{deviceInfo.Id(), deviceInfo.Name()};
|
||||
portList_.push_back(p);
|
||||
}
|
||||
portAddedEvent_(p);
|
||||
}
|
||||
|
||||
void on_device_removed(DeviceWatcher sender, DeviceInformationUpdate deviceUpdate)
|
||||
{
|
||||
const auto id = deviceUpdate.Id();
|
||||
auto pred = [&id](const port_info& portInfo) { return portInfo.id == id; };
|
||||
std::optional<port_info> p;
|
||||
hstring name;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(portListMutex_);
|
||||
auto iter = std::find_if(portList_.begin(), portList_.end(), pred);
|
||||
if (iter != portList_.end())
|
||||
{
|
||||
p = *iter;
|
||||
portList_.erase(iter);
|
||||
}
|
||||
}
|
||||
if (p)
|
||||
portRemovedEvent_(*p);
|
||||
}
|
||||
|
||||
void on_device_updated(DeviceWatcher sender, DeviceInformationUpdate deviceUpdate) { }
|
||||
|
||||
void on_device_enumeration_completed(DeviceWatcher sender, IInspectable const&) { }
|
||||
|
||||
private:
|
||||
std::vector<port_info> portList_;
|
||||
mutable std::mutex portListMutex_;
|
||||
|
||||
DeviceWatcher deviceWatcher_{nullptr};
|
||||
event_token evTokenOnDeviceAdded_;
|
||||
event_token evTokenOnDeviceRemoved_;
|
||||
event_token evTokenOnDeviceUpdated_;
|
||||
event_token evTokenOnDeviceEnumerationCompleted_;
|
||||
|
||||
callbacks portAddedEvent_;
|
||||
callbacks portRemovedEvent_;
|
||||
};
|
||||
|
||||
class observer_winuwp final : public observer_api
|
||||
{
|
||||
public:
|
||||
struct
|
||||
: observer_configuration
|
||||
, winuwp_observer_configuration
|
||||
{
|
||||
} configuration;
|
||||
|
||||
using port_info = observer_winuwp_internal::port_info;
|
||||
explicit observer_winuwp(observer_configuration&& conf, winuwp_observer_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
{
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
|
||||
if (configuration.notify_in_constructor)
|
||||
{
|
||||
if (configuration.input_added)
|
||||
for (const auto& p : get_input_ports())
|
||||
configuration.input_added(p);
|
||||
|
||||
if (configuration.output_added)
|
||||
for (const auto& p : get_output_ports())
|
||||
configuration.output_added(p);
|
||||
}
|
||||
|
||||
evTokenOnInputAdded_
|
||||
= internalInPortObserver_.PortAdded([this](const port_info& p) { on_input_added(p); });
|
||||
evTokenOnInputRemoved_
|
||||
= internalInPortObserver_.PortRemoved([this](const port_info& p) { on_input_removed(p); });
|
||||
evTokenOnOutputAdded_
|
||||
= internalOutPortObserver_.PortAdded([this](const port_info& p) { on_output_added(p); });
|
||||
evTokenOnOutputRemoved_ = internalOutPortObserver_.PortRemoved(
|
||||
[this](const port_info& p) { on_output_removed(p); });
|
||||
}
|
||||
|
||||
~observer_winuwp()
|
||||
{
|
||||
if (!configuration.has_callbacks())
|
||||
return;
|
||||
internalInPortObserver_.PortAdded(evTokenOnInputAdded_);
|
||||
internalInPortObserver_.PortRemoved(evTokenOnInputRemoved_);
|
||||
internalOutPortObserver_.PortAdded(evTokenOnOutputAdded_);
|
||||
internalOutPortObserver_.PortRemoved(evTokenOnOutputRemoved_);
|
||||
}
|
||||
|
||||
libremidi::API get_current_api() const noexcept override { return libremidi::API::WINDOWS_UWP; }
|
||||
|
||||
template <bool Input>
|
||||
auto to_port_info(const observer_winuwp_internal::port_info& p) const noexcept
|
||||
-> std::conditional_t<Input, input_port, output_port>
|
||||
{
|
||||
return {
|
||||
{.client = 0,
|
||||
.port = 0,
|
||||
.manufacturer = "",
|
||||
.device_name = "",
|
||||
.port_name = to_string(p.id),
|
||||
.display_name = to_string(p.name)}};
|
||||
}
|
||||
|
||||
std::vector<libremidi::input_port> get_input_ports() const noexcept override
|
||||
{
|
||||
std::vector<libremidi::input_port> ret;
|
||||
for (auto& port : internalInPortObserver_.get_ports())
|
||||
ret.push_back(to_port_info<true>(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<libremidi::output_port> get_output_ports() const noexcept override
|
||||
{
|
||||
std::vector<libremidi::output_port> ret;
|
||||
for (auto& port : internalOutPortObserver_.get_ports())
|
||||
ret.push_back(to_port_info<false>(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static observer_winuwp_internal& get_internal_in_port_observer()
|
||||
{
|
||||
return internalInPortObserver_;
|
||||
}
|
||||
|
||||
static observer_winuwp_internal& get_internal_out_port_observer()
|
||||
{
|
||||
return internalOutPortObserver_;
|
||||
}
|
||||
|
||||
void on_input_added(const observer_winuwp_internal::port_info& name)
|
||||
{
|
||||
if (configuration.input_added)
|
||||
configuration.input_added(to_port_info<true>(name));
|
||||
}
|
||||
|
||||
void on_input_removed(const observer_winuwp_internal::port_info& name)
|
||||
{
|
||||
if (configuration.input_removed)
|
||||
configuration.input_removed(to_port_info<true>(name));
|
||||
}
|
||||
|
||||
void on_output_added(const observer_winuwp_internal::port_info& name)
|
||||
{
|
||||
if (configuration.output_added)
|
||||
configuration.output_added(to_port_info<false>(name));
|
||||
}
|
||||
|
||||
void on_output_removed(const observer_winuwp_internal::port_info& name)
|
||||
{
|
||||
if (configuration.output_removed)
|
||||
configuration.output_removed(to_port_info<false>(name));
|
||||
}
|
||||
|
||||
private:
|
||||
static inline observer_winuwp_internal internalInPortObserver_{MidiInPort::GetDeviceSelector()};
|
||||
static inline observer_winuwp_internal internalOutPortObserver_{
|
||||
MidiOutPort::GetDeviceSelector()};
|
||||
|
||||
int evTokenOnInputAdded_{-1};
|
||||
int evTokenOnInputRemoved_{-1};
|
||||
int evTokenOnOutputAdded_{-1};
|
||||
int evTokenOnOutputRemoved_{-1};
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user