math-sonify 1.4.0

Real-time procedural audio from mathematical dynamical systems (Lorenz, Rossler, Double Pendulum, and more)
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
//! Physical modeling synthesis modes.
//!
//! This module wraps the lower-level [`KarplusStrong`] and [`WaveguideString`]
//! DSP primitives (in `src/synth/`) in higher-level "instrument" abstractions
//! that accept normalised state vectors from the dynamical system and produce
//! audio frames.
//!
//! # Synthesis modes
//!
//! | Mode | DSP core | Character |
//! |------|----------|-----------|
//! | [`PluckedString`] | Karplus-Strong | Bright plucked string, guitar/harp |
//! | [`TubeResonator`] | Waveguide (two-delay) + resonator | Wind instrument, tube resonance |
//!
//! Both instruments expose a common [`PhysicalSynth`] trait so the audio
//! engine can dispatch dynamically.

#![allow(dead_code)]

use crate::synth::{KarplusStrong, ResonatorBank, WaveguideString};

// ── Trait ─────────────────────────────────────────────────────────────────────

/// Common interface for physical modeling synthesis modes.
pub trait PhysicalSynth: Send {
    /// Map a normalised state vector (values in roughly `[-1, 1]`) to synthesis
    /// parameters and produce the next audio sample.
    ///
    /// - `state[0]` → pitch / frequency
    /// - `state[1]` → brightness / timbre
    /// - `state[2]` → excitation / pluck strength (if present)
    fn next_sample(&mut self, state: &[f64], sample_rate: f32) -> f32;

    /// True if this synth is currently active (producing non-zero output).
    fn is_active(&self) -> bool;

    /// Force re-excitation (e.g. when the attractor crosses a threshold).
    fn excite(&mut self, freq_hz: f32, sample_rate: f32);

    /// Set master output volume (0–1).
    fn set_volume(&mut self, vol: f32);
}

// ── Plucked string ────────────────────────────────────────────────────────────

/// Karplus-Strong plucked string instrument driven by ODE state.
///
/// The dynamical system state modulates:
/// - `state[0]` → pitch (mapped to a frequency range)
/// - `state[1]` → brightness (IIR coefficient)
/// - Automatic re-excitation when the pitch changes by more than a threshold
pub struct PluckedString {
    ks: KarplusStrong,
    /// Minimum frequency in Hz.
    freq_min: f32,
    /// Maximum frequency in Hz.
    freq_max: f32,
    last_freq: f32,
    /// Frequency change that triggers a re-excitation (cents).
    retrigger_cents: f32,
    /// Excitation cooldown in samples to avoid clicking.
    cooldown: u32,
    cooldown_remaining: u32,
    volume: f32,
}

impl PluckedString {
    /// Create a plucked string with the given frequency range.
    pub fn new(freq_min: f32, freq_max: f32, sample_rate: f32) -> Self {
        Self {
            ks: KarplusStrong::new(freq_min.max(10.0), sample_rate),
            freq_min: freq_min.max(10.0),
            freq_max: freq_max.max(freq_min + 1.0),
            last_freq: 0.0,
            retrigger_cents: 200.0, // 2 semitones
            cooldown: (sample_rate * 0.05) as u32, // 50 ms
            cooldown_remaining: 0,
            volume: 0.7,
        }
    }

    /// Map a normalised value in `[-1, 1]` to a frequency in Hz (log scale).
    fn map_freq(&self, x: f64) -> f32 {
        let t = ((x + 1.0) * 0.5).clamp(0.0, 1.0) as f32;
        let log_min = self.freq_min.ln();
        let log_max = self.freq_max.ln();
        (log_min + t * (log_max - log_min)).exp()
    }

    /// Cents distance between two frequencies.
    fn cents_diff(f1: f32, f2: f32) -> f32 {
        if f1 <= 0.0 || f2 <= 0.0 {
            return f32::MAX;
        }
        (1200.0 * (f2 / f1).log2()).abs()
    }
}

