From cdd8f26a12b119219b256f5c8ae4d7f0ab88c985 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 14 Feb 2024 17:55:08 +0100 Subject: Improve playback code --- source/saw/main.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'source') diff --git a/source/saw/main.c b/source/saw/main.c index 7bcc6d7..12b271e 100644 --- a/source/saw/main.c +++ b/source/saw/main.c @@ -60,7 +60,7 @@ enum { #ifdef __EMSCRIPTEN__ BUFFER_SIZE = 1024 * 16, #else - BUFFER_SIZE = 1024 * 2, + BUFFER_SIZE = 1024 * 8, #endif #ifdef __EMSCRIPTEN__ @@ -348,8 +348,14 @@ static void saw_audio_render(void) { // FIXME // Improve performance. - i64 frame_count = (BUFFER_SIZE / CHANNEL_COUNT) - - saw_playback_lookahead; + i64 frame_count = (BUFFER_SIZE / CHANNEL_COUNT); + + if (mtx_lock(&saw_playback_mutex) != thrd_success) { + assert(0); + return; + } + frame_count -= saw_playback_lookahead; + mtx_unlock(&saw_playback_mutex); if (frame_count > 0) { memset(saw_playback_temp, 0, @@ -362,26 +368,18 @@ static void saw_audio_render(void) { for (i32 k = 0; k < ROLL_COUNT; k++) { saw_roll_t *roll = saw_rolls + k; - if (!roll->enabled || - saw_playback_frame + 1 <= roll->time || - saw_playback_frame + 1 > roll->time + roll->duration) + + if (!roll->enabled || saw_playback_frame < roll->time || + saw_playback_frame > roll->time + roll->duration) continue; - i64 play_frame = roll->loop_duration == 0 - ? saw_playback_frame - : saw_playback_frame - - ((saw_playback_frame + 1 - - roll->time) / - roll->loop_duration) * - roll->loop_duration; + i64 roll_frame = saw_playback_frame - roll->time; + if (roll->loop_duration != 0) + roll_frame = roll_frame % roll->loop_duration; for (i32 i = 0; i < SHEET_SIZE; i++) { saw_roll_note_t *note = roll->notes + i; - if (!note->enabled) - continue; - i64 note_frame = roll->time + note->time; - if (play_frame + 1 <= note_frame || - play_frame > note_frame) + if (!note->enabled || note->time != roll_frame) continue; saw_play_voice(saw_tracks + roll->track, roll, @@ -467,14 +465,13 @@ static void saw_audio_render(void) { } #ifndef __EMSCRIPTEN__ - if (frame_count == 0 && !saw_playback_on) { + if (frame_count == 0 && !saw_playback_on) // Sleep for 1/5 of the buffer duration thrd_sleep( &(struct timespec) { .tv_nsec = (200000000ll * BUFFER_SIZE / CHANNEL_COUNT) / SAMPLE_RATE }, NULL); - } #endif } @@ -1299,14 +1296,14 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, // Loop control // { - i64 x = x0 + pianokey_width + sheet_offset; - i64 y = frame_height - y0 - height + text_height; - i64 w = width - pianokey_width - sheet_offset; - i64 h = header_height / 5; + i64 x = x0 + pianokey_width + sheet_offset; + i64 y = frame_height - y0 - height + text_height; + i64 w = width - pianokey_width - sheet_offset; + i64 h = header_height / 5; + i64 border = 5; char repeat[] = "\uf363"; - i64 border = 5; nvgFontSize(saw_nvg, header_height - border * 2); nvgFontFaceId(saw_nvg, saw_font_icons); if (roll->loop_duration == 0) -- cgit v1.2.3