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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
use crate::composition::TrackBuilder;

impl<'a> TrackBuilder<'a> {
    /// Set swing/groove timing (0.5 = straight, 0.67 = triplet swing, 0.75 = heavy swing)
    pub fn swing(mut self, swing: f32) -> Self {
        self.swing = swing.clamp(0.5, 0.9);
        self
    }
    pub fn at(mut self, time: f32) -> Self {
        self.cursor = time;
        self
    }

    pub fn wait(mut self, duration: f32) -> Self {
        self.cursor += duration;
        self
    }

    pub fn seek(mut self, offset: f32) -> Self {
        self.cursor += offset;
        self
    }

    /// Save the current cursor position with a name for later use
    ///
    /// Markers are stored globally in the composition and can be accessed
    /// by any track. This makes it easy to synchronize timing across tracks.
    ///
    /// # Arguments
    /// * `name` - The name to give this time marker
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("melody")
    ///     .notes(&[C4, E4, G4], 0.5)
    ///     .mark("verse_start")  // Save this position
    ///     .notes(&[C4, E4, G4], 0.5);
    ///
    /// comp.track("bass")
    ///     .at_mark("verse_start")  // Jump to saved position
    ///     .notes(&[C2, G2], 1.0);
    /// ```
    pub fn mark(self, name: &str) -> Self {
        self.composition
            .markers
            .insert(name.to_string(), self.cursor);
        self
    }

    /// Jump to a previously saved marker position
    ///
    /// # Arguments
    /// * `name` - The name of the marker to jump to
    ///
    /// # Panics
    /// Panics if the marker name doesn't exist. Use `.mark()` to create markers first.
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("drums")
    ///     .notes(&[C4], 0.5)
    ///     .mark("drop");
    ///
    /// comp.track("bass")
    ///     .at_mark("drop")  // Start exactly where drums marked "drop"
    ///     .notes(&[C2], 0.5);
    /// ```
    pub fn at_mark(mut self, name: &str) -> Self {
        let marker_time = match self.composition.markers.get(name) {
            Some(time) => *time,
            None => {
                eprintln!(
                    "Warning: Marker '{}' not found. Use .mark(\"{}\") to create it first. Cursor position unchanged.",
                    name, name
                );
                return self;
            }
        };
        self.cursor = marker_time;
        self
    }

    /// Jump to a previously saved marker position (alias for `at_mark`)
    ///
    /// This is an alias for `.at_mark()` that matches the naming convention
    /// of the composition-level marker API (`.mark_at()`, `.marker_time()`).
    ///
    /// # Arguments
    /// * `name` - The name of the marker to jump to
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// // Mark important points at the composition level
    /// comp.mark_at("drop", 16.0);
    ///
    /// // Jump to that marker from a track
    /// comp.track("bass")
    ///     .at_marker("drop")  // Start at the drop
    ///     .notes(&[C1, C1, E2], 0.5);
    /// ```
    pub fn at_marker(self, name: &str) -> Self {
        self.at_mark(name)
    }

    /// Get the current cursor position without modifying the builder
    ///
    /// Useful for debugging or conditional logic based on timing.
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// let builder = comp.track("melody")
    ///     .notes(&[C4, E4, G4], 0.5);
    ///
    /// let current_time = builder.peek_cursor();
    /// println!("Currently at: {} seconds", current_time);
    /// ```
    pub fn peek_cursor(&self) -> f32 {
        self.cursor
    }

    /// Insert a tempo change at the current cursor position
    ///
    /// # Arguments
    /// * `bpm` - The new tempo in beats per minute
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("melody")
    ///     .notes(&[C4, E4, G4], 0.5)  // 120 BPM
    ///     .tempo(90.0)                 // Slow down to 90 BPM
    ///     .notes(&[C4, E4, G4], 0.5); // These notes at 90 BPM (affects MIDI export)
    /// ```
    pub fn tempo(mut self, bpm: f32) -> Self {
        let cursor = self.cursor;
        self.get_track_mut()
            .events
            .push(crate::track::AudioEvent::TempoChange(
                crate::track::TempoChangeEvent {
                    start_time: cursor,
                    bpm,
                },
            ));
        self.get_track_mut().invalidate_time_cache();
        self
    }