impl PhysicalSynth for PluckedString {
    fn next_sample(&mut self, state: &[f64], sample_rate: f32) -> f32 {
        if self.cooldown_remaining > 0 {
            self.cooldown_remaining -= 1;
        }

        let freq = self
            .map_freq(state.first().copied().unwrap_or(0.0));

        // Re-excite if pitch has moved significantly.
        if self.cooldown_remaining == 0
            && Self::cents_diff(self.last_freq, freq) > self.retrigger_cents
        {
            self.ks.trigger(freq, sample_rate);
            self.last_freq = freq;
            self.cooldown_remaining = self.cooldown;
        }

        // Modulate brightness from state[1].
        if let Some(&s1) = state.get(1) {
            let b = ((s1 + 1.0) * 0.5).clamp(0.0, 1.0) as f32 * 0.8;
            self.ks.brightness = b;
        }

        self.ks.volume = self.volume;
        self.ks.next_sample()
    }

    fn is_active(&self) -> bool {
        self.ks.active
    }

    fn excite(&mut self, freq_hz: f32, sample_rate: f32) {
        self.ks.trigger(freq_hz, sample_rate);
        self.last_freq = freq_hz;
        self.cooldown_remaining = self.cooldown;
    }

    fn set_volume(&mut self, vol: f32) {
        self.volume = vol.clamp(0.0, 1.0);
    }
}

// ── Tube resonator ────────────────────────────────────────────────────────────

/// Cylindrical/conical tube resonator using a bidirectional waveguide model.
///
/// Models a wind-instrument resonating column.  The ODE state modulates:
/// - `state[0]` → resonant frequency (embouchure / fingering)
/// - `state[1]` → damping (open vs. stopped)
/// - `state[2]` → excitation pressure (breathiness)
///
/// A [`ResonatorBank`] adds body resonance on top of the waveguide output.
pub struct TubeResonator {
    wg: WaveguideString,
    resonators: ResonatorBank,
    /// Frequency range.
    freq_min: f32,
    freq_max: f32,
    /// Continuous excitation level derived from `state[2]`.
    excite_level: f32,
    volume: f32,
    sample_rate: f32,
    /// Noise state for breath excitation.
    noise_seed: u64,
}

impl TubeResonator {
    /// Create a tube resonator for the given frequency range.
    pub fn new(freq_min: f32, freq_max: f32, sample_rate: f32) -> Self {
        let mut wg = WaveguideString::new(sample_rate);
        wg.set_freq(freq_min);
        wg.damping = 0.998;
        wg.brightness = 0.2; // bright tube
        wg.dispersion = 0.0; // ideal tube (no stiffness)

        // Initialise resonator bank at base frequency.
        let mut resonators = ResonatorBank::new(sample_rate);
        resonators.tune_to_scale(freq_min, 2.0, &[0.0, 7.0, 12.0]);
        resonators.q = 20.0;

        Self {
            wg,
            resonators,
            freq_min: freq_min.max(20.0),
            freq_max: freq_max.max(freq_min + 1.0),
            excite_level: 0.0,
            volume: 0.6,
            sample_rate,
            noise_seed: 0xDEAD_CAFE_1234_5678,
        }
    }

    fn map_freq(&self, x: f64) -> f32 {
        let t = ((x + 1.0) * 0.5).clamp(0.0, 1.0) as f32;
        let log_min = self.freq_min.ln();
        let log_max = self.freq_max.ln();
        (log_min + t * (log_max - log_min)).exp()
    }

    /// Produce a breath noise sample scaled by `level`.
    fn breath_noise(&mut self, level: f32) -> f32 {
        self.noise_seed = self
            .noise_seed
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        let n = (self.noise_seed >> 33) as f32 / (1u64 << 31) as f32 * 2.0 - 1.0;
        n * level * 0.1
    }
}

impl PhysicalSynth for TubeResonator {
    fn next_sample(&mut self, state: &[f64], _sample_rate: f32) -> f32 {
        // Map frequency.
        let freq = self.map_freq(state.first().copied().unwrap_or(0.0));
        self.wg.set_freq(freq);

        // Map damping from state[1].
        if let Some(&s1) = state.get(1) {
            let d = 0.990 + ((s1 + 1.0) * 0.5).clamp(0.0, 1.0) as f32 * 0.008;
            self.wg.damping = d;
        }

        // Breath pressure from state[2] — continuous excitation.
        let pressure = state.get(2).copied().unwrap_or(0.5);
        self.excite_level = ((pressure + 1.0) * 0.5).clamp(0.0, 1.0) as f32;

        // Inject breath noise into the waveguide.
        if self.excite_level > 0.01 {
            let noise = self.breath_noise(self.excite_level);
            // Pulse the excite flag when noise is large enough.
            if noise.abs() > 0.05 {
                self.wg.excite = true;
                self.wg.excite_pos = 0.1; // near the mouthpiece
            }
        }

        let wg_out = self.wg.next_sample();
        let (res_l, res_r) = self.resonators.process(wg_out);
        let resonated = (res_l + res_r) * 0.5;

        (wg_out * 0.7 + resonated * 0.3) * self.volume
    }

