ringkernel-audio-fft 1.1.0

GPU-accelerated audio FFT processing with direct/ambience separation using RingKernel actors
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
//! Dry/wet mixer with gain control.
//!
//! This module provides mixing functionality to combine the separated
//! direct and ambient signals with user-controllable parameters.

use crate::messages::{Complex, SeparatedBin};

/// Configuration for the mixer.
#[derive(Debug, Clone)]
pub struct MixerConfig {
    /// Dry/wet mix (0.0 = all direct, 1.0 = all ambience).
    pub dry_wet: f32,
    /// Direct signal gain (1.0 = unity).
    pub direct_gain: f32,
    /// Ambience signal gain (1.0 = unity).
    pub ambience_gain: f32,
    /// Output gain (applied to final mix).
    pub output_gain: f32,
    /// Soft clip threshold (None = no limiting).
    pub soft_clip_threshold: Option<f32>,
}

impl Default for MixerConfig {
    fn default() -> Self {
        Self {
            dry_wet: 0.5,
            direct_gain: 1.0,
            ambience_gain: 1.0,
            output_gain: 1.0,
            soft_clip_threshold: Some(0.95),
        }
    }
}

impl MixerConfig {
    /// Create a new mixer configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the dry/wet mix.
    pub fn with_dry_wet(mut self, dry_wet: f32) -> Self {
        self.dry_wet = dry_wet.clamp(0.0, 1.0);
        self
    }

    /// Set the direct gain.
    pub fn with_direct_gain(mut self, gain: f32) -> Self {
        self.direct_gain = gain.max(0.0);
        self
    }

    /// Set the ambience gain.
    pub fn with_ambience_gain(mut self, gain: f32) -> Self {
        self.ambience_gain = gain.max(0.0);
        self
    }

    /// Set the output gain.
    pub fn with_output_gain(mut self, gain: f32) -> Self {
        self.output_gain = gain.max(0.0);
        self
    }

    /// Set soft clip threshold.
    pub fn with_soft_clip(mut self, threshold: Option<f32>) -> Self {
        self.soft_clip_threshold = threshold.map(|t| t.clamp(0.1, 1.0));
        self
    }

    /// Preset for direct-only output.
    pub fn direct_only() -> Self {
        Self {
            dry_wet: 0.0,
            direct_gain: 1.0,
            ambience_gain: 0.0,
            output_gain: 1.0,
            soft_clip_threshold: Some(0.95),
        }
    }

    /// Preset for ambience-only output.
    pub fn ambience_only() -> Self {
        Self {
            dry_wet: 1.0,
            direct_gain: 0.0,
            ambience_gain: 1.0,
            output_gain: 1.0,
            soft_clip_threshold: Some(0.95),
        }
    }

    /// Preset for balanced mix with boost.
    pub fn balanced_with_boost(boost_db: f32) -> Self {
        let linear_gain = 10.0_f32.powf(boost_db / 20.0);
        Self {
            dry_wet: 0.5,
            direct_gain: 1.0,
            ambience_gain: 1.0,
            output_gain: linear_gain,
            soft_clip_threshold: Some(0.95),
        }
    }
}

/// Dry/wet mixer for separated signals.
pub struct DryWetMixer {
    config: MixerConfig,
    /// Peak level tracking for direct signal.
    direct_peak: f32,
    /// Peak level tracking for ambience signal.
    ambience_peak: f32,
    /// Peak level tracking for output.
    output_peak: f32,
}

impl DryWetMixer {
    /// Create a new mixer with default configuration.
    pub fn new() -> Self {
        Self::with_config(MixerConfig::default())
    }

    /// Create a new mixer with specific configuration.
    pub fn with_config(config: MixerConfig) -> Self {
        Self {
            config,
            direct_peak: 0.0,
            ambience_peak: 0.0,
            output_peak: 0.0,
        }
    }

    /// Get the current configuration.
    pub fn config(&self) -> &MixerConfig {
        &self.config
    }

    /// Update the configuration.
    pub fn set_config(&mut self, config: MixerConfig) {
        self.config = config;
    }

    /// Set dry/wet mix (0.0 = all direct, 1.0 = all ambience).
    pub fn set_dry_wet(&mut self, dry_wet: f32) {
        self.config.dry_wet = dry_wet.clamp(0.0, 1.0);
    }

    /// Set direct signal gain.
    pub fn set_direct_gain(&mut self, gain: f32) {
        self.config.direct_gain = gain.max(0.0);
    }

    /// Set ambience signal gain.
    pub fn set_ambience_gain(&mut self, gain: f32) {
        self.config.ambience_gain = gain.max(0.0);
    }

