From da2d36ed76fe438f04b5578538de91710aad19df Mon Sep 17 00:00:00 2001 From: S Handley Date: Tue, 10 Dec 2019 14:02:51 +0000 Subject: [PATCH] (fix) Set the music and sfx volumes immediately after initialising the audio frontend. Fixes #373 --- voxygen/src/audio/channel.rs | 4 ++ voxygen/src/audio/mod.rs | 89 ++++++++++++++++++------------------ voxygen/src/main.rs | 7 ++- voxygen/src/menu/main/mod.rs | 10 ++-- 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/voxygen/src/audio/channel.rs b/voxygen/src/audio/channel.rs index d9a341324c..9adb8d9fd1 100644 --- a/voxygen/src/audio/channel.rs +++ b/voxygen/src/audio/channel.rs @@ -103,6 +103,10 @@ impl Channel { self.audio_type } + pub fn set_audio_type(&mut self, audio_type: AudioType) { + self.audio_type = audio_type; + } + pub fn set_volume(&mut self, volume: f32) { self.sink.set_volume(volume); } diff --git a/voxygen/src/audio/mod.rs b/voxygen/src/audio/mod.rs index 4aa56d4426..2f37783e11 100644 --- a/voxygen/src/audio/mod.rs +++ b/voxygen/src/audio/mod.rs @@ -84,21 +84,29 @@ impl AudioFrontend { } } - pub fn get_channel(&mut self) -> Option<&mut Channel> { - self.channels.iter_mut().find(|c| c.is_done()) + pub fn get_channel(&mut self, audio_type: AudioType) -> Option<&mut Channel> { + if let Some(channel) = self.channels.iter_mut().find(|c| c.is_done()) { + let id = self.next_channel_id; + self.next_channel_id += 1; + + let volume = match audio_type { + AudioType::Music => self.music_volume, + _ => self.sfx_volume, + }; + + channel.set_id(id); + channel.set_audio_type(audio_type); + channel.set_volume(volume); + + Some(channel) + } else { + None + } } /// Play specfied sound file. - ///```ignore - ///audio.play_sound("voxygen.audio.sfx.step"); - ///``` - pub fn play_sound(&mut self, sound: &str, pos: Vec3) -> usize { - let id = self.next_channel_id; - self.next_channel_id += 1; - - let sfx_volume = self.sfx_volume; - - if let Some(_) = &self.audio_device { + pub fn play_sound(&mut self, sound: &str, pos: Vec3) -> Option { + if self.audio_device.is_some() { let calc_pos = ((pos - self.listener_pos) * FALLOFF).into_array(); let sound = self.sound_cache.load_sound(sound); @@ -106,19 +114,32 @@ impl AudioFrontend { let left_ear = self.listener_ear_left.into_array(); let right_ear = self.listener_ear_right.into_array(); - if let Some(channel) = self.get_channel() { - channel.set_id(id); - channel.set_volume(sfx_volume); + if let Some(channel) = self.get_channel(AudioType::Sfx) { channel.set_emitter_position(calc_pos); channel.set_left_ear_position(left_ear); channel.set_right_ear_position(right_ear); channel.play(sound); - } else { - log::warn!("No available channels!"); + + return Some(channel.get_id()); } } - id + None + } + + pub fn play_music(&mut self, sound: &str) -> Option { + if self.audio_device.is_some() { + if let Some(channel) = self.get_channel(AudioType::Music) { + let file = assets::load_file(&sound, &["ogg"]).expect("Failed to load sound"); + let sound = Decoder::new(file).expect("Failed to decode sound"); + + channel.play(sound); + + return Some(channel.get_id()); + } + } + + None } pub fn set_listener_pos(&mut self, pos: &Vec3, ori: &Vec3) { @@ -134,38 +155,18 @@ impl AudioFrontend { self.listener_ear_right = pos_right; for channel in self.channels.iter_mut() { - if channel.get_audio_type() == AudioType::Sfx { - channel.set_emitter_position( - ((channel.pos - self.listener_pos) * FALLOFF).into_array(), - ); + if !channel.is_done() && channel.get_audio_type() == AudioType::Sfx { + // TODO: Update this to correctly determine the updated relative position of + // the SFX emitter when the player (listener) moves + // channel.set_emitter_position( + // ((channel.pos - self.listener_pos) * FALLOFF).into_array(), + // ); channel.set_left_ear_position(pos_left.into_array()); channel.set_right_ear_position(pos_right.into_array()); } } } - pub fn play_music(&mut self, sound: &str) -> usize { - let id = self.next_channel_id; - self.next_channel_id += 1; - - let music_volume = self.music_volume; - - if let Some(_) = &self.audio_device { - let file = assets::load_file(&sound, &["ogg"]).unwrap(); - let sound = Decoder::new(file).unwrap(); - - if let Some(channel) = self.get_channel() { - channel.set_id(id); - channel.set_volume(music_volume); - channel.play(sound); - } - } else { - log::warn!("No available channels!"); - } - - id - } - pub fn stop_channel(&mut self, channel_id: usize, fader: Fader) { let index = self.channels.iter().position(|c| c.get_id() == channel_id); if let Some(index) = index { diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index fc3da70a71..2d6ff274e3 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -107,18 +107,23 @@ fn main() { Some(d) => d.to_string(), None => audio::get_default_device(), }; - let audio = if settings.audio.audio_on { + + let mut audio = if settings.audio.audio_on { AudioFrontend::new(audio_device, 16) } else { AudioFrontend::no_audio() }; + audio.set_music_volume(settings.audio.music_volume); + audio.set_sfx_volume(settings.audio.sfx_volume); + let mut global_state = GlobalState { audio, window: Window::new(&settings).expect("Failed to create window!"), settings, info_message: None, }; + let settings = &global_state.settings; // Initialize logging. diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 621c759ac0..6605a06218 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -42,12 +42,10 @@ impl PlayState for MainMenuState { let mut client_init: Option = None; // Kick off title music - if None == self.title_music_channel { - self.title_music_channel = Some( - global_state - .audio - .play_music("voxygen.audio.soundtrack.veloren_title_tune-3"), - ) + if self.title_music_channel.is_none() && global_state.settings.audio.audio_on { + self.title_music_channel = global_state + .audio + .play_music("voxygen.audio.soundtrack.veloren_title_tune-3"); } // Reset singleplayer server if it was running already