    fn is_active(&self) -> bool {
        // The tube resonator is continuously active when driven by breath.
        self.excite_level > 0.001
    }

    fn excite(&mut self, freq_hz: f32, _sample_rate: f32) {
        self.wg.set_freq(freq_hz);
        self.wg.excite = true;
        self.excite_level = 0.5;
    }

    fn set_volume(&mut self, vol: f32) {
        self.volume = vol.clamp(0.0, 1.0);
    }
}

// ── FM Synthesis ──────────────────────────────────────────────────────────────

/// Configuration for DX7-style FM synthesis.
///
/// Frequency modulation synthesis using a single carrier and modulator operator.
/// The modulator adds sidebands to the carrier, controlled by `modulation_index`.
#[derive(Debug, Clone)]
pub struct FmConfig {
    /// Carrier-to-fundamental frequency ratio.
    pub carrier_ratio: f32,
    /// Modulator-to-fundamental frequency ratio.
    pub modulator_ratio: f32,
    /// Modulation index: controls FM sideband depth (0 = pure sine, 4 = rich spectrum).
    pub modulation_index: f32,
}

impl Default for FmConfig {
    fn default() -> Self {
        Self {
            carrier_ratio: 1.0,
            modulator_ratio: 2.0,
            modulation_index: 2.0,
        }
    }
}

/// A simple ADSR envelope generator.
///
/// Cycles through Attack, Decay, Sustain (held until released), and Release.
/// The envelope is driven by sample count rather than wall-clock time.
#[derive(Debug, Clone)]
pub struct AdsrEnvelope {
    /// Attack length in samples.
    pub attack_samples: u32,
    /// Decay length in samples.
    pub decay_samples: u32,
    /// Sustain level (0..=1).
    pub sustain_level: f32,
    /// Release length in samples.
    pub release_samples: u32,

    // Private state
    phase: AdsrPhase,
    sample_counter: u32,
    current_level: f32,
    release_start_level: f32,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum AdsrPhase {
    Attack,
    Decay,
    Sustain,
    Release,
    Idle,
}

impl AdsrEnvelope {
    /// Create a new ADSR envelope (starts idle until triggered).
    pub fn new(attack_samples: u32, decay_samples: u32, sustain_level: f32, release_samples: u32) -> Self {
        Self {
            attack_samples,
            decay_samples,
            sustain_level: sustain_level.clamp(0.0, 1.0),
            release_samples,
            phase: AdsrPhase::Idle,
            sample_counter: 0,
            current_level: 0.0,
            release_start_level: 0.0,
        }
    }

    /// Trigger the envelope (starts Attack phase).
    pub fn trigger(&mut self) {
        self.phase = AdsrPhase::Attack;
        self.sample_counter = 0;
    }

    /// Begin release phase.
    pub fn release(&mut self) {
        self.release_start_level = self.current_level;
        self.phase = AdsrPhase::Release;
        self.sample_counter = 0;
    }

    /// Return whether the envelope has fully decayed to idle.
    pub fn is_idle(&self) -> bool {
        self.phase == AdsrPhase::Idle
    }

