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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use super::TrackBuilder;
impl<'a> TrackBuilder<'a> {
/// Rapidly alternate between two notes (trill)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("trill", &Instrument::pluck())
/// .trill(C4, D4, 16, 0.05); // Alternates C4-D4-C4-D4... 16 times total
/// ```
pub fn trill(mut self, note1: f32, note2: f32, count: usize, note_duration: f32) -> Self {
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
for i in 0..count {
let freq = if i % 2 == 0 { note1 } else { note2 };
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[freq],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
let swung_duration = self.apply_swing(note_duration);
self.cursor += swung_duration;
}
self.update_section_duration();
self
}
/// Create a cascade/waterfall effect - notes start staggered but sustain
///
/// Each note in the sequence starts slightly offset from the previous one,
/// creating a lush, layered effect as they overlap.
///
/// # Arguments
/// * `notes` - The notes to cascade
/// * `note_duration` - How long each note sustains
/// * `stagger` - Time offset between each note start (smaller = more overlap)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// use tunes::consts::chords::C4_MAJOR;
/// comp.instrument("cascade", &Instrument::warm_pad())
/// .cascade(C4_MAJOR, 2.0, 0.1); // Notes start 0.1s apart but sustain 2s each
/// ```
pub fn cascade(mut self, notes: &[f32], note_duration: f32, stagger: f32) -> Self {
let start_cursor = self.cursor;
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
for (i, &freq) in notes.iter().enumerate() {
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[freq],
start_cursor + (i as f32 * stagger),
note_duration,
waveform,
envelope,
pitch_bend,
);
}
// Move cursor to the end of the cascade (last note start + its duration)
self.cursor = start_cursor + ((notes.len() - 1) as f32 * stagger) + note_duration;
self.update_section_duration();
self
}
/// Rapidly repeat the same note (tremolo note ornament)
///
/// Creates a rapid repetition of a single note, common in strings and synth pads.
/// Different from trill which alternates between two notes.
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("strings", &Instrument::pluck())
/// .tremolo_note(C4, 16, 0.05); // Rapid C4-C4-C4-C4... (16 times)
/// ```
pub fn tremolo_note(mut self, note: f32, count: usize, note_duration: f32) -> Self {
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
for _ in 0..count {
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
let swung_duration = self.apply_swing(note_duration);
self.cursor += swung_duration;
}
self.update_section_duration();
self
}
/// Guitar strum effect - quick cascade of chord notes
///
/// Plays a chord with notes slightly staggered in time to simulate
/// a guitar strum. This is essentially a very fast cascade optimized
/// for short stagger times (typically 10-30ms).
///
/// # Arguments
/// * `chord` - The chord notes to strum (bottom to top)
/// * `note_duration` - How long each note sustains
/// * `stagger` - Time delay between notes (0.01-0.03 for realistic guitar strum)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// use tunes::consts::chords::C4_MAJOR;
/// comp.instrument("guitar", &Instrument::pluck())
/// .strum(&C4_MAJOR, 0.5, 0.02); // Quick upstroke (20ms between strings)
/// ```
pub fn strum(self, chord: &[f32], note_duration: f32, stagger: f32) -> Self {
// Strum is just a fast cascade - keep it simple
self.cascade(chord, note_duration, stagger)
}
/// Grace note ornament
///
/// Plays a quick ornamental note before the main note.
/// Common in classical, jazz, and folk music.
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("ornament", &Instrument::pluck())
/// .grace(C4, B3, 0.05, 0.5); // Quick B3 grace note before sustained C4
pub fn grace(
mut self,
main_note: f32,
grace_note: f32,
grace_duration: f32,
main_duration: f32,
) -> Self {
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
// Play grace note
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[grace_note],
cursor,
grace_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += grace_duration;
// Play main note
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[main_note],
cursor,
main_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += main_duration;
self.update_section_duration();
self
}
/// Classical mordent ornament
///
/// Rapidly plays: main note → upper neighbor → main note
/// Creates a quick decorative flourish common in Baroque and Classical music.
///
/// # Arguments
/// * `main_note` - The principal note
/// * `duration` - Total duration for the ornament (typically 0.1-0.2 seconds)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("harpsichord", &Instrument::harpsichord())
/// .mordent(C4, 0.15); // Quick C4→D4→C4 ornament
/// ```
pub fn mordent(mut self, main_note: f32, duration: f32) -> Self {
if duration <= 0.0 || !duration.is_finite() {
return self;
}
let note_duration = duration / 3.0;
let upper_note = main_note * 2.0f32.powf(2.0 / 12.0); // Whole step up
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
// Main → Upper → Main
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[main_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[upper_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[main_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
self.update_section_duration();
self
}
/// Classical inverted mordent ornament
///
/// Rapidly plays: main note → lower neighbor → main note
/// The inverted version of the standard mordent, going down instead of up.
///
/// # Arguments
/// * `main_note` - The principal note
/// * `duration` - Total duration for the ornament (typically 0.1-0.2 seconds)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("harpsichord", &Instrument::harpsichord())
/// .inverted_mordent(C4, 0.15); // Quick C4→B3→C4 ornament
/// ```
pub fn inverted_mordent(mut self, main_note: f32, duration: f32) -> Self {
if duration <= 0.0 || !duration.is_finite() {
return self;
}
let note_duration = duration / 3.0;
let lower_note = main_note * 2.0f32.powf(-2.0 / 12.0); // Whole step down
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
// Main → Lower → Main
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[main_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[lower_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[main_note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
self.update_section_duration();
self
}
/// Classical turn ornament
///
/// Rapidly plays: upper neighbor → main note → lower neighbor → main note
/// A common embellishment in Classical and Romantic music.
///
/// # Arguments
/// * `main_note` - The principal note
/// * `duration` - Total duration for the ornament (typically 0.15-0.3 seconds)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("flute", &Instrument::synth_lead())
/// .turn(E4, 0.2); // F4→E4→D4→E4 ornament
/// ```
pub fn turn(mut self, main_note: f32, duration: f32) -> Self {
if duration <= 0.0 || !duration.is_finite() {
return self;
}
let note_duration = duration / 4.0;
let upper_note = main_note * 2.0f32.powf(2.0 / 12.0); // Whole step up
let lower_note = main_note * 2.0f32.powf(-2.0 / 12.0); // Whole step down
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
// Upper → Main → Lower → Main
let notes = [upper_note, main_note, lower_note, main_note];
for ¬e in ¬es {
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
}
self.update_section_duration();
self
}
/// Classical inverted turn ornament
///
/// Rapidly plays: lower neighbor → main note → upper neighbor → main note
/// The inverted version of the standard turn, starting from below.
///
/// # Arguments
/// * `main_note` - The principal note
/// * `duration` - Total duration for the ornament (typically 0.15-0.3 seconds)
///
/// # Example
/// ```
/// # use tunes::composition::Composition;
/// # use tunes::instruments::Instrument;
/// # use tunes::composition::timing::Tempo;
/// # use tunes::consts::notes::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// comp.instrument("flute", &Instrument::synth_lead())
/// .inverted_turn(E4, 0.2); // D4→E4→F4→E4 ornament
/// ```
pub fn inverted_turn(mut self, main_note: f32, duration: f32) -> Self {
if duration <= 0.0 || !duration.is_finite() {
return self;
}
let note_duration = duration / 4.0;
let upper_note = main_note * 2.0f32.powf(2.0 / 12.0); // Whole step up
let lower_note = main_note * 2.0f32.powf(-2.0 / 12.0); // Whole step down
let waveform = self.waveform;
let envelope = self.envelope;
let pitch_bend = self.pitch_bend;
// Lower → Main → Upper → Main
let notes = [lower_note, main_note, upper_note, main_note];
for ¬e in ¬es {
let cursor = self.cursor;
self.get_track_mut()
.add_note_with_waveform_envelope_and_bend(
&[note],
cursor,
note_duration,
waveform,
envelope,
pitch_bend,
);
self.cursor += note_duration;
}
self.update_section_duration();
self
}
}
#[cfg(test)]
mod tests {
use crate::composition::Composition;
use crate::consts::notes::*;
use crate::composition::timing::Tempo;
use crate::track::AudioEvent;
#[test]
fn test_trill_alternates_notes() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("trill").trill(C4, D4, 6, 0.1);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 6);
// Should alternate: C4, D4, C4, D4, C4, D4
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.frequencies[0], C4);
}
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.frequencies[0], D4);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert_eq!(note.frequencies[0], C4);
}
if let AudioEvent::Note(note) = &track.events[3] {
assert_eq!(note.frequencies[0], D4);
}
}
#[test]
fn test_trill_advances_cursor() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("trill").trill(C4, D4, 4, 0.25);
// 4 notes * 0.25 = 1.0
assert_eq!(builder.cursor, 1.0);
}
#[test]
fn test_trill_with_zero_count() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("trill").trill(C4, D4, 0, 0.1);
let mixer = comp.into_mixer();
// With zero count, no track is created since loop doesn't execute
assert_eq!(mixer.tracks().len(), 0, "Zero count should create no track");
}
#[test]
fn test_cascade_staggers_notes() {
let mut comp = Composition::new(Tempo::new(120.0));
let chord = [C4, E4, G4];
comp.track("cascade").cascade(&chord, 2.0, 0.1);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 3);
// Notes should start at 0.0, 0.1, 0.2 but all sustain 2.0s
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.start_time, 0.0);
assert_eq!(note.duration, 2.0);
}
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.start_time, 0.1);
assert_eq!(note.duration, 2.0);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert_eq!(note.start_time, 0.2);
assert_eq!(note.duration, 2.0);
}
}
#[test]
fn test_cascade_cursor_position() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("cascade").cascade(&[C4, E4, G4], 2.0, 0.1);
// Last note starts at 0.2 (2 staggers of 0.1) + duration 2.0 = 2.2
assert_eq!(builder.cursor, 2.2);
}
#[test]
fn test_tremolo_note_repeats_note() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("tremolo").tremolo_note(C4, 5, 0.05);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 5);
// All should be same note
for event in &track.events {
if let AudioEvent::Note(note) = event {
assert_eq!(note.frequencies[0], C4);
}
}
}
#[test]
fn test_tremolo_note_advances_cursor() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("tremolo").tremolo_note(C4, 10, 0.1);
// 10 notes * 0.1 = 1.0 (with floating point tolerance)
assert!((builder.cursor - 1.0).abs() < 0.01);
}
#[test]
fn test_strum_is_cascade() {
let mut comp = Composition::new(Tempo::new(120.0));
let chord = [C4, E4, G4];
comp.track("strum").strum(&chord, 1.0, 0.05);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 3);
// Should behave like cascade with small stagger
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.start_time, 0.0);
}
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.start_time, 0.05);
}
}
#[test]
fn test_grace_plays_two_notes() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("grace").grace(C4, B3, 0.05, 0.5);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 2);
// Grace note first
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.frequencies[0], B3);
assert_eq!(note.start_time, 0.0);
assert_eq!(note.duration, 0.05);
}
// Main note second
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.frequencies[0], C4);
assert_eq!(note.start_time, 0.05);
assert_eq!(note.duration, 0.5);
}
}
#[test]
fn test_grace_advances_cursor() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("grace").grace(C4, B3, 0.1, 0.9);
// 0.1 + 0.9 = 1.0
assert_eq!(builder.cursor, 1.0);
}
#[test]
fn test_mordent_pattern() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("mord").mordent(C4, 0.3);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 3);
// Pattern: Main → Upper → Main
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.frequencies[0], C4); // Main
assert_eq!(note.duration, 0.1); // 0.3 / 3
}
if let AudioEvent::Note(note) = &track.events[1] {
// Upper (whole step up = 2 semitones)
let expected_upper = C4 * 2.0f32.powf(2.0 / 12.0);
assert!((note.frequencies[0] - expected_upper).abs() < 0.1);
assert_eq!(note.duration, 0.1);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert_eq!(note.frequencies[0], C4); // Main
}
}
#[test]
fn test_mordent_with_zero_duration() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("mord").mordent(C4, 0.0);
// Should be no-op
assert_eq!(builder.cursor, 0.0);
}
#[test]
fn test_inverted_mordent_pattern() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("inv_mord").inverted_mordent(C4, 0.3);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 3);
// Pattern: Main → Lower → Main
if let AudioEvent::Note(note) = &track.events[0] {
assert_eq!(note.frequencies[0], C4); // Main
}
if let AudioEvent::Note(note) = &track.events[1] {
// Lower (whole step down = -2 semitones)
let expected_lower = C4 * 2.0f32.powf(-2.0 / 12.0);
assert!((note.frequencies[0] - expected_lower).abs() < 0.1);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert_eq!(note.frequencies[0], C4); // Main
}
}
#[test]
fn test_turn_pattern() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("turn").turn(C4, 0.4);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 4);
// Pattern: Upper → Main → Lower → Main
let upper = C4 * 2.0f32.powf(2.0 / 12.0);
let lower = C4 * 2.0f32.powf(-2.0 / 12.0);
if let AudioEvent::Note(note) = &track.events[0] {
assert!((note.frequencies[0] - upper).abs() < 0.1);
assert_eq!(note.duration, 0.1); // 0.4 / 4
}
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.frequencies[0], C4);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert!((note.frequencies[0] - lower).abs() < 0.1);
}
if let AudioEvent::Note(note) = &track.events[3] {
assert_eq!(note.frequencies[0], C4);
}
}
#[test]
fn test_inverted_turn_pattern() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("inv_turn").inverted_turn(C4, 0.4);
let track = &comp.into_mixer().tracks()[0];
assert_eq!(track.events.len(), 4);
// Pattern: Lower → Main → Upper → Main
let upper = C4 * 2.0f32.powf(2.0 / 12.0);
let lower = C4 * 2.0f32.powf(-2.0 / 12.0);
if let AudioEvent::Note(note) = &track.events[0] {
assert!((note.frequencies[0] - lower).abs() < 0.1);
}
if let AudioEvent::Note(note) = &track.events[1] {
assert_eq!(note.frequencies[0], C4);
}
if let AudioEvent::Note(note) = &track.events[2] {
assert!((note.frequencies[0] - upper).abs() < 0.1);
}
if let AudioEvent::Note(note) = &track.events[3] {
assert_eq!(note.frequencies[0], C4);
}
}
#[test]
fn test_ornament_chaining() {
let mut comp = Composition::new(Tempo::new(120.0));
comp.track("chain")
.grace(C4, B3, 0.05, 0.5)
.trill(D4, E4, 4, 0.1)
.tremolo_note(G4, 3, 0.1);
let track = &comp.into_mixer().tracks()[0];
// 2 (grace) + 4 (trill) + 3 (tremolo_note) = 9 total
assert_eq!(track.events.len(), 9);
}
#[test]
fn test_turn_with_negative_duration() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("neg").turn(C4, -0.1);
// Should be no-op due to guard
assert_eq!(builder.cursor, 0.0);
}
#[test]
fn test_mordent_advances_cursor_correctly() {
let mut comp = Composition::new(Tempo::new(120.0));
let builder = comp.track("mord").mordent(C4, 0.6);
// 0.6 total duration
assert_eq!(builder.cursor, 0.6);
}
}