    /// Set output gain.
    pub fn set_output_gain(&mut self, gain: f32) {
        self.config.output_gain = gain.max(0.0);
    }

    /// Set output gain in dB.
    pub fn set_output_gain_db(&mut self, gain_db: f32) {
        self.config.output_gain = 10.0_f32.powf(gain_db / 20.0);
    }

    /// Mix a separated bin and return the combined value.
    pub fn mix_bin(&mut self, bin: &SeparatedBin) -> Complex {
        // Apply gains to each component
        let direct = bin.direct.scale(self.config.direct_gain);
        let ambience = bin.ambience.scale(self.config.ambience_gain);

        // Track peaks
        self.direct_peak = self.direct_peak.max(direct.magnitude());
        self.ambience_peak = self.ambience_peak.max(ambience.magnitude());

        // Mix based on dry/wet
        let dry_amount = 1.0 - self.config.dry_wet;
        let wet_amount = self.config.dry_wet;

        let mixed = Complex {
            re: direct.re * dry_amount + ambience.re * wet_amount,
            im: direct.im * dry_amount + ambience.im * wet_amount,
        };

        // Apply output gain
        let output = mixed.scale(self.config.output_gain);

        // Track output peak
        self.output_peak = self.output_peak.max(output.magnitude());

        // Apply soft clipping if enabled
        if let Some(threshold) = self.config.soft_clip_threshold {
            self.soft_clip(output, threshold)
        } else {
            output
        }
    }

    /// Mix multiple separated bins.
    pub fn mix_frame(&mut self, bins: &[SeparatedBin]) -> Vec<Complex> {
        bins.iter().map(|bin| self.mix_bin(bin)).collect()
    }

    /// Get only the direct component (with gain).
    pub fn direct_only(&self, bin: &SeparatedBin) -> Complex {
        bin.direct
            .scale(self.config.direct_gain * self.config.output_gain)
    }

    /// Get only the ambience component (with gain).
    pub fn ambience_only(&self, bin: &SeparatedBin) -> Complex {
        bin.ambience
            .scale(self.config.ambience_gain * self.config.output_gain)
    }

    /// Extract direct bins from separated data.
    pub fn extract_direct(&self, bins: &[SeparatedBin]) -> Vec<Complex> {
        bins.iter().map(|bin| self.direct_only(bin)).collect()
    }

    /// Extract ambience bins from separated data.
    pub fn extract_ambience(&self, bins: &[SeparatedBin]) -> Vec<Complex> {
        bins.iter().map(|bin| self.ambience_only(bin)).collect()
    }

    /// Apply soft clipping to a complex value.
    fn soft_clip(&self, value: Complex, threshold: f32) -> Complex {
        let magnitude = value.magnitude();

        if magnitude <= threshold {
            return value;
        }

        // Soft knee compression above threshold
        let overshoot = magnitude - threshold;
        let compressed = threshold + overshoot.tanh() * (1.0 - threshold);

        // Scale to new magnitude while preserving phase
        if magnitude > 1e-10 {
            value.scale(compressed / magnitude)
        } else {
            value
        }
    }

    /// Get peak levels (direct, ambience, output).
    pub fn peak_levels(&self) -> (f32, f32, f32) {
        (self.direct_peak, self.ambience_peak, self.output_peak)
    }

    /// Get peak levels in dB.
    pub fn peak_levels_db(&self) -> (f32, f32, f32) {
        let to_db = |level: f32| {
            if level > 1e-10 {
                20.0 * level.log10()
            } else {
                -200.0
            }
        };

        (
            to_db(self.direct_peak),
            to_db(self.ambience_peak),
            to_db(self.output_peak),
        )
    }

    /// Reset peak tracking.
    pub fn reset_peaks(&mut self) {
        self.direct_peak = 0.0;
        self.ambience_peak = 0.0;
        self.output_peak = 0.0;
    }
}

impl Default for DryWetMixer {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of mixing a frame.
#[derive(Debug, Clone)]
pub struct MixedFrame {
    /// Mixed frequency bins.
    pub bins: Vec<Complex>,
    /// Direct component only.
    pub direct_bins: Vec<Complex>,
    /// Ambience component only.
    pub ambience_bins: Vec<Complex>,
    /// Frame ID.
    pub frame_id: u64,
}

impl MixedFrame {
    /// Create a new mixed frame.
    pub fn new(
        bins: Vec<Complex>,
        direct: Vec<Complex>,
        ambience: Vec<Complex>,
        frame_id: u64,
    ) -> Self {
        Self {
            bins,
            direct_bins: direct,
            ambience_bins: ambience,
            frame_id,
        }
    }
}

/// Full frame mixer that produces all output variants.
pub struct FrameMixer {
    mixer: DryWetMixer,
}

impl FrameMixer {
    /// Create a new frame mixer.
    pub fn new(config: MixerConfig) -> Self {
        Self {
            mixer: DryWetMixer::with_config(config),
        }
    }