    /// Advance one sample and return the current amplitude (0..=1).
    pub fn next_sample(&mut self) -> f32 {
        match self.phase {
            AdsrPhase::Idle => {
                self.current_level = 0.0;
            }
            AdsrPhase::Attack => {
                if self.attack_samples == 0 {
                    self.current_level = 1.0;
                    self.phase = AdsrPhase::Decay;
                    self.sample_counter = 0;
                } else {
                    self.current_level = self.sample_counter as f32 / self.attack_samples as f32;
                    self.sample_counter += 1;
                    if self.sample_counter >= self.attack_samples {
                        self.current_level = 1.0;
                        self.phase = AdsrPhase::Decay;
                        self.sample_counter = 0;
                    }
                }
            }
            AdsrPhase::Decay => {
                if self.decay_samples == 0 {
                    self.current_level = self.sustain_level;
                    self.phase = AdsrPhase::Sustain;
                } else {
                    let t = self.sample_counter as f32 / self.decay_samples as f32;
                    self.current_level = 1.0 - t * (1.0 - self.sustain_level);
                    self.sample_counter += 1;
                    if self.sample_counter >= self.decay_samples {
                        self.current_level = self.sustain_level;
                        self.phase = AdsrPhase::Sustain;
                        self.sample_counter = 0;
                    }
                }
            }
            AdsrPhase::Sustain => {
                self.current_level = self.sustain_level;
            }
            AdsrPhase::Release => {
                if self.release_samples == 0 {
                    self.current_level = 0.0;
                    self.phase = AdsrPhase::Idle;
                } else {
                    let t = self.sample_counter as f32 / self.release_samples as f32;
                    self.current_level = self.release_start_level * (1.0 - t);
                    self.sample_counter += 1;
                    if self.sample_counter >= self.release_samples {
                        self.current_level = 0.0;
                        self.phase = AdsrPhase::Idle;
                    }
                }
            }
        }
        self.current_level.clamp(0.0, 1.0)
    }
}

impl Default for AdsrEnvelope {
    fn default() -> Self {
        Self::new(441, 4410, 0.7, 8820) // 10ms attack, 100ms decay, 0.7 sustain, 200ms release at 44100 Hz
    }
}

/// DX7-style FM synthesizer implementing [`PhysicalSynth`].
///
/// Maps ODE state to carrier frequency (log-mapped), modulation index, and
/// amplitude envelope (ADSR). Uses a single carrier + modulator operator pair.
///
/// State mapping:
/// - `state[0]` → carrier frequency (log-mapped over `freq_min..freq_max`)
/// - `state[1]` → modulation index (0→4)
/// - `state[2]` → ADSR decay driven by normalized value
pub struct FmSynth {
    config: FmConfig,
    sample_rate: f32,
    freq_min: f32,
    freq_max: f32,
    // Synthesis state
    carrier_phase: f32,
    modulator_phase: f32,
    carrier_freq: f32,
    modulation_index: f32,
    envelope: AdsrEnvelope,
    volume: f32,
    /// Decrement decay counter when env drops below threshold
    last_excite_value: f32,
}

impl FmSynth {
    /// Create a new FM synthesizer.
    pub fn new(config: FmConfig, sample_rate: f32) -> Self {
        let mut env = AdsrEnvelope::new(
            (sample_rate * 0.01) as u32, // 10ms attack
            (sample_rate * 0.1) as u32,  // 100ms decay
            0.7,
            (sample_rate * 0.2) as u32,  // 200ms release
        );
        env.trigger();
        Self {
            config,
            sample_rate: sample_rate.max(1.0),
            freq_min: 80.0,
            freq_max: 1200.0,
            carrier_phase: 0.0,
            modulator_phase: 0.0,
            carrier_freq: 440.0,
            modulation_index: 2.0,
            envelope: env,
            volume: 0.7,
            last_excite_value: 0.0,
        }
    }

    fn map_freq(&self, x: f64) -> f32 {
        let t = ((x + 1.0) * 0.5).clamp(0.0, 1.0) as f32;
        let log_min = self.freq_min.ln();
        let log_max = self.freq_max.ln();
        (log_min + t * (log_max - log_min)).exp()
    }
}

impl PhysicalSynth for FmSynth {
    fn next_sample(&mut self, state: &[f64], _sample_rate: f32) -> f32 {
        let sr = self.sample_rate;

        // Map state[0] → carrier frequency (log-mapped)
        if let Some(&s0) = state.first() {
            self.carrier_freq = self.map_freq(s0);
        }

        // Map state[1] → modulation index (0→4)
        if let Some(&s1) = state.get(1) {
            self.modulation_index = (((s1 + 1.0) * 0.5).clamp(0.0, 1.0) * 4.0) as f32;
        }

        // Map state[2] → ADSR decay trigger: large changes re-trigger envelope
        if let Some(&s2) = state.get(2) {
            let val = s2 as f32;
            if (val - self.last_excite_value).abs() > 2.0 {
                self.envelope.trigger();
                self.last_excite_value = val;
            }
        }

        // FM synthesis: modulator drives carrier phase
        let mod_freq = self.carrier_freq * self.config.modulator_ratio;
        let carrier_freq = self.carrier_freq * self.config.carrier_ratio;

        let mod_sample = (self.modulator_phase * std::f32::consts::TAU).sin()
            * self.modulation_index;

        let carrier_sample = ((self.carrier_phase * std::f32::consts::TAU) + mod_sample).sin();

        // Advance phases
        self.modulator_phase = (self.modulator_phase + mod_freq / sr).fract();
        self.carrier_phase = (self.carrier_phase + carrier_freq / sr).fract();

        let env_level = self.envelope.next_sample();
        carrier_sample * env_level * self.volume
    }

