From 619c8951a606e1cfcebdf2848048a30a4a84e0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Mon, 19 Sep 2022 23:21:26 +0200 Subject: [PATCH 01/26] basic spatial audio support from rodio --- Cargo.toml | 10 ++ crates/bevy_audio/Cargo.toml | 2 + crates/bevy_audio/src/audio.rs | 129 ++++++++++++++- crates/bevy_audio/src/audio_output.rs | 174 +++++++------------- crates/bevy_audio/src/lib.rs | 7 +- crates/bevy_audio/src/sinks.rs | 224 ++++++++++++++++++++++++++ examples/README.md | 1 + examples/audio/audio_control.rs | 2 +- examples/audio/spatial_audio.rs | 95 +++++++++++ 9 files changed, 521 insertions(+), 123 deletions(-) create mode 100644 crates/bevy_audio/src/sinks.rs create mode 100644 examples/audio/spatial_audio.rs diff --git a/Cargo.toml b/Cargo.toml index b810a3fa4aa2c..c5c19e0a0b082 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -814,6 +814,16 @@ description = "Shows how to create and register a custom audio source by impleme category = "Audio" wasm = true +[[example]] +name = "spatial_audio" +path = "examples/audio/spatial_audio.rs" + +[package.metadata.example.spatial_audio] +name = "Spatial Audio" +description = "Shows how to play spacial audio, and moving the emitter" +category = "Audio" +wasm = true + # Diagnostics [[example]] name = "log_diagnostics" diff --git a/crates/bevy_audio/Cargo.toml b/crates/bevy_audio/Cargo.toml index 8ea7dee8d63b9..bea3041c61c87 100644 --- a/crates/bevy_audio/Cargo.toml +++ b/crates/bevy_audio/Cargo.toml @@ -13,7 +13,9 @@ keywords = ["bevy"] bevy_app = { path = "../bevy_app", version = "0.9.0" } bevy_asset = { path = "../bevy_asset", version = "0.9.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" } +bevy_math = { path = "../bevy_math", version = "0.9.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.9.0", features = ["bevy"] } +bevy_transform = { path = "../bevy_transform", version = "0.9.0" } bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # other diff --git a/crates/bevy_audio/src/audio.rs b/crates/bevy_audio/src/audio.rs index d7ac8d615795b..34d3fc1452967 100644 --- a/crates/bevy_audio/src/audio.rs +++ b/crates/bevy_audio/src/audio.rs @@ -1,6 +1,8 @@ -use crate::{AudioSink, AudioSource, Decodable}; +use crate::{AudioSink, AudioSource, Decodable, SpatialAudioSink}; use bevy_asset::{Asset, Handle, HandleId}; use bevy_ecs::system::Resource; +use bevy_math::Vec3; +use bevy_transform::prelude::Transform; use parking_lot::RwLock; use std::{collections::VecDeque, fmt}; @@ -83,6 +85,7 @@ where settings: PlaybackSettings::ONCE, sink_handle: id, source_handle: audio_source, + spatial: None, }; self.queue.write().push_back(config); Handle::::weak(id) @@ -115,10 +118,126 @@ where settings, sink_handle: id, source_handle: audio_source, + spatial: None, }; self.queue.write().push_back(config); Handle::::weak(id) } + + /// Play audio from a [`Handle`] to the audio source, placing the listener at the given + /// transform, an ear on each side separated by `gap`. The audio emitter will placed at + /// `emitter`. + /// + /// bevy_audio is not using HRTF for spatial audio, but is transforming the sound to a mono + /// track, and then changing the level of each stereo channel according to the distance between + /// the emitter and each ear. + /// + /// ``` + /// # use bevy_ecs::system::Res; + /// # use bevy_asset::AssetServer; + /// # use bevy_audio::Audio; + /// fn play_spatial_audio_system(asset_server: Res, audio: Res