tunes 1.1.0

A music composition, synthesis, and audio generation library
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use std::f32::consts::PI;

/// Types of filters available
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterType {
    LowPass,
    HighPass,
    BandPass,
    Notch,    // Notch/band-reject filter (removes frequencies at cutoff)
    AllPass,  // All-pass filter (affects phase but not amplitude)
    Moog,     // Moog ladder filter (classic analog sound with saturation)
    None,     // Bypass filter
}

/// Filter slope/steepness options
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterSlope {
    Pole12dB, // 12dB/octave - smoother, more musical
    Pole24dB, // 24dB/octave - steeper, more aggressive
}

/// A simple state-variable filter implementation
/// This is a 2-pole resonant filter with controllable cutoff and resonance
#[derive(Clone, Copy)]
pub struct Filter {
    pub filter_type: FilterType,
    pub cutoff: f32,    // Cutoff frequency in Hz
    pub resonance: f32, // Resonance/Q factor (0.0 to 1.0)
    pub slope: FilterSlope, // Filter steepness (12dB or 24dB per octave)

    // Internal state for filter processing
    low: f32,
    high: f32,
    band: f32,
    notch: f32,

    // Second stage for 24dB/octave mode
    low2: f32,
    high2: f32,
    band2: f32,
    notch2: f32,

    // Smoothing for parameter changes to avoid zipper noise
    smooth_cutoff: f32,
    smooth_resonance: f32,

    // Moog ladder filter state (4 stages)
    moog_stage: [f32; 4],
    moog_stage_tanh: [f32; 4],
    moog_delay: [f32; 4],

    // Cached coefficients for performance (avoid recalculating sin/cos every sample!)
    cached_f: f32,        // SVF frequency coefficient
    cached_q: f32,        // SVF resonance coefficient
    cached_moog_f: f32,   // Moog frequency coefficient
    cached_moog_k: f32,   // Moog resonance coefficient
    last_smooth_cutoff: f32,
    last_smooth_resonance: f32,

    // Track previous parameter values to skip smoothing when stable
    last_cutoff: f32,
    last_resonance: f32,

    // Function pointer for branchless dispatch (set at construction)
    process_fn: fn(&mut Filter, f32, f32) -> f32,
}

impl std::fmt::Debug for Filter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Filter")
            .field("filter_type", &self.filter_type)
            .field("cutoff", &self.cutoff)
            .field("resonance", &self.resonance)
            .field("slope", &self.slope)
            .finish()
    }
}

impl Filter {
    /// Create a new filter with default 12dB/octave slope
    pub fn new(filter_type: FilterType, cutoff: f32, resonance: f32) -> Self {
        Self::with_slope(filter_type, cutoff, resonance, FilterSlope::Pole12dB)
    }

    /// Create a new filter with specified slope
    pub fn with_slope(filter_type: FilterType, cutoff: f32, resonance: f32, slope: FilterSlope) -> Self {
        let cutoff = cutoff.clamp(20.0, 20000.0);
        let resonance = resonance.clamp(0.0, 0.99);

        // Select appropriate process function (eliminates per-sample branches!)
        let process_fn: fn(&mut Filter, f32, f32) -> f32 = match filter_type {
            FilterType::None => Self::process_none,
            FilterType::Moog => Self::process_moog,
            _ => Self::process_svf,  // All state-variable filters (LowPass, HighPass, etc.)
        };

        Self {
            filter_type,
            cutoff,
            resonance,
            slope,
            low: 0.0,
            high: 0.0,
            band: 0.0,
            notch: 0.0,
            low2: 0.0,
            high2: 0.0,
            band2: 0.0,
            notch2: 0.0,
            smooth_cutoff: cutoff,
            smooth_resonance: resonance,
            moog_stage: [0.0; 4],
            moog_stage_tanh: [0.0; 4],
            moog_delay: [0.0; 4],
            cached_f: 0.0,
            cached_q: 0.0,
            cached_moog_f: 0.0,
            cached_moog_k: 0.0,
            last_smooth_cutoff: -1.0,  // Force initial calculation
            last_smooth_resonance: -1.0,
            last_cutoff: cutoff,
            last_resonance: resonance,
            process_fn,
        }
    }

