fmod/core/sound/
relationship.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 std::ffi::c_int;
8
9use fmod_sys::*;
10
11use crate::{Sound, SoundGroup};
12
13impl Sound {
14    /// Moves the sound from its existing [`SoundGroup`] to the specified sound group.
15    ///
16    /// By default, a sound is located in the 'master sound group'.
17    /// This can be retrieved with System::getMasterSoundGroup.
18    pub fn set_sound_group(&self, group: SoundGroup) -> Result<()> {
19        unsafe { FMOD_Sound_SetSoundGroup(self.inner, group.into()).to_result() }
20    }
21
22    /// Retrieves the sound's current sound group.
23    pub fn sound_group(&self) -> Result<SoundGroup> {
24        let mut group = std::ptr::null_mut();
25        unsafe {
26            FMOD_Sound_GetSoundGroup(self.inner, &mut group).to_result()?;
27        }
28        Ok(group.into())
29    }
30
31    /// Retrieves the number of subsounds stored within a sound.
32    ///
33    /// A format that has subsounds is a container format, such as FSB, DLS, MOD, S3M, XM, IT.
34    pub fn get_sub_sound_count(&self) -> Result<c_int> {
35        let mut count = 0;
36        unsafe {
37            FMOD_Sound_GetNumSubSounds(self.inner, &mut count).to_result()?;
38        }
39        Ok(count)
40    }
41
42    /// Retrieves a handle to a Sound object that is contained within the parent sound.
43    ///
44    /// If the sound is a stream and FMOD_NONBLOCKING was not used,
45    /// then this call will perform a blocking seek/flush to the specified subsound.
46    ///
47    /// If FMOD_NONBLOCKING was used to open this sound and the sound is a stream,
48    /// FMOD will do a non blocking seek/flush and set the state of the subsound to FMOD_OPENSTATE_SEEKING.
49    ///
50    /// The sound won't be ready to be used when FMOD_NONBLOCKING is used,
51    /// until the state of the sound becomes FMOD_OPENSTATE_READY or FMOD_OPENSTATE_ERROR.
52    pub fn get_sub_sound(&self, index: c_int) -> Result<Sound> {
53        let mut sound = std::ptr::null_mut();
54        unsafe {
55            FMOD_Sound_GetSubSound(self.inner, index, &mut sound).to_result()?;
56        }
57        Ok(sound.into())
58    }
59
60    /// Retrieves the parent Sound object that contains this subsound.
61    pub fn get_sub_sound_parent(&self) -> Result<Option<Sound>> {
62        let mut sound = std::ptr::null_mut();
63        unsafe {
64            FMOD_Sound_GetSubSoundParent(self.inner, &mut sound).to_result()?;
65        }
66        if sound.is_null() {
67            Ok(None)
68        } else {
69            Ok(Some(sound.into()))
70        }
71    }
72}