2020-07-16 20:46:51 +00:00
|
|
|
use anyhow::Result;
|
2023-01-17 22:42:00 +00:00
|
|
|
use bevy_asset::{Asset, AssetLoader, LoadContext, LoadedAsset};
|
2020-11-28 00:39:59 +00:00
|
|
|
use bevy_reflect::TypeUuid;
|
2020-10-20 00:29:31 +00:00
|
|
|
use bevy_utils::BoxedFuture;
|
2020-10-18 20:48:15 +00:00
|
|
|
use std::{io::Cursor, sync::Arc};
|
2020-07-16 20:46:51 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A source of audio data
|
2020-10-18 20:48:15 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid)]
|
|
|
|
#[uuid = "7a14806a-672b-443b-8d16-4f18afefa463"]
|
2020-07-16 20:46:51 +00:00
|
|
|
pub struct AudioSource {
|
2022-12-11 18:10:02 +00:00
|
|
|
/// Raw data of the audio source.
|
|
|
|
///
|
|
|
|
/// The data must be one of the file formats supported by Bevy (`wav`, `ogg`, `flac`, or `mp3`).
|
|
|
|
/// It is decoded using [`rodio::decoder::Decoder`](https://docs.rs/rodio/latest/rodio/decoder/struct.Decoder.html).
|
|
|
|
///
|
|
|
|
/// The decoder has conditionally compiled methods
|
|
|
|
/// depending on the features enabled.
|
|
|
|
/// If the format used is not enabled,
|
|
|
|
/// then this will panic with an `UnrecognizedFormat` error.
|
2020-09-26 04:34:47 +00:00
|
|
|
pub bytes: Arc<[u8]>,
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for AudioSource {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
/// Loads files as [`AudioSource`] [`Assets`](bevy_asset::Assets)
|
|
|
|
///
|
|
|
|
/// This asset loader supports different audio formats based on the enable Bevy features.
|
|
|
|
/// The feature `bevy/vorbis` enables loading from `.ogg` files and is enabled by default.
|
|
|
|
/// Other file endings can be loaded from with additional features:
|
|
|
|
/// `.mp3` with `bevy/mp3`
|
|
|
|
/// `.flac` with `bevy/flac`
|
|
|
|
/// `.wav` with `bevy/wav`
|
2020-07-16 20:46:51 +00:00
|
|
|
#[derive(Default)]
|
2022-01-05 22:30:15 +00:00
|
|
|
pub struct AudioLoader;
|
2020-07-16 20:46:51 +00:00
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
impl AssetLoader for AudioLoader {
|
2020-10-20 00:29:31 +00:00
|
|
|
fn load(&self, bytes: &[u8], load_context: &mut LoadContext) -> BoxedFuture<Result<()>> {
|
2020-10-18 20:48:15 +00:00
|
|
|
load_context.set_default_asset(LoadedAsset::new(AudioSource {
|
2020-09-26 04:34:47 +00:00
|
|
|
bytes: bytes.into(),
|
2020-10-18 20:48:15 +00:00
|
|
|
}));
|
2020-10-20 00:29:31 +00:00
|
|
|
Box::pin(async move { Ok(()) })
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|
2020-07-28 21:24:03 +00:00
|
|
|
|
2020-07-16 20:46:51 +00:00
|
|
|
fn extensions(&self) -> &[&str] {
|
2021-06-03 19:58:08 +00:00
|
|
|
&[
|
|
|
|
#[cfg(feature = "mp3")]
|
|
|
|
"mp3",
|
|
|
|
#[cfg(feature = "flac")]
|
|
|
|
"flac",
|
|
|
|
#[cfg(feature = "wav")]
|
|
|
|
"wav",
|
|
|
|
#[cfg(feature = "vorbis")]
|
2022-05-09 13:37:40 +00:00
|
|
|
"oga",
|
|
|
|
#[cfg(feature = "vorbis")]
|
2021-06-03 19:58:08 +00:00
|
|
|
"ogg",
|
2022-05-09 13:37:40 +00:00
|
|
|
#[cfg(feature = "vorbis")]
|
|
|
|
"spx",
|
2021-06-03 19:58:08 +00:00
|
|
|
]
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 20:56:45 +00:00
|
|
|
|
2023-01-17 22:42:00 +00:00
|
|
|
/// A type implementing this trait can be converted to a [`rodio::Source`] type.
|
|
|
|
/// It must be [`Send`] and [`Sync`], and usually implements [`Asset`] so needs to be [`TypeUuid`],
|
|
|
|
/// in order to be registered.
|
|
|
|
/// Types that implement this trait usually contain raw sound data that can be converted into an iterator of samples.
|
|
|
|
/// This trait is implemented for [`AudioSource`].
|
|
|
|
/// Check the example `audio/decodable` for how to implement this trait on a custom type.
|
2020-09-08 20:56:45 +00:00
|
|
|
pub trait Decodable: Send + Sync + 'static {
|
2023-01-17 22:42:00 +00:00
|
|
|
/// The type of the audio samples.
|
|
|
|
/// Usually a [`u16`], [`i16`] or [`f32`], as those implement [`rodio::Sample`].
|
|
|
|
/// Other types can implement the [`rodio::Sample`] trait as well.
|
2022-01-05 22:30:15 +00:00
|
|
|
type DecoderItem: rodio::Sample + Send + Sync;
|
2020-09-08 20:56:45 +00:00
|
|
|
|
2023-01-17 22:42:00 +00:00
|
|
|
/// The type of the iterator of the audio samples,
|
|
|
|
/// which iterates over samples of type [`Self::DecoderItem`].
|
|
|
|
/// Must be a [`rodio::Source`] so that it can provide information on the audio it is iterating over.
|
|
|
|
type Decoder: rodio::Source + Send + Iterator<Item = Self::DecoderItem>;
|
|
|
|
|
|
|
|
/// Build and return a [`Self::Decoder`] of the implementing type
|
2020-09-08 20:56:45 +00:00
|
|
|
fn decoder(&self) -> Self::Decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for AudioSource {
|
|
|
|
type Decoder = rodio::Decoder<Cursor<AudioSource>>;
|
2022-01-05 22:30:15 +00:00
|
|
|
type DecoderItem = <rodio::Decoder<Cursor<AudioSource>> as Iterator>::Item;
|
2020-09-08 20:56:45 +00:00
|
|
|
|
|
|
|
fn decoder(&self) -> Self::Decoder {
|
|
|
|
rodio::Decoder::new(Cursor::new(self.clone())).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 22:42:00 +00:00
|
|
|
|
|
|
|
/// A trait that allows adding a custom audio source to the object.
|
|
|
|
/// This is implemented for [`App`][bevy_app::App] to allow registering custom [`Decodable`] types.
|
|
|
|
pub trait AddAudioSource {
|
|
|
|
/// Registers an audio source.
|
|
|
|
/// The type must implement [`Decodable`],
|
|
|
|
/// so that it can be converted to a [`rodio::Source`] type,
|
|
|
|
/// and [`Asset`], so that it can be registered as an asset.
|
|
|
|
/// To use this method on [`App`][bevy_app::App],
|
|
|
|
/// the [audio][super::AudioPlugin] and [asset][bevy_asset::AssetPlugin] plugins must be added first.
|
|
|
|
fn add_audio_source<T>(&mut self) -> &mut Self
|
|
|
|
where
|
|
|
|
T: Decodable + Asset;
|
|
|
|
}
|