    /// Get the underlying mixer.
    pub fn mixer(&self) -> &DryWetMixer {
        &self.mixer
    }

    /// Get mutable reference to the mixer.
    pub fn mixer_mut(&mut self) -> &mut DryWetMixer {
        &mut self.mixer
    }

    /// Process a frame of separated bins.
    pub fn process(&mut self, bins: &[SeparatedBin]) -> MixedFrame {
        let frame_id = bins.first().map(|b| b.frame_id).unwrap_or(0);

        let mixed = self.mixer.mix_frame(bins);
        let direct = self.mixer.extract_direct(bins);
        let ambience = self.mixer.extract_ambience(bins);

        MixedFrame::new(mixed, direct, ambience, frame_id)
    }

    /// Update the dry/wet mix.
    pub fn set_dry_wet(&mut self, dry_wet: f32) {
        self.mixer.set_dry_wet(dry_wet);
    }

    /// Update the output gain in dB.
    pub fn set_gain_db(&mut self, gain_db: f32) {
        self.mixer.set_output_gain_db(gain_db);
    }
}

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

    #[test]
    fn test_mixer_config() {
        let config = MixerConfig::new()
            .with_dry_wet(0.3)
            .with_direct_gain(1.2)
            .with_ambience_gain(0.8);

        assert!((config.dry_wet - 0.3).abs() < 1e-6);
        assert!((config.direct_gain - 1.2).abs() < 1e-6);
    }

    #[test]
    fn test_dry_wet_mix() {
        // Disable soft clipping for predictable test results
        let config = MixerConfig::new().with_soft_clip(None);
        let mut mixer = DryWetMixer::with_config(config);

        let bin = SeparatedBin::new(
            0,
            0,
            Complex::new(1.0, 0.0), // Direct
            Complex::new(0.5, 0.0), // Ambience
            0.6,
            0.0,
        );

        // All direct (dry = 1.0)
        mixer.set_dry_wet(0.0);
        let result = mixer.mix_bin(&bin);
        assert!((result.re - 1.0).abs() < 1e-6);

        // All ambience (wet = 1.0)
        mixer.set_dry_wet(1.0);
        let result = mixer.mix_bin(&bin);
        assert!((result.re - 0.5).abs() < 1e-6);

        // 50/50 mix
        mixer.set_dry_wet(0.5);
        let result = mixer.mix_bin(&bin);
        assert!((result.re - 0.75).abs() < 1e-6); // 0.5 * 1.0 + 0.5 * 0.5
    }

    #[test]
    fn test_gain_application() {
        let config = MixerConfig::new()
            .with_dry_wet(0.0) // All direct
            .with_direct_gain(2.0)
            .with_output_gain(0.5)
            .with_soft_clip(None); // Disable soft clipping

        let mut mixer = DryWetMixer::with_config(config);

        let bin = SeparatedBin::new(
            0,
            0,
            Complex::new(1.0, 0.0),
            Complex::new(0.0, 0.0),
            1.0,
            0.0,
        );

        let result = mixer.mix_bin(&bin);
        // 1.0 * 2.0 (direct gain) * 0.5 (output gain) = 1.0
        assert!((result.re - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_soft_clipping() {
        let config = MixerConfig::new()
            .with_output_gain(10.0) // High gain to trigger clipping
            .with_soft_clip(Some(0.9));

        let mut mixer = DryWetMixer::with_config(config);

        let bin = SeparatedBin::new(
            0,
            0,
            Complex::new(0.5, 0.0),
            Complex::new(0.0, 0.0),
            1.0,
            0.0,
        );

        let result = mixer.mix_bin(&bin);
        // Should be soft-clipped below 1.0
        assert!(result.magnitude() < 1.0);
    }

    #[test]
    fn test_peak_tracking() {
        let mut mixer = DryWetMixer::new();

        let bin = SeparatedBin::new(
            0,
            0,
            Complex::new(0.8, 0.0),
            Complex::new(0.3, 0.0),
            0.5,
            0.0,
        );

        mixer.mix_bin(&bin);
        let (direct, ambience, _output) = mixer.peak_levels();

        assert!((direct - 0.8).abs() < 1e-6);
        assert!((ambience - 0.3).abs() < 1e-6);

        mixer.reset_peaks();
        let (direct, ambience, output) = mixer.peak_levels();
        assert_eq!(direct, 0.0);
        assert_eq!(ambience, 0.0);
        assert_eq!(output, 0.0);
    }
}