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