From 2279e01ea50b8e4229ad40efb4b1cb54c32c802f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 25 Oct 2020 19:20:03 +0100 Subject: [PATCH] Improve sdl audio --- main_sdl.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main_sdl.c b/main_sdl.c index b45aba7..fda8787 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -302,7 +302,8 @@ void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infin #endif uint16_t audioBuff[SFG_SFX_SAMPLE_COUNT]; -uint16_t audioPos = 0; +uint16_t audioPos = 0; // audio position for the next audio buffer fill +uint32_t audioUpdateFrame = 0; // game frame at which audio buffer fill happened static inline int16_t mixSamples(int16_t sample1, int16_t sample2) { @@ -326,6 +327,8 @@ void audioFillCallback(void *userdata, uint8_t *s, int l) audioBuff[audioPos] = 0; audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0; } + + audioUpdateFrame = SFG_game.frame; } void SFG_setMusic(uint8_t value) @@ -341,7 +344,9 @@ void SFG_setMusic(uint8_t value) void SFG_playSound(uint8_t soundIndex, uint8_t volume) { - uint16_t pos = audioPos; + uint16_t pos = audioPos + + ((SFG_game.frame - audioUpdateFrame) * SFG_MS_PER_FRAME * 8); + uint16_t volumeScale = 1 << (volume / 37); for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) @@ -403,6 +408,8 @@ int main(int argc, char *argv[]) return 0; } + SFG_init(); + puts("SDL: initializing SDL"); window = @@ -463,8 +470,6 @@ int main(int argc, char *argv[]) SDL_ShowCursor(0); - SFG_init(); - SDL_PumpEvents(); SDL_WarpMouseInWindow(window,