fmod/core/system/general.rs
1// Copyright (c) 2024 Melody Madeline Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use std::ffi::c_void;
8
9use fmod_sys::*;
10
11use crate::System;
12use crate::{FmodResultExt, Result};
13
14#[derive(Debug)]
15pub struct DspLockGuard(System);
16
17impl Drop for DspLockGuard {
18 fn drop(&mut self) {
19 let result = unsafe { FMOD_System_UnlockDSP(self.0.inner.as_ptr()).to_result() };
20 if let Err(e) = result {
21 eprintln!("FMOD_System_UnlockDSP errored! {e}");
22 }
23 }
24}
25
26impl System {
27 /// Mutual exclusion function to lock the FMOD DSP engine (which runs asynchronously in another thread), so that it will not execute.
28 ///
29 /// If the FMOD DSP engine is already executing, this function will block until it has completed.
30 ///
31 /// The function may be used to synchronize DSP network operations carried out by the user.
32 ///
33 /// An example of using this function may be for when the user wants to construct a DSP sub-network, without the DSP engine executing in the background while the sub-network is still under construction.
34 ///
35 /// Note that the DSP engine should not be locked for a significant amount of time, otherwise inconsistency in the audio output may result. (audio skipping / stuttering).
36 pub fn lock_dsp(&self) -> Result<DspLockGuard> {
37 unsafe { FMOD_System_LockDSP(self.inner.as_ptr()).to_result()? };
38 Ok(DspLockGuard(*self))
39 }
40
41 /// Sets the user data.
42 #[allow(clippy::not_unsafe_ptr_arg_deref)] // fmod doesn't dereference the passed in pointer, and the user dereferencing it is unsafe anyway
43 pub fn set_userdata(&self, userdata: *mut c_void) -> Result<()> {
44 unsafe { FMOD_System_SetUserData(self.inner.as_ptr(), userdata).to_result() }
45 }
46
47 /// Retrieves user data.
48 pub fn get_userdata(&self) -> Result<*mut c_void> {
49 let mut userdata = std::ptr::null_mut();
50 unsafe {
51 FMOD_System_GetUserData(self.inner.as_ptr(), &raw mut userdata).to_result()?;
52 }
53 Ok(userdata)
54 }
55}