1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Provides structures used to load audio files.
//!
use amethyst_assets::{
    Asset, AssetStorage, Handle, Loader, PrefabData, ProcessableAsset, ProcessingState,
};
use amethyst_core::ecs::prelude::{Entity, Read, ReadExpect, VecStorage};
use amethyst_error::Error;

use crate::formats::AudioData;

/// A handle to a source asset.
pub type SourceHandle = Handle<Source>;

/// A loaded audio file
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Source {
    /// The bytes of this audio source.
    pub bytes: Vec<u8>,
}

impl AsRef<[u8]> for Source {
    fn as_ref(&self) -> &[u8] {
        &self.bytes
    }
}

impl Asset for Source {
    const NAME: &'static str = "audio::Source";
    type Data = AudioData;
    type HandleStorage = VecStorage<SourceHandle>;
}

impl ProcessableAsset for Source {
    fn process(data: AudioData) -> Result<ProcessingState<Source>, Error> {
        Ok(ProcessingState::Loaded(Source { bytes: data.0 }))
    }
}

impl<'a> PrefabData<'a> for AudioData {
    type SystemData = (ReadExpect<'a, Loader>, Read<'a, AssetStorage<Source>>);
    type Result = Handle<Source>;

    fn add_to_entity(
        &self,
        _: Entity,
        system_data: &mut Self::SystemData,
        _: &[Entity],
        _: &[Entity],
    ) -> Result<Handle<Source>, Error> {
        Ok(system_data
            .0
            .load_from_data(self.clone(), (), &system_data.1))
    }
}