fyrox_sound/buffer/
loader.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Sound buffer loader.
22
23use crate::buffer::{DataSource, SoundBuffer};
24use fyrox_core::{reflect::prelude::*, uuid::Uuid, TypeUuidProvider};
25use fyrox_resource::{
26    io::ResourceIo,
27    loader::{BoxedImportOptionsLoaderFuture, BoxedLoaderFuture, LoaderPayload, ResourceLoader},
28    options::{
29        try_get_import_settings, try_get_import_settings_opaque, BaseImportOptions, ImportOptions,
30    },
31    state::LoadError,
32};
33use serde::{Deserialize, Serialize};
34use std::{path::PathBuf, sync::Arc};
35
36/// Defines sound buffer resource import options.
37#[derive(Clone, Deserialize, Serialize, Default, Debug, Reflect)]
38pub struct SoundBufferImportOptions {
39    /// Whether the buffer is streaming or not.
40    pub stream: bool,
41}
42
43impl ImportOptions for SoundBufferImportOptions {}
44
45/// Default implementation for sound buffer loading.
46pub struct SoundBufferLoader {
47    /// Default import options for sound buffer resources.
48    pub default_import_options: SoundBufferImportOptions,
49}
50
51impl ResourceLoader for SoundBufferLoader {
52    fn extensions(&self) -> &[&str] {
53        &["wav", "ogg"]
54    }
55
56    fn data_type_uuid(&self) -> Uuid {
57        SoundBuffer::type_uuid()
58    }
59
60    fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
61        let default_import_options = self.default_import_options.clone();
62
63        Box::pin(async move {
64            let io = io.as_ref();
65
66            let import_options = try_get_import_settings(&path, io)
67                .await
68                .unwrap_or(default_import_options);
69
70            let source = DataSource::from_file(&path, io)
71                .await
72                .map_err(LoadError::new)?;
73
74            let result = if import_options.stream {
75                SoundBuffer::raw_streaming(source)
76            } else {
77                SoundBuffer::raw_generic(source)
78            };
79
80            match result {
81                Ok(buffer) => Ok(LoaderPayload::new(buffer)),
82                Err(_) => Err(LoadError::new("Invalid data source.")),
83            }
84        })
85    }
86
87    fn try_load_import_settings(
88        &self,
89        resource_path: PathBuf,
90        io: Arc<dyn ResourceIo>,
91    ) -> BoxedImportOptionsLoaderFuture {
92        Box::pin(async move {
93            try_get_import_settings_opaque::<SoundBufferImportOptions>(&resource_path, &*io).await
94        })
95    }
96
97    fn default_import_options(&self) -> Option<Box<dyn BaseImportOptions>> {
98        Some(Box::<SoundBufferImportOptions>::default())
99    }
100}