    /// Insert a time signature change at the current cursor position
    ///
    /// # Arguments
    /// * `numerator` - Top number of time signature (beats per measure)
    /// * `denominator` - Bottom number of time signature (note value per beat)
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("melody")
    ///     .time_signature(4, 4)        // 4/4 time
    ///     .notes(&[C4, E4, G4, C5], 0.5)
    ///     .time_signature(3, 4)        // Switch to 3/4 time
    ///     .notes(&[C4, E4, G4], 0.5); // These notes in 3/4 (affects MIDI export)
    /// ```
    pub fn time_signature(mut self, numerator: u8, denominator: u8) -> Self {
        let cursor = self.cursor;
        self.get_track_mut()
            .events
            .push(crate::track::AudioEvent::TimeSignature(
                crate::track::TimeSignatureEvent {
                    start_time: cursor,
                    numerator,
                    denominator,
                },
            ));
        self.get_track_mut().invalidate_time_cache();
        self
    }

    /// Insert a key signature change at the current cursor position
    ///
    /// Sets the key signature for MIDI export and musical notation context.
    ///
    /// # Arguments
    /// * `key_signature` - The key signature to set (e.g., KeySignature::C_MAJOR)
    ///
    /// # Example
    /// ```
    /// # use tunes::prelude::*;
    /// # let mut comp = Composition::new(Tempo::new(120.0));
    /// comp.track("melody")
    ///     .key_signature(KeySignature::C_MAJOR)
    ///     .notes(&[C4, E4, G4], 0.5)
    ///     .key_signature(KeySignature::A_MINOR)  // Modulate to A minor
    ///     .notes(&[A3, C4, E4], 0.5);
    /// ```
    pub fn key_signature(mut self, key_signature: crate::theory::key_signature::KeySignature) -> Self {
        let cursor = self.cursor;
        self.get_track_mut()
            .events
            .push(crate::track::AudioEvent::KeySignature(
                crate::track::KeySignatureEvent {
                    start_time: cursor,
                    key_signature,
                },
            ));
        self.get_track_mut().invalidate_time_cache();
        self
    }
}

#[cfg(test)]
mod tests {
    use crate::composition::Composition;
    use crate::consts::notes::{C3, C4, E3, E4, G3, G4};
    use crate::composition::timing::Tempo;

    #[test]
    fn test_swing_sets_value() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").swing(0.67);

