From c3313fda31c049b67dcbff6501743254a4075f6a Mon Sep 17 00:00:00 2001 From: Zakru <23387227+Zakru@users.noreply.github.com> Date: Fri, 21 Jul 2023 23:09:29 +0300 Subject: [PATCH 1/9] Add spatial audio effects Replace `bevy_audio` with `bevy_kira_audio`. Sounds are unedited/derived from CC0 sounds from FreeSound.org. Works towards #394, although many vital effects are still missing. --- .gitattributes | 1 + Cargo.lock | 132 +++++++++++++++++++---- Cargo.toml | 29 ++++- assets/audio/sounds/construct.ogg | 3 + assets/audio/sounds/destruction.ogg | 3 + assets/audio/sounds/manufacture.ogg | 3 + crates/audio/Cargo.toml | 2 + crates/audio/src/lib.rs | 6 +- crates/audio/src/music.rs | 16 +-- crates/audio/src/spatial.rs | 128 ++++++++++++++++++++++ crates/camera/src/camera.rs | 6 +- crates/camera/src/lib.rs | 4 +- crates/construction/Cargo.toml | 3 + crates/construction/src/manufacturing.rs | 32 +++++- crates/controller/Cargo.toml | 3 + crates/controller/src/draft.rs | 31 +++++- crates/spawner/Cargo.toml | 3 + crates/spawner/src/destroyer.rs | 75 +++++++++++-- src/main.rs | 2 + 19 files changed, 435 insertions(+), 47 deletions(-) create mode 100644 assets/audio/sounds/construct.ogg create mode 100644 assets/audio/sounds/destruction.ogg create mode 100644 assets/audio/sounds/manufacture.ogg create mode 100644 crates/audio/src/spatial.rs diff --git a/.gitattributes b/.gitattributes index f872481a..a725c82c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ assets/audio/**/*.mp3 filter=lfs diff=lfs merge=lfs -text +assets/audio/**/*.ogg filter=lfs diff=lfs merge=lfs -text assets/fonts/**/*.ttf filter=lfs diff=lfs merge=lfs -text assets/maps/* filter=lfs diff=lfs merge=lfs -text assets/models/* filter=lfs diff=lfs merge=lfs -text diff --git a/Cargo.lock b/Cargo.lock index 6df570b7..8f214253 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -669,6 +669,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atomic-arena" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5450eca8ce5abcfd5520727e975ebab30ccca96030550406b0ca718b224ead10" + [[package]] name = "atomic-polyfill" version = "0.1.11" @@ -1045,6 +1051,19 @@ dependencies = [ "bevy_winit", ] +[[package]] +name = "bevy_kira_audio" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "468257f6c7980d847aba0e9b6c9e4ff4045598510637d6e484b5f290c00a1f92" +dependencies = [ + "anyhow", + "bevy", + "kira", + "parking_lot 0.12.1", + "thiserror", +] + [[package]] name = "bevy_log" version = "0.10.1" @@ -2095,6 +2114,8 @@ name = "de_audio" version = "0.1.0-dev" dependencies = [ "bevy", + "bevy_kira_audio", + "de_camera", "de_conf", "de_core", "iyes_progress", @@ -2188,12 +2209,15 @@ version = "0.1.0-dev" dependencies = [ "ahash 0.7.6", "bevy", + "bevy_kira_audio", + "de_audio", "de_core", "de_index", "de_objects", "de_pathing", "de_signs", "de_spawner", + "iyes_progress", "parry2d", "parry3d", ] @@ -2204,6 +2228,8 @@ version = "0.1.0-dev" dependencies = [ "ahash 0.7.6", "bevy", + "bevy_kira_audio", + "de_audio", "de_behaviour", "de_camera", "de_combat", @@ -2221,6 +2247,7 @@ dependencies = [ "de_terrain", "enum-map", "glam", + "iyes_progress", "parry2d", "parry3d", ] @@ -2255,6 +2282,7 @@ name = "de_game" version = "0.1.0-dev" dependencies = [ "bevy", + "bevy_kira_audio", "de_audio", "de_behaviour", "de_camera", @@ -2517,12 +2545,15 @@ version = "0.1.0-dev" dependencies = [ "ahash 0.7.6", "bevy", + "bevy_kira_audio", + "de_audio", "de_core", "de_energy", "de_index", "de_map", "de_objects", "de_terrain", + "iyes_progress", "parry2d", "parry3d", ] @@ -3707,6 +3738,18 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "kira" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e53aef6f6ec851da68a0ad6d6283bd99ea1a9164f38fd7ac353e28ccd6f5cda" +dependencies = [ + "atomic-arena", + "cpal", + "ringbuf", + "symphonia", +] + [[package]] name = "kqueue" version = "1.0.7" @@ -3763,17 +3806,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" -[[package]] -name = "lewton" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777b48df9aaab155475a83a7df3070395ea1ac6902f5cd062b8f2b028075c030" -dependencies = [ - "byteorder", - "ogg", - "tinyvec", -] - [[package]] name = "libc" version = "0.2.147" @@ -4368,15 +4400,6 @@ dependencies = [ "cc", ] -[[package]] -name = "ogg" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6951b4e8bf21c8193da321bcce9c9dd2e13c858fe078bf9054a288b419ae5d6e" -dependencies = [ - "byteorder", -] - [[package]] name = "once_cell" version = "1.18.0" @@ -5020,6 +5043,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "ringbuf" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79abed428d1fd2a128201cec72c5f6938e2da607c6f3745f769fabea399d950a" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "robust" version = "0.2.3" @@ -5033,8 +5065,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdf1d4dea18dff2e9eb6dca123724f8b60ef44ad74a9ad283cdfe025df7e73fa" dependencies = [ "cpal", - "lewton", - "symphonia", ] [[package]] @@ -5588,7 +5618,11 @@ checksum = "62e48dba70095f265fdb269b99619b95d04c89e619538138383e63310b14d941" dependencies = [ "lazy_static", "symphonia-bundle-mp3", + "symphonia-codec-pcm", + "symphonia-codec-vorbis", "symphonia-core", + "symphonia-format-ogg", + "symphonia-format-wav", "symphonia-metadata", ] @@ -5605,6 +5639,27 @@ dependencies = [ "symphonia-metadata", ] +[[package]] +name = "symphonia-codec-pcm" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47f1fbd220a06a641c8ce2ddad10f5ef6ee5cc0c54d9044d25d43b0d3119deaa" +dependencies = [ + "log", + "symphonia-core", +] + +[[package]] +name = "symphonia-codec-vorbis" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3953397e3506aa01350c4205817e4f95b58d476877a42f0458d07b665749e203" +dependencies = [ + "log", + "symphonia-core", + "symphonia-utils-xiph", +] + [[package]] name = "symphonia-core" version = "0.5.3" @@ -5618,6 +5673,29 @@ dependencies = [ "log", ] +[[package]] +name = "symphonia-format-ogg" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bf1a00ccd11452d44048a0368828040f778ae650418dbd9d8765b7ee2574c8d" +dependencies = [ + "log", + "symphonia-core", + "symphonia-metadata", + "symphonia-utils-xiph", +] + +[[package]] +name = "symphonia-format-wav" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da76614728fa27c003bdcdfbac51396bd8fcbf94c95fe8e62f1d2bac58ef03a4" +dependencies = [ + "log", + "symphonia-core", + "symphonia-metadata", +] + [[package]] name = "symphonia-metadata" version = "0.5.3" @@ -5630,6 +5708,16 @@ dependencies = [ "symphonia-core", ] +[[package]] +name = "symphonia-utils-xiph" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a450ca645b80d69aff8b35576cbfdc7f20940b29998202aab910045714c951f8" +dependencies = [ + "symphonia-core", + "symphonia-metadata", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index fad181f3..891511f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ de_terrain.workspace = true # Other bevy.workspace = true +bevy_kira_audio.workspace = true tracing.workspace = true [workspace] @@ -111,7 +112,7 @@ assert_cmd = "2.0.10" async-compat = "0.2.1" async-std = "1.11" async-tar = "0.4.2" -bevy = { version = "0.10", features = ["mp3"] } +bevy_kira_audio = { version = "0.15.0", features = ["mp3", "wav"] } bincode = "2.0.0-rc.3" chrono = "0.4.24" clap = { version = "4.0", features = ["derive"] } @@ -154,3 +155,29 @@ tracing-subscriber = { version = "0.3.17", features = ["json"] } trybuild = "1.0.80" url = { version = "2.3.1", features = ["serde"] } urlencoding = "2.1.2" + +[workspace.dependencies.bevy] +version = "0.10" +default-features = false +features = [ + "animation", + "bevy_asset", + "bevy_gilrs", + "bevy_scene", + "bevy_winit", + "bevy_core_pipeline", + "bevy_pbr", + "bevy_gltf", + "bevy_render", + "bevy_sprite", + "bevy_text", + "bevy_ui", + "png", + "hdr", + "ktx2", + "zstd", + "x11", + "filesystem_watcher", + "android_shared_stdcxx", + "tonemapping_luts" +] diff --git a/assets/audio/sounds/construct.ogg b/assets/audio/sounds/construct.ogg new file mode 100644 index 00000000..cb0ab916 --- /dev/null +++ b/assets/audio/sounds/construct.ogg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ff3c63efcbee88d0a1dc2beb0f69dd54c43f23ac7e86377471b7974ef450353 +size 79121 diff --git a/assets/audio/sounds/destruction.ogg b/assets/audio/sounds/destruction.ogg new file mode 100644 index 00000000..360368da --- /dev/null +++ b/assets/audio/sounds/destruction.ogg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a2db392df94fa3d31a8649339ec2f1454177582fd704b185c2d1061e5aefa72 +size 164193 diff --git a/assets/audio/sounds/manufacture.ogg b/assets/audio/sounds/manufacture.ogg new file mode 100644 index 00000000..77db5630 --- /dev/null +++ b/assets/audio/sounds/manufacture.ogg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0fc7b0e1b41ec37ef9d136f8f0c376ff9d806ccfac1e36c3ef5d2cac2a5fa1f9 +size 50650 diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 7fa6c924..8eaedd6b 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -13,9 +13,11 @@ categories.workspace = true [dependencies] # DE +de_camera.workspace = true de_conf.workspace = true de_core.workspace = true # Other bevy.workspace = true +bevy_kira_audio.workspace = true iyes_progress.workspace = true diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index e66d7d03..8e6a53ed 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -1,13 +1,17 @@ use bevy::{app::PluginGroupBuilder, prelude::PluginGroup}; use crate::music::MusicPlugin; +use crate::spatial::SpatialSoundPlugin; mod music; +pub mod spatial; pub struct AudioPluginGroup; impl PluginGroup for AudioPluginGroup { fn build(self) -> PluginGroupBuilder { - PluginGroupBuilder::start::().add(MusicPlugin) + PluginGroupBuilder::start::() + .add(MusicPlugin) + .add(SpatialSoundPlugin) } } diff --git a/crates/audio/src/music.rs b/crates/audio/src/music.rs index 7c0d3044..d62c2888 100644 --- a/crates/audio/src/music.rs +++ b/crates/audio/src/music.rs @@ -1,4 +1,8 @@ use bevy::{asset::LoadState, prelude::*}; +use bevy_kira_audio::{ + prelude::{Audio as KAudio, AudioSource as KAudioSource, Volume}, + AudioControl, +}; use de_conf::Configuration; use de_core::state::AppState; use iyes_progress::prelude::*; @@ -14,7 +18,7 @@ impl Plugin for MusicPlugin { } #[derive(Resource)] -struct Tracks(Handle); +struct Tracks(Handle); fn setup(mut commands: Commands, server: Res) { commands.insert_resource(Tracks(server.load("audio/music/menu_loop.mp3"))); @@ -28,12 +32,12 @@ fn load(server: Res, tracks: Res) -> Progress { } } -fn start(audio: Res