mtrack 0.12.0

A multitrack audio and MIDI player for live performances.
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
use crate::lighting::parser::*;
use std::time::Duration;

#[test]
fn test_measure_offset() {
    // Test that offset commands correctly adjust measure numbers
    // Measures 1-8 repeat once, so measure 9 should be at playback measure 17
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s, duration: 5s

    offset 8 measures

    @1/1
    all_wash: static, color: "green", duration: 5s, duration: 5s

    @8/1
    all_wash: static, color: "yellow", duration: 5s, duration: 5s

    @9/1
    all_wash: static, color: "red", duration: 5s, duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 measure = 2.0s
    // Measure 1, beat 1 = 0.0s + (0 measures * 2.0s) = 0.0s
    // After offset 8: measure 1 becomes playback measure 9 = 0.0s + (8 measures * 2.0s) = 16.0s
    // After offset 8: measure 8 becomes playback measure 16 = 0.0s + (15 measures * 2.0s) = 30.0s
    // After offset 8: measure 9 becomes playback measure 17 = 0.0s + (16 measures * 2.0s) = 32.0s

    assert!(show.cues.len() >= 4, "Should have at least 4 cues");

    // First cue at measure 1 = 0.0s
    let first_cue_time = show.cues[0].time;
    let expected_first = Duration::from_secs_f64(0.0);
    assert!(
        (first_cue_time.as_secs_f64() - expected_first.as_secs_f64()).abs() < 0.001,
        "First cue should be at measure 1 (0.0s), got {:?}",
        first_cue_time
    );

    // Second cue at measure 1 (with offset) = playback measure 9 = 16.0s
    let second_cue_time = show.cues[1].time;
    let expected_second = Duration::from_secs_f64(16.0);
    assert!(
        (second_cue_time.as_secs_f64() - expected_second.as_secs_f64()).abs() < 0.001,
        "Second cue should be at playback measure 9 (16.0s), got {:?}",
        second_cue_time
    );

    // Third cue at measure 8 (with offset) = playback measure 16 = 30.0s
    let third_cue_time = show.cues[2].time;
    let expected_third = Duration::from_secs_f64(30.0);
    assert!(
        (third_cue_time.as_secs_f64() - expected_third.as_secs_f64()).abs() < 0.001,
        "Third cue should be at playback measure 16 (30.0s), got {:?}",
        third_cue_time
    );

    // Fourth cue at measure 9 (with offset) = playback measure 17 = 32.0s
    let fourth_cue_time = show.cues[3].time;
    let expected_fourth = Duration::from_secs_f64(32.0);
    assert!(
        (fourth_cue_time.as_secs_f64() - expected_fourth.as_secs_f64()).abs() < 0.001,
        "Fourth cue should be at playback measure 17 (32.0s), got {:?}",
        fourth_cue_time
    );
}

