fyrox_sound/buffer/
loader.rs1use 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#[derive(Clone, Deserialize, Serialize, Default, Debug, Reflect)]
38pub struct SoundBufferImportOptions {
39 pub stream: bool,
41}
42
43impl ImportOptions for SoundBufferImportOptions {}
44
45pub struct SoundBufferLoader {
47 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}