From 39d4ee8a96ff704a401cf58b3d763a74ee8e1c29 Mon Sep 17 00:00:00 2001 From: jiminycrick Date: Sat, 31 Oct 2020 16:41:08 -0700 Subject: [PATCH] Owls, campfires, and better bird sfx handling --- assets/voxygen/audio/sfx.ron | 16 ++- assets/voxygen/audio/sfx/ambient/embers.wav | 4 +- assets/voxygen/audio/sfx/ambient/fire.wav | 3 + assets/voxygen/audio/sfx/ambient/owl_1.wav | 3 + .../src/audio/sfx/event_mapper/block/mod.rs | 78 +++++++------- .../audio/sfx/event_mapper/campfire/mod.rs | 100 ++++++++++++++---- .../src/audio/sfx/event_mapper/combat/mod.rs | 2 +- voxygen/src/audio/sfx/event_mapper/mod.rs | 6 +- .../audio/sfx/event_mapper/movement/mod.rs | 2 +- voxygen/src/audio/sfx/mod.rs | 2 + 10 files changed, 146 insertions(+), 70 deletions(-) create mode 100644 assets/voxygen/audio/sfx/ambient/fire.wav create mode 100644 assets/voxygen/audio/sfx/ambient/owl_1.wav diff --git a/assets/voxygen/audio/sfx.ron b/assets/voxygen/audio/sfx.ron index b7a3e3ab4c..374d65584e 100644 --- a/assets/voxygen/audio/sfx.ron +++ b/assets/voxygen/audio/sfx.ron @@ -3,11 +3,17 @@ // // Ambient // + Campfire: ( + files: [ + "voxygen.audio.sfx.ambient.fire", + ], + threshold: 0.5, + ), Embers: ( files: [ "voxygen.audio.sfx.ambient.embers", ], - threshold: 1.2, + threshold: 0.5, ), Birdcall: ( files: [ @@ -15,7 +21,13 @@ "voxygen.audio.sfx.ambient.birdcall_2", "voxygen.audio.sfx.ambient.birdcall_3", ], - threshold: 30.0, + threshold: 10.0, + ), + Owl: ( + files: [ + "voxygen.audio.sfx.ambient.owl_1", + ], + threshold: 14.0, ), Cricket: ( files: [ diff --git a/assets/voxygen/audio/sfx/ambient/embers.wav b/assets/voxygen/audio/sfx/ambient/embers.wav index dd3450abef..96d5a0a941 100644 --- a/assets/voxygen/audio/sfx/ambient/embers.wav +++ b/assets/voxygen/audio/sfx/ambient/embers.wav @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:596c20b9b9cffed74274bac3bf22f7868f1adaf3a1780af7897c06b058f1cb94 -size 217472 +oid sha256:47d04804be81c882ad5e0314016ab6fb7659d81997ab4bcc5154487b15771dfe +size 372540 diff --git a/assets/voxygen/audio/sfx/ambient/fire.wav b/assets/voxygen/audio/sfx/ambient/fire.wav new file mode 100644 index 0000000000..fc78392d20 --- /dev/null +++ b/assets/voxygen/audio/sfx/ambient/fire.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fa0da468a1af98a51565f4430bfd5eb3a12db3bce2959c24b0d11b0e99c9c7b7 +size 372540 diff --git a/assets/voxygen/audio/sfx/ambient/owl_1.wav b/assets/voxygen/audio/sfx/ambient/owl_1.wav new file mode 100644 index 0000000000..a6d59653ef --- /dev/null +++ b/assets/voxygen/audio/sfx/ambient/owl_1.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7309844c204faf31bce19d4faf6657018b87fa5b1729f4f2fc4276fa139c6bc2 +size 775048 diff --git a/voxygen/src/audio/sfx/event_mapper/block/mod.rs b/voxygen/src/audio/sfx/event_mapper/block/mod.rs index 77eda8ffbb..051bd7005d 100644 --- a/voxygen/src/audio/sfx/event_mapper/block/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/block/mod.rs @@ -11,20 +11,14 @@ use common::{ vol::RectRasterableVol, }; use hashbrown::HashMap; -use rand::{prelude::SliceRandom, thread_rng, Rng}; +use rand::{ + prelude::{IteratorRandom, SliceRandom}, + thread_rng, Rng, +}; use specs::WorldExt; use std::time::Instant; use vek::*; -//enum BlockEmitter { -// Leaves, -// Grass, -// Embers, -// Beehives, -// Reeds, -// Flowers, -//} - #[derive(Clone, PartialEq)] struct PreviousBlockState { event: SfxEvent, @@ -50,8 +44,6 @@ impl PreviousBlockState { } pub struct BlockEventMapper { - timer: Instant, - counter: usize, history: HashMap, PreviousBlockState>, } @@ -88,7 +80,7 @@ impl EventMapper for BlockEventMapper { sfx: SfxEvent, // The volume of the sfx volume: f32, - // Condition that must be true + // Condition that must be true to play cond: fn(&State) -> bool, } let sounds: &[BlockSounds] = &[ @@ -97,14 +89,20 @@ impl EventMapper for BlockEventMapper { range: 1, sfx: SfxEvent::Birdcall, volume: 1.0, - //cond: |_| true, cond: |st| st.get_day_period().is_light(), }, + BlockSounds { + blocks: |boi| &boi.leaves, + range: 1, + sfx: SfxEvent::Owl, + volume: 1.0, + cond: |st| st.get_day_period().is_dark(), + }, BlockSounds { blocks: |boi| &boi.embers, range: 1, sfx: SfxEvent::Embers, - volume: 0.05, + volume: 0.15, //volume: 0.05, cond: |_| true, //cond: |st| st.get_day_period().is_dark(), @@ -136,7 +134,8 @@ impl EventMapper for BlockEventMapper { range: 1, sfx: SfxEvent::Bees, volume: 1.0, - cond: |_| true, + //cond: |_| true, + cond: |st| st.get_day_period().is_light(), }, ]; @@ -144,6 +143,8 @@ impl EventMapper for BlockEventMapper { for sounds in sounds.iter() { if !(sounds.cond)(state) { continue; + } else if sounds.sfx == SfxEvent::Birdcall && thread_rng().gen_bool(0.99) { + continue; } // For chunks surrounding the player position @@ -152,31 +153,37 @@ impl EventMapper for BlockEventMapper { // Get all the blocks of interest in this chunk terrain.get(chunk_pos).map(|chunk_data| { - // Get all the blocks of type sounds + // Get the positions of the blocks of type sounds let blocks = (sounds.blocks)(&chunk_data.blocks_of_interest); + //let mut my_blocks = blocks.to_vec(); + //// Reduce the number of bird calls from trees + //if sounds.sfx == SfxEvent::Birdcall { + // my_blocks = my_blocks.choose_multiple(&mut thread_rng(), 6).cloned().collect(); + // //blocks = blocks.to_vec().choose_multiple(&mut thread_rng(), 6).cloned().collect::>>().as_slice(); + //} else if sounds.sfx == SfxEvent::Cricket { + // my_blocks = my_blocks.choose_multiple(&mut thread_rng(), 6).cloned().collect(); + //} + let absolute_pos: Vec3 = Vec3::from(chunk_pos * TerrainChunk::RECT_SIZE.map(|e| e as i32)); // Iterate through each individual block for block in blocks { - // Reduce the number of bird calls from trees - if sounds.sfx == SfxEvent::Birdcall && thread_rng().gen::() > 0.05 { - println!("skipped a bird"); - continue; - } else if sounds.sfx == SfxEvent::Cricket && thread_rng().gen::() > 0.5 { - continue; - } + if sounds.sfx == SfxEvent::Birdcall && thread_rng().gen_bool(0.999) { + continue; + } let block_pos: Vec3 = absolute_pos + block; let state = self.history.entry(block_pos).or_default(); // Convert to f32 for sfx emitter - let block_pos = Vec3::new( - block_pos[0] as f32, - block_pos[1] as f32, - block_pos[2] as f32, - ); + //let block_pos = Vec3::new( + // block_pos[0] as f32, + // block_pos[1] as f32, + // block_pos[2] as f32, + //); + let block_pos = block_pos.map(|x| x as f32); if Self::should_emit(state, triggers.get_key_value(&sounds.sfx)) { // If the camera is within SFX distance @@ -238,8 +245,6 @@ impl EventMapper for BlockEventMapper { impl BlockEventMapper { pub fn new() -> Self { Self { - timer: Instant::now(), - counter: 0, history: HashMap::new(), } } @@ -250,16 +255,7 @@ impl BlockEventMapper { ) -> bool { if let Some((event, item)) = sfx_trigger_item { if &previous_state.event == event { - if event == &SfxEvent::Birdcall { - if thread_rng().gen_bool(0.5) { - previous_state.time.elapsed().as_secs_f64() - >= (item.threshold + thread_rng().gen_range(-3.0, 3.0)) - } else { - false - } - } else { - previous_state.time.elapsed().as_secs_f64() >= item.threshold - } + previous_state.time.elapsed().as_secs_f64() >= item.threshold } else { true } diff --git a/voxygen/src/audio/sfx/event_mapper/campfire/mod.rs b/voxygen/src/audio/sfx/event_mapper/campfire/mod.rs index b86640c5db..a6f6a973d1 100644 --- a/voxygen/src/audio/sfx/event_mapper/campfire/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/campfire/mod.rs @@ -1,6 +1,6 @@ /// EventMapper::Campfire maps sfx to campfires use crate::{ - audio::sfx::{SfxEvent, SfxEventItem, SfxTriggers, SFX_DIST_LIMIT_SQR}, + audio::sfx::{SfxEvent, SfxEventItem, SfxTriggerItem, SfxTriggers, SFX_DIST_LIMIT_SQR}, scene::{Camera, Terrain}, }; @@ -12,11 +12,28 @@ use common::{ state::State, terrain::TerrainChunk, }; +use hashbrown::HashMap; use specs::{Entity as EcsEntity, Join, WorldExt}; use std::time::{Duration, Instant}; +#[derive(Clone)] +struct PreviousEntityState { + event: SfxEvent, + time: Instant, +} + +impl Default for PreviousEntityState { + fn default() -> Self { + Self { + event: SfxEvent::Idle, + time: Instant::now(), + } + } +} + pub struct CampfireEventMapper { timer: Instant, + event_history: HashMap, } impl EventMapper for CampfireEventMapper { @@ -31,35 +48,43 @@ impl EventMapper for CampfireEventMapper { let ecs = state.ecs(); let sfx_event_bus = ecs.read_resource::>(); - let sfx_emitter = sfx_event_bus.emitter(); + let mut sfx_emitter = sfx_event_bus.emitter(); let focus_off = camera.get_focus_pos().map(f32::trunc); let cam_pos = camera.dependents().cam_pos + focus_off; - for (body, pos) in (&ecs.read_storage::(), &ecs.read_storage::()).join() { + for (entity, body, pos) in ( + &ecs.entities(), + &ecs.read_storage::(), + &ecs.read_storage::(), + ) + .join() + .filter(|(_, _, e_pos)| (e_pos.0.distance_squared(cam_pos)) < SFX_DIST_LIMIT_SQR) + { match body { Body::Object(object::Body::CampfireLit) => { - if (pos.0.distance_squared(cam_pos)) < SFX_DIST_LIMIT_SQR { - if self.timer.elapsed().as_secs_f32() > 3.0 - /* TODO Replace with sensible time */ - { - self.timer = Instant::now(); - let sfx_trigger_item = triggers.get_trigger(&SfxEvent::LevelUp); - if sfx_trigger_item.is_some() { - println!("sound"); - ecs.read_resource::>().emit_now( - SfxEventItem::new( - SfxEvent::LevelUp.clone(), - Some(pos.0), - Some(0.0), - ), - ); - } - } + let state = self.event_history.entry(entity).or_default(); + + let mapped_event = SfxEvent::Campfire; + + // Check for SFX config entry for this movement + if Self::should_emit(state, triggers.get_key_value(&mapped_event)) { + sfx_emitter.emit(SfxEventItem::new( + mapped_event.clone(), + Some(pos.0), + Some(0.25), + )); + + state.time = Instant::now(); } + + // update state to determine the next event. We only record the time (above) if + // it was dispatched + state.event = mapped_event; }, _ => {}, } } + self.cleanup(player_entity); } } @@ -67,6 +92,41 @@ impl CampfireEventMapper { pub fn new() -> Self { Self { timer: Instant::now(), + event_history: HashMap::new(), + } + } + + /// As the player explores the world, we track the last event of the nearby + /// entities to determine the correct SFX item to play next based on + /// their activity. `cleanup` will remove entities from event tracking if + /// they have not triggered an event for > n seconds. This prevents + /// stale records from bloating the Map size. + fn cleanup(&mut self, player: EcsEntity) { + const TRACKING_TIMEOUT: u64 = 10; + + let now = Instant::now(); + self.event_history.retain(|entity, event| { + now.duration_since(event.time) < Duration::from_secs(TRACKING_TIMEOUT) + || entity.id() == player.id() + }); + } + + /// Ensures that: + /// 1. An sfx.ron entry exists for an SFX event + /// 2. The sfx has not been played since it's timeout threshold has elapsed, + /// which prevents firing every tick + fn should_emit( + previous_state: &PreviousEntityState, + sfx_trigger_item: Option<(&SfxEvent, &SfxTriggerItem)>, + ) -> bool { + if let Some((event, item)) = sfx_trigger_item { + if &previous_state.event == event { + previous_state.time.elapsed().as_secs_f64() >= item.threshold + } else { + true + } + } else { + false } } } diff --git a/voxygen/src/audio/sfx/event_mapper/combat/mod.rs b/voxygen/src/audio/sfx/event_mapper/combat/mod.rs index ff2c9b82e6..426a0db6c4 100644 --- a/voxygen/src/audio/sfx/event_mapper/combat/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/combat/mod.rs @@ -45,7 +45,7 @@ impl EventMapper for CombatEventMapper { player_entity: specs::Entity, camera: &Camera, triggers: &SfxTriggers, - terrain: &Terrain, + _terrain: &Terrain, ) { let ecs = state.ecs(); diff --git a/voxygen/src/audio/sfx/event_mapper/mod.rs b/voxygen/src/audio/sfx/event_mapper/mod.rs index c414d0c6af..a1e2419223 100644 --- a/voxygen/src/audio/sfx/event_mapper/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/mod.rs @@ -1,5 +1,5 @@ mod block; -//mod campfire; +mod campfire; mod combat; mod movement; mod progression; @@ -7,7 +7,7 @@ mod progression; use common::{state::State, terrain::TerrainChunk}; use block::BlockEventMapper; -//use campfire::CampfireEventMapper; +use campfire::CampfireEventMapper; use combat::CombatEventMapper; use movement::MovementEventMapper; use progression::ProgressionEventMapper; @@ -38,7 +38,7 @@ impl SfxEventMapper { Box::new(MovementEventMapper::new()), Box::new(ProgressionEventMapper::new()), Box::new(BlockEventMapper::new()), - //Box::new(CampfireEventMapper::new()), + Box::new(CampfireEventMapper::new()), ], } } diff --git a/voxygen/src/audio/sfx/event_mapper/movement/mod.rs b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs index 6c29dc4a89..6cd104c4d3 100644 --- a/voxygen/src/audio/sfx/event_mapper/movement/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs @@ -165,7 +165,7 @@ impl MovementEventMapper { ) -> SfxEvent { // Match run / roll / swim state if physics_state.in_fluid.is_some() - && physics_state.in_fluid.unwrap() < 2.0 + //&& physics_state.in_fluid.unwrap() < 2.0 // To control different sound based on depth && vel.magnitude() > 0.1 || !previous_state.in_water && physics_state.in_fluid.is_some() { diff --git a/voxygen/src/audio/sfx/mod.rs b/voxygen/src/audio/sfx/mod.rs index 1bc35411c4..b7992d9bae 100644 --- a/voxygen/src/audio/sfx/mod.rs +++ b/voxygen/src/audio/sfx/mod.rs @@ -135,8 +135,10 @@ impl SfxEventItem { #[derive(Clone, Debug, PartialEq, Deserialize, Hash, Eq)] pub enum SfxEvent { + Campfire, Embers, Birdcall, + Owl, Cricket, Frog, Bees,