fmod/core/dsp_connection/
general.rs

1// Copyright (c) 2024 Lily 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::{Dsp, DspConnection, DspConnectionType};
12
13impl DspConnection {
14    /// Retrieves the connection's input [`Dsp`] unit.
15    ///
16    /// If [`Dsp::add_input`] was just called, the connection might not be ready because the [`Dsp`] system is still queued to be connected,
17    /// and may need to wait several milliseconds for the next mix to occur.
18    /// If so the function will return [`FMOD_RESULT::FMOD_ERR_NOTREADY`].
19    pub fn get_input(&self) -> Result<Dsp> {
20        let mut dsp = std::ptr::null_mut();
21        unsafe { FMOD_DSPConnection_GetInput(self.inner, &mut dsp).to_result()? };
22        Ok(dsp.into())
23    }
24
25    /// Retrieves the connection's output DSP unit.
26    ///
27    /// If [`Dsp::add_input`] was just called, the connection might not be ready because the [`Dsp`] system is still queued to be connected,
28    /// and may need to wait several milliseconds for the next mix to occur.
29    /// If so the function will return [`FMOD_RESULT::FMOD_ERR_NOTREADY`].
30    pub fn get_output(&self) -> Result<Dsp> {
31        let mut dsp = std::ptr::null_mut();
32        unsafe { FMOD_DSPConnection_GetOutput(self.inner, &mut dsp).to_result()? };
33        Ok(dsp.into())
34    }
35
36    /// Retrieves the type of the connection between 2 DSP units.
37    pub fn get_type(&self) -> Result<DspConnectionType> {
38        let mut connection_type = 0;
39        unsafe { FMOD_DSPConnection_GetType(self.inner, &mut connection_type).to_result()? };
40        let connection_type = connection_type.try_into()?;
41        Ok(connection_type)
42    }
43
44    #[allow(clippy::not_unsafe_ptr_arg_deref)] // fmod doesn't dereference the passed in pointer, and the user dereferencing it is unsafe anyway
45    pub fn set_raw_userdata(&self, userdata: *mut c_void) -> Result<()> {
46        unsafe { FMOD_DSPConnection_SetUserData(self.inner, userdata).to_result() }
47    }
48
49    pub fn get_raw_userdata(&self) -> Result<*mut c_void> {
50        let mut userdata = std::ptr::null_mut();
51        unsafe {
52            FMOD_DSPConnection_GetUserData(self.inner, &mut userdata).to_result()?;
53        }
54        Ok(userdata)
55    }
56}
57
58#[cfg(feature = "userdata-abstraction")]
59impl DspConnection {
60    pub fn set_userdata(&self, userdata: crate::userdata::Userdata) -> Result<()> {
61        use crate::userdata::{insert_userdata, set_userdata};
62
63        let pointer = self.get_raw_userdata()?;
64        if pointer.is_null() {
65            let key = insert_userdata(userdata, *self);
66            self.set_raw_userdata(key.into())?;
67        } else {
68            set_userdata(pointer.into(), userdata);
69        }
70
71        Ok(())
72    }
73
74    pub fn get_userdata(&self) -> Result<Option<crate::userdata::Userdata>> {
75        use crate::userdata::get_userdata;
76
77        let pointer = self.get_raw_userdata()?;
78        Ok(get_userdata(pointer.into()))
79    }
80}