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
use crate::composition::TrackBuilder;
use crate::synthesis::granular::{GranularParams, create_granular_events};
use crate::synthesis::noise::NoiseType;
use crate::prelude::{FMParams, FilterEnvelope};
use crate::synthesis::sample::Sample;
use crate::track::{AudioEvent, SampleEvent};

/// Synthesis methods for TrackBuilder
///
/// These methods provide convenient access to advanced synthesis features:
/// - Filter envelopes for subtractive synthesis
/// - FM synthesis for complex harmonic timbres
impl<'a> TrackBuilder<'a> {
    /// Set the filter envelope for subsequent notes
    ///
    /// The filter envelope controls how the filter cutoff frequency changes over time,
    /// creating classic subtractive synthesis sweeps.
    ///
    /// # Arguments
    /// * `filter_env` - FilterEnvelope to use
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("synth")
    ///     .filter_envelope(FilterEnvelope::classic())
    ///     .note(&[440.0], 1.0);
    /// ```
    pub fn filter_envelope(mut self, filter_env: FilterEnvelope) -> Self {
        // Store the filter envelope for subsequent notes
        // We'll need to track this in the builder state
        self.filter_envelope = filter_env;
        self
    }

    /// Set FM synthesis parameters for subsequent notes
    ///
    /// FM (Frequency Modulation) synthesis creates complex, harmonically rich timbres
    /// by modulating the frequency of one oscillator with another.
    ///
    /// # Arguments
    /// * `fm_params` - FM synthesis parameters
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("fm_piano")
    ///     .fm(FMParams::electric_piano())
    ///     .note(&[C4], 0.5)
    ///     .note(&[E4], 0.5);
    /// ```
    pub fn fm(mut self, fm_params: FMParams) -> Self {
        self.fm_params = fm_params;
        self
    }

    /// Create a custom FM sound with specific parameters
    ///
    /// # Arguments
    /// * `mod_ratio` - Modulator to carrier frequency ratio
    /// * `mod_index` - Modulation index (brightness, 0.0 to 10.0+)
    pub fn fm_custom(self, mod_ratio: f32, mod_index: f32) -> Self {
        self.fm(FMParams::new(mod_ratio, mod_index))
    }

    /// Combine filter envelope and FM for rich, evolving timbres
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("fm_synth")
    ///     .fm(FMParams::bell())
    ///     .filter_envelope(FilterEnvelope::classic())
    ///     .note(&[C4], 1.0);
    /// ```
    pub fn fm_with_filter(self, fm_params: FMParams, filter_env: FilterEnvelope) -> Self {
        self.fm(fm_params).filter_envelope(filter_env)
    }

    /// Use a custom wavetable for subsequent notes
    ///
    /// This allows you to use custom waveforms created with `Wavetable::from_fn()`,
    /// `Wavetable::from_harmonics()`, or other wavetable constructors.
    /// When set, this overrides the standard `Waveform` enum.
    ///
    /// # Arguments
    /// * `wavetable` - Custom wavetable to use for oscillation
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # use tunes::synthesis::wavetable::{Wavetable, DEFAULT_TABLE_SIZE};
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Create a custom wavetable with odd harmonics
    /// let organ_wt = Wavetable::from_harmonics(
    ///     DEFAULT_TABLE_SIZE,
    ///     &[(1, 1.0), (3, 0.5), (5, 0.3), (7, 0.2)],
    /// );
    ///
    /// comp.track("organ")
    ///     .custom_waveform(organ_wt)
    ///     .notes(&[C3, E3, G3], 0.5);
    /// ```
    pub fn custom_waveform(mut self, wavetable: crate::synthesis::wavetable::Wavetable) -> Self {
        self.custom_wavetable = Some(wavetable);
        self
    }

    /// Use additive synthesis with specified harmonic amplitudes
    ///
    /// Creates a custom waveform by summing harmonics with the given amplitudes.
    /// Each value in the array represents the amplitude of that harmonic (1st, 2nd, 3rd, etc.).
    ///
    /// # Arguments
    /// * `harmonic_amps` - Array of harmonic amplitudes (e.g., `&[1.0, 0.5, 0.25]`)
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Sawtooth-like sound (harmonic series with decreasing amplitudes)
    /// comp.track("saw")
    ///     .additive_synth(&[1.0, 0.5, 0.33, 0.25, 0.2])
    ///     .notes(&[C4, E4, G4], 0.5);
    ///
    /// // Organ sound (odd harmonics only)
    /// comp.track("organ")
    ///     .additive_synth(&[1.0, 0.0, 0.5, 0.0, 0.3])
    ///     .notes(&[C3], 1.0);
    /// ```
    pub fn additive_synth(self, harmonic_amps: &[f32]) -> Self {
        use crate::synthesis::wavetable::{Wavetable, DEFAULT_TABLE_SIZE};

        // Convert harmonic amplitudes to (harmonic_number, amplitude) pairs
        let harmonics: Vec<(usize, f32)> = harmonic_amps
            .iter()
            .enumerate()
            .filter(|(_, &amp)| amp > 0.0)  // Skip zero-amplitude harmonics
            .map(|(i, &amp)| (i + 1, amp))
            .collect();

        // Create wavetable from harmonics
        let wavetable = Wavetable::from_harmonics(DEFAULT_TABLE_SIZE, &harmonics);

        self.custom_waveform(wavetable)
    }

