Moving audio code

This commit is contained in:
DaforLynx 2022-05-06 12:20:36 -07:00 committed by IsseW
parent 6215ccd522
commit dc8424c549
6 changed files with 70 additions and 79 deletions

View File

@ -1,4 +1,3 @@
// TODO: Add an ambient-soundtrack that runs independently from the musical soundtrack
// Times: Some(Day), Some(Night), None [both]
// Biomes: Grassland, Forest, Desert, Snowland, Lake, Mountain, Ocean, Jungle, Savannah, Taiga
// planned biomes: Swamp

View File

@ -1685,7 +1685,7 @@ impl Client {
self.invite = None;
}
// Lerp the clientside weather.
// Lerp the clientside weather.
self.weather.update(&mut self.state.weather_grid_mut());
// Lerp towards the target time of day - this ensures a smooth transition for

View File

@ -13,9 +13,7 @@ pub struct WeatherSim {
}
impl WeatherSim {
pub fn new(size: Vec2<u32>, _world: &World) -> Self {
Self { size }
}
pub fn new(size: Vec2<u32>, _world: &World) -> Self { Self { size } }
// Time step is cell size / maximum wind speed
pub fn tick(&mut self, time_of_day: &TimeOfDay, out: &mut WeatherGrid) {

View File

@ -48,7 +48,7 @@ impl AmbientMgr {
// Iterate through each tag
for tag in AmbientChannelTag::iter() {
// If the conditions warrant creating a channel of that tag
if self.check_ambience_necessity(tag, client, camera)
if AmbientMgr::check_ambience_necessity(tag, client, camera)
&& audio.get_ambient_channel(tag).is_none()
{
// Iterate through the supposed number of channels - one for each tag
@ -71,8 +71,7 @@ impl AmbientMgr {
if audio.ambient_channels[index].get_tag() == tag {
// Maintain: get the correct multiplier of whatever the tag of the current
// channel is
let target_volume =
audio.ambient_channels[index].maintain(state, client, camera);
let target_volume = AmbientChannel::maintain(tag, state, client, camera);
// Get multiplier of the current channel
let initial_volume = audio.ambient_channels[index].get_multiplier();
@ -128,12 +127,7 @@ impl AmbientMgr {
}
}
fn check_ambience_necessity(
&mut self,
tag: AmbientChannelTag,
client: &Client,
camera: &Camera,
) -> bool {
fn check_ambience_necessity(tag: AmbientChannelTag, client: &Client, camera: &Camera) -> bool {
match tag {
AmbientChannelTag::Wind => {
let focus_off = camera.get_focus_pos().map(f32::trunc);
@ -188,57 +182,9 @@ impl AmbientMgr {
}
}
impl AmbientChannel {
pub fn maintain(&mut self, state: &State, client: &Client, camera: &Camera) -> f32 {
let tag = self.get_tag();
let focus_off = camera.get_focus_pos().map(f32::trunc);
let cam_pos = camera.dependents().cam_pos + focus_off;
let mut target_volume: f32 = self.get_ambience_volume(tag, client, camera);
target_volume = self.check_camera(state, client, cam_pos, target_volume);
target_volume
}
fn check_camera(
&mut self,
state: &State,
client: &Client,
cam_pos: Vec3<f32>,
initial_volume: f32,
) -> f32 {
let mut volume_multiplier = initial_volume;
let terrain_alt = if let Some(chunk) = client.current_chunk() {
chunk.meta().alt()
} else {
0.0
};
// Checks if the camera is underwater to diminish ambient sounds
if state
.terrain()
.get((cam_pos).map(|e| e.floor() as i32))
.map(|b| b.is_liquid())
.unwrap_or(false)
{
volume_multiplier *= 0.1;
}
// Is the camera roughly under the terrain?
if cam_pos.z < terrain_alt - 20.0 {
volume_multiplier = 0.0;
}
volume_multiplier.clamped(0.0, 1.0)
}
impl AmbientChannelTag {
// Gets appropriate volume for each tag
fn get_ambience_volume(
&mut self,
tag: AmbientChannelTag,
client: &Client,
camera: &Camera,
) -> f32 {
pub fn get_tag_volume(tag: AmbientChannelTag, client: &Client, camera: &Camera) -> f32 {
match tag {
AmbientChannelTag::Wind => {
let focus_off = camera.get_focus_pos().map(f32::trunc);
@ -327,6 +273,53 @@ impl AmbientChannel {
}
}
impl AmbientChannel {
pub fn maintain(
tag: AmbientChannelTag,
state: &State,
client: &Client,
camera: &Camera,
) -> f32 {
let focus_off = camera.get_focus_pos().map(f32::trunc);
let cam_pos = camera.dependents().cam_pos + focus_off;
let mut target_volume: f32 = AmbientChannelTag::get_tag_volume(tag, client, camera);
target_volume = AmbientChannel::check_camera(state, client, cam_pos, target_volume);
target_volume
}
fn check_camera(
state: &State,
client: &Client,
cam_pos: Vec3<f32>,
initial_volume: f32,
) -> f32 {
let mut volume_multiplier = initial_volume;
let terrain_alt = if let Some(chunk) = client.current_chunk() {
chunk.meta().alt()
} else {
0.0
};
// Checks if the camera is underwater to diminish ambient sounds
if state
.terrain()
.get((cam_pos).map(|e| e.floor() as i32))
.map(|b| b.is_liquid())
.unwrap_or(false)
{
volume_multiplier *= 0.1;
}
// Is the camera roughly under the terrain?
if cam_pos.z < terrain_alt - 20.0 {
volume_multiplier = 0.0;
}
volume_multiplier.clamped(0.0, 1.0)
}
}
pub fn load_ambience_items() -> AssetHandle<AmbientCollection> {
AmbientCollection::load_or_insert_with("voxygen.audio.ambient", |error| {
warn!(

View File

@ -166,6 +166,7 @@ pub enum AmbientChannelTag {
Thunder,
Leaves,
}
/// A AmbientChannel uses a non-positional audio sink designed to play sounds
/// which are always heard at the camera's position.
pub struct AmbientChannel {
@ -297,11 +298,11 @@ impl SfxChannel {
}
}
pub struct UIChannel {
pub struct UiChannel {
sink: Sink,
}
impl UIChannel {
impl UiChannel {
pub fn new(stream: &OutputStreamHandle) -> Self {
Self {
sink: Sink::try_new(stream).unwrap(),

View File

@ -8,7 +8,7 @@ pub mod sfx;
pub mod soundcache;
use channel::{
AmbientChannel, AmbientChannelTag, MusicChannel, MusicChannelTag, SfxChannel, UIChannel,
AmbientChannel, AmbientChannelTag, MusicChannel, MusicChannelTag, SfxChannel, UiChannel,
};
use fader::Fader;
use music::MusicTransitionManifest;
@ -40,17 +40,17 @@ pub struct AudioFrontend {
//pub device_list: Vec<String>,
//pub audio_device: Option<Device>,
pub stream: Option<rodio::OutputStream>,
pub audio_stream: Option<rodio::OutputStreamHandle>,
audio_stream: Option<rodio::OutputStreamHandle>,
pub music_channels: Vec<MusicChannel>,
pub ambient_channels: Vec<AmbientChannel>,
pub sfx_channels: Vec<SfxChannel>,
pub ui_channels: Vec<UIChannel>,
pub sfx_volume: f32,
pub ambience_volume: f32,
pub music_volume: f32,
pub master_volume: f32,
pub listener: Listener,
music_channels: Vec<MusicChannel>,
ambient_channels: Vec<AmbientChannel>,
sfx_channels: Vec<SfxChannel>,
ui_channels: Vec<UiChannel>,
sfx_volume: f32,
ambience_volume: f32,
music_volume: f32,
master_volume: f32,
listener: Listener,
mtm: AssetHandle<MusicTransitionManifest>,
}
@ -87,7 +87,7 @@ impl AudioFrontend {
let mut ui_channels = Vec::with_capacity(num_ui_channels);
if let Some(audio_stream) = &audio_stream {
ui_channels.resize_with(num_ui_channels, || UIChannel::new(audio_stream))
ui_channels.resize_with(num_ui_channels, || UiChannel::new(audio_stream))
}
Self {
@ -164,7 +164,7 @@ impl AudioFrontend {
None
}
fn get_ui_channel(&mut self) -> Option<&mut UIChannel> {
fn get_ui_channel(&mut self) -> Option<&mut UiChannel> {
if self.audio_stream.is_some() {
let sfx_volume = self.get_sfx_volume();
if let Some(channel) = self.ui_channels.iter_mut().find(|c| c.is_done()) {