From 26ffed8627678f95480550cdbca7dfd55b619cea Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Mon, 29 Jul 2024 20:35:14 +0200 Subject: [PATCH] web: Fix broken audio by using 48kHz and 2048 samples No idea why these values are needed though, found them by trying. --- src/audio/constants.hpp | 4 ++++ src/audio/sdl/engine.cpp | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/audio/constants.hpp b/src/audio/constants.hpp index 7d7fa7cc..4d48951f 100644 --- a/src/audio/constants.hpp +++ b/src/audio/constants.hpp @@ -9,7 +9,11 @@ namespace jngl::audio { +#ifdef __EMSCRIPTEN__ +constexpr int frequency = 48000; +#else constexpr int frequency = 44100; +#endif constexpr float inv_frequency = 1.f / frequency; inline std::int64_t seconds_to_samples(float seconds) { diff --git a/src/audio/sdl/engine.cpp b/src/audio/sdl/engine.cpp index cfa1b568..82f8c70a 100644 --- a/src/audio/sdl/engine.cpp +++ b/src/audio/sdl/engine.cpp @@ -5,6 +5,7 @@ #include "../engine.hpp" #include "../../jngl/debug.hpp" +#include "../../jngl/log.hpp" #include "../../jngl/other.hpp" #include "../Stream.hpp" #include "../constants.hpp" @@ -92,15 +93,19 @@ struct engine::Impl { desired.freq = frequency; desired.channels = 2; desired.format = AUDIO_S16SYS; +#ifdef __EMSCRIPTEN__ + desired.samples = 2048; +#else desired.samples = 256; +#endif desired.callback = &callback; desired.userdata = this; if (device = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0); device == 0) { throw std::runtime_error(SDL_GetError()); } - // log::info() << "Initialized audio: " << static_cast(obtained.channels) << " - // channels, " << obtained.freq << " Hz, " << obtained.samples << " samples"; + trace("Initialized audio: {} channels, {} Hz, {} samples", + static_cast(obtained.channels), obtained.freq, obtained.samples); buffer.resize(obtained.samples * obtained.channels); SDL_PauseAudioDevice(device, 0);