From 29405c7307a4b993130a88e3c4696b1e640cb5d5 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 23 May 2022 13:35:25 -0500 Subject: [PATCH] Port to Synthizer. --- Cargo.toml | 4 +- src/core.rs | 2 +- src/lib.rs | 2 +- src/pitch_shift.rs | 8 +-- src/sound/footstep.rs | 106 ++++++++++++++++++++++++++++-------- src/sound/icon.rs | 124 ++++++++++++++++++++++++++++++++---------- src/sound/mod.rs | 13 ++--- 7 files changed, 192 insertions(+), 67 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f51fa31..0fb9114 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ features = [ [dependencies] backtrace = "0.3" bevy_input_actionmap = { git = "https://github.com/lightsoutgames/bevy_input_actionmap" } -bevy_openal = { git = "https://github.com/lightsoutgames/bevy_openal" } bevy_rapier2d = "0.13" +bevy_synthizer = { git = "https://labs.lightsout.games/projects/bevy_synthizer" } bevy_tts = { git = "https://github.com/lightsoutgames/bevy_tts", default-features = false, features = ["tolk"] } coord_2d = "0.3" futures-lite = "1" @@ -36,6 +36,6 @@ maze_generator = "2" once_cell = "1" pathfinding = "3" rand = "0.8" -sentry = "0.25" +sentry = "0.26" serde = "1" shadowcast = "0.8" \ No newline at end of file diff --git a/src/core.rs b/src/core.rs index 18c7f80..33266b2 100644 --- a/src/core.rs +++ b/src/core.rs @@ -586,7 +586,7 @@ impl PluginGroup fn build(&mut self, group: &mut bevy::app::PluginGroupBuilder) { group .add(crate::bevy_tts::TtsPlugin) - .add(crate::bevy_openal::OpenAlPlugin) + .add(crate::bevy_synthizer::SynthizerPlugin) .add(CorePlugin::::default()); } } diff --git a/src/lib.rs b/src/lib.rs index 9d86d96..354f1a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ #![allow(clippy::type_complexity)] pub use bevy_input_actionmap; -pub use bevy_openal; +pub use bevy_synthizer; pub use bevy_rapier2d; pub use bevy_tts; pub mod commands; diff --git a/src/pitch_shift.rs b/src/pitch_shift.rs index b14bb41..0ab6a2c 100644 --- a/src/pitch_shift.rs +++ b/src/pitch_shift.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use bevy::prelude::*; use crate::{ - bevy_openal::{Listener, Sound}, + bevy_synthizer::{Listener, Sound}, commands::RunIfExistsExt, sound::SoundIcon, }; @@ -14,7 +14,7 @@ struct Behind; #[derive(Clone, Copy, Debug)] pub struct PitchShiftConfig { pub downshift_behind: bool, - pub downshift: f32, + pub downshift: f64, } impl Default for PitchShiftConfig { @@ -64,10 +64,10 @@ fn tag_behind( } #[derive(Clone, Debug, Default, Deref, DerefMut)] -struct LastIconPitch(HashMap); +struct LastIconPitch(HashMap); #[derive(Clone, Debug, Default, Deref, DerefMut)] -struct LastSoundPitch(HashMap); +struct LastSoundPitch(HashMap); fn behind_added( config: Res, diff --git a/src/sound/footstep.rs b/src/sound/footstep.rs index a372771..7f53f94 100644 --- a/src/sound/footstep.rs +++ b/src/sound/footstep.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use bevy::{asset::HandleId, prelude::*, transform::TransformSystem}; -use bevy_openal::{Buffer, Sound, SoundState}; +use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound}; use rand::random; use crate::{commands::RunIfExistsExt, core::PointLike}; @@ -11,12 +11,12 @@ use crate::{commands::RunIfExistsExt, core::PointLike}; pub struct Footstep { pub sound: HandleId, pub step_length: f32, - pub gain: f32, - pub reference_distance: f32, - pub max_distance: f32, - pub rolloff_factor: f32, - pub pitch: f32, - pub pitch_variation: Option, + pub gain: f64, + pub reference_distance: Option, + pub max_distance: Option, + pub rolloff: Option, + pub pitch: Option, + pub pitch_variation: Option, } impl Default for Footstep { @@ -25,10 +25,10 @@ impl Default for Footstep { sound: "".into(), step_length: 0.8, gain: 1., - reference_distance: 1., - max_distance: f32::MAX, - rolloff_factor: 1., - pitch: 1., + reference_distance: None, + max_distance: None, + rolloff: None, + pitch: None, pitch_variation: Some(0.15), } } @@ -43,15 +43,15 @@ pub struct FootstepBundle { fn added( mut commands: Commands, + asset_server: Res, footsteps: Query<(Entity, &Footstep), Added>, - assets: Res>, ) { for (entity, footstep) in footsteps.iter() { - let buffer = assets.get_handle(footstep.sound); + let buffer = asset_server.get_handle(footstep.sound); commands.run_if_exists(entity, move |mut entity| { entity.insert(Sound { buffer, - state: SoundState::Stopped, + paused: true, ..default() }); }); @@ -59,27 +59,87 @@ fn added( } fn update( + mut commands: Commands, mut last_step_distance: Local>, - mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed>, + mut footsteps: Query< + ( + Entity, + &Footstep, + &Parent, + &mut Sound, + Option<&DistanceRef>, + Option<&DistanceMax>, + Option<&Rolloff>, + ), + Changed, + >, transforms_storage: Query<&Transform>, ) { - for (entity, footstep, parent, mut sound) in footsteps.iter_mut() { + for (entity, footstep, parent, mut sound, reference_distance, max_distance, rolloff) in + footsteps.iter_mut() + { let coordinates = transforms_storage.get(**parent).unwrap(); if let Some(last) = last_step_distance.get(&entity) { let distance = last.0 + (last.1.distance(coordinates)); if distance >= footstep.step_length { last_step_distance.insert(entity, (0., *coordinates)); sound.gain = footstep.gain; - sound.reference_distance = footstep.reference_distance; - sound.max_distance = footstep.max_distance; - sound.rolloff_factor = footstep.rolloff_factor; - sound.pitch = footstep.pitch; + if let Some(v) = footstep.reference_distance { + let insert = if let Some(v2) = reference_distance { + v != **v2 + } else { + true + }; + if insert { + commands.run_if_exists(entity, move |mut entity| { + entity.insert(DistanceRef(v)); + }) + } + } else if reference_distance.is_some() { + commands.run_if_exists(entity, |mut entity| { + entity.remove::(); + }); + } + if let Some(v) = footstep.max_distance { + let insert = if let Some(v2) = max_distance { + v != **v2 + } else { + true + }; + if insert { + commands.run_if_exists(entity, move |mut entity| { + entity.insert(DistanceMax(v)); + }) + } + } else if max_distance.is_some() { + commands.run_if_exists(entity, |mut entity| { + entity.remove::(); + }); + } + if let Some(v) = footstep.rolloff { + let insert = if let Some(v2) = rolloff { + v != **v2 + } else { + true + }; + if insert { + commands.run_if_exists(entity, move |mut entity| { + entity.insert(Rolloff(v)); + }) + } + } else if rolloff.is_some() { + commands.run_if_exists(entity, |mut entity| { + entity.remove::(); + }); + } + sound.pitch = footstep.pitch.unwrap_or(1.); if let Some(pitch_variation) = footstep.pitch_variation { - let mut pitch = footstep.pitch - pitch_variation / 2.; - pitch += random::() * pitch_variation; + let mut pitch = sound.pitch - pitch_variation / 2.; + pitch += random::() * pitch_variation; sound.pitch = pitch; } - sound.play(); + sound.paused = false; + sound.restart = true; } else if last.1 != *coordinates { last_step_distance.insert(entity, (distance, *coordinates)); } diff --git a/src/sound/icon.rs b/src/sound/icon.rs index 52da287..b042c16 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -1,7 +1,7 @@ use std::{fmt::Debug, hash::Hash, time::Duration}; use bevy::{asset::HandleId, prelude::*, transform::TransformSystem}; -use bevy_openal::{Buffer, Sound, SoundState}; +use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound}; use rand::random; @@ -15,11 +15,11 @@ use crate::{ #[derive(Component, Clone, Debug)] pub struct SoundIcon { pub sound: HandleId, - pub gain: f32, - pub pitch: f32, - pub reference_distance: f32, - pub max_distance: f32, - pub rolloff_factor: f32, + pub gain: f64, + pub pitch: f64, + pub reference_distance: Option, + pub max_distance: Option, + pub rolloff: Option, pub interval: Option, } @@ -30,9 +30,9 @@ impl Default for SoundIcon { sound: "".into(), gain: 0.3, pitch: 1., - reference_distance: 1., - max_distance: f32::MAX, - rolloff_factor: 1., + reference_distance: None, + max_distance: None, + rolloff: None, interval: Some(Timer::from_seconds(seconds, true)), }; if let Some(ref mut interval) = icon.interval { @@ -50,36 +50,43 @@ pub struct SoundIconBundle { pub global_transform: GlobalTransform, } -fn add_sound_icon_sounds( +fn added( mut commands: Commands, + asset_server: Res, icons: Query<(Entity, &SoundIcon), Added>, - assets: Res>, ) { for (entity, icon) in icons.iter() { - let buffer = assets.get_handle(icon.sound); + let buffer = asset_server.get_handle(icon.sound); let gain = icon.gain; let pitch = icon.pitch; let looping = icon.interval.is_none(); let reference_distance = icon.reference_distance; let max_distance = icon.max_distance; - let rolloff_factor = icon.rolloff_factor; + let rolloff = icon.rolloff; commands.run_if_exists(entity, move |mut entity| { entity.insert(Sound { buffer, gain, pitch, looping, - state: SoundState::Stopped, - reference_distance, - max_distance, - rolloff_factor, + paused: true, ..default() }); + if let Some(v) = reference_distance { + entity.insert(DistanceRef(v)); + } + if let Some(v) = max_distance { + entity.insert(DistanceMax(v)); + } + if let Some(v) = rolloff { + entity.insert(Rolloff(v)); + } }); } } fn update( + mut commands: Commands, config: Res>, state: Res>, time: Res