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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
pub use function::*;
pub use map::*;
pub use mix::*;

mod function {
    use crate::simple_processor::MonoAudioProcessor;
    use crate::{AudioContext, SimpleAudioProcessor};
    use std::marker::PhantomData;

    pub struct AudioProcessorFunction<F, SampleType> {
        f: F,
        phantom: PhantomData<SampleType>,
    }

    impl<F, SampleType> SimpleAudioProcessor for AudioProcessorFunction<F, SampleType>
    where
        F: FnMut(&mut AudioContext, &mut [SampleType]),
        SampleType: Copy,
    {
        type SampleType = SampleType;

        fn s_process_frame(&mut self, context: &mut AudioContext, frame: &mut [Self::SampleType]) {
            (self.f)(context, frame)
        }
    }

    pub fn processor_function<F, SampleType>(
        f: F,
    ) -> impl SimpleAudioProcessor<SampleType = SampleType>
    where
        F: FnMut(&mut AudioContext, &mut [SampleType]),
        SampleType: Copy,
    {
        AudioProcessorFunction {
            f,
            phantom: PhantomData,
        }
    }

    struct MonoAudioProcessorFunction<F, SampleType> {
        f: F,
        phantom: PhantomData<SampleType>,
    }

    impl<F, SampleType> MonoAudioProcessor for MonoAudioProcessorFunction<F, SampleType>
    where
        F: FnMut(&mut AudioContext, SampleType) -> SampleType,
        SampleType: Copy,
    {
        type SampleType = SampleType;

        fn m_process(
            &mut self,
            context: &mut AudioContext,
            sample: Self::SampleType,
        ) -> Self::SampleType {
            (self.f)(context, sample)
        }
    }

    pub fn mono_processor_function<F, SampleType>(
        f: F,
    ) -> impl MonoAudioProcessor<SampleType = SampleType>
    where
        F: FnMut(&mut AudioContext, SampleType) -> SampleType,
        SampleType: Copy,
    {
        MonoAudioProcessorFunction {
            f,
            phantom: PhantomData,
        }
    }

    pub fn mono_generator_function<F, SampleType>(
        mut f: F,
    ) -> impl MonoAudioProcessor<SampleType = SampleType>
    where
        F: FnMut(&mut AudioContext) -> SampleType,
        SampleType: Copy,
    {
        mono_processor_function(move |context, _sample| f(context))
    }
}

mod map {
    use crate::{AudioBuffer, AudioContext, AudioProcessor};

    struct MapProcessor<P, F> {
        processor: P,
        f: F,
    }

    impl<P, F> AudioProcessor for MapProcessor<P, F>
    where
        P: AudioProcessor,
        F: FnMut(&mut AudioContext, &mut [P::SampleType]),
    {
        type SampleType = P::SampleType;

        fn process<BufferType: AudioBuffer<SampleType = Self::SampleType>>(
            &mut self,
            context: &mut AudioContext,
            output: &mut BufferType,
        ) {
            self.processor.process(context, output);
            for frame in output.frames_mut() {
                (self.f)(context, frame);
            }
        }
    }

    pub fn map_processor<P: AudioProcessor, F: FnMut(&mut AudioContext, &mut [P::SampleType])>(
        processor: P,
        f: F,
    ) -> impl AudioProcessor<SampleType = P::SampleType> {
        MapProcessor { processor, f }
    }
}

mod mix {
    use super::map::map_processor;
    use crate::AudioProcessor;
    use num::{Float, Zero};
    use std::ops::AddAssign;

    pub fn mono<P>(processor: P) -> impl AudioProcessor<SampleType = P::SampleType>
    where
        P: AudioProcessor,
        P::SampleType: Float + AddAssign,
    {
        map_processor(processor, |_ctx, frame| {
            let mut sum = P::SampleType::zero();
            for sample in frame.iter() {
                sum += *sample;
            }
            for sample in frame.iter_mut() {
                *sample = sum;
            }
        })
    }
}