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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2024 Melody Madeline Lyons
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use std::ffi::c_int;
use crate::{
FileSystemAsync, FileSystemSync, async_filesystem_cancel, async_filesystem_read,
filesystem_close, filesystem_open, filesystem_read, filesystem_seek,
};
use crate::{FmodResultExt, Result};
use fmod_sys::*;
use super::System;
#[cfg(doc)]
use crate::Sound;
impl System {
/// Set callbacks to implement all file I/O instead of using the platform native method.
///
/// Setting these callbacks have no effect on sounds loaded with [`FMOD_OPENMEMORY`] or [`FMOD_OPENUSER`].
///
/// Setting blockalign to 0 will disable file buffering and cause every read to invoke the relevant callback (not recommended),
/// current default is tuned for memory usage vs performance.
/// Be mindful of the I/O capabilities of the platform before increasing this default.
pub fn set_default_filesystem(&self, block_align: c_int) -> Result<()> {
unsafe {
FMOD_System_SetFileSystem(
self.inner.as_ptr(),
None,
None,
None,
None,
None,
None,
block_align,
)
.to_result()
}
}
/// Set callbacks to implement all file I/O instead of using the platform native method.
///
/// Setting these callbacks have no effect on sounds loaded with [`FMOD_OPENMEMORY`] or [`FMOD_OPENUSER`].
///
/// Setting blockalign to 0 will disable file buffering and cause every read to invoke the relevant callback (not recommended),
/// current default is tuned for memory usage vs performance.
/// Be mindful of the I/O capabilities of the platform before increasing this default.
pub fn set_filesystem_sync<F: FileSystemSync>(&self, block_align: c_int) -> Result<()> {
unsafe {
FMOD_System_SetFileSystem(
self.inner.as_ptr(),
Some(filesystem_open::<F>),
Some(filesystem_close::<F>),
Some(filesystem_read::<F>),
Some(filesystem_seek::<F>),
None,
None,
block_align,
)
.to_result()
}
}
/// Set callbacks to implement all file I/O instead of using the platform native method.
///
/// Setting these callbacks have no effect on sounds loaded with [`FMOD_OPENMEMORY`] or [`FMOD_OPENUSER`].
///
/// Setting blockalign to 0 will disable file buffering and cause every read to invoke the relevant callback (not recommended),
/// current default is tuned for memory usage vs performance.
/// Be mindful of the I/O capabilities of the platform before increasing this default.
/// - it is recommended to consult the 'asyncio' example for reference implementation.
/// There is also a tutorial on the subject, Asynchronous I/O.
/// - [`FileSystemAsync::read`] allows the user to return immediately before the data is ready.
/// FMOD will either wait internally (see note below about thread safety), or continuously check in the streamer until data arrives.
/// It is the user's responsibility to provide data in time in the stream case, or the stream may stutter.
/// Data starvation can be detected with [`Sound::get_open_state`].
/// - Important: If [`FileSystemAsync::read`] is processed in the main thread, then it will hang the application,
/// because FMOD will wait internally until data is ready, and the main thread will not be able to supply the data.
/// For this reason the user's file access should normally be from a separate thread.
/// - A [`FileSystemAsync::cancel`] must either service or prevent an async read issued previously via [`FileSystemAsync::read`] before returning.
pub fn set_filesystem_async<F: FileSystemAsync>(&self, block_align: c_int) -> Result<()> {
unsafe {
FMOD_System_SetFileSystem(
self.inner.as_ptr(),
Some(filesystem_open::<F>),
Some(filesystem_close::<F>),
None,
None,
Some(async_filesystem_read::<F>),
Some(async_filesystem_cancel::<F>),
block_align,
)
.to_result()
}
}
/// 'Piggyback' on FMOD file reading routines to capture data as it's read.
///
/// This allows users to capture data as FMOD reads it,
/// which may be useful for extracting the raw data that FMOD reads for hard to support sources (for example internet streams).
///
/// Note: This function is not to replace FMOD's file system. For this functionality, see [`System::set_filesystem_sync`].
pub fn attach_filesystem<F: FileSystemSync>(&self) -> Result<()> {
unsafe {
FMOD_System_AttachFileSystem(
self.inner.as_ptr(),
Some(filesystem_open::<F>),
Some(filesystem_close::<F>),
Some(filesystem_read::<F>),
Some(filesystem_seek::<F>),
)
.to_result()
}
}
/// Detach the currently attached listener filesystem.
pub fn detach_filesystem(&self) -> Result<()> {
unsafe {
FMOD_System_AttachFileSystem(self.inner.as_ptr(), None, None, None, None).to_result()
}
}
}