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
use crate::parameters::{AudioProcessorHandleProvider, AudioProcessorHandleRef};
use crate::{
AudioBuffer, AudioProcessor, AudioProcessorSettings, MidiEventHandler, MidiMessageLike,
};
use std::ops::Deref;
pub trait SimpleAudioProcessor {
type SampleType: Copy;
fn s_prepare(&mut self, _settings: AudioProcessorSettings) {}
fn s_process(&mut self, sample: Self::SampleType) -> Self::SampleType {
sample
}
fn s_process_frame(&mut self, frame: &mut [Self::SampleType]) {
for sample in frame {
*sample = self.s_process(*sample);
}
}
}
#[derive(Clone, Default, Debug)]
pub struct BufferProcessor<Processor>(pub Processor);
#[inline]
pub fn process_buffer<Processor, BufferType>(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(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, settings: AudioProcessorSettings) {
self.0.s_prepare(settings);
}
#[inline]
fn process<BufferType: AudioBuffer<SampleType = Self::SampleType>>(
&mut self,
data: &mut BufferType,
) {
process_buffer(&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)
}
}