From 66f35a5efeec0391deda3d8be6327809f278c051 Mon Sep 17 00:00:00 2001 From: RockasMockas <6405564+RockasMockas@users.noreply.github.com> Date: Sat, 24 Aug 2024 05:01:17 +0200 Subject: [PATCH] feat: Integrating AudioCenter into bones (#445) Moved AudioCenter out of jumpy -> bones. Reworked to remove the dependency on using a game's settings struct directly + streamlining the interfaces such as having both play_sound and play_music be super easy to use, with extra _advanced and _custom methods for different levels of advanced features/usability on music. Tested both music/sounds work. --- Cargo.lock | 28 +- framework_crates/bones_framework/Cargo.toml | 2 +- framework_crates/bones_framework/src/audio.rs | 43 +++ .../bones_framework/src/audio/audio_center.rs | 348 ++++++++++++++++++ .../audio.rs => audio/audio_manager.rs} | 20 +- framework_crates/bones_framework/src/lib.rs | 5 +- .../bones_framework/src/render.rs | 5 +- 7 files changed, 419 insertions(+), 32 deletions(-) create mode 100644 framework_crates/bones_framework/src/audio.rs create mode 100644 framework_crates/bones_framework/src/audio/audio_center.rs rename framework_crates/bones_framework/src/{render/audio.rs => audio/audio_manager.rs} (89%) diff --git a/Cargo.lock b/Cargo.lock index a275607be0..358c9ce48f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -392,12 +392,6 @@ dependencies = [ "syn 2.0.71", ] -[[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 = "1.0.3" @@ -3207,9 +3201,9 @@ dependencies = [ [[package]] name = "glam" -version = "0.25.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3" +checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94" dependencies = [ "mint", ] @@ -4169,17 +4163,18 @@ dependencies = [ [[package]] name = "kira" -version = "0.8.7" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8968f1eda49cdf4f6406fd5ffe590c3ca2778a1b0e50b5684974b138a99dfb2f" +checksum = "5c48d4719537e9af8467214f42c76425afbde24fb33e8d06686ab764e2a98e0b" dependencies = [ - "atomic-arena", "cpal", - "glam 0.25.0", + "glam 0.28.0", "mint", + "paste", "ringbuf", "send_wrapper", "symphonia", + "triple_buffer", ] [[package]] @@ -7439,6 +7434,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "triple_buffer" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e66931c8eca6381f0d34656a9341f09bd462010488c1a3bc0acd3f2d08dffce" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "try-lock" version = "0.2.5" diff --git a/framework_crates/bones_framework/Cargo.toml b/framework_crates/bones_framework/Cargo.toml index eb67bf0e67..766498f60d 100644 --- a/framework_crates/bones_framework/Cargo.toml +++ b/framework_crates/bones_framework/Cargo.toml @@ -105,7 +105,7 @@ egui_plot = "0.23" ttf-parser = { version = "0.24", default-features = false, optional = true } # Audio -kira = { version = "0.8.6", features = ["cpal"], default-features = false, optional = true } +kira = { version = "0.9.4", features = ["cpal"], default-features = false, optional = true } # Localization fluent = { version = "0.15", optional = true } diff --git a/framework_crates/bones_framework/src/audio.rs b/framework_crates/bones_framework/src/audio.rs new file mode 100644 index 0000000000..c0ac4b6967 --- /dev/null +++ b/framework_crates/bones_framework/src/audio.rs @@ -0,0 +1,43 @@ +//! Audio session, systems, and resources. + +pub mod audio_center; +pub mod audio_manager; + +use crate::prelude::*; +pub use audio_center::*; +pub use audio_manager::*; +pub use kira; +pub use kira::sound::static_sound::StaticSoundData; +use kira::sound::static_sound::StaticSoundHandle; + +/// Name of the default bones audio session +pub const DEFAULT_BONES_AUDIO_SESSION: &str = "BONES_AUDIO"; + +/// Sets up audio-related resources and the default bones audio session +pub fn game_plugin(game: &mut Game) { + AudioSource::register_schema(); + game.init_shared_resource::(); + game.insert_shared_resource(AudioManager::default()); + game.init_shared_resource::(); + + let session = game.sessions.create(DEFAULT_BONES_AUDIO_SESSION); + // Audio doesn't do any rendering + session.visible = false; + session + .stages + .add_system_to_stage(First, _process_audio_events) + .add_system_to_stage(Last, _kill_finished_audios); +} + +/// Holds the handles and the volume to be played for a piece of Audio. +#[derive(HasSchema)] +#[schema(no_clone, no_default, opaque)] +#[repr(C)] +pub struct Audio { + /// The handle for the audio. + handle: StaticSoundHandle, + /// The original volume requested for the audio. + volume: f64, + /// The bones handle for the audio source. + bones_handle: Handle, +} diff --git a/framework_crates/bones_framework/src/audio/audio_center.rs b/framework_crates/bones_framework/src/audio/audio_center.rs new file mode 100644 index 0000000000..9440ea832c --- /dev/null +++ b/framework_crates/bones_framework/src/audio/audio_center.rs @@ -0,0 +1,348 @@ +//! Audio Center resource and systems. + +use super::{Audio, AudioManager, AudioSource}; +use crate::prelude::*; +use kira; +use kira::{ + sound::{static_sound::StaticSoundSettings, PlaybackState}, + tween, + tween::Tween, + Volume, +}; +use std::collections::VecDeque; +use std::time::Duration; +use tracing::warn; + +/// A resource that can be used to control game audios. +#[derive(HasSchema)] +#[schema(no_clone)] +pub struct AudioCenter { + /// Buffer for audio events that have not yet been processed. + events: VecDeque, + /// The handle to the current music. + music: Option