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
//! Karplus-Strong plucked string synthesis
//!
//! Physical modeling synthesis that simulates plucked strings using a delay line
//! with feedback and filtering. Produces realistic guitar, harp, and other
//! plucked instrument sounds with minimal computational cost.
//!
//! # How It Works
//!
//! 1. A delay line (circular buffer) is filled with noise
//! 2. The buffer is fed back through a lowpass filter
//! 3. This creates a decaying harmonic series that sounds like a plucked string
//! 4. Buffer length determines pitch, filter strength determines decay/brightness
//!
//! # Example
//!
//! ```
//! use tunes::synthesis::karplus_strong::KarplusStrong;
//!
//! // Create a plucked string at A440
//! let mut string = KarplusStrong::new(440.0, 44100.0);
//!
//! // Set decay and brightness
//! string.set_decay(0.995);      // Longer sustain
//! string.set_brightness(0.5);   // Medium brightness
//!
//! // Generate samples
//! let samples = string.generate(44100); // 1 second of audio
//! ```

use rand::{Rng, SeedableRng};

/// Karplus-Strong plucked string synthesizer
///
/// Simulates a plucked string using a delay line with feedback filtering.
/// The algorithm produces natural-sounding plucks with harmonic overtones
/// that decay realistically.
///
/// # Parameters
/// - **Frequency**: Determined by delay line length
/// - **Decay**: How quickly the sound fades (0.0 - 1.0)
/// - **Brightness**: High-frequency content (0.0 = dark, 1.0 = bright)
///
/// # Example
/// ```
/// use tunes::synthesis::karplus_strong::KarplusStrong;
///
/// // Guitar-like pluck at C4 (261.63 Hz)
/// let mut guitar = KarplusStrong::new(261.63, 44100.0)
///     .with_decay(0.996)
///     .with_brightness(0.7);
///
/// let pluck = guitar.generate(22050); // 0.5 seconds
/// ```
#[derive(Debug)]
pub struct KarplusStrong {
    /// Circular delay buffer
    buffer: Vec<f32>,
    /// Current position in buffer
    position: usize,
    /// Decay factor (0.0 - 1.0) - how long the sound sustains
    decay: f32,
    /// Brightness factor (0.0 - 1.0) - amount of high frequency content
    brightness: f32,
    /// Previous sample for lowpass filtering
    prev_sample: f32,
    /// Random number generator for initial excitation
    rng: rand::rngs::StdRng,
}

impl KarplusStrong {
    /// Create a new Karplus-Strong synthesizer
    ///
    /// # Arguments
    /// * `frequency` - Pitch in Hz (e.g., 440.0 for A4)
    /// * `sample_rate` - Sample rate in Hz (e.g., 44100.0)
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::karplus_strong::KarplusStrong;
    ///
    /// let string = KarplusStrong::new(440.0, 44100.0);
    /// ```
    pub fn new(frequency: f32, sample_rate: f32) -> Self {
        // Calculate delay line length from frequency
        let buffer_length = (sample_rate / frequency).round() as usize;
        let buffer_length = buffer_length.max(1); // Prevent zero-length buffer

        let mut rng = rand::rngs::StdRng::from_rng(&mut rand::rng());

        // Initialize buffer with white noise
        let buffer: Vec<f32> = (0..buffer_length)
            .map(|_| rng.random_range(-1.0..1.0))
            .collect();

        Self {
            buffer,
            position: 0,
            decay: 0.996,      // Good default for realistic decay
            brightness: 0.5,   // Medium brightness
            prev_sample: 0.0,
            rng,
        }
    }

    /// Create with a specific seed for deterministic output
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::karplus_strong::KarplusStrong;
    ///
    /// let string1 = KarplusStrong::with_seed(440.0, 44100.0, 12345);
    /// let string2 = KarplusStrong::with_seed(440.0, 44100.0, 12345);
    /// // Both will produce identical output
    /// ```
    pub fn with_seed(frequency: f32, sample_rate: f32, seed: u64) -> Self {
        let buffer_length = (sample_rate / frequency).round() as usize;
        let buffer_length = buffer_length.max(1);

        let mut rng = rand::rngs::StdRng::seed_from_u64(seed);

        let buffer: Vec<f32> = (0..buffer_length)
            .map(|_| rng.random_range(-1.0..1.0))
            .collect();

        Self {
            buffer,
            position: 0,
            decay: 0.996,
            brightness: 0.5,
            prev_sample: 0.0,
            rng,
        }
    }

