From 46a73c5ace60386616666111341e1a61026f228e Mon Sep 17 00:00:00 2001 From: Ryan Walters Date: Wed, 10 Sep 2025 23:08:08 -0500 Subject: [PATCH] fix: solve audio glitch/crackling on Emscripten via use higher buffer and AUDIO_S16LSB --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/audio.rs | 26 +++++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e408151..5d0f5dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -663,7 +663,7 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "pacman" -version = "0.79.1" +version = "0.79.2" dependencies = [ "anyhow", "bevy_ecs", diff --git a/Cargo.toml b/Cargo.toml index 9d880b0..6169366 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pacman" -version = "0.79.1" +version = "0.79.2" authors = ["Xevion"] edition = "2021" rust-version = "1.86.0" diff --git a/src/audio.rs b/src/audio.rs index 180ee31..e33fe58 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,7 +1,7 @@ //! This module handles the audio playback for the game. use crate::asset::{get_asset_bytes, Asset}; use sdl2::{ - mixer::{self, Chunk, InitFlag, LoaderRWops, DEFAULT_FORMAT}, + mixer::{self, Chunk, InitFlag, LoaderRWops, AUDIO_S16LSB, DEFAULT_CHANNELS}, rwops::RWops, }; @@ -33,13 +33,24 @@ impl Audio { /// If audio fails to initialize, the audio system will be disabled and /// all functions will silently do nothing. pub fn new() -> Self { - let frequency = 44100; - let format = DEFAULT_FORMAT; - let channels = 4; - let chunk_size = 256; // 256 is minimum for emscripten + let frequency = 44_100; + let format = AUDIO_S16LSB; + let chunk_size = { + // 256 is the minimum for Emscripten, but in practice 1024 is much more reliable + #[cfg(target_os = "emscripten")] + { + 1024 + } + + // Otherwise, 256 is plenty safe. + #[cfg(not(target_os = "emscripten"))] + { + 256 + } + }; // Try to open audio, but don't panic if it fails - if let Err(e) = mixer::open_audio(frequency, format, 1, chunk_size) { + if let Err(e) = mixer::open_audio(frequency, format, DEFAULT_CHANNELS, chunk_size) { tracing::warn!("Failed to open audio: {}. Audio will be disabled.", e); return Self { _mixer_context: None, @@ -51,7 +62,8 @@ impl Audio { }; } - mixer::allocate_channels(channels); + let channels = 4; + mixer::allocate_channels(4); // set channel volume for i in 0..channels {