summaryrefslogtreecommitdiffhomepage
path: root/alsa.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'alsa.cpp')
-rw-r--r--alsa.cpp46
1 files changed, 25 insertions, 21 deletions
diff --git a/alsa.cpp b/alsa.cpp
index 2eb5662..e669593 100644
--- a/alsa.cpp
+++ b/alsa.cpp
@@ -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) {