2020-07-16 20:46:51 +00:00
|
|
|
use anyhow::Result;
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_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-01-05 22:30:15 +00:00
|
|
|
/// Raw data of the audio source
|
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
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
/// A type implementing this trait can be decoded as a rodio source
|
2020-09-08 20:56:45 +00:00
|
|
|
pub trait Decodable: Send + Sync + 'static {
|
2022-07-21 20:46:54 +00:00
|
|
|
/// The decoder that can decode the implementing type
|
2022-08-29 23:02:12 +00:00
|
|
|
type Decoder: rodio::Source + Send + Iterator<Item = Self::DecoderItem>;
|
2022-01-05 22:30:15 +00:00
|
|
|
/// A single value given by the decoder
|
|
|
|
type DecoderItem: rodio::Sample + Send + Sync;
|
2020-09-08 20:56:45 +00:00
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
/// Build and return a [`Self::Decoder`] for 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()
|
|
|
|
}
|
|
|
|
}
|