    /// Set the decay factor (how long the sound sustains)
    ///
    /// # Arguments
    /// * `decay` - Decay coefficient (0.0 - 1.0)
    ///   - 0.99 = fast decay (staccato, short pluck)
    ///   - 0.996 = medium decay (realistic guitar)
    ///   - 0.999 = slow decay (long, ringing sustain)
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let mut string = KarplusStrong::new(440.0, 44100.0);
    /// string.set_decay(0.999); // Long sustain
    /// ```
    pub fn set_decay(&mut self, decay: f32) {
        self.decay = decay.clamp(0.0, 1.0);
    }

    /// Set the decay factor (builder pattern)
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let string = KarplusStrong::new(440.0, 44100.0)
    ///     .with_decay(0.998);
    /// ```
    pub fn with_decay(mut self, decay: f32) -> Self {
        self.set_decay(decay);
        self
    }

    /// Set the brightness (high-frequency content)
    ///
    /// # Arguments
    /// * `brightness` - Brightness factor (0.0 - 1.0)
    ///   - 0.0 = very dark, muffled (like a bass guitar)
    ///   - 0.5 = balanced (realistic guitar)
    ///   - 1.0 = very bright, metallic (like a harpsichord)
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let mut string = KarplusStrong::new(440.0, 44100.0);
    /// string.set_brightness(0.8); // Bright, twangy sound
    /// ```
    pub fn set_brightness(&mut self, brightness: f32) {
        self.brightness = brightness.clamp(0.0, 1.0);
    }

    /// Set the brightness (builder pattern)
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let string = KarplusStrong::new(440.0, 44100.0)
    ///     .with_brightness(0.3);
    /// ```
    pub fn with_brightness(mut self, brightness: f32) -> Self {
        self.set_brightness(brightness);
        self
    }

    /// Re-excite the string with a new pluck
    ///
    /// Fills the delay buffer with fresh noise, creating a new attack.
    /// Use this to re-trigger the sound without recreating the object.
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let mut string = KarplusStrong::new(440.0, 44100.0);
    ///
    /// let pluck1 = string.generate(22050); // First pluck
    /// string.pluck(); // Re-excite
    /// let pluck2 = string.generate(22050); // Second pluck
    /// ```
    pub fn pluck(&mut self) {
        for sample in &mut self.buffer {
            *sample = self.rng.random_range(-1.0..1.0);
        }
        self.prev_sample = 0.0;
    }

    /// Generate a single sample
    ///
    /// This implements the core Karplus-Strong algorithm:
    /// 1. Read current buffer position
    /// 2. Apply lowpass filter (average with previous sample)
    /// 3. Apply decay
    /// 4. Write back to buffer
    /// 5. Advance position
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Read current sample from buffer
        let current = self.buffer[self.position];

        // Calculate next position (wrapping)
        let next_pos = (self.position + 1) % self.buffer.len();

        // Lowpass filter: blend current and previous based on brightness
        // brightness = 0.0 -> full averaging (dark)
        // brightness = 1.0 -> no averaging (bright)
        let filtered = (1.0 - self.brightness) * 0.5 * (current + self.prev_sample)
                     + self.brightness * current;

        // Apply decay
        let output = filtered * self.decay;

        // Write filtered output back to buffer for next cycle
        self.buffer[self.position] = output;

        // Update state
        self.prev_sample = current;
        self.position = next_pos;

