fmod/core/sound/
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 fmod_sys::*;
8use std::ffi::c_void;
9
10use crate::{Sound, System};
11
12impl Sound {
13    /// Frees a sound object.
14    ///
15    /// This will stop any instances of this sound, and free the sound object and its children if it is a multi-sound object.
16    /// If the sound was opened with FMOD_NONBLOCKING and hasn't finished opening yet, it will block.
17    /// Additionally, if the sound is still playing or has recently been stopped, the release may stall, as the mixer may still be using the sound.
18    /// Using Sound::getOpenState and checking the open state for FMOD_OPENSTATE_READY and FMOD_OPENSTATE_ERROR is a good way to avoid stalls.
19    pub fn release(&self) -> Result<()> {
20        // release userdata
21        #[cfg(feature = "userdata-abstraction")]
22        let userdata = self.get_raw_userdata()?;
23
24        unsafe {
25            FMOD_Sound_Release(self.inner).to_result()?;
26        }
27
28        // release/remove userdata if it is not null
29        #[cfg(feature = "userdata-abstraction")]
30        if !userdata.is_null() {
31            crate::userdata::remove_userdata(userdata.into());
32            self.set_raw_userdata(std::ptr::null_mut())?;
33        }
34
35        Ok(())
36    }
37
38    #[allow(clippy::not_unsafe_ptr_arg_deref)] // fmod doesn't dereference the passed in pointer, and the user dereferencing it is unsafe anyway
39    pub fn set_raw_userdata(&self, userdata: *mut c_void) -> Result<()> {
40        unsafe { FMOD_Sound_SetUserData(self.inner, userdata).to_result() }
41    }
42
43    pub fn get_raw_userdata(&self) -> Result<*mut c_void> {
44        let mut userdata = std::ptr::null_mut();
45        unsafe {
46            FMOD_Sound_GetUserData(self.inner, &mut userdata).to_result()?;
47        }
48        Ok(userdata)
49    }
50
51    /// Retrieves the parent System object.
52    pub fn get_system(&self) -> Result<System> {
53        let mut system = std::ptr::null_mut();
54        unsafe {
55            FMOD_Sound_GetSystemObject(self.inner, &mut system).to_result()?;
56        }
57        Ok(system.into())
58    }
59}
60
61#[cfg(feature = "userdata-abstraction")]
62impl Sound {
63    pub fn set_userdata(&self, userdata: crate::userdata::Userdata) -> Result<()> {
64        use crate::userdata::{insert_userdata, set_userdata};
65
66        let pointer = self.get_raw_userdata()?;
67        if pointer.is_null() {
68            let key = insert_userdata(userdata, *self);
69            self.set_raw_userdata(key.into())?;
70        } else {
71            set_userdata(pointer.into(), userdata);
72        }
73
74        Ok(())
75    }
76
77    pub fn get_userdata(&self) -> Result<Option<crate::userdata::Userdata>> {
78        use crate::userdata::get_userdata;
79
80        let pointer = self.get_raw_userdata()?;
81        Ok(get_userdata(pointer.into()))
82    }
83}