rawdio/effects/gain/
gain_node.rs

1use crate::{
2    commands::Id, graph::GraphNode, parameter::*, prelude::*, utility::create_parameters, DspNode,
3};
4
5use super::gain_processor::GainProcessor;
6
7/// A node that applies gain to the input
8///
9/// A gain node must be used in the graph with the same number of input to
10/// output channels
11///
12/// # Parameters
13/// - gain
14pub struct Gain {
15    /// The node to connect into the audio graph
16    pub node: GraphNode,
17
18    params: Parameters,
19}
20
21const MIN_GAIN: f64 = f64::NEG_INFINITY;
22const MAX_GAIN: f64 = f64::INFINITY;
23const DEFAULT_GAIN: f64 = 1.0;
24
25impl DspNode for Gain {
26    fn get_parameters_mut(&mut self) -> &mut Parameters {
27        &mut self.params
28    }
29}
30
31impl Gain {
32    /// Create a new gain node with a given channel count
33    pub fn new(context: &dyn Context, channel_count: usize) -> Self {
34        let id = Id::generate();
35
36        let (params, realtime_params) = create_parameters(
37            id,
38            context,
39            [(
40                "gain",
41                ParameterRange::new(DEFAULT_GAIN, MIN_GAIN, MAX_GAIN),
42            )],
43        );
44
45        let processor = Box::new(GainProcessor::new());
46
47        Self {
48            node: GraphNode::new(
49                id,
50                context,
51                channel_count,
52                channel_count,
53                processor,
54                realtime_params,
55            ),
56            params,
57        }
58    }
59
60    /// Get the gain parameter
61    pub fn gain(&mut self) -> &mut AudioParameter {
62        self.get_parameter_mut("gain")
63    }
64}