fmod/studio/event_instance/
playback_properties.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_float, c_int};
9
10use crate::studio::{EventInstance, EventProperty};
11
12impl EventInstance {
13    /// Sets the pitch multiplier.
14    ///
15    /// The pitch multiplier is used to modulate the event instance's pitch.
16    /// The pitch multiplier can be set to any value greater than or equal to zero but the final combined pitch is clamped to the range [0, 100] before being applied.
17    pub fn set_pitch(&self, pitch: c_float) -> Result<()> {
18        unsafe { FMOD_Studio_EventInstance_SetPitch(self.inner, pitch).to_result() }
19    }
20
21    /// Retrieves the pitch multiplier.
22    ///
23    /// The final combined value returned in second tuple field combines the pitch set using [`EventInstance::set_pitch`] with the result of any automation or modulation.
24    /// The final combined pitch is calculated asynchronously when the Studio system updates.
25    pub fn get_pitch(&self) -> Result<(c_float, c_float)> {
26        let mut pitch = 0.0;
27        let mut final_pitch = 0.0;
28        unsafe {
29            FMOD_Studio_EventInstance_GetPitch(self.inner, &mut pitch, &mut final_pitch)
30                .to_result()?;
31        }
32        Ok((pitch, final_pitch))
33    }
34
35    /// Sets the value of a built-in property.
36    ///
37    /// This will override the value set in Studio. Using the default [`EventProperty`] value will revert back to the default values set in Studio.
38    ///
39    /// An FMOD spatializer or object spatializer may override the values set for [`EventProperty::MinimumDistance`] and [`EventProperty::MaximumDistance`]].
40    pub fn set_property(&self, property: EventProperty, value: c_float) -> Result<()> {
41        unsafe {
42            FMOD_Studio_EventInstance_SetProperty(self.inner, property.into(), value).to_result()
43        }
44    }
45
46    /// Retrieves the value of a built-in property.
47    ///
48    /// A default [`EventProperty`] value means that the Instance is using the value set in Studio and it has not been overridden using [`EventInstance::set_property`].
49    /// Access the default property values through the [`EventDescription`].
50    pub fn get_property(&self, property: EventProperty) -> Result<c_float> {
51        let mut value = 0.0;
52        unsafe {
53            FMOD_Studio_EventInstance_GetProperty(self.inner, property.into(), &mut value)
54                .to_result()?;
55        }
56        Ok(value)
57    }
58
59    /// Sets the timeline cursor position.
60    pub fn set_timeline_position(&self, position: c_int) -> Result<()> {
61        unsafe { FMOD_Studio_EventInstance_SetTimelinePosition(self.inner, position).to_result() }
62    }
63
64    /// Retrieves the timeline cursor position.
65    pub fn get_timeline_position(&self) -> Result<c_int> {
66        let mut position = 0;
67        unsafe {
68            FMOD_Studio_EventInstance_GetTimelinePosition(self.inner, &mut position).to_result()?;
69        }
70        Ok(position)
71    }
72
73    /// Sets the volume level.
74    ///
75    /// This volume is applied as a scaling factor for the event volume. It does not override the volume level set in FMOD Studio, nor any internal volume automation or modulation.
76    pub fn set_volume(&self, volume: c_float) -> Result<()> {
77        unsafe { FMOD_Studio_EventInstance_SetVolume(self.inner, volume).to_result() }
78    }
79
80    /// Retrieves the volume level.
81    ///
82    /// The value returned in the second tuple field combines the volume set using the public API with the result of any automation or modulation.
83    /// The final combined volume is calculated asynchronously when the Studio system updates.
84    pub fn get_volume(&self) -> Result<(c_float, c_float)> {
85        let mut volume = 0.0;
86        let mut final_volume = 0.0;
87        unsafe {
88            FMOD_Studio_EventInstance_GetVolume(self.inner, &mut volume, &mut final_volume)
89                .to_result()?;
90        }
91        Ok((volume, final_volume))
92    }
93
94    /// Retrieves the virtualization state.
95    ///
96    /// This function checks whether an event instance has been virtualized due to the polyphony limit being exceeded.
97    pub fn is_virtual(&self) -> Result<bool> {
98        let mut is_virtual = FMOD_BOOL::FALSE;
99        unsafe {
100            FMOD_Studio_EventInstance_IsVirtual(self.inner, &mut is_virtual).to_result()?;
101        }
102        Ok(is_virtual.into())
103    }
104}