    /// Create a low-pass filter
    pub fn low_pass(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::LowPass, cutoff, resonance)
    }

    /// Create a high-pass filter
    pub fn high_pass(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::HighPass, cutoff, resonance)
    }

    /// Create a band-pass filter
    pub fn band_pass(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::BandPass, cutoff, resonance)
    }

    /// Create a notch filter (band-reject)
    pub fn notch(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::Notch, cutoff, resonance)
    }

    /// Create an all-pass filter (phase shift without amplitude change)
    pub fn all_pass(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::AllPass, cutoff, resonance)
    }

    /// Create a Moog ladder filter (classic analog sound)
    pub fn moog(cutoff: f32, resonance: f32) -> Self {
        Self::new(FilterType::Moog, cutoff, resonance)
    }

    /// Create a bypass filter (no filtering)
    pub fn none() -> Self {
        Self::new(FilterType::None, 20000.0, 0.0)
    }

    /// Process a single sample through the filter (branchless dispatch via function pointer!)
    #[inline]
    pub fn process(&mut self, input: f32, sample_rate: f32) -> f32 {
        (self.process_fn)(self, input, sample_rate)
    }

    /// Process an entire buffer of samples (optimized for reduced overhead)
    ///
    /// This is significantly faster than calling process() in a loop because:
    /// - Reduced function call overhead
    /// - Better inlining and optimization opportunities
    /// - Improved cache locality
    /// - Branch predictor friendly
    #[inline]
    pub fn process_buffer(&mut self, buffer: &mut [f32], sample_rate: f32) {
        // Dispatch to specialized buffer processing based on filter type
        match self.filter_type {
            FilterType::None => {
                // Bypass - no processing needed
                return;
            }
            FilterType::Moog => {
                self.process_buffer_moog(buffer, sample_rate);
            }
            _ => {
                // State-variable filters (LowPass, HighPass, BandPass, Notch, AllPass)
                self.process_buffer_svf(buffer, sample_rate);
            }
        }
    }

    /// Bypass filter (no processing)
    #[inline]
    fn process_none(&mut self, input: f32, _sample_rate: f32) -> f32 {
        input
    }

    /// State-variable filter processing (LowPass, HighPass, BandPass, Notch, AllPass)
    #[inline]
    fn process_svf(&mut self, input: f32, sample_rate: f32) -> f32 {
        // Smooth parameter changes to avoid zipper noise (simple one-pole smoothing)
        // Rewritten to use FMA operations
        // Reduced smoothing for better modulation response
        const SMOOTHING: f32 = 0.95;
        const INV_SMOOTHING: f32 = 1.0 - SMOOTHING;
        self.smooth_cutoff = self.smooth_cutoff.mul_add(SMOOTHING, self.cutoff * INV_SMOOTHING);
        self.smooth_resonance = self.smooth_resonance.mul_add(SMOOTHING, self.resonance * INV_SMOOTHING);

        // Calculate filter coefficients using smoothed parameters
        let f = 2.0 * (PI * self.smooth_cutoff / sample_rate).sin();
        let q = 1.0 - self.smooth_resonance;

        // First stage: State-variable filter algorithm
        self.low = self.low.mul_add(1.0, f * self.band);
        self.high = input - self.low - q * self.band;
        self.band = self.band.mul_add(1.0, f * self.high);
        self.notch = self.high + self.low;

        // Flush denormals to zero (faster than checking is_finite)
        // This adds a tiny DC offset but prevents denormal slowdown
        self.low = if self.low.abs() < 1e-15 { 0.0 } else { self.low };
        self.band = if self.band.abs() < 1e-15 { 0.0 } else { self.band };

        // Stability check - prevent infinity by clamping instead of resetting
        // Resetting causes audible glitches, so we clamp the state instead
        if self.low.abs() > 100.0 {
            self.low = self.low.clamp(-100.0, 100.0);
            self.band = self.band.clamp(-100.0, 100.0);
            self.high = self.high.clamp(-100.0, 100.0);
        }

        // Get output from first stage
        let stage1_output = match self.filter_type {
            FilterType::LowPass => self.low,
            FilterType::HighPass => self.high,
            FilterType::BandPass => self.band,
            FilterType::Notch => self.notch,
            FilterType::AllPass => self.notch - self.band,
            FilterType::Moog => unreachable!(), // Handled separately above
            FilterType::None => input,
        };

        // For 24dB/octave, run a second stage in series
        let output = match self.slope {
            FilterSlope::Pole12dB => stage1_output,
            FilterSlope::Pole24dB => {
                // Second stage processing (same algorithm, different state)
                self.low2 = self.low2.mul_add(1.0, f * self.band2);
                self.high2 = stage1_output - self.low2 - q * self.band2;
                self.band2 = self.band2.mul_add(1.0, f * self.high2);
                self.notch2 = self.high2 + self.low2;

                // Flush denormals to zero
                self.low2 = if self.low2.abs() < 1e-15 { 0.0 } else { self.low2 };
                self.band2 = if self.band2.abs() < 1e-15 { 0.0 } else { self.band2 };

                // Stability check for second stage - clamp instead of resetting
                if self.low2.abs() > 100.0 {
                    self.low2 = self.low2.clamp(-100.0, 100.0);
                    self.band2 = self.band2.clamp(-100.0, 100.0);
                    self.high2 = self.high2.clamp(-100.0, 100.0);
                }

                // Get output from second stage
                match self.filter_type {
                    FilterType::LowPass => self.low2,
                    FilterType::HighPass => self.high2,
                    FilterType::BandPass => self.band2,
                    FilterType::Notch => self.notch2,
                    FilterType::AllPass => self.notch2 - self.band2,
                    FilterType::Moog => unreachable!(), // Handled separately above
                    FilterType::None => stage1_output,
                }
            }
        };

        // Clamp output to prevent explosion
        output.clamp(-2.0, 2.0)
    }

    /// Moog ladder filter processing (4-pole lowpass with resonance and saturation)
    ///
    /// This is a digital implementation of the legendary Moog ladder filter.
    /// It features:
    /// - 24dB/octave slope (4-pole)
    /// - Self-oscillation at high resonance
    /// - Soft saturation for analog warmth
    /// - Classic "fat" Moog sound
    #[inline]
    fn process_moog(&mut self, input: f32, sample_rate: f32) -> f32 {
        // Smooth parameter changes - reduced for better modulation response
        const SMOOTHING: f32 = 0.95;
        const INV_SMOOTHING: f32 = 1.0 - SMOOTHING;
        self.smooth_cutoff = self.smooth_cutoff.mul_add(SMOOTHING, self.cutoff * INV_SMOOTHING);
        self.smooth_resonance = self.smooth_resonance.mul_add(SMOOTHING, self.resonance * INV_SMOOTHING);

        // Calculate filter coefficient
        // Moog uses a different tuning than SVF
        let fc = self.smooth_cutoff / sample_rate;
        let f = fc.mul_add(1.16, 0.0); // Frequency warping for better tracking

        // Nonlinear feedback coefficient for resonance
        // At resonance = 1.0, filter self-oscillates
        let k = self.smooth_resonance.mul_add(3.96, 0.0);

        // Input with resonance feedback
        // The tanh provides soft saturation (analog modeling)
        let input_compensated = input - k * self.moog_delay[3];
        let input_tanh = input_compensated.tanh();

        // Process through 4 cascaded one-pole filters (the "ladder")
        // Each stage adds saturation for analog warmth
        for i in 0..4 {
            let stage_input = if i == 0 {
                input_tanh
            } else {
                self.moog_stage_tanh[i - 1]
            };

            // One-pole lowpass: y[n] = y[n-1] + f * (x[n] - y[n-1])
            self.moog_stage[i] = self.moog_delay[i].mul_add(1.0 - f, stage_input * f);

            // Soft saturation using tanh
            self.moog_stage_tanh[i] = self.moog_stage[i].tanh();

            // Store for next sample
            self.moog_delay[i] = self.moog_stage[i];
        }

        // Output is from the 4th stage
        // Apply compensation for resonance boost
        let output = self.moog_stage_tanh[3];

        // Clamp output
        output.clamp(-2.0, 2.0)
    }

    /// Reset the filter state (call when starting new notes or to avoid clicks)
    pub fn reset(&mut self) {
        self.low = 0.0;
        self.high = 0.0;
        self.band = 0.0;
        self.notch = 0.0;
        self.low2 = 0.0;
        self.high2 = 0.0;
        self.band2 = 0.0;
        self.notch2 = 0.0;
        self.smooth_cutoff = self.cutoff;
        self.smooth_resonance = self.resonance;
        self.moog_stage = [0.0; 4];
        self.moog_stage_tanh = [0.0; 4];
        self.moog_delay = [0.0; 4];
        self.last_smooth_cutoff = -1.0;  // Force recalculation
        self.last_smooth_resonance = -1.0;
        self.last_cutoff = self.cutoff;
        self.last_resonance = self.resonance;
    }

    /// Optimized buffer processing for state-variable filters
    #[inline]
    fn process_buffer_svf(&mut self, buffer: &mut [f32], sample_rate: f32) {
        use crate::synthesis::simd::SimdLanes;

        // Precompute smoothing constants (outside the loop!)
        const SMOOTHING: f32 = 0.95;
        const INV_SMOOTHING: f32 = 1.0 - SMOOTHING;
        const COEFF_UPDATE_THRESHOLD: f32 = 0.0001;  // 0.01% change threshold
        const CONVERGENCE_THRESHOLD: f32 = 0.0001;   // Consider converged when this close to target

        // Check if parameters have changed or if smoothing is still needed
        let params_changed = self.cutoff != self.last_cutoff || self.resonance != self.last_resonance;
        let cutoff_converged = (self.smooth_cutoff - self.cutoff).abs() < CONVERGENCE_THRESHOLD;
        let resonance_converged = (self.smooth_resonance - self.resonance).abs() < CONVERGENCE_THRESHOLD;
        let needs_smoothing = params_changed || !cutoff_converged || !resonance_converged;

        if params_changed {
            self.last_cutoff = self.cutoff;
            self.last_resonance = self.resonance;
        }

        // Process buffer
        for sample in buffer.iter_mut() {
            let input = *sample;

            // Only smooth if parameters changed or not yet converged (saves 2 FMA ops per sample!)
            if needs_smoothing {
                self.smooth_cutoff = self.smooth_cutoff.mul_add(SMOOTHING, self.cutoff * INV_SMOOTHING);
                self.smooth_resonance = self.smooth_resonance.mul_add(SMOOTHING, self.resonance * INV_SMOOTHING);
            }

            // Update cached coefficients only if parameters changed significantly
            // This avoids expensive sin() calls when parameters are stable
            let cutoff_changed = (self.smooth_cutoff - self.last_smooth_cutoff).abs()
                > self.last_smooth_cutoff.abs() * COEFF_UPDATE_THRESHOLD;
            let resonance_changed = (self.smooth_resonance - self.last_smooth_resonance).abs()
                > COEFF_UPDATE_THRESHOLD;

            if cutoff_changed || resonance_changed {
                // OPTIMIZATION: Use fast_sin instead of stdlib sin!
                self.cached_f = 2.0 * f32::fast_sin(PI * self.smooth_cutoff / sample_rate);
                self.cached_q = 1.0 - self.smooth_resonance;
                self.last_smooth_cutoff = self.smooth_cutoff;
                self.last_smooth_resonance = self.smooth_resonance;
            }

            // Use cached coefficients
            let f = self.cached_f;
            let q = self.cached_q;

            // First stage: State-variable filter
            self.low = self.low.mul_add(1.0, f * self.band);
            self.high = input - self.low - q * self.band;
            self.band = self.band.mul_add(1.0, f * self.high);
            self.notch = self.high + self.low;

            // Flush denormals to zero (compilers optimize to branchless cmov)
            self.low = if self.low.abs() < 1e-15 { 0.0 } else { self.low };
            self.band = if self.band.abs() < 1e-15 { 0.0 } else { self.band };

            // Clamp state for stability (branchless)
            self.low = self.low.clamp(-100.0, 100.0);
            self.band = self.band.clamp(-100.0, 100.0);
            self.high = self.high.clamp(-100.0, 100.0);

            // Get output from first stage (branchless using multiplication)
            let stage1_output = match self.filter_type {
                FilterType::LowPass => self.low,
                FilterType::HighPass => self.high,
                FilterType::BandPass => self.band,
                FilterType::Notch => self.notch,
                FilterType::AllPass => self.notch - self.band,
                _ => input,
            };

            // Second stage for 24dB/octave
            let output = if matches!(self.slope, FilterSlope::Pole24dB) {
                self.low2 = self.low2.mul_add(1.0, f * self.band2);
                self.high2 = stage1_output - self.low2 - q * self.band2;
                self.band2 = self.band2.mul_add(1.0, f * self.high2);
                self.notch2 = self.high2 + self.low2;

                // Flush denormals to zero (compilers optimize to branchless cmov)
                self.low2 = if self.low2.abs() < 1e-15 { 0.0 } else { self.low2 };
                self.band2 = if self.band2.abs() < 1e-15 { 0.0 } else { self.band2 };

                // Clamp for stability
                self.low2 = self.low2.clamp(-100.0, 100.0);
                self.band2 = self.band2.clamp(-100.0, 100.0);
                self.high2 = self.high2.clamp(-100.0, 100.0);

                match self.filter_type {
                    FilterType::LowPass => self.low2,
                    FilterType::HighPass => self.high2,
                    FilterType::BandPass => self.band2,
                    FilterType::Notch => self.notch2,
                    FilterType::AllPass => self.notch2 - self.band2,
                    _ => stage1_output,
                }
            } else {
                stage1_output
            };

            *sample = output.clamp(-2.0, 2.0);
        }
    }

    /// Optimized buffer processing for Moog ladder filter with fast_tanh
    #[inline]
    fn process_buffer_moog(&mut self, buffer: &mut [f32], sample_rate: f32) {
        use crate::synthesis::simd::SimdLanes;

        const SMOOTHING: f32 = 0.95;
        const INV_SMOOTHING: f32 = 1.0 - SMOOTHING;
        const COEFF_UPDATE_THRESHOLD: f32 = 0.0001;
        const CONVERGENCE_THRESHOLD: f32 = 0.0001;

        // Check if parameters have changed or if smoothing is still needed
        let params_changed = self.cutoff != self.last_cutoff || self.resonance != self.last_resonance;
        let cutoff_converged = (self.smooth_cutoff - self.cutoff).abs() < CONVERGENCE_THRESHOLD;
        let resonance_converged = (self.smooth_resonance - self.resonance).abs() < CONVERGENCE_THRESHOLD;
        let needs_smoothing = params_changed || !cutoff_converged || !resonance_converged;

        if params_changed {
            self.last_cutoff = self.cutoff;
            self.last_resonance = self.resonance;
        }

        for sample in buffer.iter_mut() {
            let input = *sample;

            // Only smooth if parameters changed or not yet converged (saves 2 FMA ops per sample!)
            if needs_smoothing {
                self.smooth_cutoff = self.smooth_cutoff.mul_add(SMOOTHING, self.cutoff * INV_SMOOTHING);
                self.smooth_resonance = self.smooth_resonance.mul_add(SMOOTHING, self.resonance * INV_SMOOTHING);
            }

            // Update cached coefficients only if needed
            let cutoff_changed = (self.smooth_cutoff - self.last_smooth_cutoff).abs()
                > self.last_smooth_cutoff.abs() * COEFF_UPDATE_THRESHOLD;
            let resonance_changed = (self.smooth_resonance - self.last_smooth_resonance).abs()
                > COEFF_UPDATE_THRESHOLD;

            if cutoff_changed || resonance_changed {
                let fc = self.smooth_cutoff / sample_rate;
                self.cached_moog_f = fc.mul_add(1.16, 0.0);
                self.cached_moog_k = self.smooth_resonance.mul_add(3.96, 0.0);
                self.last_smooth_cutoff = self.smooth_cutoff;
                self.last_smooth_resonance = self.smooth_resonance;
            }

            // Use cached coefficients
            let f = self.cached_moog_f;
            let k = self.cached_moog_k;

            // Input with feedback
            let input_compensated = input - k * self.moog_delay[3];

            // OPTIMIZATION: Use fast_tanh instead of stdlib tanh!
            let input_tanh = f32::fast_tanh(input_compensated);

            // Process through 4 stages
            for i in 0..4 {
                let stage_input = if i == 0 {
                    input_tanh
                } else {
                    self.moog_stage_tanh[i - 1]
                };

                // One-pole lowpass
                self.moog_stage[i] = self.moog_delay[i].mul_add(1.0 - f, stage_input * f);

                // OPTIMIZATION: Use fast_tanh!
                self.moog_stage_tanh[i] = f32::fast_tanh(self.moog_stage[i]);

                // Store for next sample
                self.moog_delay[i] = self.moog_stage[i];
            }

            // Output from 4th stage
            let output = self.moog_stage_tanh[3];
            *sample = output.clamp(-2.0, 2.0);
        }
    }
}

