(fix) Set the music and sfx volumes immediately after initialising the

audio frontend.

Fixes #373
This commit is contained in:
S Handley 2019-12-10 14:02:51 +00:00 committed by Imbris
parent 7cb177bb44
commit da2d36ed76
4 changed files with 59 additions and 51 deletions

View File

@ -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);
}

View File

@ -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<f32>) -> 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<f32>) -> Option<usize> {
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<usize> {
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<f32>, ori: &Vec3<f32>) {
@ -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 {

View File

@ -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.

View File

@ -42,12 +42,10 @@ impl PlayState for MainMenuState {
let mut client_init: Option<ClientInit> = 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