1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::fmt::Debug;

use crate::context::{AudioContextRegistration, BaseAudioContext};
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};

use super::{
    AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
};

/// Options for constructing a [`ChannelMergerNode`]
// dictionary ChannelMergerOptions : AudioNodeOptions {
//   unsigned long numberOfInputs = 6;
// };
#[derive(Clone, Debug)]
pub struct ChannelMergerOptions {
    pub number_of_inputs: usize,
    pub channel_config: ChannelConfigOptions,
}

impl Default for ChannelMergerOptions {
    fn default() -> Self {
        Self {
            number_of_inputs: 6,
            channel_config: ChannelConfigOptions {
                count: 1,
                count_mode: ChannelCountMode::Explicit,
                interpretation: ChannelInterpretation::Speakers,
            },
        }
    }
}

/// AudioNode for combining channels from multiple audio streams into a single audio stream.
pub struct ChannelMergerNode {
    registration: AudioContextRegistration,
    channel_config: ChannelConfig,
}

impl AudioNode for ChannelMergerNode {
    fn registration(&self) -> &AudioContextRegistration {
        &self.registration
    }

    fn channel_config(&self) -> &ChannelConfig {
        &self.channel_config
    }

    fn set_channel_count(&self, _v: usize) {
        panic!("InvalidStateError: Cannot edit channel count of ChannelMergerNode")
    }

    fn set_channel_count_mode(&self, _v: ChannelCountMode) {
        panic!("InvalidStateError: Cannot edit channel count mode of ChannelMergerNode")
    }

    fn number_of_inputs(&self) -> usize {
        self.channel_count()
    }

    fn number_of_outputs(&self) -> usize {
        1
    }
}

impl ChannelMergerNode {
    pub fn new<C: BaseAudioContext>(context: &C, mut options: ChannelMergerOptions) -> Self {
        context.register(move |registration| {
            crate::assert_valid_number_of_channels(options.number_of_inputs);
            options.channel_config.count = options.number_of_inputs;

            let node = ChannelMergerNode {
                registration,
                channel_config: options.channel_config.into(),
            };

            let render = ChannelMergerRenderer {};

            (node, Box::new(render))
        })
    }
}

#[derive(Debug)]
struct ChannelMergerRenderer {}

impl AudioProcessor for ChannelMergerRenderer {
    fn process(
        &mut self,
        inputs: &[AudioRenderQuantum],
        outputs: &mut [AudioRenderQuantum],
        _params: AudioParamValues<'_>,
        _scope: &RenderScope,
    ) -> bool {
        // single output node
        let output = &mut outputs[0];

        // [spec] There is a single output whose audio stream has a number of
        // channels equal to the number of inputs when any of the inputs is actively
        // processing. If none of the inputs are actively processing, then output
        // is a single channel of silence.
        if inputs.iter().any(|input| !input.is_silent()) {
            output.set_number_of_channels(inputs.len());

            inputs.iter().enumerate().for_each(|(i, input)| {
                *output.channel_data_mut(i) = input.channel_data(0).clone();
            });
        } else {
            output.make_silent();
        }

        false
    }
}