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};
|
|
|
|
use bevy_type_registry::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 {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// Loads mp3 files as [AudioSource] [Assets](bevy_asset::Assets)
|
2020-07-16 20:46:51 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Mp3Loader;
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
impl AssetLoader for Mp3Loader {
|
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] {
|
2020-11-17 21:40:18 +00:00
|
|
|
&["mp3", "flac", "wav", "ogg"]
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 20:56:45 +00:00
|
|
|
|
|
|
|
pub trait Decodable: Send + Sync + 'static {
|
|
|
|
type Decoder;
|
|
|
|
|
|
|
|
fn decoder(&self) -> Self::Decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for AudioSource {
|
|
|
|
type Decoder = rodio::Decoder<Cursor<AudioSource>>;
|
|
|
|
|
|
|
|
fn decoder(&self) -> Self::Decoder {
|
|
|
|
rodio::Decoder::new(Cursor::new(self.clone())).unwrap()
|
|
|
|
}
|
|
|
|
}
|