use bevy::prelude::*;
#[cfg(all(feature = "bevy_kira_audio", not(feature = "firewheel")))]
mod kira_backend {
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
pub(crate) struct AudioPluginImpl;
impl Plugin for AudioPluginImpl {
fn build(&self, app: &mut App) {
app.add_plugins(AudioPlugin);
}
}
pub fn play_sound_impl(audio: &Audio, asset_server: &AssetServer, sound_path: &str) {
let sound_handle = asset_server.load(format!("audios/sfx/{}", sound_path));
audio.play(sound_handle);
}
pub fn play_bgm_impl(
audio: &Audio,
asset_server: &AssetServer,
music_path: &str,
) -> Handle<AudioInstance> {
let music_handle = asset_server.load(format!("audios/music/{}", music_path));
audio.play(music_handle).looped().handle()
}
}
#[cfg(all(feature = "bevy_kira_audio", not(feature = "firewheel")))]
pub use kira_backend::*;
#[cfg(feature = "firewheel")]
mod seedling_backend {
use bevy::prelude::*;
use bevy_seedling::prelude::*;
use bevy_seedling::sample::SampleQueueLifetime;
#[derive(PoolLabel, PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub struct BgmPool;
#[derive(PoolLabel, PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub struct SfxPool;
pub(crate) struct AudioPluginImpl;
impl Plugin for AudioPluginImpl {
fn build(&self, app: &mut App) {
let config = FirewheelConfig {
declick_seconds: 0.0,
..Default::default()
};
let plugin = SeedlingPlugin {
config,
..Default::default()
};
app.add_plugins(plugin)
.add_systems(Startup, setup_audio_pools);
}
}
fn setup_audio_pools(mut commands: Commands) {
commands.spawn((
Name::new("BGM Pool"),
SamplerPool(BgmPool),
PoolSize(4..=4), ));
commands.spawn((
Name::new("SFX Pool"),
SamplerPool(SfxPool),
PoolSize(64..=64), SamplerConfig {
num_declickers: 0,
..Default::default()
},
));
}
pub fn play_sound_impl(commands: &mut Commands, asset_server: &AssetServer, sound_path: &str) {
let sound_handle = asset_server.load(format!("audios/sfx/{}", sound_path));
commands.spawn((
Name::new(format!("SFX: {}", sound_path)),
SamplePlayer::new(sound_handle).with_volume(Volume::Decibels(0.0)), SfxPool,
SampleQueueLifetime(std::time::Duration::from_secs(10)),
));
}
pub fn play_bgm_impl(
commands: &mut Commands,
asset_server: &AssetServer,
music_path: &str,
) -> Entity {
let music_handle = asset_server.load(format!("audios/music/{}", music_path));
commands
.spawn((
Name::new(format!("BGM: {}", music_path)),
SamplePlayer::new(music_handle)
.looping()
.with_volume(Volume::Decibels(0.0)), BgmPool,
))
.id()
}
}
#[cfg(feature = "firewheel")]
pub use seedling_backend::*;
pub(crate) struct AudioPlugin;
impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(AudioPluginImpl);
}
}
#[cfg(all(feature = "bevy_kira_audio", not(feature = "firewheel")))]
pub fn play_sound(audio: &bevy_kira_audio::Audio, asset_server: &AssetServer, sound_path: &str) {
play_sound_impl(audio, asset_server, sound_path);
}
#[cfg(feature = "firewheel")]
pub fn play_sound(commands: &mut Commands, asset_server: &AssetServer, sound_path: &str) {
play_sound_impl(commands, asset_server, sound_path);
}
#[cfg(all(feature = "bevy_kira_audio", not(feature = "firewheel")))]
pub fn play_bgm(
audio: &bevy_kira_audio::Audio,
asset_server: &AssetServer,
music_path: &str,
) -> Handle<bevy_kira_audio::AudioInstance> {
play_bgm_impl(audio, asset_server, music_path)
}
#[cfg(feature = "firewheel")]
pub fn play_bgm(commands: &mut Commands, asset_server: &AssetServer, music_path: &str) -> Entity {
play_bgm_impl(commands, asset_server, music_path)
}