firewheel_pool/
volume_pan.rs

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