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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//! Core traits for synchronous sample and block processing.
/// Processing block
///
/// Single-input processing with state held in `self`.
///
/// This is the simplest trait in the crate: one new sample goes in, one output
/// value comes out. Override [`block()`](Self::block) when a specialized loop can
/// reuse scratch storage, reduce bounds checks, or better match the desired data
/// layout.
///
/// [`SplitProcess`] is the corresponding trait when immutable configuration and
/// mutable runtime state should be separated.
///
/// # Examples
///
/// ```rust
/// use dsp_process::Process;
///
/// #[derive(Default)]
/// struct Acc(i32);
///
/// impl Process<i32> for Acc {
/// fn process(&mut self, x: i32) -> i32 {
/// self.0 += x;
/// self.0
/// }
/// }
///
/// let mut acc = Acc::default();
/// assert_eq!(acc.process(2), 2);
/// assert_eq!(acc.process(3), 5);
/// ```
/// Inplace processing
///
/// This is a convenience trait for processors where input and output element
/// types are identical and the computation can be expressed as overwriting a
/// mutable slice.
///
/// See also [`SplitInplace`] for the split configuration/state form.
/// Processing with split state
///
/// Splitting configuration (the part of the filter that is unaffected
/// by processing inputs, e.g. "coefficients"), from state (the part
/// that is modified by processing) allows:
///
/// * Separating mutable from immutable state guarantees consistency
/// (configuration can not change state and processing can
/// not change configuration)
/// * Reduces memory traffic when swapping configuration
/// * Allows the same filter to be applied to multiple states
/// (e.g. IQ data, multiple lanes) guaranteeing consistency,
/// reducing memory usage, and improving caching.
///
/// This is the central abstraction used throughout `dsp-process`. A typical DSP
/// filter coefficient set becomes `Self`, while delay lines, accumulators, and
/// history buffers become the separate `state` argument.
///
/// Use this when one configuration should drive many runtime states, or when it
/// is beneficial to keep mutable state small and move immutable data out of hot
/// loops.
///
/// [`Process`] is often easier when state and configuration naturally live
/// together, while [`crate::Split`] turns a `SplitProcess` back into a stateful
/// [`Process`] value.
///
/// # Examples
///
/// ```rust
/// use dsp_process::SplitProcess;
///
/// #[derive(Copy, Clone)]
/// struct Gain(i32);
///
/// impl SplitProcess<i32> for Gain {
/// fn process(&self, _: &mut (), x: i32) -> i32 {
/// self.0 * x
/// }
/// }
///
/// let mut state = ();
/// assert_eq!(Gain(4).process(&mut state, 3), 12);
/// ```
/// Inplace processing with a split state
///
/// This is the split-state companion to [`Inplace`]. Implement it when a
/// `SplitProcess<X, X, S>` can update a buffer in place more efficiently than
/// routing through a separate output slice.
//////////// BLANKET ////////////
/// Wrap a `FnMut` into a `Process`/`Inplace`
///
/// This is useful for quick experiments, benchmarks, or adapters at the edge of
/// a pipeline. For reusable DSP stages, prefer a named type once the closure
/// starts carrying real semantics.
///
/// # Examples
///
/// ```rust
/// use dsp_process::{FnProcess, Process};
///
/// let mut square = FnProcess(|x: i32| x * x);
/// assert_eq!(square.process(7), 49);
/// ```
;
/// Wrap a `Fn` into a `SplitProcess`/`SplitInplace`
///
/// The closure receives both the mutable split state and the new input sample.
/// This is a compact way to prototype split-state processors before promoting
/// them to named types.
///
/// # Examples
///
/// ```rust
/// use dsp_process::{FnSplitProcess, SplitProcess};
///
/// let proc = FnSplitProcess(|state: &mut i32, x: i32| {
/// *state += x;
/// *state
/// });
///
/// let mut state = 0;
/// assert_eq!(proc.process(&mut state, 2), 2);
/// assert_eq!(proc.process(&mut state, 3), 5);
/// ```
;