working on win32 uwp! and libremidi update
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <libremidi/detail/midi_in.hpp>
|
||||
|
||||
#include <libremidi/cmidi2.hpp>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cinttypes>
|
||||
@@ -28,16 +29,87 @@ struct timestamp_backend_info
|
||||
bool has_samples{};
|
||||
};
|
||||
|
||||
namespace midi1
|
||||
template<typename Configuration>
|
||||
struct input_state_machine_base
|
||||
{
|
||||
struct input_state_machine
|
||||
{
|
||||
const input_configuration& configuration;
|
||||
explicit input_state_machine(const input_configuration& conf)
|
||||
const Configuration& configuration;
|
||||
|
||||
explicit input_state_machine_base(const Configuration& conf)
|
||||
: configuration{conf}
|
||||
{
|
||||
}
|
||||
|
||||
template <timestamp_backend_info info>
|
||||
int64_t timestamp(auto to_ns, int64_t samples)
|
||||
{
|
||||
switch (configuration.timestamps)
|
||||
{
|
||||
default:
|
||||
case timestamp_mode::NoTimestamp:
|
||||
return 0;
|
||||
|
||||
case timestamp_mode::Relative: {
|
||||
int64_t time_ns;
|
||||
|
||||
if constexpr (info.has_absolute_timestamps)
|
||||
time_ns = to_ns();
|
||||
else
|
||||
time_ns = system_ns();
|
||||
|
||||
int64_t res;
|
||||
if (first_message)
|
||||
{
|
||||
first_message = false;
|
||||
res = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = time_ns - last_time_ns;
|
||||
}
|
||||
|
||||
last_time_ns = time_ns;
|
||||
return res;
|
||||
}
|
||||
|
||||
case timestamp_mode::Absolute:
|
||||
if constexpr (info.has_absolute_timestamps)
|
||||
return to_ns();
|
||||
else
|
||||
return system_ns();
|
||||
|
||||
case timestamp_mode::SystemMonotonic:
|
||||
if constexpr (info.absolute_is_monotonic)
|
||||
return to_ns();
|
||||
else
|
||||
return system_ns();
|
||||
|
||||
case timestamp_mode::AudioFrame:
|
||||
if constexpr (info.has_samples)
|
||||
return samples;
|
||||
else
|
||||
return 0;
|
||||
|
||||
case timestamp_mode::Custom:
|
||||
return configuration.get_timestamp(to_ns());
|
||||
}
|
||||
}
|
||||
int64_t last_time_ns = 0;
|
||||
bool first_message = true;
|
||||
};
|
||||
|
||||
namespace midi1
|
||||
{
|
||||
struct input_state_machine : input_state_machine_base<input_configuration>
|
||||
{
|
||||
using input_state_machine_base::input_state_machine_base;
|
||||
|
||||
void reset()
|
||||
{
|
||||
message.bytes.clear();
|
||||
message.timestamp = {};
|
||||
state = main;
|
||||
}
|
||||
|
||||
bool has_finished_sysex(std::span<const uint8_t> bytes) const noexcept
|
||||
{
|
||||
return (((bytes.front() == 0xF0) || (state == in_sysex)) && (bytes.back() == 0xF7));
|
||||
@@ -46,6 +118,26 @@ struct input_state_machine
|
||||
// Function to process a byte stream which may contain multiple successive
|
||||
// MIDI events (CoreMIDI, ALSA Sequencer can work like this)
|
||||
void on_bytes_multi(std::span<const uint8_t> bytes, int64_t timestamp)
|
||||
{
|
||||
if (this->configuration.on_message)
|
||||
on_bytes_multi_segmented(this->configuration.on_message, bytes, timestamp);
|
||||
if (this->configuration.on_raw_data)
|
||||
this->configuration.on_raw_data(bytes, timestamp);
|
||||
}
|
||||
|
||||
// Function to process bytes corresponding to at most one midi event
|
||||
// e.g. a midi channel event or a single sysex
|
||||
void on_bytes(std::span<const uint8_t> bytes, int64_t timestamp)
|
||||
{
|
||||
if (this->configuration.on_message)
|
||||
on_bytes_segmented(this->configuration.on_message, bytes, timestamp);
|
||||
if (this->configuration.on_raw_data)
|
||||
this->configuration.on_raw_data(bytes, timestamp);
|
||||
}
|
||||
|
||||
private:
|
||||
void on_bytes_multi_segmented(
|
||||
const message_callback& cb, std::span<const uint8_t> bytes, int64_t timestamp)
|
||||
{
|
||||
int64_t nBytes = bytes.size();
|
||||
int64_t iByte = 0;
|
||||
@@ -54,7 +146,7 @@ struct input_state_machine
|
||||
switch (state)
|
||||
{
|
||||
case in_sysex: {
|
||||
return on_continue_sysex(bytes, finished_sysex);
|
||||
return on_continue_sysex(cb, bytes, finished_sysex);
|
||||
}
|
||||
case main: {
|
||||
while (iByte < nBytes)
|
||||
@@ -150,7 +242,7 @@ struct input_state_machine
|
||||
message.assign(begin, begin + size);
|
||||
message.timestamp = timestamp;
|
||||
|
||||
this->configuration.on_message(std::move(message));
|
||||
cb(std::move(message));
|
||||
message.clear();
|
||||
|
||||
iByte += size;
|
||||
@@ -160,7 +252,8 @@ struct input_state_machine
|
||||
}
|
||||
}
|
||||
|
||||
void on_continue_sysex(std::span<const uint8_t> bytes, bool finished_sysex)
|
||||
void on_continue_sysex(
|
||||
const message_callback& cb, std::span<const uint8_t> bytes, bool finished_sysex)
|
||||
{
|
||||
if (finished_sysex)
|
||||
state = main;
|
||||
@@ -174,14 +267,16 @@ struct input_state_machine
|
||||
message.insert(message.end(), bytes.begin(), bytes.end());
|
||||
if (finished_sysex)
|
||||
{
|
||||
this->configuration.on_message(std::move(message));
|
||||
cb(std::move(message));
|
||||
message.clear();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void on_main(std::span<const uint8_t> bytes, int64_t timestamp, bool finished_sysex)
|
||||
void on_main(
|
||||
const message_callback& cb, std::span<const uint8_t> bytes, int64_t timestamp,
|
||||
bool finished_sysex)
|
||||
{
|
||||
switch (bytes[0])
|
||||
{
|
||||
@@ -196,7 +291,7 @@ struct input_state_machine
|
||||
message.timestamp = timestamp;
|
||||
if (finished_sysex)
|
||||
{
|
||||
this->configuration.on_message(std::move(message));
|
||||
cb(std::move(message));
|
||||
message.clear();
|
||||
}
|
||||
}
|
||||
@@ -222,13 +317,12 @@ struct input_state_machine
|
||||
message.assign(bytes.begin(), bytes.end());
|
||||
message.timestamp = timestamp;
|
||||
|
||||
this->configuration.on_message(std::move(message));
|
||||
cb(std::move(message));
|
||||
message.clear();
|
||||
}
|
||||
|
||||
// Function to process bytes corresponding to at most one midi event
|
||||
// e.g. a midi channel event or a single sysex
|
||||
void on_bytes(std::span<const uint8_t> bytes, int64_t timestamp)
|
||||
void
|
||||
on_bytes_segmented(const message_callback& cb, std::span<const uint8_t> bytes, int64_t timestamp)
|
||||
{
|
||||
if (bytes.empty())
|
||||
return;
|
||||
@@ -237,78 +331,135 @@ struct input_state_machine
|
||||
switch (state)
|
||||
{
|
||||
case in_sysex:
|
||||
return on_continue_sysex(bytes, finished_sysex);
|
||||
return on_continue_sysex(cb, bytes, finished_sysex);
|
||||
|
||||
case main:
|
||||
return on_main(bytes, timestamp, finished_sysex);
|
||||
}
|
||||
}
|
||||
|
||||
template <timestamp_backend_info info>
|
||||
int64_t timestamp(auto to_ns, int64_t samples)
|
||||
{
|
||||
switch (configuration.timestamps)
|
||||
{
|
||||
default:
|
||||
case timestamp_mode::NoTimestamp:
|
||||
return 0;
|
||||
|
||||
case timestamp_mode::Relative: {
|
||||
int64_t time_ns;
|
||||
|
||||
if constexpr (info.has_absolute_timestamps)
|
||||
time_ns = to_ns();
|
||||
else
|
||||
time_ns = system_ns();
|
||||
|
||||
int64_t res;
|
||||
if (first_message)
|
||||
{
|
||||
first_message = false;
|
||||
res = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = time_ns - last_time_ns;
|
||||
}
|
||||
|
||||
last_time_ns = time_ns;
|
||||
return res;
|
||||
}
|
||||
|
||||
case timestamp_mode::Absolute:
|
||||
if constexpr (info.has_absolute_timestamps)
|
||||
return to_ns();
|
||||
else
|
||||
return system_ns();
|
||||
|
||||
case timestamp_mode::SystemMonotonic:
|
||||
if constexpr (info.absolute_is_monotonic)
|
||||
return to_ns();
|
||||
else
|
||||
return system_ns();
|
||||
|
||||
case timestamp_mode::AudioFrame:
|
||||
if constexpr (info.has_samples)
|
||||
return samples;
|
||||
else
|
||||
return 0;
|
||||
|
||||
case timestamp_mode::Custom:
|
||||
return configuration.get_timestamp(to_ns());
|
||||
return on_main(cb, bytes, timestamp, finished_sysex);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
libremidi::message message;
|
||||
|
||||
int64_t last_time_ns = 0;
|
||||
private:
|
||||
enum
|
||||
{
|
||||
main,
|
||||
in_sysex
|
||||
} state{main};
|
||||
};
|
||||
}
|
||||
|
||||
bool first_message = true;
|
||||
namespace midi2
|
||||
{
|
||||
struct input_state_machine : input_state_machine_base<ump_input_configuration>
|
||||
{
|
||||
using input_state_machine_base::input_state_machine_base;
|
||||
|
||||
public:
|
||||
void on_bytes_multi(std::span<const unsigned char> bytes, int64_t timestamp)
|
||||
{
|
||||
auto ptr = reinterpret_cast<const uint32_t*>(bytes.data());
|
||||
auto sz = bytes.size() / 4;
|
||||
return on_bytes_multi({ptr, sz}, timestamp);
|
||||
}
|
||||
|
||||
void on_bytes_multi(std::span<const uint32_t> bytes, int64_t timestamp)
|
||||
{
|
||||
if (this->configuration.on_message)
|
||||
on_bytes_multi_segmented(this->configuration.on_message, bytes, timestamp);
|
||||
if (this->configuration.on_raw_data)
|
||||
this->configuration.on_raw_data(bytes, timestamp);
|
||||
}
|
||||
|
||||
void on_bytes(std::span<const uint32_t> bytes, int64_t timestamp)
|
||||
{
|
||||
if (this->configuration.on_message)
|
||||
on_bytes_segmented(this->configuration.on_message, bytes, timestamp);
|
||||
if (this->configuration.on_raw_data)
|
||||
this->configuration.on_raw_data(bytes, timestamp);
|
||||
}
|
||||
|
||||
private:
|
||||
// Function to process a byte stream which may contain multiple successive
|
||||
// MIDI events (CoreMIDI, ALSA Sequencer can work like this)
|
||||
void on_bytes_multi_segmented(
|
||||
const ump_callback& cb, std::span<const uint32_t> bytes, int64_t timestamp)
|
||||
{
|
||||
auto count = bytes.size();
|
||||
auto ump_stream = bytes.data();
|
||||
while (count > 0)
|
||||
{
|
||||
// Handle NOOP (or padding)
|
||||
while (count > 0 && ump_stream[0] == 0)
|
||||
{
|
||||
count--;
|
||||
ump_stream++;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
break;
|
||||
|
||||
const auto ump_uints = cmidi2_ump_get_num_bytes(ump_stream[0]) / 4;
|
||||
on_bytes_segmented(cb, {ump_stream, ump_stream + ump_uints}, timestamp);
|
||||
|
||||
ump_stream += ump_uints;
|
||||
count -= ump_uints;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to process bytes corresponding to at most one midi event
|
||||
void
|
||||
on_bytes_segmented(const ump_callback& cb, std::span<const uint32_t> bytes, int64_t timestamp)
|
||||
{
|
||||
// Filter according to message type
|
||||
switch(cmidi2_ump_get_message_type(bytes.data()))
|
||||
{
|
||||
case CMIDI2_MESSAGE_TYPE_UTILITY:
|
||||
{
|
||||
// All the utility messages are about timing
|
||||
if (this->configuration.ignore_timing)
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
case CMIDI2_MESSAGE_TYPE_SYSTEM:
|
||||
{
|
||||
if (this->configuration.ignore_timing)
|
||||
{
|
||||
auto status = cmidi2_ump_get_system_message_byte2(bytes.data());
|
||||
switch(status)
|
||||
{
|
||||
case CMIDI2_SYSTEM_STATUS_MIDI_TIME_CODE:
|
||||
case CMIDI2_SYSTEM_STATUS_SONG_POSITION:
|
||||
case CMIDI2_SYSTEM_STATUS_TIMING_CLOCK:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (this->configuration.ignore_sensing)
|
||||
{
|
||||
auto status = cmidi2_ump_get_system_message_byte2(bytes.data());
|
||||
if(status == CMIDI2_SYSTEM_STATUS_ACTIVE_SENSING)
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CMIDI2_MESSAGE_TYPE_SYSEX7:
|
||||
case CMIDI2_MESSAGE_TYPE_SYSEX8_MDS:
|
||||
{
|
||||
if (this->configuration.ignore_sysex)
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
libremidi::ump msg;
|
||||
std::copy(bytes.begin(), bytes.end(), msg.data);
|
||||
msg.timestamp = timestamp;
|
||||
cb(std::move(msg));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user