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
142
143
144
145
146
147
148
149
150
151
152
153
use crate::parameters::{AudioProcessorHandleProvider, AudioProcessorHandleRef};
use crate::{
AudioBuffer, AudioContext, AudioProcessor, AudioProcessorSettings, MidiEventHandler,
MidiMessageLike,
};
use num::traits::FloatConst;
use num::Float;
use std::ops::Deref;
pub trait MonoAudioProcessor {
type SampleType: Copy;
fn m_prepare(&mut self, _context: &mut AudioContext, _settings: AudioProcessorSettings) {}
fn m_process(
&mut self,
_context: &mut AudioContext,
sample: Self::SampleType,
) -> Self::SampleType {
sample
}
}
impl<M, ST> SimpleAudioProcessor for M
where
M: MonoAudioProcessor<SampleType = ST>,
ST: Copy + std::iter::Sum<ST> + Float + FloatConst,
{
type SampleType = M::SampleType;
fn s_prepare(&mut self, context: &mut AudioContext, settings: AudioProcessorSettings) {
self.m_prepare(context, settings);
}
fn s_process_frame(&mut self, context: &mut AudioContext, frame: &mut [Self::SampleType]) {
let sum = frame.iter().cloned().sum();
let output =
self.m_process(context, sum) / ST::from(frame.len()).unwrap_or_else(|| ST::one());
for sample in frame.iter_mut() {
*sample = output;
}
}
}
pub trait SimpleAudioProcessor {
type SampleType: Copy;
fn s_prepare(&mut self, _context: &mut AudioContext, _settings: AudioProcessorSettings) {}
fn s_process_frame(&mut self, _context: &mut AudioContext, _frame: &mut [Self::SampleType]) {}
}
#[derive(Clone, Default, Debug)]
pub struct BufferProcessor<Processor>(pub Processor);
#[inline]
pub fn process_buffer<Processor, BufferType>(
context: &mut AudioContext,
processor: &mut Processor,
data: &mut BufferType,
) where
Processor: SimpleAudioProcessor,
<Processor as SimpleAudioProcessor>::SampleType: Copy,
BufferType: AudioBuffer<SampleType = Processor::SampleType>,
{
for frame in data.frames_mut() {
processor.s_process_frame(context, frame);
}
}
impl<Processor> Deref for BufferProcessor<Processor> {
type Target = Processor;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<Processor> AudioProcessorHandleProvider for BufferProcessor<Processor>
where
Processor: AudioProcessorHandleProvider,
{
fn generic_handle(&self) -> AudioProcessorHandleRef {
self.0.generic_handle()
}
}
impl<Processor> AudioProcessor for BufferProcessor<Processor>
where
Processor: SimpleAudioProcessor,
<Processor as SimpleAudioProcessor>::SampleType: Copy,
{
type SampleType = <Processor as SimpleAudioProcessor>::SampleType;
fn prepare(&mut self, context: &mut AudioContext, settings: AudioProcessorSettings) {
self.0.s_prepare(context, settings);
}
#[inline]
fn process<BufferType: AudioBuffer<SampleType = Self::SampleType>>(
&mut self,
context: &mut AudioContext,
data: &mut BufferType,
) {
process_buffer(context, &mut self.0, data)
}
}
impl<Processor> MidiEventHandler for BufferProcessor<Processor>
where
Processor: MidiEventHandler,
{
fn process_midi_events<Message: MidiMessageLike>(&mut self, midi_messages: &[Message]) {
self.0.process_midi_events(midi_messages)
}
}