fmod/core/sound/
synchronization.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, c_uint};
8
9use fmod_sys::*;
10use lanyard::{Utf8CStr, Utf8CString};
11
12use crate::{get_string, Sound, TimeUnit};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(transparent)] // so we can transmute between types
16pub struct SyncPoint {
17    pub(crate) inner: *mut FMOD_SYNCPOINT,
18}
19
20unsafe impl Send for SyncPoint {}
21unsafe impl Sync for SyncPoint {}
22
23impl From<*mut FMOD_SYNCPOINT> for SyncPoint {
24    fn from(value: *mut FMOD_SYNCPOINT) -> Self {
25        SyncPoint { inner: value }
26    }
27}
28
29impl From<SyncPoint> for *mut FMOD_SYNCPOINT {
30    fn from(value: SyncPoint) -> Self {
31        value.inner
32    }
33}
34
35impl Sound {
36    /// Retrieve a sync point.
37    ///
38    /// For for more information on sync points see Sync Points.
39    pub fn get_sync_point(&self, index: i32) -> Result<SyncPoint> {
40        let mut sync_point = std::ptr::null_mut();
41        unsafe {
42            FMOD_Sound_GetSyncPoint(self.inner, index, &mut sync_point).to_result()?;
43        }
44        Ok(sync_point.into())
45    }
46
47    /// Retrieves information on an embedded sync point.
48    ///
49    /// For for more information on sync points see Sync Points.
50    pub fn get_sync_point_info(
51        &self,
52        point: SyncPoint,
53        offset_type: TimeUnit,
54    ) -> Result<(Utf8CString, c_uint)> {
55        let mut offset = 0;
56        let name = get_string(|name| unsafe {
57            FMOD_Sound_GetSyncPointInfo(
58                self.inner,
59                point.into(),
60                name.as_mut_ptr().cast(),
61                name.len() as c_int,
62                &mut offset,
63                offset_type.into(),
64            )
65        })?;
66        Ok((name, offset))
67    }
68
69    /// Retrieves the number of sync points stored within a sound.
70    ///
71    /// For for more information on sync points see Sync Points.
72    pub fn get_sync_point_count(&self) -> Result<i32> {
73        let mut count = 0;
74        unsafe {
75            FMOD_Sound_GetNumSyncPoints(self.inner, &mut count).to_result()?;
76        }
77        Ok(count)
78    }
79
80    /// Adds a sync point at a specific time within the sound.
81    ///
82    /// For more information on sync points see Sync Points.
83    pub fn add_sync_point(
84        &self,
85        offset: c_uint,
86        offset_type: TimeUnit,
87        name: &Utf8CStr,
88    ) -> Result<SyncPoint> {
89        let mut sync_point = std::ptr::null_mut();
90        unsafe {
91            FMOD_Sound_AddSyncPoint(
92                self.inner,
93                offset,
94                offset_type.into(),
95                name.as_ptr(),
96                &mut sync_point,
97            )
98            .to_result()?;
99        }
100        Ok(sync_point.into())
101    }
102
103    /// Deletes a sync point within the sound.
104    ///
105    /// For for more information on sync points see Sync Points.
106    pub fn delete_sync_point(&self, point: SyncPoint) -> Result<()> {
107        unsafe {
108            FMOD_Sound_DeleteSyncPoint(self.inner, point.into()).to_result()?;
109        }
110        Ok(())
111    }
112}