firewheel_nodes/
stereo_to_mono.rs1use firewheel_core::{
2 channel_config::{ChannelConfig, ChannelCount},
3 event::ProcEvents,
4 node::{
5 AudioNode, AudioNodeInfo, AudioNodeProcessor, ConstructProcessorContext, EmptyConfig,
6 ProcBuffers, ProcExtra, ProcInfo, ProcessStatus,
7 },
8};
9
10#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
12#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Component))]
13#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct StereoToMonoNode;
16
17impl AudioNode for StereoToMonoNode {
18 type Configuration = EmptyConfig;
19
20 fn info(&self, _config: &Self::Configuration) -> AudioNodeInfo {
21 AudioNodeInfo::new()
22 .debug_name("stereo_to_mono")
23 .channel_config(ChannelConfig {
24 num_inputs: ChannelCount::STEREO,
25 num_outputs: ChannelCount::MONO,
26 })
27 }
28
29 fn construct_processor(
30 &self,
31 _config: &Self::Configuration,
32 _cx: ConstructProcessorContext,
33 ) -> impl AudioNodeProcessor {
34 StereoToMonoProcessor
35 }
36}
37
38struct StereoToMonoProcessor;
39
40impl AudioNodeProcessor for StereoToMonoProcessor {
41 fn process(
42 &mut self,
43 info: &ProcInfo,
44 buffers: ProcBuffers,
45 _events: &mut ProcEvents,
46 _extra: &mut ProcExtra,
47 ) -> ProcessStatus {
48 if info.in_silence_mask.all_channels_silent(2)
49 || buffers.inputs.len() < 2
50 || buffers.outputs.is_empty()
51 {
52 return ProcessStatus::ClearAllOutputs;
53 }
54
55 for (out_s, (&in1, &in2)) in buffers.outputs[0]
56 .iter_mut()
57 .zip(buffers.inputs[0].iter().zip(buffers.inputs[1].iter()))
58 {
59 *out_s = (in1 + in2) * 0.5;
60 }
61
62 ProcessStatus::OutputsModified
63 }
64}