mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-09 02:07:56 -06:00
feat: allow graceful disabling of audio subsystem in background
This commit is contained in:
141
src/audio.rs
141
src/audio.rs
@@ -10,13 +10,15 @@ const SOUND_ASSETS: [Asset; 4] = [Asset::Wav1, Asset::Wav2, Asset::Wav3, Asset::
|
|||||||
/// The audio system for the game.
|
/// The audio system for the game.
|
||||||
///
|
///
|
||||||
/// This struct is responsible for initializing the audio device, loading sounds,
|
/// This struct is responsible for initializing the audio device, loading sounds,
|
||||||
/// and playing them.
|
/// and playing them. If audio fails to initialize, it will be disabled and all
|
||||||
|
/// functions will silently do nothing.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Audio {
|
pub struct Audio {
|
||||||
_mixer_context: mixer::Sdl2MixerContext,
|
_mixer_context: Option<mixer::Sdl2MixerContext>,
|
||||||
sounds: Vec<Chunk>,
|
sounds: Vec<Chunk>,
|
||||||
next_sound_index: usize,
|
next_sound_index: usize,
|
||||||
muted: bool,
|
muted: bool,
|
||||||
|
disabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Audio {
|
impl Default for Audio {
|
||||||
@@ -27,13 +29,27 @@ impl Default for Audio {
|
|||||||
|
|
||||||
impl Audio {
|
impl Audio {
|
||||||
/// Creates a new `Audio` instance.
|
/// Creates a new `Audio` instance.
|
||||||
|
///
|
||||||
|
/// If audio fails to initialize, the audio system will be disabled and
|
||||||
|
/// all functions will silently do nothing.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let frequency = 44100;
|
let frequency = 44100;
|
||||||
let format = DEFAULT_FORMAT;
|
let format = DEFAULT_FORMAT;
|
||||||
let channels = 4;
|
let channels = 4;
|
||||||
let chunk_size = 256; // 256 is minimum for emscripten
|
let chunk_size = 256; // 256 is minimum for emscripten
|
||||||
|
|
||||||
mixer::open_audio(frequency, format, 1, chunk_size).expect("Failed to open audio");
|
// Try to open audio, but don't panic if it fails
|
||||||
|
if let Err(e) = mixer::open_audio(frequency, format, 1, chunk_size) {
|
||||||
|
tracing::warn!("Failed to open audio: {}. Audio will be disabled.", e);
|
||||||
|
return Self {
|
||||||
|
_mixer_context: None,
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
mixer::allocate_channels(channels);
|
mixer::allocate_channels(channels);
|
||||||
|
|
||||||
// set channel volume
|
// set channel volume
|
||||||
@@ -41,31 +57,72 @@ impl Audio {
|
|||||||
mixer::Channel(i).set_volume(32);
|
mixer::Channel(i).set_volume(32);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mixer_context = mixer::init(InitFlag::OGG).expect("Failed to initialize SDL2_mixer");
|
// Try to initialize mixer, but don't panic if it fails
|
||||||
|
let mixer_context = match mixer::init(InitFlag::OGG) {
|
||||||
|
Ok(ctx) => ctx,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to initialize SDL2_mixer: {}. Audio will be disabled.", e);
|
||||||
|
return Self {
|
||||||
|
_mixer_context: None,
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let sounds: Vec<Chunk> = SOUND_ASSETS
|
// Try to load sounds, but don't panic if any fail
|
||||||
.iter()
|
let mut sounds = Vec::new();
|
||||||
.enumerate()
|
for (i, asset) in SOUND_ASSETS.iter().enumerate() {
|
||||||
.map(|(i, asset)| {
|
match get_asset_bytes(*asset) {
|
||||||
let data = get_asset_bytes(*asset).expect("Failed to load sound asset");
|
Ok(data) => match RWops::from_bytes(&data) {
|
||||||
let rwops = RWops::from_bytes(&data).unwrap_or_else(|_| panic!("Failed to create RWops for sound {}", i + 1));
|
Ok(rwops) => match rwops.load_wav() {
|
||||||
rwops
|
Ok(chunk) => sounds.push(chunk),
|
||||||
.load_wav()
|
Err(e) => {
|
||||||
.unwrap_or_else(|_| panic!("Failed to load sound {} from asset API", i + 1))
|
tracing::warn!("Failed to load sound {} from asset API: {}", i + 1, e);
|
||||||
})
|
}
|
||||||
.collect();
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to create RWops for sound {}: {}", i + 1, e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to load sound asset {}: {}", i + 1, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no sounds loaded successfully, disable audio
|
||||||
|
if sounds.is_empty() {
|
||||||
|
tracing::warn!("No sounds loaded successfully. Audio will be disabled.");
|
||||||
|
return Self {
|
||||||
|
_mixer_context: Some(mixer_context),
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Audio {
|
Audio {
|
||||||
_mixer_context: mixer_context,
|
_mixer_context: Some(mixer_context),
|
||||||
sounds,
|
sounds,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
|
disabled: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Plays the "eat" sound effect.
|
/// Plays the "eat" sound effect.
|
||||||
|
///
|
||||||
|
/// If audio is disabled or muted, this function does nothing.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn eat(&mut self) {
|
pub fn eat(&mut self) {
|
||||||
|
if self.disabled || self.muted || self.sounds.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(chunk) = self.sounds.get(self.next_sound_index) {
|
if let Some(chunk) = self.sounds.get(self.next_sound_index) {
|
||||||
match mixer::Channel(0).play(chunk, 0) {
|
match mixer::Channel(0).play(chunk, 0) {
|
||||||
Ok(channel) => {
|
Ok(channel) => {
|
||||||
@@ -80,7 +137,13 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Instantly mute or unmute all channels.
|
/// Instantly mute or unmute all channels.
|
||||||
|
///
|
||||||
|
/// If audio is disabled, this function does nothing.
|
||||||
pub fn set_mute(&mut self, mute: bool) {
|
pub fn set_mute(&mut self, mute: bool) {
|
||||||
|
if self.disabled {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let channels = 4;
|
let channels = 4;
|
||||||
let volume = if mute { 0 } else { 32 };
|
let volume = if mute { 0 } else { 32 };
|
||||||
for i in 0..channels {
|
for i in 0..channels {
|
||||||
@@ -93,6 +156,11 @@ impl Audio {
|
|||||||
pub fn is_muted(&self) -> bool {
|
pub fn is_muted(&self) -> bool {
|
||||||
self.muted
|
self.muted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the audio system is disabled.
|
||||||
|
pub fn is_disabled(&self) -> bool {
|
||||||
|
self.disabled
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -143,8 +211,12 @@ mod tests {
|
|||||||
let audio = Audio::new();
|
let audio = Audio::new();
|
||||||
assert_eq!(audio.is_muted(), false);
|
assert_eq!(audio.is_muted(), false);
|
||||||
assert_eq!(audio.next_sound_index, 0);
|
assert_eq!(audio.next_sound_index, 0);
|
||||||
|
|
||||||
|
// Audio might be disabled if initialization failed
|
||||||
|
if !audio.is_disabled() {
|
||||||
assert_eq!(audio.sounds.len(), 4);
|
assert_eq!(audio.sounds.len(), 4);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_audio_mute_functionality() {
|
fn test_audio_mute_functionality() {
|
||||||
@@ -171,6 +243,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut audio = Audio::new();
|
let mut audio = Audio::new();
|
||||||
|
|
||||||
|
// Skip test if audio is disabled
|
||||||
|
if audio.is_disabled() {
|
||||||
|
eprintln!("Skipping sound rotation test due to disabled audio");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let initial_index = audio.next_sound_index;
|
let initial_index = audio.next_sound_index;
|
||||||
|
|
||||||
// Test sound rotation
|
// Test sound rotation
|
||||||
@@ -190,6 +269,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let audio = Audio::new();
|
let audio = Audio::new();
|
||||||
|
|
||||||
|
// Skip test if audio is disabled
|
||||||
|
if audio.is_disabled() {
|
||||||
|
eprintln!("Skipping sound index bounds test due to disabled audio");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert!(audio.next_sound_index < audio.sounds.len());
|
assert!(audio.next_sound_index < audio.sounds.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,6 +289,29 @@ mod tests {
|
|||||||
let audio = Audio::default();
|
let audio = Audio::default();
|
||||||
assert_eq!(audio.is_muted(), false);
|
assert_eq!(audio.is_muted(), false);
|
||||||
assert_eq!(audio.next_sound_index, 0);
|
assert_eq!(audio.next_sound_index, 0);
|
||||||
|
|
||||||
|
// Audio might be disabled if initialization failed
|
||||||
|
if !audio.is_disabled() {
|
||||||
assert_eq!(audio.sounds.len(), 4);
|
assert_eq!(audio.sounds.len(), 4);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_audio_disabled_state() {
|
||||||
|
if let Err(_) = init_sdl() {
|
||||||
|
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that disabled audio doesn't crash when calling functions
|
||||||
|
let mut audio = Audio::new();
|
||||||
|
|
||||||
|
// These should not panic even if audio is disabled
|
||||||
|
audio.eat();
|
||||||
|
audio.set_mute(true);
|
||||||
|
audio.set_mute(false);
|
||||||
|
|
||||||
|
// Test that we can check the disabled state
|
||||||
|
let _is_disabled = audio.is_disabled();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user