fmod/core/dsp/
connections.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 fmod_sys::*;
8use std::ffi::c_int;
9
10use crate::{Dsp, DspConnection, DspConnectionType};
11
12impl Dsp {
13    /// Adds a [`Dsp`] unit as an input to this object.
14    ///
15    /// When a [`Dsp`] has multiple inputs the signals are automatically mixed together, sent to the unit's output(s).
16    ///
17    /// The returned [`DspConnection`] will remain valid until the units are disconnected.
18    pub fn add_input(&self, input: Dsp, kind: DspConnectionType) -> Result<DspConnection> {
19        let mut connection = std::ptr::null_mut();
20        unsafe {
21            FMOD_DSP_AddInput(self.inner, input.inner, &mut connection, kind.into()).to_result()?;
22        };
23        Ok(connection.into())
24    }
25
26    /// Retrieves the [`Dsp`] unit at the specified index in the input list.
27    ///
28    /// This will flush the [`Dsp`] queue (which blocks against the mixer) to ensure the input list is correct, avoid this during time sensitive operations.
29    ///
30    /// The returned [`DspConnection`] will remain valid until the units are disconnected.
31    pub fn get_input(&self, index: c_int) -> Result<(Dsp, DspConnection)> {
32        let mut connection = std::ptr::null_mut();
33        let mut dsp = std::ptr::null_mut();
34        unsafe {
35            FMOD_DSP_GetInput(self.inner, index, &mut dsp, &mut connection).to_result()?;
36        };
37        Ok((dsp.into(), connection.into()))
38    }
39
40    /// Retrieves the [`Dsp`] unit at the specified index in the output list.
41    ///
42    /// This will flush the [`Dsp`] queue (which blocks against the mixer) to ensure the output list is correct, avoid this during time sensitive operations.
43    ///
44    /// The returned [`DspConnection`] will remain valid until the units are disconnected.
45    pub fn get_output(&self, index: c_int) -> Result<(Dsp, DspConnection)> {
46        let mut connection = std::ptr::null_mut();
47        let mut dsp = std::ptr::null_mut();
48        unsafe {
49            FMOD_DSP_GetOutput(self.inner, index, &mut dsp, &mut connection).to_result()?;
50        };
51        Ok((dsp.into(), connection.into()))
52    }
53
54    /// Retrieves the number of [`Dsp`] units in the input list.
55    ///
56    /// This will flush the [`Dsp`] queue (which blocks against the mixer) to ensure the input list is correct, avoid this during time sensitive operations.
57    pub fn get_input_count(&self) -> Result<c_int> {
58        let mut count = 0;
59        unsafe {
60            FMOD_DSP_GetNumInputs(self.inner, &mut count).to_result()?;
61        }
62        Ok(count)
63    }
64
65    /// Retrieves the number of [`Dsp`] units in the output list.
66    ///
67    /// This will flush the [`Dsp`] queue (which blocks against the mixer) to ensure the output list is correct, avoid this during time sensitive operations.
68    pub fn get_output_count(&self) -> Result<c_int> {
69        let mut count = 0;
70        unsafe { FMOD_DSP_GetNumOutputs(self.inner, &mut count).to_result()? };
71        Ok(count)
72    }
73
74    /// Disconnects all inputs and/or outputs.
75    ///
76    /// This is a convenience function that is faster than disconnecting all inputs and outputs individually.
77    pub fn disconnect_all(&self, inputs: bool, outputs: bool) -> Result<()> {
78        unsafe { FMOD_DSP_DisconnectAll(self.inner, inputs.into(), outputs.into()).to_result() }
79    }
80
81    /// Disconnect the specified input [`Dsp`].
82    ///
83    /// If target had only one output, after this operation that entire sub graph will no longer be connected to the [`Dsp`] network.
84    ///
85    /// After this operation `connection` is no longer valid.
86    pub fn disconnect_from(
87        &self,
88        target: Option<Dsp>,
89        connection: Option<DspConnection>,
90    ) -> Result<()> {
91        let target = target.map_or(std::ptr::null_mut(), Into::into);
92        let connection = connection.map_or(std::ptr::null_mut(), Into::into);
93        unsafe { FMOD_DSP_DisconnectFrom(self.inner, target, connection).to_result() }
94    }
95}