From 4af400141af0c97c4e4bcd47acf78107a17eafbe Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 3 Jan 2025 10:31:38 +0100 Subject: Separated class files --- main.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 main.cpp (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..629b92c --- /dev/null +++ b/main.cpp @@ -0,0 +1,78 @@ +#include "ClickStream.h" +#include "MIDI.h" +#include "PCM.h" +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +using namespace std::string_literals; + +double diff_timespec(const struct timespec *time1, const struct timespec *time0) { + return (time1->tv_sec - time0->tv_sec) + + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0; +} + +int main(void) +{ + try { + MIDI midi; + ClickStream stream; + PCM pcm{stream}; + + pcm.write(); + + midi.signal_click.connect([&](){stream.click();}); + + while (true) { + //std::cout << "Main loop entered." << std::endl; + 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()) + { + //std::cout << "read..." << std::endl; + auto event = midi.read(); + //std::cout << "process..." << std::endl; + midi.process(event); + } + + if (pcm.write_available()) { + //std::cout << "DEBUG: WRITE" << std::endl; + pcm.write(); + } + } + } catch (const std::exception& ex) { + std::cerr << "Error: " << ex.what() << std::endl; + } + + return 0; +} + -- cgit v1.2.3