summaryrefslogtreecommitdiffhomepage
path: root/alsa.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-02 22:25:51 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-02 22:25:51 +0100
commit125870779ffbd633b00d051ce96da912f2dd6860 (patch)
treec785f48bee0fc0c8874bf554fd53380b35d91866 /alsa.cpp
parentddc31844940b9afedb352643349649fac38269ef (diff)
Use select() for input and output
Diffstat (limited to 'alsa.cpp')
-rw-r--r--alsa.cpp43
1 files changed, 39 insertions, 4 deletions
diff --git a/alsa.cpp b/alsa.cpp
index 9aca9be..d9d7cf9 100644
--- a/alsa.cpp
+++ b/alsa.cpp
@@ -8,7 +8,10 @@
#include <limits>
#include <exception>
#include <stdexcept>
+
#include <stdio.h>
+#include <sys/types.h>
+#include <sys/time.h>
using namespace std::string_literals;
@@ -100,7 +103,9 @@ public:
throw std::runtime_error("PCM could not be started");
}
- std::cout << "Info: " << std::to_string(npfd) << " poll fds for pcm" << std::endl;
+ if (npfd != 1) {
+ std::cout << "Warning: " << std::to_string(npfd) << " poll fds for pcm" << std::endl;
+ }
}
~PCM()
@@ -114,6 +119,11 @@ public:
free(pfd);
}
+ int fd()
+ {
+ return pfd->fd;
+ }
+
// write from buffer to ALSA PCM
void write()
{
@@ -204,7 +214,7 @@ public:
snd_seq_port_subscribe_set_sender(subs, &sender);
snd_seq_port_subscribe_set_dest(subs, &dest);
snd_seq_port_subscribe_set_queue(subs, 1);
- snd_seq_port_subscribe_set_time_update(subs, 1);
+ //snd_seq_port_subscribe_set_time_update(subs, 1);
snd_seq_port_subscribe_set_time_real(subs, 1);
// TODO: fix timestamp (currently always 0)
if (0 > snd_seq_subscribe_port(seq_handle, subs))
@@ -227,7 +237,9 @@ public:
throw std::runtime_error("snd_seq_poll_descriptors() failure");
}
- std::cout << "Info: " << std::to_string(npfd) << " poll fds for MIDI" << std::endl;
+ if (npfd != 1) {
+ std::cout << "Warning: " << std::to_string(npfd) << " poll fds for MIDI" << std::endl;
+ }
}
~MIDI()
@@ -235,6 +247,11 @@ public:
free(pfd);
}
+ int fd()
+ {
+ return pfd->fd;
+ }
+
bool event_ready()
{
int result = snd_seq_event_input_pending(seq_handle, 1);
@@ -324,8 +341,26 @@ int main(void)
stream.generate();
while (true) {
- midi.wait_for_event();
+ //midi.wait_for_event();
//pcm.wait_for_event();
+ fd_set read_set;
+ FD_ZERO(&read_set);
+ FD_SET(midi.fd(), &read_set);
+
+ fd_set write_set;
+ FD_ZERO(&write_set);
+ FD_SET(pcm.fd(), &write_set);
+
+ struct timeval timeout;
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+
+ int result = select(FD_SETSIZE, &read_set, &write_set, NULL, &timeout);
+ if (result < 0) {
+ throw std::runtime_error("select() failed");
+ } else if (result == 0) {
+ throw std::runtime_error("select() timeout");
+ }
if (midi.event_ready())
{