Skip to main content

firewheel_pool/
volume_pan.rs

1#[cfg(not(feature = "std"))]
2use bevy_platform::prelude::{vec, Vec};
3
4#[cfg(feature = "scheduled_events")]
5use firewheel_core::clock::EventInstant;
6
7use firewheel_core::{
8    channel_config::NonZeroChannelCount,
9    diff::{Diff, PathBuilder},
10    node::NodeID,
11};
12use firewheel_graph::{backend::AudioBackend, FirewheelCtx};
13
14use crate::FxChain;
15
16/// A default [`FxChain`] for 2D game audio.
17///
18/// This chain contains a single `VolumePan` node.
19#[derive(Default, Debug, Clone, Copy, PartialEq)]
20pub struct VolumePanChain {
21    pub volume_pan: firewheel_nodes::volume_pan::VolumePanNode,
22    pub config: firewheel_nodes::volume_pan::VolumeNodeConfig,
23}
24
25impl VolumePanChain {
26    /// Set the parameters of the volume pan node.
27    ///
28    /// * `params` - The new parameters.
29    /// * `time` - The instant these new parameters should take effect. If this
30    /// is `None`, then the parameters will take effect as soon as the node receives
31    /// the event.
32    pub fn set_params<B: AudioBackend>(
33        &mut self,
34        params: firewheel_nodes::volume_pan::VolumePanNode,
35        #[cfg(feature = "scheduled_events")] time: Option<EventInstant>,
36        node_ids: &[NodeID],
37        cx: &mut FirewheelCtx<B>,
38    ) {
39        let node_id = node_ids[0];
40
41        self.volume_pan.diff(
42            &params,
43            PathBuilder::default(),
44            #[cfg(not(feature = "scheduled_events"))]
45            &mut cx.event_queue(node_id),
46            #[cfg(feature = "scheduled_events")]
47            &mut cx.event_queue_scheduled(node_id, time),
48        );
49    }
50}
51
52impl FxChain for VolumePanChain {
53    fn construct_and_connect<B: AudioBackend>(
54        &mut self,
55        first_node_id: NodeID,
56        first_node_num_out_channels: NonZeroChannelCount,
57        dst_node_id: NodeID,
58        dst_num_channels: NonZeroChannelCount,
59        cx: &mut FirewheelCtx<B>,
60    ) -> Vec<NodeID> {
61        let volume_pan_params = firewheel_nodes::volume_pan::VolumePanNode::default();
62
63        let volume_pan_node_id = cx.add_node(volume_pan_params, Some(self.config));
64
65        cx.connect(
66            first_node_id,
67            volume_pan_node_id,
68            if first_node_num_out_channels.get().get() == 1 {
69                &[(0, 0), (0, 1)]
70            } else {
71                &[(0, 0), (1, 1)]
72            },
73            false,
74        )
75        .unwrap();
76
77        cx.connect(
78            volume_pan_node_id,
79            dst_node_id,
80            if dst_num_channels.get().get() == 1 {
81                &[(0, 0), (1, 0)]
82            } else {
83                &[(0, 0), (1, 1)]
84            },
85            false,
86        )
87        .unwrap();
88
89        vec![volume_pan_node_id]
90    }
91}