    fn is_active(&self) -> bool {
        !self.envelope.is_idle()
    }

    fn excite(&mut self, freq_hz: f32, _sample_rate: f32) {
        self.carrier_freq = freq_hz;
        self.envelope.trigger();
    }

    fn set_volume(&mut self, vol: f32) {
        self.volume = vol.clamp(0.0, 1.0);
    }
}

// ── Factory ───────────────────────────────────────────────────────────────────

/// Available physical synthesis modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PhysicalMode {
    PluckedString,
    TubeResonator,
    /// DX7-style FM synthesis.
    Fm,
}

/// Construct the appropriate physical synth.
pub fn build_physical_synth(
    mode: PhysicalMode,
    freq_min: f32,
    freq_max: f32,
    sample_rate: f32,
) -> Box<dyn PhysicalSynth> {
    match mode {
        PhysicalMode::PluckedString => {
            Box::new(PluckedString::new(freq_min, freq_max, sample_rate))
        }
        PhysicalMode::TubeResonator => {
            Box::new(TubeResonator::new(freq_min, freq_max, sample_rate))
        }
        PhysicalMode::Fm => {
            let mut s = FmSynth::new(FmConfig::default(), sample_rate);
            s.freq_min = freq_min.max(20.0);
            s.freq_max = freq_max.max(freq_min + 1.0);
            Box::new(s)
        }
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    const SR: f32 = 44100.0;

    #[test]
    fn test_plucked_string_produces_output_after_excite() {
        let mut ps = PluckedString::new(80.0, 1200.0, SR);
        ps.excite(440.0, SR);
        let mut max = 0.0_f32;
        let state = [0.0f64, 0.0, 0.0];
        for _ in 0..4410 {
            let s = ps.next_sample(&state, SR);
            max = max.max(s.abs());
        }
        assert!(max > 0.0, "PluckedString should produce output after excite");
    }

    #[test]
    fn test_plucked_string_output_finite() {
        let mut ps = PluckedString::new(80.0, 1200.0, SR);
        ps.excite(220.0, SR);
        let state = [0.0f64, 0.5, -0.5];
        for i in 0..22050 {
            let s = ps.next_sample(&state, SR);
            assert!(s.is_finite(), "non-finite at sample {i}");
        }
    }

    #[test]
    fn test_tube_resonator_produces_output() {
        let mut tr = TubeResonator::new(60.0, 800.0, SR);
        tr.excite(220.0, SR);
        let state = [0.0f64, 0.0, 0.5];
        let mut max = 0.0_f32;
        for _ in 0..4410 {
            max = max.max(tr.next_sample(&state, SR).abs());
        }
        assert!(max > 0.0, "TubeResonator should produce output after excite");
    }

    #[test]
    fn test_tube_resonator_output_finite() {
        let mut tr = TubeResonator::new(60.0, 800.0, SR);
        tr.excite(110.0, SR);
        let state = [0.3f64, -0.2, 0.8];
        for i in 0..22050 {
            let s = tr.next_sample(&state, SR);
            assert!(s.is_finite(), "non-finite at sample {i}");
        }
    }

    #[test]
    fn test_build_physical_synth_factory() {
        let mut ps = build_physical_synth(PhysicalMode::PluckedString, 80.0, 1200.0, SR);
        ps.excite(440.0, SR);
        let state = [0.0f64];
        let _ = ps.next_sample(&state, SR);

        let mut tr = build_physical_synth(PhysicalMode::TubeResonator, 60.0, 800.0, SR);
        tr.excite(220.0, SR);
        let _ = tr.next_sample(&state, SR);
    }
}