    /// Use wavetable synthesis with a rich, harmonically complex waveform
    ///
    /// Convenience method that creates a wavetable with a rich harmonic spectrum,
    /// good for synth leads and pads. For custom wavetables, use `.custom_waveform()`.
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Rich wavetable lead
    /// comp.track("lead")
    ///     .wavetable()
    ///     .notes(&[C4, D4, E4, G4], 0.5);
    /// ```
    pub fn wavetable(self) -> Self {
        use crate::synthesis::wavetable::{Wavetable, DEFAULT_TABLE_SIZE};

        // Create a rich wavetable with multiple harmonics for a complex timbre
        // Similar to a sawtooth but with more controlled harmonics
        let harmonics = vec![
            (1, 1.0),   // Fundamental
            (2, 0.6),   // Octave
            (3, 0.4),   // Fifth above octave
            (4, 0.3),   // Two octaves
            (5, 0.25),  // Major third above that
            (6, 0.2),   // Fifth above two octaves
            (7, 0.15),  // Minor seventh
            (8, 0.1),   // Three octaves
        ];

        let wavetable = Wavetable::from_harmonics(DEFAULT_TABLE_SIZE, &harmonics);
        self.custom_waveform(wavetable)
    }

    /// Add noise to the track
    ///
    /// Generates noise of the specified type and adds it as a sample at the current cursor position.
    ///
    /// # Arguments
    /// * `noise_type` - Type of noise to generate (White or Brown)
    /// * `duration` - Duration in seconds
    /// * `amplitude` - Volume/amplitude (0.0 to 1.0)
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # use tunes::synthesis::noise::NoiseType;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Add white noise hi-hat
    /// comp.track("drums")
    ///     .noise(NoiseType::White, 0.1, 0.3);
    ///
    /// // Add brown noise bass rumble
    /// comp.track("fx")
    ///     .noise(NoiseType::Brown, 2.0, 0.5)
    ///     .filter(Filter::new(FilterType::LowPass, 500.0, 0.8));  // Low-pass for bass
    /// ```
    pub fn noise(mut self, noise_type: NoiseType, duration: f32, amplitude: f32) -> Self {
        // Get sample rate from composition
        let sample_rate = self.composition.sample_rate;
        // duration is already in seconds (consistent with .note() and other methods)
        let sample_count = (duration * sample_rate as f32) as usize;

        // Capture cursor position before mutable borrow
        let start_time = self.cursor;

        // Generate noise samples
        let mut noise_samples = noise_type.generate(sample_count);

        // Apply amplitude
        for sample in &mut noise_samples {
            *sample *= amplitude;
        }

        // Create a Sample from the noise
        let noise_sample = Sample::from_mono(noise_samples, sample_rate);

        // Create a SampleEvent
        let sample_event = SampleEvent {
            sample: noise_sample,
            start_time,
            playback_rate: 1.0,
            volume: 1.0, // Already applied amplitude above
            spatial_position: None,
        };

        // Get the track and add the event
        let track = self.get_track_mut();
        track.events.push(AudioEvent::Sample(sample_event));

        // Advance cursor (duration is in seconds)
        self.cursor += duration;

        self
    }

    /// Apply granular synthesis to a sample file
    ///
    /// Granular synthesis breaks audio into tiny "grains" (5-100ms) and rearranges/overlaps them
    /// to create rich textures, time-stretching effects, or surreal sonic transformations.
    ///
    /// # Arguments
    /// * `sample_path` - Path to the audio file to granulate
    /// * `params` - Granular synthesis parameters (use presets or create custom)
    /// * `duration` - Output duration in seconds
    ///
    /// # Example
    /// ```no_run
    /// # use tunes::prelude::*;
    /// # use tunes::synthesis::granular::GranularParams;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Create lush texture from a vocal sample
    /// comp.track("texture")
    ///     .granular("voice.wav", GranularParams::texture(), 5.0);
    ///
    /// // Freeze a moment in time
    /// comp.track("frozen")
    ///     .granular("guitar.wav", GranularParams::freeze(), 3.0);
    ///
    /// // Glitchy stutters
    /// comp.track("glitch")
    ///     .granular("drums.wav", GranularParams::glitch(), 2.0);
    /// ```
    pub fn granular(mut self, sample_path: &str, params: GranularParams, duration: f32) -> Self {
        // Load the sample
        let source_sample = match Sample::from_file(sample_path) {
            Ok(sample) => sample,
            Err(e) => {
                eprintln!("Error loading sample '{}': {}", sample_path, e);
                return self;
            }
        };

        // Capture cursor before mutable borrow
        let start_time = self.cursor;

        // Generate all the grain events
        let grain_events = create_granular_events(&source_sample, &params, duration, start_time);

        // Add all events to the track
        let track = self.get_track_mut();
        for event in grain_events {
            track.events.push(AudioEvent::Sample(event));
        }

        // Advance cursor
        self.cursor += duration;

        self
    }
}