#[test]
fn test_reset_measures() {
    // Test that reset_measures resets the offset back to 0
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    offset 8 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
    
    reset_measures
    
    @1/1
    all_wash: static, color: "red", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with reset_measures: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 measure = 2.0s
    // First cue at measure 1 = 0.0s
    // Second cue at measure 1 (with offset 8) = playback measure 9 = 16.0s
    // Third cue at measure 1 (after reset) = 0.0s again

    assert!(show.cues.len() >= 3, "Should have at least 3 cues");

    let first_cue_time = show.cues[0].time;
    let second_cue_time = show.cues[1].time;
    let third_cue_time = show.cues[2].time;

    assert!(
        (first_cue_time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s, got {:?}",
        first_cue_time
    );

    assert!(
        (second_cue_time.as_secs_f64() - 16.0).abs() < 0.001,
        "Second cue should be at 16.0s (measure 9), got {:?}",
        second_cue_time
    );

    assert!(
        (third_cue_time.as_secs_f64() - 0.0).abs() < 0.001,
        "Third cue should be at 0.0s (after reset), got {:?}",
        third_cue_time
    );
}

#[test]
fn test_measure_offset_accumulation() {
    // Test that multiple offset commands accumulate
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    offset 4 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
    
    offset 4 measures
    
    @1/1
    all_wash: static, color: "red", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with accumulating offsets: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 measure = 2.0s
    // First cue at measure 1 = 0.0s
    // After offset 4: measure 1 becomes playback measure 5 = 8.0s
    // After offset 8 (4+4): measure 1 becomes playback measure 9 = 16.0s

    assert!(show.cues.len() >= 3, "Should have at least 3 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 8.0).abs() < 0.001,
        "Second cue should be at 8.0s (measure 5)"
    );

    assert!(
        (show.cues[2].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Third cue should be at 16.0s (measure 9)"
    );
}

#[test]
fn test_measure_offset_in_sequence() {
    // Test that offset works correctly in sequences
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

sequence "verse" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    offset 8 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
    
    @9/1
    all_wash: static, color: "red", duration: 5s
}

show "Test" {
    @1/1
    sequence "verse"
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse sequence with offset: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // Sequence is referenced at measure 1, so its @1/1 becomes measure 1
    // After offset 8 in sequence: @1/1 becomes playback measure 9
    // After offset 8 in sequence: @9/1 becomes playback measure 17
    // But these are relative to sequence start (measure 1), so:
    // First cue: measure 1 = 0.0s
    // Second cue: measure 9 = 16.0s
    // Third cue: measure 17 = 32.0s

    assert!(show.cues.len() >= 3, "Should have at least 3 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Second cue should be at 16.0s (measure 9)"
    );

    assert!(
        (show.cues[2].time.as_secs_f64() - 32.0).abs() < 0.001,
        "Third cue should be at 32.0s (measure 17)"
    );
}

#[test]
fn test_measure_offset_with_fractional_beats() {
    // Test that offset works with fractional beats
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1.5
    all_wash: static, color: "blue", duration: 5s
    
    offset 8 measures
    
    @1/2.5
    all_wash: static, color: "green", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset and fractional beats: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 beat = 0.5s
    // First cue: measure 1, beat 1.5 = 0.0s + (0 measures + 0.5 beats) * 0.5s = 0.25s
    // After offset 8: measure 1, beat 2.5 = (8 measures + 1.5 beats) * 0.5s = 16.0s + 0.75s = 16.75s

    assert!(show.cues.len() >= 2, "Should have at least 2 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.25).abs() < 0.001,
        "First cue should be at 0.25s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 16.75).abs() < 0.001,
        "Second cue should be at 16.75s"
    );
}

#[test]
fn test_measure_offset_with_tempo_changes() {
    // Test that offset works correctly with tempo changes
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
    changes: [
    @8/1 {
    bpm: 60
    }
    ]
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    offset 8 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset and tempo change: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 measure = 2.0s
    // With offset 8 applied to tempo changes:
    // - Tempo change at score measure 8/1 + offset 8 = playback measure 16
    // - Second cue at score measure 1 + offset 8 = playback measure 9
    // Since the tempo change is at playback measure 16 (after the cue at measure 9),
    // the second cue is still at 120 BPM
    // Measures 1-8 at 120 BPM = 8 * 2.0s = 16.0s
    // Start of measure 9: 16.0s

    assert!(show.cues.len() >= 2, "Should have at least 2 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Second cue should be at 16.0s (measure 9 at 120 BPM, tempo change is at measure 16)"
    );
}

#[test]
fn test_measure_offset_in_same_cue() {
    // Test that when a cue has both a measure time and an offset command,
    // the offset does NOT apply to that cue's measure time, but DOES apply to subsequent cues.
    // This tests the scenario where offset is in the same cue definition as a measure time.
    let content = r#"tempo {
        start: 1.5s
    bpm: 160
    time_signature: 4/4
    changes: [
        @68/1 { bpm: 180 },
        @104/1 { bpm: 160 }
    ]
    }

show "Test" {
    @70/1
    all_wash: static, color: "blue", duration: 5s
    
    @74/1
    all_wash: static, color: "green", duration: 5s
    offset 5 measures
    
    @70/1
    all_wash: static, color: "red", duration: 5s
    
    @74/1
    all_wash: static, color: "yellow", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset in same cue: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // Verify we have 4 cues
    assert!(show.cues.len() >= 4, "Should have at least 4 cues");

    // First cue at @70/1 (no offset yet)
    // Tempo changes at @68/1 to 180 BPM
    // Measures 1-67 at 160 BPM: 67 * 1.5s = 100.5s
    // Measures 68-70 at 180 BPM: 2 * 1.333s = 2.667s
    // Total: 100.5 + 2.667 = 103.167s
    // Plus start_offset 1.5s: 103.167 + 1.5 = 104.667s
    let first_cue_time = show.cues[0].time;
    let expected_first = Duration::from_secs_f64(104.666666667);
    assert!(
        (first_cue_time.as_secs_f64() - expected_first.as_secs_f64()).abs() < 0.01,
        "First cue should be at measure 70 (104.667s), got {:?} ({:.3}s)",
        first_cue_time,
        first_cue_time.as_secs_f64()
    );

    // Second cue at @74/1 (no offset yet)
    // Measures 1-67 @160 BPM: 100.5s
    // Measures 68-73 @180 BPM: 6 * 1.333s = 8.0s
    // Plus start_offset 1.5s => 110.0s
    let second_cue_time = show.cues[1].time;
    let expected_second = Duration::from_secs_f64(110.0);
    assert!(
        (second_cue_time.as_secs_f64() - expected_second.as_secs_f64()).abs() < 0.01,
        "Second cue should be at measure 74 (110.0s), got {:?} ({:.3}s)",
        second_cue_time,
        second_cue_time.as_secs_f64()
    );

    // Third cue at @70/1 with offset 5 measures = playback measure 75
    // Base @70/1 time: 104.667s, offset 5 measures at 180 BPM = 6.667s
    let third_cue_time = show.cues[2].time;
    let expected_third = Duration::from_secs_f64(111.333333333);
    assert!(
        (third_cue_time.as_secs_f64() - expected_third.as_secs_f64()).abs() < 0.01,
        "Third cue should be at playback measure 75 (~111.33s) after offset 5, got {:?} ({:.3}s)",
        third_cue_time,
        third_cue_time.as_secs_f64()
    );

    // Fourth cue at @74/1 with offset 5 measures = playback measure 79
    // Base @74/1 time: 110.0s, offset still 5 measures at 180 BPM = 6.667s => ~116.67s
    let fourth_cue_time = show.cues[3].time;
    let expected_fourth = Duration::from_secs_f64(116.666666667);
    assert!(
        (fourth_cue_time.as_secs_f64() - expected_fourth.as_secs_f64()).abs() < 0.01,
        "Fourth cue should be at playback measure 79 (~116.67s) after offset 5, got {:?} ({:.3}s)",
        fourth_cue_time,
        fourth_cue_time.as_secs_f64()
    );
}

#[test]
fn test_measure_offset_with_measure_time_in_same_cue() {
    // Test that when a cue has both a measure time AND an offset command in the same
    // cue definition, the offset does NOT apply to the current cue's measure time.
    // The offset should only affect subsequent cues. The effect should still be included.
    let content = r#"tempo {
        start: 0.0s
    bpm: 120
    time_signature: 4/4
    }

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    @5/1
    all_wash: static, color: "green", duration: 5s
    offset 10 measures
    
    @1/1
    all_wash: static, color: "red", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset and measure time in same cue: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // Verify we have 3 cues
    assert!(show.cues.len() >= 3, "Should have at least 3 cues");

    // At 120 BPM in 4/4: 1 measure = 2.0s

    // First cue at @1/1 (no offset) = 0.0s
    let first_cue_time = show.cues[0].time;
    let expected_first = Duration::from_secs_f64(0.0);
    assert!(
        (first_cue_time.as_secs_f64() - expected_first.as_secs_f64()).abs() < 0.001,
        "First cue should be at measure 1 (0.0s), got {:?} ({:.3}s)",
        first_cue_time,
        first_cue_time.as_secs_f64()
    );
    // Verify first cue has the effect
    assert!(
        !show.cues[0].effects.is_empty(),
        "First cue should have effects"
    );

    // Second cue at @5/1 with offset 10 measures in the SAME cue
    // IMPORTANT: The offset should NOT apply to this cue's measure time.
    // So @5/1 should be at playback measure 5 = 4 measures * 2.0s = 8.0s
    let second_cue_time = show.cues[1].time;
    let expected_second = Duration::from_secs_f64(8.0);
    assert!(
        (second_cue_time.as_secs_f64() - expected_second.as_secs_f64()).abs() < 0.001,
        "Second cue should be at playback measure 5 (8.0s), offset should NOT apply to current cue, got {:?} ({:.3}s)",
        second_cue_time,
        second_cue_time.as_secs_f64()
    );
    // Verify second cue has the effect (this is the key test - effect should not be lost)
    assert!(
        !show.cues[1].effects.is_empty(),
        "Second cue should have effects even though it has an offset command"
    );

    // Third cue at @1/1 (with offset 10 from previous cue) = playback measure 11 = 10 measures * 2.0s = 20.0s
    let third_cue_time = show.cues[2].time;
    let expected_third = Duration::from_secs_f64(20.0);
    assert!(
        (third_cue_time.as_secs_f64() - expected_third.as_secs_f64()).abs() < 0.001,
        "Third cue should be at playback measure 11 (20.0s) with offset 10, got {:?} ({:.3}s)",
        third_cue_time,
        third_cue_time.as_secs_f64()
    );
}

#[test]
fn test_offset_timing_at_180_bpm_with_tempo_change() {
    // Test the exact scenario: @70/1, @74/1 with offset 5, @70/1 at 180 BPM
    // This verifies that the offset is applied correctly and timing is accurate
    // when there's a tempo change from 160 to 180 BPM at @68/1
    let content = r#"tempo {
        start: 1.5s
    bpm: 160
    time_signature: 4/4
    changes: [
        @68/1 { bpm: 180 },
        @104/1 { bpm: 160 }
    ]
    }

show "Test" {
    @70/1
    all_wash: static, color: "blue", duration: 5s
    
    @74/1
    all_wash: static, color: "green", duration: 5s
    offset 5 measures
    
    @70/1
    all_wash: static, color: "red", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(result.is_ok(), "Failed to parse show: {:?}", result.err());

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    assert!(show.cues.len() >= 3, "Should have at least 3 cues");

    // At 180 BPM in 4/4: 1 measure = 4 beats * (60/180) = 1.333... seconds
    // Measure 74 to measure 75 = 1 measure = 1.333 seconds

    let second_cue_time = show.cues[1].time; // @74/1
    let third_cue_time = show.cues[2].time; // @70/1 with offset 5 (should be measure 75)

    println!(
        "Second cue (@74/1) time: {:.3}s",
        second_cue_time.as_secs_f64()
    );
    println!(
        "Third cue (@70/1 with offset 5) time: {:.3}s",
        third_cue_time.as_secs_f64()
    );

    // Also compute directly from tempo map for visibility
    let tm = show.tempo_map.as_ref().unwrap();
    let calc_74 = tm
        .measure_to_time_with_offset(74, 1.0, 0, 0.0)
        .unwrap()
        .as_secs_f64();
    let calc_70_off5 = tm
        .measure_to_time_with_offset(70, 1.0, 5, 0.0)
        .unwrap()
        .as_secs_f64();
    println!(
        "Calc tempo map: @74/1 = {:.3}s, @70/1 (offset 5) = {:.3}s, diff = {:.3}s",
        calc_74,
        calc_70_off5,
        calc_70_off5 - calc_74
    );

    let time_diff = third_cue_time.as_secs_f64() - second_cue_time.as_secs_f64();
    let expected_diff = 1.333333333; // 1 measure at 180 BPM

    // Calculate what measure the third cue is actually at based on the time difference
    let actual_measures = time_diff / 1.333333333; // measures at 180 BPM
    println!(
        "Time difference: {:.3}s = {:.3} measures at 180 BPM (expected: 1.0 measure)",
        time_diff, actual_measures
    );

    assert!(
        (time_diff - expected_diff).abs() < 0.01,
        "Time difference between @74/1 and second @70/1 (with offset 5) should be ~1.333s (1 measure at 180 BPM), got {:.3}s (difference: {:.3}s, actual: {:.3} measures)",
        time_diff,
        time_diff - expected_diff,
        actual_measures
    );
}

#[test]
fn test_measure_offset_at_start() {
    // Test that offset can be in the first cue
    let content = r#"tempo {
        start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    offset 8 measures
    
    @1/1
    all_wash: static, color: "blue", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with offset in first cue: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // First cue at @1/1 = 0.0s (parsed before offset command), then offset is set to 8
    // Second cue at @1/1 with offset 8 = playback measure 9 = 16.0s

    assert!(show.cues.len() >= 2, "Should have at least 2 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s (before offset takes effect)"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Second cue should be at 16.0s (measure 9, after offset)"
    );
}

#[test]
fn test_measure_offset_reset_and_reoffset() {
    // Test reset followed by another offset
    // Note: offset commands affect subsequent cues, not the current one
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    offset 8 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
    
    @1/1
    all_wash: static, color: "yellow", duration: 5s
    reset_measures
    offset 4 measures
    
    @1/1
    all_wash: static, color: "red", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with reset and reoffset: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // First cue: measure 1 = 0.0s, then offset becomes 8
    // Second cue: measure 1 with offset 8 = measure 9 = 16.0s
    // Third cue: measure 1 with offset 8 (still) = measure 9 = 16.0s, then reset and offset 4
    // Fourth cue: measure 1 with offset 4 (after reset) = measure 5 = 8.0s

    assert!(show.cues.len() >= 4, "Should have at least 4 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Second cue should be at 16.0s (measure 9)"
    );

    assert!(
        (show.cues[2].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Third cue should be at 16.0s (measure 9, before reset takes effect)"
    );

    let fourth_cue_time = show.cues[3].time.as_secs_f64();
    assert!(
        (fourth_cue_time - 8.0).abs() < 0.001,
        "Fourth cue should be at 8.0s (measure 5 after reset and offset 4), got {}",
        fourth_cue_time
    );
}

#[test]
fn test_measure_offset_with_alternate_endings_scenario() {
    // Test the original use case: measures 1-8 repeat twice, measure 9 is alternate ending
    // This simulates: measures 1-8 play, then 1-8 again, but measure 9 replaces measure 8 on 2nd repeat
    let content = r#"tempo {
    start: 0.0s
    bpm: 120
    time_signature: 4/4
}

show "Test" {
    @1/1
    all_wash: static, color: "blue", duration: 5s
    
    @8/1
    all_wash: static, color: "yellow", duration: 5s
    
    offset 8 measures
    
    @1/1
    all_wash: static, color: "green", duration: 5s
    
    @7/1
    all_wash: static, color: "orange", duration: 5s
    
    @9/1
    all_wash: static, color: "red", duration: 5s
    
    @10/1
    all_wash: static, color: "purple", duration: 5s
}
"#;

    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Failed to parse show with alternate endings scenario: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Test").unwrap();

    // At 120 BPM in 4/4: 1 measure = 2.0s
    // First playthrough: measures 1-8
    //   @1/1 = 0.0s (measure 1)
    //   @8/1 = 14.0s (measure 8)
    // Second playthrough (after offset 8): measures 1-8 again, but measure 9 replaces 8
    //   @1/1 = 16.0s (playback measure 9)
    //   @7/1 = 28.0s (playback measure 15 = 14 measures * 2.0s)
    //   @9/1 = 32.0s (playback measure 17 = 16 measures * 2.0s) - this is the alternate ending
    //   @10/1 = 34.0s (playback measure 18 = 17 measures * 2.0s) - continues after repeat

    assert!(show.cues.len() >= 6, "Should have at least 6 cues");

    assert!(
        (show.cues[0].time.as_secs_f64() - 0.0).abs() < 0.001,
        "First cue should be at 0.0s"
    );

    assert!(
        (show.cues[1].time.as_secs_f64() - 14.0).abs() < 0.001,
        "Second cue should be at 14.0s (measure 8)"
    );

    assert!(
        (show.cues[2].time.as_secs_f64() - 16.0).abs() < 0.001,
        "Third cue should be at 16.0s (measure 9, second repeat)"
    );

    assert!(
        (show.cues[3].time.as_secs_f64() - 28.0).abs() < 0.001,
        "Fourth cue should be at 28.0s (measure 15)"
    );

    assert!(
        (show.cues[4].time.as_secs_f64() - 32.0).abs() < 0.001,
        "Fifth cue should be at 32.0s (measure 17, alternate ending)"
    );

    assert!(
        (show.cues[5].time.as_secs_f64() - 34.0).abs() < 0.001,
        "Sixth cue should be at 34.0s (measure 18, after repeat)"
    );
}

#[test]
fn test_esaweg_strobe_cue_generation() {
    // Verify that the Esaweg show's @236/1 strobe cue is generated with
    // the correct time and effect parameters. Uses the same tempo structure
    // and offset as the real show, but only the cues near the strobe.
    let content = r##"tempo {
    bpm: 110
    time_signature: 4/4
    changes: [
        @30/1 { bpm: 160 },
        @136/1 { bpm: 110 },
        @188/1 { bpm: 160 }
    ]
}

show "Esaweg" {
    @1/1
    offset 1 measures

    @221/1
    clear()
    all_wash: static, color: "#FF4400", layer: background, blend_mode: replace, duration: 5s

    @228/1
    all_wash: pulse, frequency: 3, layer: foreground, blend_mode: overlay, duration: 5s

    @236/1
    all_wash: strobe, frequency: 10, layer: foreground, blend_mode: overlay, duration: 5s

    @236/4.75
    clear()
    all_wash: static, color: "black", layer: background, blend_mode: replace, duration: 5s
}"##;

    let shows = parse_light_shows(content).expect("Should parse Esaweg show");
    let show = shows.get("Esaweg").expect("Should have Esaweg show");

    // Find the cue that has a strobe effect
    let strobe_cue = show.cues.iter().find(|cue| {
        cue.effects.iter().any(|e| {
            matches!(
                e.effect_type,
                crate::lighting::effects::EffectType::Strobe { .. }
            )
        })
    });
    assert!(
        strobe_cue.is_some(),
        "Should have a cue with a strobe effect"
    );
    let strobe_cue = strobe_cue.unwrap();

    // Verify the strobe effect parameters
    let strobe_effect = strobe_cue
        .effects
        .iter()
        .find(|e| {
            matches!(
                e.effect_type,
                crate::lighting::effects::EffectType::Strobe { .. }
            )
        })
        .unwrap();

    assert_eq!(strobe_effect.groups, vec!["all_wash"]);
    assert_eq!(
        strobe_effect.layer,
        Some(crate::lighting::effects::EffectLayer::Foreground)
    );
    assert_eq!(
        strobe_effect.blend_mode,
        Some(crate::lighting::effects::BlendMode::Overlay)
    );

    // Verify frequency is 10Hz
    if let crate::lighting::effects::EffectType::Strobe { frequency, .. } =
        &strobe_effect.effect_type
    {
        let hz = frequency.to_hz(None, Duration::ZERO);
        assert!(
            (hz - 10.0).abs() < 0.001,
            "Strobe frequency should be 10Hz, got {}",
            hz
        );
    }

    // Verify the strobe cue time is reasonable.
    // @236/1 with 1 measure offset and the given tempo changes should be around 409.9s.
    let strobe_time = strobe_cue.time.as_secs_f64();
    assert!(
        strobe_time > 400.0 && strobe_time < 420.0,
        "Strobe cue time should be around 409.9s, got {}",
        strobe_time
    );

    // Also verify the final cue (@236/4.75) exists and comes after the strobe
    let final_cue = show.cues.last().unwrap();
    assert!(
        final_cue.time > strobe_cue.time,
        "Final cue should come after strobe cue"
    );

    // The window between strobe and final cue should be about 1.4s (3.75 beats at 160 BPM)
    let window = (final_cue.time - strobe_cue.time).as_secs_f64();
    assert!(
        (window - 1.40625).abs() < 0.01,
        "Strobe window should be ~1.4s, got {}",
        window
    );
}