impl Default for Filter {
    fn default() -> Self {
        Self::none()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_filter_creation() {
        let lpf = Filter::low_pass(1000.0, 0.5);
        assert_eq!(lpf.filter_type, FilterType::LowPass);
        assert_eq!(lpf.cutoff, 1000.0);

        let hpf = Filter::high_pass(500.0, 0.3);
        assert_eq!(hpf.filter_type, FilterType::HighPass);

        let bpf = Filter::band_pass(2000.0, 0.7);
        assert_eq!(bpf.filter_type, FilterType::BandPass);
    }

    #[test]
    fn test_filter_bypass() {
        let mut filter = Filter::none();
        let input = 0.5;
        let output = filter.process(input, 44100.0);
        assert_eq!(input, output);
    }

    #[test]
    fn test_notch_filter_creation() {
        let notch = Filter::notch(1000.0, 0.7);
        assert_eq!(notch.filter_type, FilterType::Notch);
        assert_eq!(notch.cutoff, 1000.0);
        assert_eq!(notch.resonance, 0.7);
    }

    #[test]
    fn test_allpass_filter_creation() {
        let allpass = Filter::all_pass(2000.0, 0.5);
        assert_eq!(allpass.filter_type, FilterType::AllPass);
        assert_eq!(allpass.cutoff, 2000.0);
    }

    #[test]
    fn test_filter_slopes() {
        let filter_12db = Filter::with_slope(FilterType::LowPass, 1000.0, 0.5, FilterSlope::Pole12dB);
        assert_eq!(filter_12db.slope, FilterSlope::Pole12dB);

        let filter_24db = Filter::with_slope(FilterType::LowPass, 1000.0, 0.5, FilterSlope::Pole24dB);
        assert_eq!(filter_24db.slope, FilterSlope::Pole24dB);
    }

    #[test]
    fn test_24db_filter_processes() {
        let mut filter = Filter::with_slope(FilterType::LowPass, 1000.0, 0.5, FilterSlope::Pole24dB);
        let input = 0.5;
        let output = filter.process(input, 44100.0);

        // Should process without crashing and produce valid output
        assert!(output.is_finite());
        assert!(output.abs() <= 2.0);
    }

    #[test]
    fn test_notch_filter_processes() {
        let mut filter = Filter::notch(1000.0, 0.7);
        let input = 0.5;
        let output = filter.process(input, 44100.0);

        // Should process without crashing
        assert!(output.is_finite());
    }

    #[test]
    fn test_allpass_filter_processes() {
        let mut filter = Filter::all_pass(1000.0, 0.5);
        let input = 0.5;
        let output = filter.process(input, 44100.0);

        // Should process without crashing
        assert!(output.is_finite());
    }

    #[test]
    fn test_moog_filter_creation() {
        let moog = Filter::moog(1000.0, 0.7);
        assert_eq!(moog.filter_type, FilterType::Moog);
        assert_eq!(moog.cutoff, 1000.0);
        assert_eq!(moog.resonance, 0.7);
    }

    #[test]
    fn test_moog_filter_processes() {
        let mut filter = Filter::moog(1000.0, 0.5);
        let input = 0.5;
        let output = filter.process(input, 44100.0);

        // Should process without crashing and produce valid output
        assert!(output.is_finite());
        assert!(output.abs() <= 2.0);
    }

    #[test]
    fn test_moog_high_resonance() {
        // Test Moog filter with high resonance (should self-oscillate)
        let mut filter = Filter::moog(440.0, 0.95);

        // Process multiple samples to let oscillation build up
        for _ in 0..100 {
            let output = filter.process(0.0, 44100.0);
            assert!(output.is_finite());
            assert!(output.abs() <= 2.0);
        }
    }

    #[test]
    fn test_moog_sweep() {
        // Test Moog filter with cutoff sweep
        let mut filter = Filter::moog(100.0, 0.7);

        for i in 0..100 {
            filter.cutoff = 100.0 + i as f32 * 100.0;
            let output = filter.process(0.5, 44100.0);
            assert!(output.is_finite());
        }
    }
}