bones_framework/
audio.rs

1//! Audio session, systems, and resources.
2
3pub mod audio_center;
4pub mod audio_manager;
5
6use crate::prelude::*;
7pub use audio_center::*;
8pub use audio_manager::*;
9pub use kira;
10pub use kira::sound::static_sound::StaticSoundData;
11use kira::sound::static_sound::StaticSoundHandle;
12
13/// Name of the default bones audio session
14pub const DEFAULT_BONES_AUDIO_SESSION: &str = "BONES_AUDIO";
15
16/// Sets up audio-related resources and the default bones audio session
17pub fn game_plugin(game: &mut Game) {
18    AudioSource::register_schema();
19    game.init_shared_resource::<AudioCenter>();
20    game.insert_shared_resource(AudioManager::default());
21    game.init_shared_resource::<AssetServer>();
22
23    let mut session = SessionBuilder::new(DEFAULT_BONES_AUDIO_SESSION);
24    // Audio doesn't do any rendering
25    session.visible = false;
26    session
27        .add_system_to_stage(First, _process_audio_events)
28        .add_system_to_stage(Last, _kill_finished_audios);
29    session.finish_and_add(&mut game.sessions);
30}
31
32/// Holds the handles and the volume to be played for a piece of Audio.
33#[derive(HasSchema)]
34#[schema(no_clone, no_default, opaque)]
35#[repr(C)]
36pub struct Audio {
37    /// The handle for the audio.
38    handle: StaticSoundHandle,
39    /// The original volume requested for the audio.
40    volume: f64,
41    /// The bones handle for the audio source.
42    bones_handle: Handle<AudioSource>,
43}