#[cfg(test)]
mod tests {
    use crate::composition::Composition;
    use crate::consts::notes::*;
    use crate::composition::timing::Tempo;
    use crate::track::AudioEvent;
    use crate::synthesis::wavetable::{DEFAULT_TABLE_SIZE, Wavetable};

    #[test]
    fn test_custom_waveform_stored_in_note() {
        let mut comp = Composition::new(Tempo::new(120.0));

        // Create a custom wavetable
        let custom_wt =
            Wavetable::from_harmonics(DEFAULT_TABLE_SIZE, &[(1, 1.0), (3, 0.5), (5, 0.3)]);

        // Use it in a track
        comp.track("test")
            .custom_waveform(custom_wt)
            .note(&[440.0], 1.0);

        // Verify the note has the custom wavetable
        let mixer = comp.into_mixer();
        let track = &mixer.tracks()[0];

        if let AudioEvent::Note(note) = &track.events[0] {
            assert!(
                note.custom_wavetable.is_some(),
                "Custom wavetable should be stored in note"
            );
        } else {
            panic!("Expected NoteEvent");
        }
    }

    #[test]
    fn test_custom_waveform_persists_across_notes() {
        let mut comp = Composition::new(Tempo::new(120.0));

        let custom_wt = Wavetable::triangle();

        // Set custom waveform and play multiple notes
        comp.track("test")
            .custom_waveform(custom_wt)
            .notes(&[C4, E4, G4], 0.5);

        let mixer = comp.into_mixer();
        let track = &mixer.tracks()[0];

        // All three notes should have the custom wavetable
        assert_eq!(track.events.len(), 3);
        for event in &track.events {
            if let AudioEvent::Note(note) = event {
                assert!(
                    note.custom_wavetable.is_some(),
                    "All notes should have custom wavetable"
                );
            }
        }
    }

    #[test]
    fn test_custom_waveform_combines_with_envelope() {
        let mut comp = Composition::new(Tempo::new(120.0));

        let custom_wt = Wavetable::pwm(0.25);
        let env = crate::synthesis::envelope::Envelope::new(0.1, 0.2, 0.7, 0.3);

        // Use custom waveform with envelope
        comp.track("test")
            .custom_waveform(custom_wt)
            .envelope(env)
            .note(&[440.0], 1.0);

        let mixer = comp.into_mixer();
        let track = &mixer.tracks()[0];

        if let AudioEvent::Note(note) = &track.events[0] {
            assert!(note.custom_wavetable.is_some());
            // Verify envelope is also set
            assert_eq!(note.envelope.attack, 0.1);
        } else {
            panic!("Expected NoteEvent");
        }
    }

    #[test]
    fn test_custom_waveform_combines_with_fm() {
        let mut comp = Composition::new(Tempo::new(120.0));

        let custom_wt = Wavetable::saw_bandlimited();

        // Set both custom waveform and FM
        comp.track("test")
            .custom_waveform(custom_wt.clone())
            .fm_custom(2.0, 3.0)
            .note(&[440.0], 1.0);

        let mixer = comp.into_mixer();
        let track = &mixer.tracks()[0];

        if let AudioEvent::Note(note) = &track.events[0] {
            // When FM is active, it takes precedence in rendering
            // But custom wavetable should still be stored
            assert!(note.custom_wavetable.is_some());
            assert!(note.fm_params.mod_index > 0.0);
        } else {
            panic!("Expected NoteEvent");
        }
    }

    #[test]
    fn test_custom_waveform_can_be_cleared() {
        let mut comp = Composition::new(Tempo::new(120.0));

        let custom_wt = Wavetable::sine();

        // Use custom waveform for first note
        comp.track("test")
            .custom_waveform(custom_wt)
            .note(&[C4], 0.5);

        // Second note without custom waveform (new builder)
        comp.track("test").note(&[E4], 0.5);

        let mixer = comp.into_mixer();
        let track = &mixer.tracks()[0];

        // First note has custom wavetable
        if let AudioEvent::Note(note) = &track.events[0] {
            assert!(note.custom_wavetable.is_some());
        }

        // Second note doesn't have custom wavetable (builder was recreated)
        if let AudioEvent::Note(note) = &track.events[1] {
            assert!(note.custom_wavetable.is_none());
        }
    }
}