#![doc = r#"
Asset types
TODO
"#]
use std::{io::Read, sync::Arc};
use thiserror::Error;
use bevy::{
asset::{AssetLoader, LoadContext, io::Reader},
prelude::*,
};
use midix_synth::{prelude::SoundFontError, soundfont::SoundFont as Sf};
#[derive(Asset, TypePath)]
pub struct SoundFont {
pub(crate) file: Arc<Sf>,
}
impl SoundFont {
fn new<R: Read + ?Sized>(file: &mut R) -> Result<Self, SoundFontError> {
let sf = Arc::new(Sf::new(file)?);
Ok(Self { file: sf })
}
}
#[non_exhaustive]
#[derive(Debug, Error)]
enum SoundFontLoadError {
#[error("Could not load asset: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Default)]
pub struct SoundFontLoader;
impl AssetLoader for SoundFontLoader {
type Asset = SoundFont;
type Settings = ();
type Error = SoundFontError;
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &(),
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
info!(
"Loading bytes...this might take a while. If taking too long, run with --release or with opt-level = 3!"
);
reader.read_to_end(&mut bytes).await?;
info!("Loaded!");
let res = SoundFont::new(&mut bytes.as_slice())?;
Ok(res)
}
fn extensions(&self) -> &[&str] {
&["custom"]
}
}