blob: 19cf681bf4a3e3b97f646552d9daa09a0c45549b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#include "ClickStream.h"
#include "MIDI.h"
#include "PCM.h"
#include "Timer.h"
#include "UI.h"
#include "config.h"
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include <exception>
#include <stdexcept>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <boost/signals2.hpp>
using namespace std::chrono_literals;
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 {
//debug_cout.activate();
MIDI midi;
ClickStream stream;
PCM pcm{stream};
UI ui;
pcm.write();
Timer timer_300ms(300ms, true);
timer_300ms.start();
// Signal-Slot Connections:
midi.signal_click.connect([&](){stream.click();});
timer_300ms.elapsed.connect([&](){ui.draw();});
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 = 0;
timeout.tv_usec = 300000;
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();
}
// handle timers, TODO: make updates more efficient at scale
timer_300ms.update();
}
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
return 0;
}
|