        assert_eq!(builder.swing, 0.67);
    }

    #[test]
    fn test_swing_clamps_low_values() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").swing(0.2);

        // Should clamp to 0.5
        assert_eq!(builder.swing, 0.5);
    }

    #[test]
    fn test_swing_clamps_high_values() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").swing(0.95);

        // Should clamp to 0.9
        assert_eq!(builder.swing, 0.9);
    }

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

        let builder_min = comp.track("test1").swing(0.5);
        assert_eq!(builder_min.swing, 0.5);

        let builder_max = comp.track("test2").swing(0.9);
        assert_eq!(builder_max.swing, 0.9);
    }

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

        // Straight timing
        let straight = comp.track("straight").swing(0.5);
        assert_eq!(straight.swing, 0.5);

        // Triplet swing
        let triplet = comp.track("triplet").swing(0.67);
        assert!((triplet.swing - 0.67).abs() < 0.01);

        // Heavy swing
        let heavy = comp.track("heavy").swing(0.75);
        assert_eq!(heavy.swing, 0.75);
    }

    #[test]
    fn test_at_sets_absolute_cursor_position() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").at(5.0);

        assert_eq!(builder.cursor, 5.0);
    }

    #[test]
    fn test_at_can_move_backward() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(10.0).at(3.0);

        // Should be at 3.0, not 13.0
        assert_eq!(builder.cursor, 3.0);
    }

    #[test]
    fn test_at_zero() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(5.0).at(0.0);

        assert_eq!(builder.cursor, 0.0);
    }

    #[test]
    fn test_wait_advances_cursor() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(2.5);

        assert_eq!(builder.cursor, 2.5);
    }

    #[test]
    fn test_wait_is_additive() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(1.0).wait(2.0).wait(0.5);

        assert_eq!(builder.cursor, 3.5);
    }

    #[test]
    fn test_wait_with_zero() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(5.0).wait(0.0);

        assert_eq!(builder.cursor, 5.0);
    }

    #[test]
    fn test_seek_advances_cursor() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").seek(3.0);

        assert_eq!(builder.cursor, 3.0);
    }

    #[test]
    fn test_seek_is_additive() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").seek(1.5).seek(2.0);

        assert_eq!(builder.cursor, 3.5);
    }

    #[test]
    fn test_seek_backward_with_negative() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").seek(5.0).seek(-2.0);

        // Should be at 3.0
        assert_eq!(builder.cursor, 3.0);
    }

    #[test]
    fn test_timing_methods_chain_together() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").at(0.0).wait(2.0).seek(1.0).swing(0.67);

        assert_eq!(builder.cursor, 3.0);
        assert!((builder.swing - 0.67).abs() < 0.01);
    }

    #[test]
    fn test_complex_timing_sequence() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp
            .track("test")
            .at(10.0) // Jump to 10.0
            .wait(5.0) // Now at 15.0
            .seek(-3.0) // Now at 12.0
            .at(0.0) // Back to 0.0
            .wait(1.0); // Now at 1.0

        assert_eq!(builder.cursor, 1.0);
    }

    #[test]
    fn test_wait_and_seek_are_equivalent() {
        let mut comp1 = Composition::new(Tempo::new(120.0));
        let mut comp2 = Composition::new(Tempo::new(120.0));

        let with_wait = comp1.track("wait").wait(2.5);
        let with_seek = comp2.track("seek").seek(2.5);

        assert_eq!(with_wait.cursor, with_seek.cursor);
    }

    #[test]
    fn test_timing_with_note_placement() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody")
            .at(0.0)
            .note(&[440.0], 0.5) // Note at 0.0, cursor advances to 0.5
            .wait(1.0) // Cursor advances to 1.5
            .note(&[550.0], 0.5); // Note at 1.5

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 2);

        if let crate::track::AudioEvent::Note(note1) = &track.events[0] {
            assert_eq!(note1.start_time, 0.0);
        }
        if let crate::track::AudioEvent::Note(note2) = &track.events[1] {
            assert_eq!(note2.start_time, 1.5); // 0.5 (after first note) + 1.0 (wait)
        }
    }

    #[test]
    fn test_tempo_adds_tempo_change_event() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody").tempo(90.0);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 1);

        if let crate::track::AudioEvent::TempoChange(tempo) = &track.events[0] {
            assert_eq!(tempo.bpm, 90.0);
            assert_eq!(tempo.start_time, 0.0);
        } else {
            panic!("Expected TempoChange event");
        }
    }

    #[test]
    fn test_tempo_at_specific_time() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody").at(5.0).tempo(140.0);

        let track = &comp.into_mixer().tracks()[0];
        if let crate::track::AudioEvent::TempoChange(tempo) = &track.events[0] {
            assert_eq!(tempo.bpm, 140.0);
            assert_eq!(tempo.start_time, 5.0);
        }
    }

    #[test]
    fn test_multiple_tempo_changes() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody")
            .tempo(100.0)
            .wait(2.0)
            .tempo(120.0)
            .wait(2.0)
            .tempo(140.0);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 3);

        if let crate::track::AudioEvent::TempoChange(tempo1) = &track.events[0] {
            assert_eq!(tempo1.bpm, 100.0);
            assert_eq!(tempo1.start_time, 0.0);
        }

        if let crate::track::AudioEvent::TempoChange(tempo2) = &track.events[1] {
            assert_eq!(tempo2.bpm, 120.0);
            assert_eq!(tempo2.start_time, 2.0);
        }

        if let crate::track::AudioEvent::TempoChange(tempo3) = &track.events[2] {
            assert_eq!(tempo3.bpm, 140.0);
            assert_eq!(tempo3.start_time, 4.0);
        }
    }

    #[test]
    fn test_tempo_with_notes() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody")
            .note(&[C4], 0.5)
            .tempo(90.0)
            .note(&[E4], 0.5);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 3); // 2 notes + 1 tempo change

        // First note
        if let crate::track::AudioEvent::Note(note) = &track.events[0] {
            assert_eq!(note.start_time, 0.0);
        }

        // Tempo change
        if let crate::track::AudioEvent::TempoChange(tempo) = &track.events[1] {
            assert_eq!(tempo.bpm, 90.0);
            assert_eq!(tempo.start_time, 0.5);
        }

        // Second note
        if let crate::track::AudioEvent::Note(note) = &track.events[2] {
            assert_eq!(note.start_time, 0.5);
        }
    }

    #[test]
    fn test_time_signature_adds_event() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody").time_signature(3, 4);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 1);

        if let crate::track::AudioEvent::TimeSignature(time_sig) = &track.events[0] {
            assert_eq!(time_sig.numerator, 3);
            assert_eq!(time_sig.denominator, 4);
            assert_eq!(time_sig.start_time, 0.0);
        } else {
            panic!("Expected TimeSignature event");
        }
    }

    #[test]
    fn test_time_signature_at_specific_time() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody").at(8.0).time_signature(6, 8);

        let track = &comp.into_mixer().tracks()[0];
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &track.events[0] {
            assert_eq!(time_sig.numerator, 6);
            assert_eq!(time_sig.denominator, 8);
            assert_eq!(time_sig.start_time, 8.0);
        }
    }

    #[test]
    fn test_multiple_time_signature_changes() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody")
            .time_signature(4, 4)
            .wait(4.0)
            .time_signature(3, 4)
            .wait(3.0)
            .time_signature(7, 8);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 3);

        if let crate::track::AudioEvent::TimeSignature(time_sig1) = &track.events[0] {
            assert_eq!(time_sig1.numerator, 4);
            assert_eq!(time_sig1.denominator, 4);
            assert_eq!(time_sig1.start_time, 0.0);
        }

        if let crate::track::AudioEvent::TimeSignature(time_sig2) = &track.events[1] {
            assert_eq!(time_sig2.numerator, 3);
            assert_eq!(time_sig2.denominator, 4);
            assert_eq!(time_sig2.start_time, 4.0);
        }

        if let crate::track::AudioEvent::TimeSignature(time_sig3) = &track.events[2] {
            assert_eq!(time_sig3.numerator, 7);
            assert_eq!(time_sig3.denominator, 8);
            assert_eq!(time_sig3.start_time, 7.0);
        }
    }

    #[test]
    fn test_time_signature_with_notes() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("melody")
            .time_signature(4, 4)
            .note(&[C4], 0.5)
            .note(&[E4], 0.5)
            .time_signature(3, 4)
            .note(&[G4], 0.5);

        let track = &comp.into_mixer().tracks()[0];
        assert_eq!(track.events.len(), 5); // 2 time sigs + 3 notes

        // First time signature
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &track.events[0] {
            assert_eq!(time_sig.numerator, 4);
            assert_eq!(time_sig.denominator, 4);
            assert_eq!(time_sig.start_time, 0.0);
        }

        // First note
        if let crate::track::AudioEvent::Note(note) = &track.events[1] {
            assert_eq!(note.start_time, 0.0);
        }

        // Second note
        if let crate::track::AudioEvent::Note(note) = &track.events[2] {
            assert_eq!(note.start_time, 0.5);
        }

        // Second time signature
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &track.events[3] {
            assert_eq!(time_sig.numerator, 3);
            assert_eq!(time_sig.denominator, 4);
            assert_eq!(time_sig.start_time, 1.0);
        }

        // Third note
        if let crate::track::AudioEvent::Note(note) = &track.events[4] {
            assert_eq!(note.start_time, 1.0);
        }
    }

    #[test]
    fn test_time_signature_common_values() {
        let mut comp1 = Composition::new(Tempo::new(120.0));
        comp1.track("test").time_signature(4, 4);
        let mixer1 = comp1.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer1.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 4);
            assert_eq!(time_sig.denominator, 4);
        }

        let mut comp2 = Composition::new(Tempo::new(120.0));
        comp2.track("test").time_signature(3, 4);
        let mixer2 = comp2.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer2.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 3);
            assert_eq!(time_sig.denominator, 4);
        }

        let mut comp3 = Composition::new(Tempo::new(120.0));
        comp3.track("test").time_signature(6, 8);
        let mixer3 = comp3.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer3.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 6);
            assert_eq!(time_sig.denominator, 8);
        }

        let mut comp4 = Composition::new(Tempo::new(120.0));
        comp4.track("test").time_signature(5, 4);
        let mixer4 = comp4.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer4.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 5);
            assert_eq!(time_sig.denominator, 4);
        }

        let mut comp5 = Composition::new(Tempo::new(120.0));
        comp5.track("test").time_signature(7, 8);
        let mixer5 = comp5.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer5.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 7);
            assert_eq!(time_sig.denominator, 8);
        }

        let mut comp6 = Composition::new(Tempo::new(120.0));
        comp6.track("test").time_signature(12, 8);
        let mixer6 = comp6.into_mixer();
        if let crate::track::AudioEvent::TimeSignature(time_sig) = &mixer6.tracks()[0].events[0] {
            assert_eq!(time_sig.numerator, 12);
            assert_eq!(time_sig.denominator, 8);
        }
    }

    #[test]
    fn test_mark_saves_cursor_position() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("test").wait(5.0).mark("test_marker");

        assert_eq!(*comp.markers.get("test_marker").unwrap(), 5.0);
    }

    #[test]
    fn test_at_mark_moves_to_saved_position() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("track1").wait(10.0).mark("drop");

        let builder = comp.track("track2").at_mark("drop");

        assert_eq!(builder.cursor, 10.0);
    }

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

        // Should not panic, just leave cursor unchanged
        comp.track("test")
            .at(1.0) // Set cursor to 1.0
            .at_mark("nonexistent") // Try to use non-existent marker
            .note(&[C4], 0.5);

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

        // Note should still be at 1.0 (cursor unchanged by missing marker)
        if let crate::track::AudioEvent::Note(note) = &track.events[0] {
            assert_eq!(note.start_time, 1.0);
        }
    }

    #[test]
    fn test_multiple_markers() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("main")
            .wait(2.0)
            .mark("verse")
            .wait(4.0)
            .mark("chorus")
            .wait(3.0)
            .mark("bridge");

        assert_eq!(*comp.markers.get("verse").unwrap(), 2.0);
        assert_eq!(*comp.markers.get("chorus").unwrap(), 6.0);
        assert_eq!(*comp.markers.get("bridge").unwrap(), 9.0);
    }

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

        // Track 1 sets markers
        comp.track("drums")
            .note(&[C4], 0.5)
            .mark("intro_end")
            .wait(2.0)
            .mark("verse_start");

        // Track 2 uses those markers
        let bass_builder = comp.track("bass").at_mark("verse_start");

        assert_eq!(bass_builder.cursor, 2.5); // 0.5 + 2.0
    }

    #[test]
    fn test_marker_can_be_overwritten() {
        let mut comp = Composition::new(Tempo::new(120.0));
        comp.track("test1").wait(5.0).mark("position");

        comp.track("test2").wait(10.0).mark("position"); // Overwrites previous

        assert_eq!(*comp.markers.get("position").unwrap(), 10.0);
    }

    #[test]
    fn test_peek_cursor_returns_current_position() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(3.5);

        assert_eq!(builder.peek_cursor(), 3.5);
    }

    #[test]
    fn test_peek_cursor_doesnt_advance() {
        let mut comp = Composition::new(Tempo::new(120.0));
        let builder = comp.track("test").wait(2.0);

        let pos1 = builder.peek_cursor();
        let pos2 = builder.peek_cursor();

        assert_eq!(pos1, 2.0);
        assert_eq!(pos2, 2.0);
    }

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

        // Create arrangement structure with markers
        comp.track("structure")
            .mark("start")
            .wait(8.0)
            .mark("verse")
            .wait(16.0)
            .mark("chorus")
            .wait(8.0)
            .mark("bridge")
            .wait(16.0)
            .mark("outro");

        // Different instruments start at different sections
        comp.track("lead").at_mark("verse").note(&[E4], 0.5);

        comp.track("pad").at_mark("chorus").note(&[C3, E3, G3], 2.0);

        let mixer = comp.into_mixer();

        // Find tracks with notes (structure track has no notes, so only lead and pad)
        let mut note_times = vec![];
        for track in &mixer.tracks() {
            if let Some(crate::track::AudioEvent::Note(note)) = track.events.first() {
                note_times.push(note.start_time);
            }
        }

        // Sort to get consistent ordering
        note_times.sort_by(|a, b| a.partial_cmp(b).unwrap());

        // Verify we have 2 notes at the correct times
        assert_eq!(note_times.len(), 2);
        assert_eq!(note_times[0], 8.0); // verse
        assert_eq!(note_times[1], 24.0); // chorus
    }

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

        comp.track("timing")
            .at(0.0)
            .wait(2.0)
            .mark("a")
            .seek(-1.0) // Back to 1.0
            .mark("b")
            .at(10.0)
            .mark("c")
            .wait(5.0)
            .seek(-3.0) // At 12.0
            .mark("d");

        assert_eq!(*comp.markers.get("a").unwrap(), 2.0);
        assert_eq!(*comp.markers.get("b").unwrap(), 1.0);
        assert_eq!(*comp.markers.get("c").unwrap(), 10.0);
        assert_eq!(*comp.markers.get("d").unwrap(), 12.0);
    }
}