        output
    }

    /// Generate multiple samples
    ///
    /// Returns a vector of synthesized audio samples.
    ///
    /// # Arguments
    /// * `length` - Number of samples to generate
    ///
    /// # Example
    /// ```
    /// # use tunes::synthesis::karplus_strong::KarplusStrong;
    /// let mut string = KarplusStrong::new(440.0, 44100.0);
    ///
    /// // Generate 1 second of audio at 44.1kHz
    /// let samples = string.generate(44100);
    /// ```
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

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

    #[test]
    fn test_karplus_strong_creates_valid_buffer() {
        let ks = KarplusStrong::new(440.0, 44100.0);

        // Buffer length should be sample_rate / frequency
        let expected_len = (44100.0_f32 / 440.0_f32).round() as usize;
        assert_eq!(ks.buffer.len(), expected_len);
    }

    #[test]
    fn test_karplus_strong_sample_in_range() {
        let mut ks = KarplusStrong::new(440.0, 44100.0);

        // Generate samples and verify range
        for _ in 0..1000 {
            let sample = ks.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_karplus_strong_decays() {
        let mut ks = KarplusStrong::new(440.0, 44100.0)
            .with_decay(0.99); // Fast decay for testing

        // Initial samples should be louder
        let initial: Vec<f32> = (0..100).map(|_| ks.sample().abs()).collect();
        let initial_avg: f32 = initial.iter().sum::<f32>() / initial.len() as f32;

        // Later samples should be quieter (after some decay)
        let later: Vec<f32> = (0..100).map(|_| ks.sample().abs()).collect();
        let later_avg: f32 = later.iter().sum::<f32>() / later.len() as f32;

        assert!(
            later_avg < initial_avg,
            "Sound should decay over time: initial {} vs later {}",
            initial_avg,
            later_avg
        );
    }

    #[test]
    fn test_karplus_strong_seeded_deterministic() {
        let mut ks1 = KarplusStrong::with_seed(440.0, 44100.0, 12345);
        let mut ks2 = KarplusStrong::with_seed(440.0, 44100.0, 12345);

        // Same seed should produce identical output
        for _ in 0..1000 {
            assert_eq!(ks1.sample(), ks2.sample());
        }
    }

    #[test]
    fn test_karplus_strong_pluck_resets() {
        let mut ks = KarplusStrong::with_seed(440.0, 44100.0, 42)
            .with_decay(0.99); // Faster decay for testing

        // Generate some samples to let it decay
        for _ in 0..10000 {
            ks.sample();
        }

        // Sample should be very quiet now
        let quiet_sample = ks.sample().abs();

        // Re-pluck
        ks.pluck();

        // After pluck, sample several times and check that at least one is louder
        // (the first sample might be small by random chance)
        let mut max_loud_sample: f32 = 0.0;
        for _ in 0..10 {
            max_loud_sample = max_loud_sample.max(ks.sample().abs());
        }

        assert!(
            max_loud_sample > quiet_sample,
            "Pluck should excite the string: {} vs {}",
            max_loud_sample,
            quiet_sample
        );
    }

    #[test]
    fn test_karplus_strong_brightness_affects_tone() {
        // Create two identical strings with different brightness
        let mut bright = KarplusStrong::with_seed(440.0, 44100.0, 42)
            .with_brightness(1.0); // Maximum brightness

        let mut dark = KarplusStrong::with_seed(440.0, 44100.0, 42)
            .with_brightness(0.0); // Minimum brightness

        // Generate samples
        let bright_samples = bright.generate(1000);
        let dark_samples = dark.generate(1000);

        // Bright should maintain more high-frequency content
        // (Quick test: check that they differ significantly)
        let mut differences = 0;
        for (b, d) in bright_samples.iter().zip(dark_samples.iter()) {
            if (b - d).abs() > 0.01 {
                differences += 1;
            }
        }

        assert!(
            differences > 100,
            "Brightness should affect the sound: {} differences found",
            differences
        );
    }

    #[test]
    fn test_karplus_strong_generate() {
        let mut ks = KarplusStrong::new(440.0, 44100.0);
        let samples = ks.generate(1000);

        assert_eq!(samples.len(), 1000);

        // All samples should be in valid range
        for &sample in &samples {
            assert!(sample >= -1.0 && sample <= 1.0);
        }
    }

    #[test]
    fn test_karplus_strong_different_frequencies() {
        // Low frequency (larger buffer)
        let low = KarplusStrong::new(110.0, 44100.0);
        let low_buffer_len = low.buffer.len();

        // High frequency (smaller buffer)
        let high = KarplusStrong::new(880.0, 44100.0);
        let high_buffer_len = high.buffer.len();

        assert!(
            low_buffer_len > high_buffer_len,
            "Lower frequency should have larger buffer"
        );
    }

    #[test]
    fn test_decay_clamping() {
        let mut ks = KarplusStrong::new(440.0, 44100.0);

        // Test clamping
        ks.set_decay(1.5); // Too high
        assert!(ks.decay <= 1.0);

        ks.set_decay(-0.5); // Too low
        assert!(ks.decay >= 0.0);
    }

    #[test]
    fn test_brightness_clamping() {
        let mut ks = KarplusStrong::new(440.0, 44100.0);

        // Test clamping
        ks.set_brightness(1.5); // Too high
        assert!(ks.brightness <= 1.0);

        ks.set_brightness(-0.5); // Too low
        assert!(ks.brightness >= 0.0);
    }
}