servo_media_audio/
channel_node.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use crate::block::{Block, Chunk, FRAMES_PER_BLOCK_USIZE};
6use crate::node::{
7    AudioNodeEngine, AudioNodeType, BlockInfo, ChannelCountMode, ChannelInfo, ChannelInterpretation,
8};
9
10#[derive(Copy, Clone, Debug)]
11pub struct ChannelNodeOptions {
12    pub channels: u8,
13}
14
15#[derive(AudioNodeCommon)]
16pub(crate) struct ChannelMergerNode {
17    channel_info: ChannelInfo,
18    channels: u8,
19}
20
21impl ChannelMergerNode {
22    pub fn new(params: ChannelNodeOptions, channel_info: ChannelInfo) -> Self {
23        ChannelMergerNode {
24            channel_info,
25            channels: params.channels,
26        }
27    }
28}
29
30impl AudioNodeEngine for ChannelMergerNode {
31    fn node_type(&self) -> AudioNodeType {
32        AudioNodeType::ChannelMergerNode
33    }
34
35    fn process(&mut self, mut inputs: Chunk, _: &BlockInfo) -> Chunk {
36        debug_assert!(inputs.len() == self.channels as usize);
37
38        let mut block = Block::default();
39        block.repeat(self.channels);
40        block.explicit_repeat();
41
42        for (i, channel) in block
43            .data_mut()
44            .chunks_mut(FRAMES_PER_BLOCK_USIZE)
45            .enumerate()
46        {
47            channel.copy_from_slice(inputs.blocks[i].data_mut())
48        }
49
50        inputs.blocks.clear();
51        inputs.blocks.push(block);
52        inputs
53    }
54
55    fn input_count(&self) -> u32 {
56        self.channels as u32
57    }
58
59    fn set_channel_count_mode(&mut self, _: ChannelCountMode) {
60        panic!("channel merger nodes cannot have their mode changed");
61    }
62
63    fn set_channel_count(&mut self, _: u8) {
64        panic!("channel merger nodes cannot have their channel count changed");
65    }
66}
67
68#[derive(AudioNodeCommon)]
69pub(crate) struct ChannelSplitterNode {
70    channel_info: ChannelInfo,
71}
72
73impl ChannelSplitterNode {
74    pub fn new(channel_info: ChannelInfo) -> Self {
75        ChannelSplitterNode { channel_info }
76    }
77}
78
79impl AudioNodeEngine for ChannelSplitterNode {
80    fn node_type(&self) -> AudioNodeType {
81        AudioNodeType::ChannelSplitterNode
82    }
83
84    fn process(&mut self, mut inputs: Chunk, _: &BlockInfo) -> Chunk {
85        debug_assert!(inputs.len() == 1);
86
87        let original = inputs.blocks.pop().unwrap();
88
89        if original.is_silence() {
90            inputs
91                .blocks
92                .resize(original.chan_count() as usize, Block::default())
93        } else {
94            for chan in 0..original.chan_count() {
95                let mut block = Block::empty();
96                block.push_chan(original.data_chan(chan));
97                inputs.blocks.push(block);
98            }
99        }
100
101        inputs
102    }
103
104    fn output_count(&self) -> u32 {
105        self.channel_count() as u32
106    }
107
108    fn set_channel_count_mode(&mut self, _: ChannelCountMode) {
109        panic!("channel splitter nodes cannot have their mode changed");
110    }
111
112    fn set_channel_interpretation(&mut self, _: ChannelInterpretation) {
113        panic!("channel splitter nodes cannot have their channel interpretation changed");
114    }
115
116    fn set_channel_count(&mut self, _: u8) {
117        panic!("channel splitter nodes cannot have their channel count changed");
118    }
119}