Voxygen: Update ambient channels volume

This commit is contained in:
João Capucho 2021-04-18 19:28:28 +01:00
parent 05279872ce
commit 1d605c2dc8
No known key found for this signature in database
GPG Key ID: 1AD5CE7CCAEBDA21
2 changed files with 27 additions and 11 deletions

View File

@ -165,18 +165,24 @@ pub enum AmbientChannelTag {
/// which are always heard at the camera's position.
pub struct AmbientChannel {
tag: AmbientChannelTag,
multiplier: f32,
sink: Sink,
}
impl AmbientChannel {
pub fn new(stream: &OutputStreamHandle, tag: AmbientChannelTag) -> Self {
pub fn new(stream: &OutputStreamHandle, tag: AmbientChannelTag, multiplier: f32) -> Self {
let new_sink = Sink::try_new(stream);
match new_sink {
Ok(sink) => Self { tag, sink },
Ok(sink) => Self {
tag,
multiplier,
sink,
},
Err(_) => {
warn!("Failed to create rodio sink. May not play wind sounds.");
Self {
tag,
multiplier,
sink: Sink::new_idle().0,
}
},
@ -195,7 +201,9 @@ impl AmbientChannel {
pub fn stop(&mut self) { self.sink.stop(); }
pub fn set_volume(&mut self, volume: f32) { self.sink.set_volume(volume); }
pub fn set_volume(&mut self, volume: f32) { self.sink.set_volume(volume * self.multiplier); }
pub fn set_multiplier(&mut self, multiplier: f32) { self.multiplier = multiplier; }
pub fn get_volume(&mut self) -> f32 { self.sink.volume() }

View File

@ -308,14 +308,16 @@ impl AudioFrontend {
) -> Option<&mut AmbientChannel> {
if let Some(audio_stream) = &self.audio_stream {
if self.ambient_channels.is_empty() {
let mut ambient_channel = AmbientChannel::new(audio_stream, channel_tag);
ambient_channel.set_volume(self.get_sfx_volume() * volume_multiplier);
let mut ambient_channel =
AmbientChannel::new(audio_stream, channel_tag, volume_multiplier);
ambient_channel.set_volume(self.get_sfx_volume());
self.ambient_channels.push(ambient_channel);
} else {
let sfx_volume = self.get_sfx_volume();
for channel in self.ambient_channels.iter_mut() {
if channel.get_tag() == channel_tag {
channel.set_volume(sfx_volume * volume_multiplier);
channel.set_multiplier(volume_multiplier);
channel.set_volume(sfx_volume);
return Some(channel);
}
}
@ -329,7 +331,8 @@ impl AudioFrontend {
if self.audio_stream.is_some() {
let sfx_volume = self.get_sfx_volume();
if let Some(channel) = self.ambient_channels.iter_mut().last() {
channel.set_volume(sfx_volume * volume_multiplier);
channel.set_multiplier(volume_multiplier);
channel.set_volume(sfx_volume);
}
}
}
@ -416,10 +419,7 @@ impl AudioFrontend {
pub fn set_sfx_volume(&mut self, sfx_volume: f32) {
self.sfx_volume = sfx_volume;
let sfx_volume = self.get_sfx_volume();
for channel in self.sfx_channels.iter_mut() {
channel.set_volume(sfx_volume);
}
self.update_sfx_volumes();
}
pub fn set_music_volume(&mut self, music_volume: f32) {
@ -439,10 +439,18 @@ impl AudioFrontend {
channel.set_volume(music_volume);
}
self.update_sfx_volumes();
}
fn update_sfx_volumes(&mut self) {
let sfx_volume = self.get_sfx_volume();
for channel in self.sfx_channels.iter_mut() {
channel.set_volume(sfx_volume);
}
for channel in self.ambient_channels.iter_mut() {
channel.set_volume(sfx_volume);
}
}
pub fn stop_ambient_sounds(&mut self) {