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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Asset loader implementation for the [`AudioFile`] type.
//!
//! This loader either loads the audio data fully into memory, or simply copies the path into the asset, as kira has
//! its own file streaming features.
//!
//! This means that the streaming feature is only available on desktop platforms, and not on the web.
use bevy::asset::io::Reader;
use bevy::asset::{AssetLoader, AsyncReadExt, LoadContext};
use kira::sound::static_sound::StaticSoundSettings;
use kira::sound::streaming::StreamingSoundSettings;
use kira::sound::FromFileError;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::sources::audio_file::source::AudioFile;

/// Loads an [`AudioFile`].
#[derive(Default)]
pub struct AudioFileLoader;

/// Possible errors that can be produced by [`AudioFileLoader`]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum AudioFileLoaderError {
    /// An [IO Error](std::io::Error)
    #[error("Could not read the file: {0}")]
    Io(#[from] std::io::Error),
    /// An Error loading sound from a file. See [`FromFileError`]
    #[error("Error while loading a sound: {0}")]
    FileError(#[from] FromFileError),
}

/// Settings applied when loading the audio file.
#[derive(Debug, Copy, Clone, Deserialize, Serialize, Default)]
pub struct AudioAssetSettings {
    /// Whether the loader should read the entire file into memory, or only load it on demand
    /// during playback. Note that some features are not available when a file is streamed from
    /// disk, and streaming is only available on desktop platforms.
    pub should_stream: bool,
}

impl AssetLoader for AudioFileLoader {
    type Asset = AudioFile;
    type Settings = AudioAssetSettings;
    type Error = AudioFileLoaderError;

    async fn load<'a>(
        &'a self,
        reader: &'a mut Reader<'_>,
        settings: &'a AudioAssetSettings,
        load_context: &'a mut LoadContext<'_>,
    ) -> Result<Self::Asset, Self::Error> {
        if settings.should_stream {
            Ok(AudioFile::Streaming {
                path: load_context.path().to_path_buf(),
                settings: StreamingSoundSettings::new(),
            })
        } else {
            let mut sound_bytes = vec![];
            reader.read_to_end(&mut sound_bytes).await?;
            Ok(AudioFile::Static(
                sound_bytes.into(),
                StaticSoundSettings::default(),
            ))
        }
    }

    fn extensions(&self) -> &[&str] {
        &["wav", "flac", "mp3", "ogg", "oga", "spx"]
    }
}