Skip to content

Commit

Permalink
web: Fix broken audio by using 48kHz and 2048 samples
Browse files Browse the repository at this point in the history
No idea why these values are needed though, found them by trying.
  • Loading branch information
jhasse committed Jul 29, 2024
1 parent eaab68a commit 26ffed8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/audio/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions src/audio/sdl/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<int>(obtained.channels) << "
// channels, " << obtained.freq << " Hz, " << obtained.samples << " samples";
trace("Initialized audio: {} channels, {} Hz, {} samples",
static_cast<int>(obtained.channels), obtained.freq, obtained.samples);

buffer.resize(obtained.samples * obtained.channels);
SDL_PauseAudioDevice(device, 0);
Expand Down

0 comments on commit 26ffed8

Please sign in to comment.