fmod/core/sound_group/
group.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::{SoundGroup, SoundGroupBehavior};
11
12impl SoundGroup {
13    /// Sets the maximum number of playbacks to be audible at once in a sound group.
14    ///
15    /// If playing instances of sounds in this group equal or exceed number specified here, attepts to play more of the sounds with be met with [`FMOD_RESULT::FMOD_ERR_MAXAUDIBLE`] by default.
16    /// Use [`SoundGroup::set_max_audible_behavior`] to change the way the sound playback behaves when too many sounds are playing.
17    /// Muting, failing and stealing behaviors can be specified. See [`SoundGroupBehavior`].
18    ///
19    /// [`SoundGroup::get_playing_count`] can be used to determine how many instances of the sounds in the [`SoundGroup`] are currently playing.
20    pub fn set_max_audible(&self, max_audible: c_int) -> Result<()> {
21        unsafe { FMOD_SoundGroup_SetMaxAudible(self.inner, max_audible).to_result() }
22    }
23
24    /// Retrieves the maximum number of playbacks to be audible at once in a sound group.
25    pub fn get_max_audible(&self) -> Result<c_int> {
26        let mut max_audible = 0;
27        unsafe { FMOD_SoundGroup_GetMaxAudible(self.inner, &mut max_audible).to_result()? };
28        Ok(max_audible)
29    }
30
31    /// This function changes the way the sound playback behaves when too many sounds are playing in a soundgroup.
32    pub fn set_max_audible_behavior(&self, behavior: SoundGroupBehavior) -> Result<()> {
33        unsafe { FMOD_SoundGroup_SetMaxAudibleBehavior(self.inner, behavior.into()).to_result() }
34    }
35
36    /// Retrieves the current max audible behavior.
37    pub fn get_max_audible_behavior(&self) -> Result<SoundGroupBehavior> {
38        let mut behavior = 0;
39        unsafe { FMOD_SoundGroup_GetMaxAudibleBehavior(self.inner, &mut behavior).to_result()? };
40        let behavior = behavior.try_into()?;
41        Ok(behavior)
42    }
43
44    /// Sets a mute fade time.
45    ///
46    /// If a mode besides [`SoundGroupBehavior::Mute`] is used, the fade speed is ignored.
47    ///
48    /// When more sounds are playing in a [`SoundGroup`] than are specified with [`SoundGroup::set_max_audible`],
49    /// the least important Sound (ie lowest priority / lowest audible volume due to 3D position, volume etc)
50    /// will fade to silence if [`SoundGroupBehavior::Mute`] is used,
51    /// and any previous sounds that were silent because of this rule will fade in if they are more important.
52    pub fn set_mute_fade_speed(&self, speed: c_float) -> Result<()> {
53        unsafe { FMOD_SoundGroup_SetMuteFadeSpeed(self.inner, speed).to_result() }
54    }
55
56    /// Retrieves the current mute fade time.
57    pub fn get_mute_fade_speed(&self) -> Result<c_float> {
58        let mut speed = 0.0;
59        unsafe { FMOD_SoundGroup_GetMuteFadeSpeed(self.inner, &mut speed).to_result()? };
60        Ok(speed)
61    }
62
63    /// Sets the volume of the sound group.
64    pub fn set_volume(&self, volume: c_float) -> Result<()> {
65        unsafe { FMOD_SoundGroup_SetVolume(self.inner, volume).to_result() }
66    }
67
68    /// Retrieves the volume of the sound group.
69    pub fn get_volume(&self) -> Result<c_float> {
70        let mut volume = 0.0;
71        unsafe { FMOD_SoundGroup_GetVolume(self.inner, &mut volume).to_result()? };
72        Ok(volume)
73    }
74}