2020-07-16 20:46:51 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use bevy_asset::AssetLoader;
|
2020-09-08 20:56:45 +00:00
|
|
|
use std::{io::Cursor, path::Path, sync::Arc};
|
2020-07-16 20:46:51 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A source of audio data
|
2020-07-16 20:46:51 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AudioSource {
|
|
|
|
pub bytes: Arc<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
impl AssetLoader<AudioSource> for Mp3Loader {
|
|
|
|
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<AudioSource> {
|
2020-07-17 00:23:50 +00:00
|
|
|
Ok(AudioSource {
|
|
|
|
bytes: Arc::new(bytes),
|
|
|
|
})
|
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-08-11 06:30:42 +00:00
|
|
|
static EXTENSIONS: &[&str] = &["mp3", "flac", "wav", "ogg"];
|
2020-07-16 20:46:51 +00:00
|
|
|
EXTENSIONS
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|