diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-02-12 22:59:01 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-02-12 22:59:01 +0100 |
commit | 5873c787c631612554d6c2daeb6db08c5306fb0c (patch) | |
tree | cf918bcc7adbc8103709cc0af2dd2150d6aa91dc /source | |
parent | b66777e914dc7fa807b99f75be57b480488da567 (diff) | |
download | saw-5873c787c631612554d6c2daeb6db08c5306fb0c.zip |
Properly resume audio after a use action in browser
Diffstat (limited to 'source')
-rw-r--r-- | source/saw/main.c | 75 |
1 files changed, 53 insertions, 22 deletions
diff --git a/source/saw/main.c b/source/saw/main.c index bdbb5ad..9b480f1 100644 --- a/source/saw/main.c +++ b/source/saw/main.c @@ -928,7 +928,7 @@ static void saw_ui_track(saw_track_t *track, i64 x0, i64 y0, i64 width, i64 height, str_t title) { i64 frame_height = sapp_height(); - i64 text_height = 35; + i64 text_height = 33; i64 header_offset = 60; i64 border = 2; i64 column_width = 200; @@ -1042,7 +1042,7 @@ static void saw_ui_track(saw_track_t *track, i64 x0, i64 y0, // nvgBeginPath(saw_nvg); - nvgFontSize(saw_nvg, text_height); + nvgFontSize(saw_nvg, text_height - border); nvgFontFaceId(saw_nvg, saw_font_text); nvgFillColor(saw_nvg, nvgRGBA(255, 255, 255, 255)); @@ -1608,6 +1608,26 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, // // ================================================================ +static void saw_init_audio(void) { + ma_device_config config = ma_device_config_init( + ma_device_type_playback); + + config.playback.format = ma_format_f32; + config.playback.channels = CHANNEL_COUNT; + config.sampleRate = SAMPLE_RATE; + config.dataCallback = saw_audio; + config.pUserData = NULL; + + if (ma_device_init(NULL, &config, &saw_audio_device) != + MA_SUCCESS) { + printf("ma_device_init failed.\n"); + fflush(stdout); + return; + } + + ma_device_start(&saw_audio_device); +} + static void saw_init(void) { // Init RNG // @@ -1629,22 +1649,9 @@ static void saw_init(void) { // Init miniaudio // - ma_device_config config = ma_device_config_init( - ma_device_type_playback); - - config.playback.format = ma_format_f32; - config.playback.channels = CHANNEL_COUNT; - config.sampleRate = SAMPLE_RATE; - config.dataCallback = saw_audio; - config.pUserData = NULL; - - if (ma_device_init(NULL, &config, &saw_audio_device) != - MA_SUCCESS) { - printf("ma_device_init failed.\n"); - return; - } - - ma_device_start(&saw_audio_device); +#ifndef __EMSCRIPTEN__ + saw_init_audio(); +#endif // Load fonts // @@ -1652,14 +1659,18 @@ static void saw_init(void) { saw_font_text = nvgCreateFontMem(saw_nvg, "", (u8 *) saw_ttf_text, SAW_TTF_TEXT_SIZE, 0); - if (saw_font_text == -1) + if (saw_font_text == -1) { printf("nvgCreateFontMem failed.\n"); + fflush(stdout); + } saw_font_icons = nvgCreateFontMem(saw_nvg, "", (u8 *) saw_ttf_icons, SAW_TTF_ICONS_SIZE, 0); - if (saw_font_icons == -1) + if (saw_font_icons == -1) { printf("nvgCreateFontMem failed.\n"); + fflush(stdout); + } // Init Saw state // @@ -1721,10 +1732,11 @@ static void saw_init(void) { s32 s = folder_create_recursive(WRAP_STR(cache)); - if (s != KIT_OK) + if (s != KIT_OK) { printf("Failed to create cache folder: %s\n (code %d)", BS(cache), (i32) s); - else { + fflush(stdout); + } else { memcpy(saw_project_file_buf, cache.values, cache.size); saw_project_file_buf[cache.size] = PATH_DELIM_C; memcpy(saw_project_file_buf + cache.size + 1, "last", 5); @@ -1735,6 +1747,7 @@ static void saw_init(void) { } printf("Project file: %s\n", BS(saw_project_file)); + fflush(stdout); // Load the project from a file // @@ -1744,6 +1757,7 @@ static void saw_init(void) { if (f == NULL) { printf("Failed to read file: %s\n", BS(saw_project_file)); + fflush(stdout); return; } @@ -1751,6 +1765,7 @@ static void saw_init(void) { do { \ if (fscanf(f, format_, __VA_ARGS__) != num_) { \ printf("Invalid syntax at \"%s\"\n", format_); \ + fflush(stdout); \ fclose(f); \ return; \ } \ @@ -1761,6 +1776,7 @@ static void saw_init(void) { if (total_rolls < 0 || total_rolls > ROLL_COUNT) { printf("Invalid roll count: %d\n", total_rolls); + fflush(stdout); fclose(f); return; } @@ -1784,6 +1800,7 @@ static void saw_init(void) { if (pitch_count < 0 || pitch_count > PITCH_COUNT) { printf("Invalid pitch count: %d\n", pitch_count); + fflush(stdout); fclose(f); return; } @@ -1801,6 +1818,7 @@ static void saw_init(void) { if (sheet_size < 0 || sheet_size > SHEET_SIZE) { printf("Invalid note count: %d\n", sheet_size); + fflush(stdout); fclose(f); return; } @@ -2117,11 +2135,13 @@ static void saw_cleanup(void) { return; printf("Save project: %s\n", BS(saw_project_file)); + fflush(stdout); FILE *f = fopen(BS(saw_project_file), "wb"); if (f == NULL) { printf("Failed to write file: %s\n", BS(saw_project_file)); + fflush(stdout); return; } @@ -2207,6 +2227,17 @@ static void saw_cleanup(void) { } static void saw_event(sapp_event const *event) { +#ifdef __EMSCRIPTEN__ + // In browser, resume the audio only after a user action. + static b8 is_audio_suspended = 1; + if (is_audio_suspended && + (event->type == SAPP_EVENTTYPE_MOUSE_DOWN || + event->type == SAPP_EVENTTYPE_TOUCHES_BEGAN)) { + saw_init_audio(); + is_audio_suspended = 0; + } +#endif + switch (event->type) { case SAPP_EVENTTYPE_MOUSE_MOVE: saw_shift_on = (event->modifiers & SAPP_MODIFIER_SHIFT) != 0; |