mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
(fix) Set the music and sfx volumes immediately after initialising the
audio frontend. Fixes #373
This commit is contained in:
@ -103,6 +103,10 @@ impl Channel {
|
|||||||
self.audio_type
|
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) {
|
pub fn set_volume(&mut self, volume: f32) {
|
||||||
self.sink.set_volume(volume);
|
self.sink.set_volume(volume);
|
||||||
}
|
}
|
||||||
|
@ -84,21 +84,29 @@ impl AudioFrontend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_channel(&mut self) -> Option<&mut Channel> {
|
pub fn get_channel(&mut self, audio_type: AudioType) -> Option<&mut Channel> {
|
||||||
self.channels.iter_mut().find(|c| c.is_done())
|
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.
|
/// Play specfied sound file.
|
||||||
///```ignore
|
pub fn play_sound(&mut self, sound: &str, pos: Vec3<f32>) -> Option<usize> {
|
||||||
///audio.play_sound("voxygen.audio.sfx.step");
|
if self.audio_device.is_some() {
|
||||||
///```
|
|
||||||
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 {
|
|
||||||
let calc_pos = ((pos - self.listener_pos) * FALLOFF).into_array();
|
let calc_pos = ((pos - self.listener_pos) * FALLOFF).into_array();
|
||||||
|
|
||||||
let sound = self.sound_cache.load_sound(sound);
|
let sound = self.sound_cache.load_sound(sound);
|
||||||
@ -106,19 +114,32 @@ impl AudioFrontend {
|
|||||||
let left_ear = self.listener_ear_left.into_array();
|
let left_ear = self.listener_ear_left.into_array();
|
||||||
let right_ear = self.listener_ear_right.into_array();
|
let right_ear = self.listener_ear_right.into_array();
|
||||||
|
|
||||||
if let Some(channel) = self.get_channel() {
|
if let Some(channel) = self.get_channel(AudioType::Sfx) {
|
||||||
channel.set_id(id);
|
|
||||||
channel.set_volume(sfx_volume);
|
|
||||||
channel.set_emitter_position(calc_pos);
|
channel.set_emitter_position(calc_pos);
|
||||||
channel.set_left_ear_position(left_ear);
|
channel.set_left_ear_position(left_ear);
|
||||||
channel.set_right_ear_position(right_ear);
|
channel.set_right_ear_position(right_ear);
|
||||||
channel.play(sound);
|
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>) {
|
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;
|
self.listener_ear_right = pos_right;
|
||||||
|
|
||||||
for channel in self.channels.iter_mut() {
|
for channel in self.channels.iter_mut() {
|
||||||
if channel.get_audio_type() == AudioType::Sfx {
|
if !channel.is_done() && channel.get_audio_type() == AudioType::Sfx {
|
||||||
channel.set_emitter_position(
|
// TODO: Update this to correctly determine the updated relative position of
|
||||||
((channel.pos - self.listener_pos) * FALLOFF).into_array(),
|
// 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_left_ear_position(pos_left.into_array());
|
||||||
channel.set_right_ear_position(pos_right.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) {
|
pub fn stop_channel(&mut self, channel_id: usize, fader: Fader) {
|
||||||
let index = self.channels.iter().position(|c| c.get_id() == channel_id);
|
let index = self.channels.iter().position(|c| c.get_id() == channel_id);
|
||||||
if let Some(index) = index {
|
if let Some(index) = index {
|
||||||
|
@ -107,18 +107,23 @@ fn main() {
|
|||||||
Some(d) => d.to_string(),
|
Some(d) => d.to_string(),
|
||||||
None => audio::get_default_device(),
|
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)
|
AudioFrontend::new(audio_device, 16)
|
||||||
} else {
|
} else {
|
||||||
AudioFrontend::no_audio()
|
AudioFrontend::no_audio()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
audio.set_music_volume(settings.audio.music_volume);
|
||||||
|
audio.set_sfx_volume(settings.audio.sfx_volume);
|
||||||
|
|
||||||
let mut global_state = GlobalState {
|
let mut global_state = GlobalState {
|
||||||
audio,
|
audio,
|
||||||
window: Window::new(&settings).expect("Failed to create window!"),
|
window: Window::new(&settings).expect("Failed to create window!"),
|
||||||
settings,
|
settings,
|
||||||
info_message: None,
|
info_message: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let settings = &global_state.settings;
|
let settings = &global_state.settings;
|
||||||
|
|
||||||
// Initialize logging.
|
// Initialize logging.
|
||||||
|
@ -42,12 +42,10 @@ impl PlayState for MainMenuState {
|
|||||||
let mut client_init: Option<ClientInit> = None;
|
let mut client_init: Option<ClientInit> = None;
|
||||||
|
|
||||||
// Kick off title music
|
// Kick off title music
|
||||||
if None == self.title_music_channel {
|
if self.title_music_channel.is_none() && global_state.settings.audio.audio_on {
|
||||||
self.title_music_channel = Some(
|
self.title_music_channel = global_state
|
||||||
global_state
|
.audio
|
||||||
.audio
|
.play_music("voxygen.audio.soundtrack.veloren_title_tune-3");
|
||||||
.play_music("voxygen.audio.soundtrack.veloren_title_tune-3"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset singleplayer server if it was running already
|
// Reset singleplayer server if it was running already
|
||||||
|
Reference in New Issue
Block a user