Skip to content

Commit

Permalink
[alsa] Add a configurable poll interval, fix #111
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 2, 2024
1 parent 3cba285 commit 280cd4f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/libremidi/backends/alsa_raw/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct manual_poll_parameters
struct alsa_raw_input_configuration
{
std::function<bool(const manual_poll_parameters&)> manual_poll;
std::chrono::milliseconds poll_period{2};
};

struct alsa_raw_output_configuration
Expand Down
5 changes: 4 additions & 1 deletion include/libremidi/backends/alsa_raw/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,14 @@ class midi_in_alsa_raw_threaded : public midi_in_impl
void run_thread(auto parse_func)
{
fds_.push_back(this->termination_event);
const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();

for (;;)
{
// Poll
ssize_t err = poll(fds_.data(), fds_.size(), -1);
ssize_t err = poll(fds_.data(), fds_.size(), period);
if (err == -EAGAIN)
continue;
else if (err < 0)
Expand Down
1 change: 1 addition & 0 deletions include/libremidi/backends/alsa_raw_ump/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace libremidi::alsa_raw_ump
struct input_configuration
{
std::function<bool(const manual_poll_parameters&)> manual_poll;
std::chrono::milliseconds poll_period{2};
};

struct output_configuration
Expand Down
3 changes: 3 additions & 0 deletions include/libremidi/backends/alsa_raw_ump/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ class midi_in_impl_threaded : public midi_in_impl
void run_thread(auto parse_func)
{
fds_.push_back(this->termination_event);
const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();

for (;;)
{
Expand Down
2 changes: 2 additions & 0 deletions include/libremidi/backends/alsa_seq/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct input_configuration
snd_seq_t* context{};
std::function<bool(const poll_parameters&)> manual_poll;
std::function<bool(snd_seq_addr_t)> stop_poll;
std::chrono::milliseconds poll_period{2};

static constexpr int midi_version = 1;
};
Expand All @@ -56,6 +57,7 @@ struct observer_configuration
snd_seq_t* context{};
std::function<bool(const poll_parameters&)> manual_poll;
std::function<bool(snd_seq_addr_t)> stop_poll;
std::chrono::milliseconds poll_period{100};

static constexpr int midi_version = 1;
};
Expand Down
5 changes: 4 additions & 1 deletion include/libremidi/backends/alsa_seq/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,15 @@ class midi_in_alsa_threaded : public midi_in_impl<ConfigurationBase, Configurati
poll_fds[0] = this->termination_event;
alsa_data::snd.seq.poll_descriptors(this->seq, poll_fds + 1, poll_fd_count - 1, POLLIN);

const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();
for (;;)
{
if (alsa_data::snd.seq.event_input_pending(this->seq, 1) == 0)
{
// No data pending
if (poll(poll_fds, poll_fd_count, -1) >= 0)
if (poll(poll_fds, poll_fd_count, period) >= 0)
{
// We got our stop-thread signal
if (termination_event.ready(poll_fds[0]))
Expand Down
5 changes: 4 additions & 1 deletion include/libremidi/backends/alsa_seq/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,12 @@ class observer_threaded : public observer_impl<ConfigurationImpl>
// Start the listening thread
thread = std::thread{[this] {
auto& snd = alsa_data::snd;
const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();
for (;;)
{
int err = poll(descriptors_.data(), descriptors_.size(), -1);
int err = poll(descriptors_.data(), descriptors_.size(), period);
if (err >= 0)
{
// We got our stop-thread signal
Expand Down
2 changes: 2 additions & 0 deletions include/libremidi/backends/alsa_seq_ump/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct input_configuration
snd_seq_t* context{};
std::function<bool(const poll_parameters&)> manual_poll;
std::function<bool(snd_seq_addr_t)> stop_poll;
std::chrono::milliseconds poll_period{2};

static constexpr int midi_version = 2;
};
Expand All @@ -46,6 +47,7 @@ struct observer_configuration
snd_seq_t* context{};
std::function<bool(const libremidi::alsa_seq::poll_parameters&)> manual_poll;
std::function<bool(snd_seq_addr_t)> stop_poll;
std::chrono::milliseconds poll_period{100};

static constexpr int midi_version = 2;
};
Expand Down

0 comments on commit 280cd4f

Please sign in to comment.