fmod/studio/
vca.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_float, mem::MaybeUninit, ptr::NonNull};
8
9use fmod_sys::*;
10use lanyard::Utf8CString;
11
12use crate::Guid;
13use crate::{FmodResultExt, Result};
14
15use super::get_string_out_size;
16
17/// Represents a global mixer VCA.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19#[repr(transparent)] // so we can transmute between types
20pub struct Vca {
21    pub(crate) inner: NonNull<FMOD_STUDIO_VCA>,
22}
23
24#[cfg(not(feature = "thread-unsafe"))]
25unsafe impl Send for Vca {}
26#[cfg(not(feature = "thread-unsafe"))]
27unsafe impl Sync for Vca {}
28
29impl Vca {
30    /// # Safety
31    ///
32    /// `value` must be a valid pointer either aquired from [`Self::as_ptr`] or FMOD.
33    ///
34    /// # Panics
35    ///
36    /// Panics if `value` is null.
37    pub unsafe fn from_ffi(value: *mut FMOD_STUDIO_VCA) -> Self {
38        let inner = NonNull::new(value).unwrap();
39        Vca { inner }
40    }
41
42    /// Converts `self` into its raw representation.
43    pub fn as_ptr(self) -> *mut FMOD_STUDIO_VCA {
44        self.inner.as_ptr()
45    }
46}
47
48impl From<Vca> for *mut FMOD_STUDIO_VCA {
49    fn from(value: Vca) -> Self {
50        value.inner.as_ptr()
51    }
52}
53
54impl Vca {
55    /// Sets the volume level.
56    ///
57    /// The VCA volume level is used to linearly modulate the levels of the buses and VCAs which it controls.
58    pub fn set_volume(&self, volume: c_float) -> Result<()> {
59        unsafe { FMOD_Studio_VCA_SetVolume(self.inner.as_ptr(), volume).to_result() }
60    }
61
62    /// Retrieves the volume level.
63    ///
64    /// The final combined volume returned in the second tuple field combines the user value set using [`Vca::set_volume`] with the result of any automation or modulation applied to the VCA.
65    /// The final combined volume is calculated asynchronously when the Studio system updates.
66    pub fn get_volume(&self) -> Result<(c_float, c_float)> {
67        let mut volume = 0.0;
68        let mut final_volume = 0.0;
69        unsafe {
70            FMOD_Studio_VCA_GetVolume(self.inner.as_ptr(), &raw mut volume, &raw mut final_volume)
71                .to_result()?;
72        }
73        Ok((volume, final_volume))
74    }
75}
76
77impl Vca {
78    /// Retrieves the GUID.
79    pub fn get_id(&self) -> Result<Guid> {
80        let mut guid = MaybeUninit::zeroed();
81        unsafe {
82            FMOD_Studio_VCA_GetID(self.inner.as_ptr(), guid.as_mut_ptr()).to_result()?;
83
84            let guid = guid.assume_init().into();
85
86            Ok(guid)
87        }
88    }
89
90    /// Retrieves the path.
91    ///
92    /// The strings bank must be loaded prior to calling this function, otherwise [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] is returned.
93    pub fn get_path(&self) -> Result<Utf8CString> {
94        get_string_out_size(|path, size, ret| unsafe {
95            FMOD_Studio_VCA_GetPath(self.inner.as_ptr(), path, size, ret)
96        })
97    }
98
99    /// Checks that the VCA reference is valid.
100    pub fn is_valid(&self) -> bool {
101        unsafe { FMOD_Studio_VCA_IsValid(self.inner.as_ptr()).into() }
102    }
103}