fmod/core/dsp/
channel_format.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_int;
9
10use crate::{ChannelMask, Dsp, SpeakerMode};
11
12impl Dsp {
13    /// Sets the PCM input format this [`Dsp`] will receive when processing.
14    ///
15    /// Setting the number of channels on a unit will force either a down or up mix to that channel count before processing the [`Dsp`] read/process callback.
16    pub fn set_channel_format(
17        &self,
18        channel_mask: ChannelMask,
19        channel_count: c_int,
20        source_speaker_mode: SpeakerMode,
21    ) -> Result<()> {
22        unsafe {
23            FMOD_DSP_SetChannelFormat(
24                self.inner,
25                channel_mask.into(),
26                channel_count,
27                source_speaker_mode.into(),
28            )
29            .to_result()
30        }
31    }
32
33    /// Retrieves the PCM input format this [`Dsp`] will receive when processing.
34    pub fn get_channel_format(&self) -> Result<(ChannelMask, c_int, SpeakerMode)> {
35        let mut channel_mask = 0;
36        let mut channel_count = 0;
37        let mut source_speaker_mode = 0;
38        unsafe {
39            FMOD_DSP_GetChannelFormat(
40                self.inner,
41                &mut channel_mask,
42                &mut channel_count,
43                &mut source_speaker_mode,
44            )
45            .to_result()?;
46        }
47        let source_speaker_mode = source_speaker_mode.try_into()?;
48        Ok((channel_mask.into(), channel_count, source_speaker_mode))
49    }
50
51    /// Retrieves the output format this [`Dsp`] will produce when processing based on the input specified.
52    pub fn get_output_channel_format(
53        &self,
54        in_mask: ChannelMask,
55        in_channels: c_int,
56        in_speaker_mode: SpeakerMode,
57    ) -> Result<(ChannelMask, c_int, SpeakerMode)> {
58        let mut out_mask = 0;
59        let mut out_channels = 0;
60        let mut out_speaker_mode = 0;
61        unsafe {
62            FMOD_DSP_GetOutputChannelFormat(
63                self.inner,
64                in_mask.into(),
65                in_channels,
66                in_speaker_mode.into(),
67                &mut out_mask,
68                &mut out_channels,
69                &mut out_speaker_mode,
70            )
71            .to_result()?;
72        }
73        let out_speaker_mode = out_speaker_mode.try_into()?;
74        Ok((out_mask.into(), out_channels, out_speaker_mode))
75    }
76}