fmod/core/dsp/
callback.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 fmod_sys::*;
8use std::ffi::c_void;
9
10use crate::panic_wrapper;
11
12use super::Dsp;
13use crate::{FmodResultExt, Result};
14
15/// Trait for this particular FMOD callback.
16///
17/// No `self` parameter is passed to the callback!
18pub trait DspCallback {
19    /// Called when a DSP's data parameter can be released.
20    // I'm not sure how FMOD_DSP_DATA_PARAMETER_INFO works we'll just pass the raw value
21    fn data_parameter_release(dsp: Dsp, info: FMOD_DSP_DATA_PARAMETER_INFO) -> Result<()>;
22}
23
24unsafe extern "C" fn callback_impl<C: DspCallback>(
25    dsp: *mut FMOD_DSP,
26    kind: FMOD_DSP_CALLBACK_TYPE,
27    data: *mut c_void,
28) -> FMOD_RESULT {
29    panic_wrapper(|| {
30        let dsp = unsafe { Dsp::from_ffi(dsp) };
31        // FMOD may add more variants in the future, so keep the match for consistency
32        #[allow(clippy::single_match_else)]
33        let result = match kind {
34            FMOD_DSP_CALLBACK_DATAPARAMETERRELEASE => {
35                let info = unsafe { std::ptr::read(data.cast()) };
36                C::data_parameter_release(dsp, info)
37            }
38            _ => {
39                eprintln!("warning: unknown dsp callback type {kind}");
40                return FMOD_RESULT::FMOD_OK;
41            }
42        };
43        FMOD_RESULT::from_result(result)
44    })
45}
46
47impl Dsp {
48    /// Sets the callback for DSP notifications.
49    pub fn set_callback<C: DspCallback>(&self) -> Result<()> {
50        unsafe { FMOD_DSP_SetCallback(self.inner.as_ptr(), Some(callback_impl::<C>)).to_result() }
51    }
52}