diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-03 10:00:28 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-03 10:00:28 +0100 |
commit | 7b9e5cb03b530458b7f9df1fee678e2ba3a746da (patch) | |
tree | 97f6cd5bbbbe5132136675dd1ed9a438b80611b8 | |
parent | a1127bca74176a79caa03dd7b3bdd600954c161b (diff) |
Use signals
-rw-r--r-- | alsa.cpp | 46 |
1 files changed, 25 insertions, 21 deletions
@@ -14,15 +14,18 @@ #include <sys/time.h> #include <time.h> -using namespace std::string_literals; +#include <boost/signals2.hpp> -//static const int CLICK_NOTE = 37; -//static const int CLICK_CHANNEL = 4; +using namespace std::string_literals; +// Config +static const int CLICK_NOTE = 37; +static const int CLICK_CHANNEL = 4; static const char *device = "default"; // playback device const snd_pcm_sframes_t nframes = 1024; // ~1/44th sec buffer size int16_t buffer[nframes]; const unsigned int f_sample = 44100; + const double pi = std::acos(-1); double diff_timespec(const struct timespec *time1, const struct timespec *time0) { @@ -40,6 +43,7 @@ public: memcpy(m_data.data(), data_s.data(), data_s.size()); } + // generate 1 buffer size void generate() { int i; @@ -73,7 +77,7 @@ private: class PCM { public: - PCM() + PCM(ClickStream& stream): m_stream(stream) { // non-blocking if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) { @@ -91,6 +95,8 @@ public: exit(EXIT_FAILURE); } + m_stream.generate(); + npfd = snd_pcm_poll_descriptors_count(handle); if (npfd < 0) { throw std::runtime_error("snd_pcm_poll_descriptors_count() failed"); @@ -148,6 +154,8 @@ public: if (written != nframes) { std::cout << "Warning: written " << std::to_string(written) << " frames instead of "<< std::to_string(nframes) << std::endl; } + + m_stream.generate(); } bool wait_for_event() @@ -183,6 +191,8 @@ private: int npfd; struct pollfd* pfd; + + ClickStream& m_stream; }; class MIDI @@ -253,6 +263,8 @@ public: free(pfd); } + boost::signals2::signal<void()> signal_click; + int fd() { return pfd->fd; @@ -291,10 +303,8 @@ public: } // returns if click starts - bool process(const snd_seq_event_t *ev) + void process(const snd_seq_event_t *ev) { - bool result = false; - if((ev->type == SND_SEQ_EVENT_NOTEON) ||(ev->type == SND_SEQ_EVENT_NOTEOFF)) { const char *type = (ev->type == SND_SEQ_EVENT_NOTEON) ? "on " : "off"; @@ -304,9 +314,9 @@ public: ev->data.note.velocity, ev->data.control.channel); if (ev->type == SND_SEQ_EVENT_NOTEON) { -// if (ev->data.note.note == CLICK_NOTE && ev->data.control.channel == CLICK_CHANNEL) { - result = true; -// } + if (true || (ev->data.note.note == CLICK_NOTE && ev->data.control.channel == CLICK_CHANNEL)) { + signal_click(); + } } } else if(ev->type == SND_SEQ_EVENT_CONTROLLER) @@ -323,7 +333,6 @@ public: { printf("[%d] Unknown: Unhandled Event Received\n", ev->time.tick); } - return result; } void wait_for_event() @@ -352,15 +361,14 @@ int main(void) try { MIDI midi; ClickStream stream; - PCM pcm; + PCM pcm{stream}; - stream.generate(); pcm.write(); - stream.generate(); + + midi.signal_click.connect([&](){stream.click();}); while (true) { - //midi.wait_for_event(); - //pcm.wait_for_event(); + //std::cout << "Main loop entered." << std::endl; fd_set read_set; FD_ZERO(&read_set); FD_SET(midi.fd(), &read_set); @@ -385,16 +393,12 @@ int main(void) //std::cout << "read..." << std::endl; auto event = midi.read(); //std::cout << "process..." << std::endl; - if (midi.process(event)) { - stream.click(); - //std::cout << "DEBUG: CLICK" << std::endl; - } + midi.process(event); } if (pcm.write_available()) { //std::cout << "DEBUG: WRITE" << std::endl; pcm.write(); - stream.generate(); } } } catch (const std::exception& ex) { |