-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinyspec.cpp
296 lines (281 loc) · 10.7 KB
/
tinyspec.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include<cmath>
#include<unistd.h>
#include<cstring>
#include<complex>
#include<cassert>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<mutex>
#include<condition_variable>
#include<thread>
#include<sys/stat.h>
#include<sys/time.h>
#include<csignal>
#include<fcntl.h>
#include<jack/jack.h>
#ifdef USE_CLING
#include"cling/Interpreter/Interpreter.h"
#include"cling/Interpreter/InterpreterCallbacks.h"
#include"cling/Utils/Casting.h"
#endif
#include "rtinc/root.hpp"
#include "rtlib/root.hpp"
using namespace std;
#define STR_(x) #x
#define STR(x) STR_(x)
const char *LLVMRESDIR = "cling_bin"; // path to cling resource directory
const char *CODE_BEGIN = "<<<";
const char *CODE_END = ">>>";
// This is in a namespace so we can cleanly access it from other compilation units with extern
namespace internals {
uint64_t system_frame_size = 0;
uint64_t window_size;
uint64_t new_window_size(1<<12); // initial size
uint64_t hop_samples(new_window_size/2);
uint64_t hop_fract = 0;
uint64_t computed_hop = hop_samples;
double hop = double(hop_samples);
deque<float> aqueue, atmp, ainqueue;
WaveBuf audio_in, audio_out;
uint64_t time_samples = 0;
uint64_t time_fract = 0;
size_t nch_in = 0, nch_out = 0, new_nch_in = 0, new_nch_out = 0;
mutex frame_notify_mtx, // for waking up processing thread
exec_mtx, // guards user code exec
data_mtx; // guards aqueue, ainqueue
condition_variable frame_notify;
bool frame_ready = false;
using func_t = function<void(WaveBuf&, WaveBuf&, double)>;
func_t fptr(nullptr);
string command_file;
string client_name;
double rate = 44100; // reset by jack
jack_client_t *client;
vector<jack_port_t *> in_ports;
vector<jack_port_t *> out_ports;
}
using namespace internals;
#ifdef USE_CLING
struct Callbacks : public cling::InterpreterCallbacks {
Callbacks(cling::Interpreter *interp) : cling::InterpreterCallbacks(interp) { }
void *EnteringUserCode() { exec_mtx.lock(); return nullptr; }
void ReturnedFromUserCode(void *) { exec_mtx.unlock(); }
};
void init_cling(int argc, char **argv) { cling::Interpreter interp(argc, argv, LLVMRESDIR, {},
true); // disable runtime for simplicity
interp.setCallbacks(make_unique<Callbacks>(&interp));
interp.setDefaultOptLevel(2);
interp.allowRedefinition();
interp.process("#include \"rtinc/root.hpp\"\n");
// make a fifo called "cmd", which commands are read from
mkfifo(command_file.c_str(), 0700);
int fd = open(command_file.c_str(), O_RDONLY);
const size_t CODE_BUF_SIZE = 1<<12;
char codebuf[CODE_BUF_SIZE];
string code;
while (true) { // read code from fifo and execute it
int n = read(fd, codebuf, CODE_BUF_SIZE);
if (n == 0) usleep(100000); // hack to avoid spinning too hard
code += string(codebuf, n);
int begin = code.find(CODE_BEGIN);
int end = code.find(CODE_END);
while (end != -1 && begin != -1) { // while there are blocks to execute
string to_exec = code.substr(begin+strlen(CODE_BEGIN), end-begin-strlen(CODE_BEGIN));
code = code.substr(end+strlen(CODE_END)-1);
size_t nl_pos = to_exec.find("\n");
int preview_len = nl_pos == string::npos ? to_exec.size() : nl_pos;
string preview = to_exec.substr(0, preview_len);
cerr << "execute: " << preview << "..." << endl;
interp.process(to_exec);
// move to next block
begin = code.find(CODE_BEGIN);
end = code.find(CODE_END);
}
}
}
#endif
double current_time_secs() {
return (double(time_samples) + time_fract/double(uint64_t(-1)))/rate;
}
void generate_frames() {
struct timespec yield_time;
clock_gettime(CLOCK_MONOTONIC, &yield_time);
while (true) {
{ // wait until there's more to do
unique_lock<mutex> lk(frame_notify_mtx);
frame_notify.wait(lk, []{ return frame_ready; });
frame_ready = false;
}
while (true) {
struct timespec current_time;
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
if (current_time.tv_sec > yield_time.tv_sec ||
(current_time.tv_sec >= yield_time.tv_sec && current_time.tv_nsec >= yield_time.tv_nsec)) {
usleep(0);
yield_time.tv_nsec = current_time.tv_nsec + 100000000;
yield_time.tv_sec = current_time.tv_sec + (yield_time.tv_nsec > 999999999);
yield_time.tv_nsec %= 1000000000;
}
lock_guard<mutex> exec_lk(exec_mtx);
{
lock_guard<mutex> data_lk(data_mtx);
if (new_nch_in != nch_in || new_nch_out != nch_out || new_window_size != window_size) {
window_size = new_window_size;
nch_in = new_nch_in;
nch_out = new_nch_out;
}
audio_in.resize(nch_in, window_size);
audio_out.resize(nch_out, window_size);
if (ainqueue.size() < nch_in*(computed_hop+window_size))
break;
if (aqueue.size() >= max(system_frame_size, window_size) * nch_out)
break;
// advance input by hop
size_t sz_tmp = ainqueue.size();
for (size_t i = 0; i < min(nch_in*computed_hop, sz_tmp); i++)
ainqueue.pop_front();
// get input
for (uint64_t i = 0; i < window_size; i++) {
for (size_t c = 0; c < nch_in; c++) {
if (i*nch_in+c < ainqueue.size())
audio_in[c][i] = ainqueue[i*nch_in+c];
else audio_in[c][i] = 0;
}
}
}
if (time_samples == 0 && time_fract == 0 && base_time == 0)
gettimeofday(&init_time, NULL); // TODO use clock_gettime
if (fptr) { // call synthesis function
fptr(audio_in, audio_out, current_time_secs());
if (time_fract + hop_fract < time_fract)
computed_hop = hop_samples + 1;
else
computed_hop = hop_samples;
time_samples += computed_hop;
time_fract += hop_fract;
}
{
lock_guard<mutex> data_lk(data_mtx);
// output region that overlaps with previous frame(s)
for (uint64_t i = 0; i < min(computed_hop, audio_out.size); i++) {
for (size_t c = 0; c < nch_out; c++) {
float overlap = 0;
if (!atmp.empty()) {
overlap = atmp.front();
atmp.pop_front();
}
aqueue.push_back(overlap + audio_out[c][i]);
}
}
// if the hop is larger than the frame size
for (int i = 0; i < int(nch_out)*(int(computed_hop)-int(audio_out.size)); i++) {
if (!atmp.empty()) {
aqueue.push_back(atmp.front()); // output overlap if any
atmp.pop_front();
} else {
aqueue.push_back(0); // otherwise output silence
}
}
}
// save region that overlaps with next frame
for (int i = 0; i < int(audio_out.size)-int(computed_hop); i++) {
for (size_t c = 0; c < nch_out; c++) {
float out_val = audio_out[c][computed_hop+i];
if (i*nch_out+c < atmp.size()) atmp[i*nch_out+c] += out_val;
else atmp.push_back(out_val);
}
}
}
}
}
int audio_cb(jack_nframes_t len, void *) {
lock_guard<mutex> data_lk(data_mtx);
system_frame_size = len;
float *in_bufs[in_ports.size()];
float *out_bufs[out_ports.size()];
for (size_t i = 0; i < in_ports.size(); i++)
in_bufs[i] = (float*) jack_port_get_buffer(in_ports[i], len);
for (size_t i = 0; i < out_ports.size(); i++)
out_bufs[i] = (float*) jack_port_get_buffer(out_ports[i], len);
for (size_t i = 0; i < len; i++)
for (size_t c = 0; c < in_ports.size(); c++)
ainqueue.push_back(in_bufs[c][i]);
{
unique_lock<mutex> lk(frame_notify_mtx);
frame_ready = true;
}
frame_notify.notify_one();
size_t underrun = 0;
for (size_t i = 0; i < len; i++)
for (size_t c = 0; c < out_ports.size(); c++) {
if (!aqueue.empty()) {
out_bufs[c][i] = aqueue.front();
aqueue.pop_front();
} else {
out_bufs[c][i] = 0;
underrun++;
}
}
static bool warmed_up = false;
if (underrun && warmed_up)
cerr << "WARNING: audio output buffer underrun (" << underrun << " samples)" << endl;
else if (!underrun)
warmed_up = true;
return 0;
}
int sample_rate_changed(jack_nframes_t sr, void*) {
cerr << "INFO: set sample rate to " << sr << endl;
rate = sr;
return 0;
}
void init_audio() {
jack_status_t status;
client = jack_client_open(client_name.c_str(), JackNullOption, &status);
if (!client) {
cerr << "FATAL: Failed to open jack client: " << status << endl;
exit(1);
}
client_name = string(jack_get_client_name(client));
set_num_channels(2,2);
jack_set_process_callback(client, audio_cb, nullptr);
jack_set_sample_rate_callback(client, sample_rate_changed, nullptr);
jack_activate(client);
cout << "Playing..." << endl;
}
sighandler_t prev_handlers[32];
void at_exit(int i) {
cout << "Cleaning up... " << endl;
jack_deactivate(client);
unlink(command_file.c_str());
signal(i, prev_handlers[i]);
raise(i);
}
int main(int argc, char **argv) {
command_file = argc > 1 ? argv[1] : "cmd";
if (access(command_file.c_str(), F_OK) != -1) {
cerr << "FATAL: command pipe \"" << command_file << "\" already exists. Remove it before continuing." << endl;
return 1;
}
#ifdef HACK
client_name = string("tinyspec_") + STR(HACK);
#else
client_name = string("tinyspec_") + command_file;
#endif
prev_handlers[SIGINT] = signal(SIGINT, at_exit);
prev_handlers[SIGABRT] = signal(SIGABRT, at_exit);
prev_handlers[SIGTERM] = signal(SIGTERM, at_exit);
init_audio();
thread worker(generate_frames);
#ifdef USE_CLING
init_cling(argc, argv);
#else
#ifdef HACK
#include STR(HACK)
this_thread::sleep_until(chrono::time_point<chrono::system_clock>::max());
#else
#error Not using cling but no hack specified. Compile with `./compile --no-cling hack.cpp`
#endif
#endif
}