Skip to content

Commit

Permalink
ensure the audio processing thread yields every 100ms so we can still…
Browse files Browse the repository at this point in the history
… execute code if the process fn is slow
  • Loading branch information
nwoeanhinnogaehr committed Aug 28, 2020
1 parent 494eb27 commit 06d1e53
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tinyspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,25 @@ void frft(FFTBuf &in, FFTBuf &out, double exponent) {
}

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;
}
lock_guard<mutex> exec_lk(exec_mtx);
while (true) {
struct timespec current_time;
clock_gettime(CLOCK_MONOTONIC, &current_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) {
Expand Down Expand Up @@ -302,15 +313,22 @@ int audio_cb(jack_nframes_t len, void *) {
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;
}

Expand Down

0 comments on commit 06d1e53

Please sign in to comment.