bms-rs 1.0.0

The BMS format parser.
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
//! Bms Processor Module.

use std::{
    collections::{BTreeMap, HashMap},
    convert::TryFrom,
    path::PathBuf,
};

use itertools::Itertools;
use strict_num_extended::{FinF64, PositiveF64};

use crate::bms::command::StringValue;
use crate::bms::parse::check_playing::PlayingError;
use crate::bms::prelude::*;
use crate::chart::event::{ChartEvent, FlowEvent, PlayheadEvent};
use crate::chart::process::{
    AllEventsIndex, BmpId, ChartEventIdGenerator, ChartResources, Process, WavId,
};
use crate::chart::{
    Chart, DEFAULT_BPM, DEFAULT_SPEED, MAX_FIN_F64, MAX_NON_NEGATIVE_F64, TimeSpan, YCoordinate,
};
use strict_num_extended::NonNegativeF64;

/// BMS format parser.
///
/// This struct serves as a namespace for BMS parsing functions.
/// It parses BMS files and returns a `Chart` containing all precomputed data.
pub struct BmsProcessor;

/// Convert STOP duration from 192nd-note units to beats (measure units).
///
/// In 4/4 time signature:
/// - 192nd-note represents 1/192 of a whole note
/// - One measure (4/4) = 4 beats = 192/48 beats
/// - Therefore: 1 unit of 192nd-note = 1/48 beat
/// - Formula: beats = `192nd_note_value` / 48
#[must_use]
fn convert_stop_duration_to_beats(duration_192nd: NonNegativeF64) -> NonNegativeF64 {
    NonNegativeF64::new(duration_192nd.as_f64() / 48.0).unwrap_or(NonNegativeF64::ZERO)
}

impl BmsProcessor {
    /// Parse BMS file and return a `Chart` containing all precomputed data.
    ///
    /// # Errors
    ///
    /// Returns [`PlayingError::InvalidBpm`] if the BPM value could not be parsed.
    pub fn parse<T: KeyLayoutMapper>(bms: &Bms) -> Result<Chart, PlayingError> {
        // === Validate all StringValue definitions ===
        let mut errors = Vec::new();

        // Validate BPM definitions
        for string_value in bms.bpm.bpm_defs.values() {
            if let Err(e) = string_value.value() {
                errors.push(PlayingError::InvalidBpm {
                    raw: string_value.raw().to_string(),
                    error: format!("{e:?}"),
                });
            }
        }

        // Validate STOP definitions
        for (obj_id, string_value) in &bms.stop.stop_defs {
            if let Err(e) = string_value.value() {
                errors.push(PlayingError::InvalidStop {
                    obj_id: *obj_id,
                    raw: string_value.raw().to_string(),
                    error: format!("{e:?}"),
                });
            }
        }

        // Validate SPEED definitions
        for (obj_id, string_value) in &bms.speed.speed_defs {
            if let Err(e) = string_value.value() {
                errors.push(PlayingError::InvalidSpeed {
                    obj_id: *obj_id,
                    raw: string_value.raw().to_string(),
                    error: format!("{e:?}"),
                });
            }
        }

        // Validate SCROLL definitions
        for (obj_id, string_value) in &bms.scroll.scroll_defs {
            if let Err(e) = string_value.value() {
                errors.push(PlayingError::InvalidScroll {
                    obj_id: *obj_id,
                    raw: string_value.raw().to_string(),
                    error: format!("{e:?}"),
                });
            }
        }

        // Validate SEEK definitions
        for (obj_id, string_value) in &bms.video.seek_defs {
            if let Err(e) = string_value.value() {
                errors.push(PlayingError::InvalidSeek {
                    obj_id: *obj_id,
                    raw: string_value.raw().to_string(),
                    error: format!("{e:?}"),
                });
            }
        }

        // If there are errors, return the first one
        if let Some(err) = errors.into_iter().next() {
            return Err(err);
        }

        // Pre-calculate Y coordinate by tracks
        let y_memo = YMemo::new(bms);

        // Initialize BPM: prefer chart initial BPM, otherwise 120
        let init_bpm = bms
            .bpm
            .bpm
            .clone()
            .unwrap_or_else(|| StringValue::from_value(DEFAULT_BPM));

        // Precompute resource maps
        let wav_files: HashMap<WavId, PathBuf> = bms
            .wav
            .wav_files
            .iter()
            .map(|(obj_id, path)| (WavId::from(obj_id.as_u16() as usize), path.clone()))
            .collect();
        let bmp_files: HashMap<BmpId, PathBuf> = bms
            .bmp
            .bmp_files
            .iter()
            .map(|(obj_id, bmp)| (BmpId::from(obj_id.as_u16() as usize), bmp.file.clone()))
            .collect();

        let all_events = AllEventsIndex::precompute_all_events::<T>(bms, &y_memo);

        // Precompute activate times
        let all_events = precompute_activate_times(bms, &all_events, &y_memo)?;

        // Get initial BPM value
        let init_bpm_value = *init_bpm
            .value()
            .as_ref()
            .map_err(|e| PlayingError::InvalidBpm {
                raw: init_bpm.raw().to_string(),
                error: format!("{e:?}"),
            })?;

        Ok(Chart::from_parts(
            ChartResources::new(wav_files, bmp_files),
            all_events,
            y_memo.flow_events().clone(),
            init_bpm_value,
            DEFAULT_SPEED,
        ))
    }

    /// Generate measure lines for BMS (generated for each track, but not exceeding other objects' Y values)
    pub fn generate_barlines_for_bms(
        bms: &Bms,
        y_memo: &YMemo,
        events_map: &mut BTreeMap<YCoordinate, Vec<PlayheadEvent>>,
        id_gen: &mut ChartEventIdGenerator,
    ) {
        // Find the maximum Y value of all events
        let Some(max_y) = events_map.last_key_value().map(|(key, _)| *key) else {
            return;
        };

        if max_y.as_f64() <= 0.0 {
            return;
        }

        // Get the track number of the last object
        let last_obj_time = bms
            .last_obj_time()
            .unwrap_or_else(|| ObjTime::start_of(0.into()));

        // Generate measure lines for each track, but not exceeding maximum Y value
        for track in 0..=last_obj_time.track().0 {
            let track = Track(track);
            let track_y = y_memo.get_section_start_y(track);

            if track_y <= max_y {
                let event = ChartEvent::BarLine;
                let evp = PlayheadEvent::new(id_gen.next_id(), track_y, event, TimeSpan::ZERO);
                events_map.entry(track_y).or_default().push(evp);
            }
        }
    }

    pub(crate) fn lane_of_channel_id<T: KeyLayoutMapper>(
        channel_id: NoteChannelId,
    ) -> Option<(PlayerSide, Key, NoteKind)> {
        let map = channel_id.try_into_map::<T>()?;
        let side = map.side();
        let key = map.key();
        let kind = map.kind();
        Some((side, key, kind))
    }
}

/// Y coordinate memoization for efficient position calculation.
///
/// This structure caches Y coordinate calculations by track, accounting for
/// section length changes and speed modifications.
#[derive(Debug)]
pub struct YMemo {
    /// Y coordinates memoization by track, which modified its length
    y_by_track: BTreeMap<Track, FinF64>,
    speed_changes: BTreeMap<ObjTime, SpeedObj>,
    zero_length_tracks: std::collections::HashSet<Track>,
    /// Flow events that affect playback speed/scroll, organized by Y coordinate
    flow_events: BTreeMap<YCoordinate, Vec<FlowEvent>>,
}

impl YMemo {
    fn new(bms: &Bms) -> Self {
        let mut y_by_track: BTreeMap<Track, FinF64> = BTreeMap::new();
        let mut last_track = 0;
        let mut y = FinF64::ZERO;
        for (&track, section_len_change) in &bms.section_len.section_len_changes {
            let passed_sections = (track.0 - last_track).saturating_sub(1);
            y = FinF64::new(y.as_f64() + passed_sections as f64).unwrap_or(MAX_FIN_F64);
            y = (y + section_len_change.length).unwrap_or(MAX_FIN_F64);
            y_by_track.insert(track, y);
            last_track = 0;
        }

        let zero_length_tracks: std::collections::HashSet<Track> = bms
            .section_len
            .section_len_changes
            .iter()
            .filter(|(_, change)| change.length.as_f64() == 0.0)
            .map(|(&track, _)| track)
            .collect();

        // Populate flow events by Y coordinate
        let get_event_y = |time: ObjTime| -> YCoordinate {
            let section_y =
                if let Some((&track_last, track_y)) = y_by_track.range(..=&time.track()).last() {
                    let passed_sections = (time.track().0 - track_last.0).saturating_sub(1);
                    FinF64::new(passed_sections as f64 + track_y.as_f64()).unwrap_or(MAX_FIN_F64)
                } else {
                    FinF64::new(time.track().0 as f64).unwrap_or(MAX_FIN_F64)
                };
            let fraction = if time.denominator().get() > 0 {
                FinF64::new(time.numerator() as f64 / time.denominator().get() as f64)
                    .unwrap_or(FinF64::ZERO)
            } else {
                FinF64::ZERO
            };
            let factor = bms
                .speed
                .speed_factor_changes
                .range(..=time)
                .last()
                .map_or_else(|| DEFAULT_SPEED, |(_, obj)| obj.factor);
            YCoordinate::new(
                NonNegativeF64::new((section_y.as_f64() + fraction.as_f64()) * factor.as_f64())
                    .unwrap_or(MAX_NON_NEGATIVE_F64),
            )
        };

        let mut flow_events: BTreeMap<YCoordinate, Vec<FlowEvent>> = BTreeMap::new();

        // BPM changes
        for change in bms.bpm.bpm_changes.values() {
            let event_y = get_event_y(change.time);
            flow_events
                .entry(event_y)
                .or_default()
                .push(FlowEvent::Bpm(change.bpm));
        }

        // Scroll changes
        for change in bms.scroll.scrolling_factor_changes.values() {
            let event_y = get_event_y(change.time);
            flow_events
                .entry(event_y)
                .or_default()
                .push(FlowEvent::Scroll(change.factor));
        }

        // Speed changes
        for change in bms.speed.speed_factor_changes.values() {
            let event_y = get_event_y(change.time);
            flow_events
                .entry(event_y)
                .or_default()
                .push(FlowEvent::Speed(change.factor));
        }

        Self {
            y_by_track,
            speed_changes: bms.speed.speed_factor_changes.clone(),
            zero_length_tracks,
            flow_events,
        }
    }

    // Finds Y coordinate at `time` efficiently
    fn get_y(&self, time: ObjTime) -> YCoordinate {
        if self.zero_length_tracks.contains(&time.track()) {
            return self.get_section_start_y(time.track());
        }

        let section_y = {
            let track = time.track();
            if let Some((&last_track, last_y)) = self.y_by_track.range(..=&track).last() {
                let passed_sections = (track.0 - last_track.0).saturating_sub(1);
                FinF64::new(passed_sections as f64 + last_y.as_f64()).unwrap_or(MAX_FIN_F64)
            } else {
                // there is no sections modified its length until
                FinF64::new(track.0 as f64).unwrap_or(MAX_FIN_F64)
            }
        };
        let fraction = if time.denominator().get() > 0 {
            FinF64::new(time.numerator() as f64 / time.denominator().get() as f64)
                .unwrap_or(FinF64::ZERO)
        } else {
            FinF64::ZERO
        };
        let factor = self
            .speed_changes
            .range(..=time)
            .last()
            .map_or_else(|| DEFAULT_SPEED, |(_, obj)| obj.factor);
        YCoordinate::new(
            NonNegativeF64::new((section_y.as_f64() + fraction.as_f64()) * factor.as_f64())
                .unwrap_or(MAX_NON_NEGATIVE_F64),
        )
    }

    // Gets the Y coordinate at the start of a track/section (without fraction)
    fn get_section_start_y(&self, track: Track) -> YCoordinate {
        let section_y = if let Some((&last_track, last_y)) = self.y_by_track.range(..=&track).last()
        {
            let passed_sections = track.0 - last_track.0;
            FinF64::new(passed_sections as f64 + last_y.as_f64()).unwrap_or(MAX_FIN_F64)
        } else {
            FinF64::new(track.0 as f64).unwrap_or(MAX_FIN_F64)
        };
        let factor = self
            .speed_changes
            .range(..=ObjTime::start_of(track))
            .last()
            .map_or_else(|| DEFAULT_SPEED, |(_, obj)| obj.factor);
        YCoordinate::new(
            NonNegativeF64::new(section_y.as_f64() * factor.as_f64())
                .unwrap_or(MAX_NON_NEGATIVE_F64),
        )
    }

    /// Get flow events organized by Y coordinate
    #[must_use]
    pub const fn flow_events(&self) -> &BTreeMap<YCoordinate, Vec<FlowEvent>> {
        &self.flow_events
    }
}

impl AllEventsIndex {
    /// Precompute all events, store grouped by Y coordinate
    /// Note: Speed effects are calculated into event positions during initialization, ensuring event trigger times remain unchanged
    #[must_use]
    pub fn precompute_all_events<T: KeyLayoutMapper>(bms: &Bms, y_memo: &YMemo) -> Self {
        let mut events_map: BTreeMap<YCoordinate, Vec<PlayheadEvent>> = BTreeMap::new();
        let mut id_gen: ChartEventIdGenerator = ChartEventIdGenerator::default();

        let get_event_y = |time: ObjTime| -> YCoordinate { y_memo.get_y(time) };

        let note_events: Vec<(YCoordinate, WavObj)> = bms
            .notes()
            .all_notes()
            .map(|obj| (get_event_y(obj.offset), obj.clone()))
            .sorted_by(|(y1, _), (y2, _)| y1.cmp(y2))
            .collect();

        // Use ordered Vec instead of HashMap since f64 doesn't implement Hash
        // and NonNegativeF64 doesn't implement Hash either
        let mut zero_length_key_tracker: Vec<(YCoordinate, PlayerSide, Key, usize)> = Vec::new();

        for (i, (y, obj)) in note_events.iter().enumerate() {
            let is_zero_length_section = y_memo.zero_length_tracks.contains(&obj.offset.track());
            let lane = BmsProcessor::lane_of_channel_id::<T>(obj.channel_id);

            if let Some((side, key, _)) = lane
                && is_zero_length_section
            {
                zero_length_key_tracker.push((*y, side, key, i));
            }
        }

        // Track LN start markers to prevent double-triggering (BMS format concern only).
        //
        // BMS format represents long notes using two consecutive Long note markers:
        //   - First marker: start of long note (with length calculated to next marker)
        //   - Second marker: end of long note (with no length)
        //
        // Example in BMS format:
        //   #00151:11  <- Long note start (Player1, Key1, time=1)
        //   #00351:22  <- Long note end   (Player1, Key1, time=3)
        //
        // Without this fix, both markers would trigger events, causing:
        //   1. Double-triggering: same long note fires twice (at start and end)
        //   2. Incorrect playback: end marker creates a zero-length note event
        //
        // IMPORTANT: This is purely a BMS FORMAT PARSING concern.
        // The term "started" here refers to PARSING STATE, not GAMEPLAY STATE.
        // The actual LN visibility (including LNs whose start has passed)
        // is handled by AllEventsIndex using precomputed indices.
        let mut ln_start_markers: std::collections::HashSet<(PlayerSide, Key)> =
            std::collections::HashSet::new();

        for (i, (y, obj)) in note_events.iter().enumerate() {
            let is_zero_length_section = y_memo.zero_length_tracks.contains(&obj.offset.track());
            let lane = BmsProcessor::lane_of_channel_id::<T>(obj.channel_id);
            let should_include = match lane {
                Some((side, key, _)) if is_zero_length_section => zero_length_key_tracker
                    .iter()
                    .any(|(y_val, s, k, idx)| y_val == y && s == &side && k == &key && idx == &i),
                _ => true,
            };

            if should_include {
                let event = event_for_note_static::<T>(bms, y_memo, obj);

                // Fix double-triggering by skipping LN end markers.
                //
                // Logic:
                //   - When we encounter a Long note:
                //     * If its lane already has a start marker → this is the END marker, skip it
                //     * If its lane has no start marker AND it has length → this is the START marker, track it
                //     * If its lane has no start marker AND no length → edge case, ignore (no next marker)
                //
                // Result: Each long note generates exactly one event with the correct length.
                //
                // IMPORTANT: This is purely a BMS FORMAT PARSING concern.
                // The term "started" here refers to PARSING STATE, not GAMEPLAY STATE.
                // The actual LN visibility (including LNs whose start has passed)
                // is handled by AllEventsIndex using precomputed indices.
                if let ChartEvent::Note {
                    side,
                    key,
                    kind: NoteKind::Long,
                    length,
                    ..
                } = &event
                {
                    let lane_key = (*side, *key);
                    if ln_start_markers.contains(&lane_key) {
                        // This lane already has a start marker.
                        // This marker is the end of that long note.
                        // Skip it to prevent double-triggering.
                        ln_start_markers.remove(&lane_key);
                        continue;
                    }
                    if length.is_some() {
                        // This lane has no start marker.
                        // This marker is the start of a new long note.
                        // Track it so we can skip the end marker when we encounter it.
                        ln_start_markers.insert(lane_key);
                    }
                    // If length is None, this is an orphan end marker or zero-length note.
                    // Skip it silently as it doesn't represent a valid playable note.
                }

                let evp = PlayheadEvent::new(id_gen.next_id(), *y, event, TimeSpan::ZERO);
                events_map.entry(*y).or_default().push(evp);
            }
        }

        for change in bms.bpm.bpm_changes.values() {
            let y = get_event_y(change.time);
            let event = ChartEvent::BpmChange { bpm: change.bpm };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // Scroll change events
        for change in bms.scroll.scrolling_factor_changes.values() {
            let y = get_event_y(change.time);
            let event = ChartEvent::ScrollChange {
                factor: change.factor,
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // Speed change events
        for change in bms.speed.speed_factor_changes.values() {
            let y = get_event_y(change.time);
            let event = ChartEvent::SpeedChange {
                factor: change.factor,
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // Stop events
        for stop in bms.stop.stops.values() {
            let y = get_event_y(stop.time);
            let event = ChartEvent::Stop {
                duration: convert_stop_duration_to_beats(stop.duration),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // BGA change events
        for bga_obj in bms.bmp.bga_changes.values() {
            let y = get_event_y(bga_obj.time);
            let bmp_index = bga_obj.id.as_u16() as usize;
            let event = ChartEvent::BgaChange {
                layer: bga_obj.layer,
                bmp_id: Some(BmpId::from(bmp_index)),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // BGA opacity change events (requires minor-command feature)

        for (layer, opacity_changes) in &bms.bmp.bga_opacity_changes {
            for opacity_obj in opacity_changes.values() {
                let y = get_event_y(opacity_obj.time);
                let event = ChartEvent::BgaOpacityChange {
                    layer: *layer,
                    opacity: opacity_obj.opacity,
                };
                events_map.entry(y).or_default().push(PlayheadEvent::new(
                    id_gen.next_id(),
                    y,
                    event,
                    TimeSpan::ZERO,
                ));
            }
        }

        // BGA ARGB color change events (requires minor-command feature)
        for (layer, argb_changes) in &bms.bmp.bga_argb_changes {
            for argb_obj in argb_changes.values() {
                let y = get_event_y(argb_obj.time);
                let event = ChartEvent::BgaArgbChange {
                    layer: *layer,
                    argb: argb_obj.argb,
                };
                events_map.entry(y).or_default().push(PlayheadEvent::new(
                    id_gen.next_id(),
                    y,
                    event,
                    TimeSpan::ZERO,
                ));
            }
        }

        // BGM volume change events
        for bgm_volume_obj in bms.volume.bgm_volume_changes.values() {
            let y = get_event_y(bgm_volume_obj.time);
            let event = ChartEvent::BgmVolumeChange {
                volume: bgm_volume_obj.volume,
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // KEY volume change events
        for key_volume_obj in bms.volume.key_volume_changes.values() {
            let y = get_event_y(key_volume_obj.time);
            let event = ChartEvent::KeyVolumeChange {
                volume: key_volume_obj.volume,
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // Text display events
        for text_obj in bms.text.text_events.values() {
            let y = get_event_y(text_obj.time);
            let event = ChartEvent::TextDisplay {
                text: text_obj.text.clone(),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        // Judge level change events
        for judge_obj in bms.judge.judge_events.values() {
            let y = get_event_y(judge_obj.time);
            let event = ChartEvent::JudgeLevelChange {
                level: judge_obj.judge_level,
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        for seek_obj in bms.video.seek_events.values() {
            let y = get_event_y(seek_obj.time);
            let event = ChartEvent::VideoSeek {
                seek_time: seek_obj.position.to_string().parse::<f64>().unwrap_or(0.0),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        for bga_keybound_obj in bms.bmp.bga_keybound_events.values() {
            let y = get_event_y(bga_keybound_obj.time);
            let event = ChartEvent::BgaKeybound {
                event: bga_keybound_obj.event.clone(),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        for option_obj in bms.option.option_events.values() {
            let y = get_event_y(option_obj.time);
            let event = ChartEvent::OptionChange {
                option: option_obj.option.clone(),
            };
            events_map.entry(y).or_default().push(PlayheadEvent::new(
                id_gen.next_id(),
                y,
                event,
                TimeSpan::ZERO,
            ));
        }

        BmsProcessor::generate_barlines_for_bms(bms, y_memo, &mut events_map, &mut id_gen);
        Self::new(events_map)
    }
}

/// Precompute absolute `activate_time` for all events based on BPM segmentation and Stops.
///
/// # Errors
///
/// Returns [`PlayingError::InvalidBpm`] if the initial BPM value could not be parsed.
pub fn precompute_activate_times(
    bms: &Bms,
    all_events: &AllEventsIndex,
    y_memo: &YMemo,
) -> Result<AllEventsIndex, PlayingError> {
    use itertools::Itertools;
    use std::collections::BTreeSet;

    let mut points: BTreeSet<YCoordinate> = BTreeSet::new();
    points.insert(YCoordinate::ZERO);
    points.extend(all_events.as_by_y().keys().copied());

    let init_bpm = bms
        .bpm
        .bpm
        .clone()
        .unwrap_or_else(|| StringValue::from_value(DEFAULT_BPM));

    let init_bpm_value = *init_bpm
        .value()
        .as_ref()
        .map_err(|e| PlayingError::InvalidBpm {
            raw: init_bpm.raw().to_string(),
            error: format!("{e:?}"),
        })?;

    let bpm_changes: Vec<(YCoordinate, PositiveF64)> = bms
        .bpm
        .bpm_changes
        .iter()
        .map(|(obj_time, change)| {
            let y = y_memo.get_y(*obj_time);
            (y, change.bpm)
        })
        .collect();
    points.extend(bpm_changes.iter().map(|(y, _)| *y));

    let stop_list: Vec<(YCoordinate, NonNegativeF64)> = bms
        .stop
        .stops
        .values()
        .map(|st| {
            let sy = y_memo.get_y(st.time);
            (sy, st.duration)
        })
        .sorted_by_key(|(y, _)| *y)
        .collect();

    let cum_map =
        super::calculate_cumulative_times(&points, init_bpm_value, &bpm_changes, &stop_list);

    let new_map: std::collections::BTreeMap<YCoordinate, Vec<PlayheadEvent>> = all_events
        .as_by_y()
        .iter()
        .map(|(y_coord, indices)| {
            let at_secs = cum_map.get(y_coord).copied().unwrap_or(0.0);
            let at = TimeSpan::from_duration(std::time::Duration::from_secs_f64(at_secs));
            let new_events: Vec<_> = all_events
                .as_events()
                .get(indices.clone())
                .into_iter()
                .flatten()
                .cloned()
                .map(|mut evp| {
                    evp.activate_time = at;
                    evp
                })
                .collect();
            (*y_coord, new_events)
        })
        .collect();
    Ok(AllEventsIndex::new(new_map))
}

/// Generate a static chart event for a BMS note object.
///
/// This function converts a BMS `WavObj` into a `ChartEvent` with all necessary
/// information, including note type, lane assignment, and long note duration.
///
/// # Type Parameters
/// - `T`: Key layout mapper (e.g., `Beat5`, `Beat7`, `Beat10`)
///
/// # Parameters
/// - `bms`: The parsed BMS chart data
/// - `y_memo`: Y coordinate memoization for position calculation
/// - `obj`: The note object to convert
///
/// # Returns
/// - `ChartEvent::Note` for playable notes
/// - `ChartEvent::Bgm` for BGM/background audio
#[must_use]
pub fn event_for_note_static<T: KeyLayoutMapper>(
    bms: &Bms,
    y_memo: &YMemo,
    obj: &WavObj,
) -> ChartEvent {
    let y = y_memo.get_y(obj.offset);
    let lane = BmsProcessor::lane_of_channel_id::<T>(obj.channel_id);
    let wav_id = Some(WavId::from(obj.wav_id.as_u16() as usize));
    let Some((side, key, kind)) = lane else {
        return ChartEvent::Bgm { wav_id };
    };
    let length = (kind == NoteKind::Long)
        .then(|| {
            bms.notes()
                .next_obj_by_key(obj.channel_id, obj.offset)
                .map(|next_obj| {
                    let next_y = y_memo.get_y(next_obj.offset);
                    NonNegativeF64::new((next_y - y).as_f64()).unwrap_or(NonNegativeF64::ZERO)
                })
        })
        .flatten();
    ChartEvent::Note {
        side,
        key,
        kind,
        wav_id,
        length,
        continue_play: None,
    }
}

impl TryFrom<Bms> for Chart {
    type Error = PlayingError;

    fn try_from(bms: Bms) -> Result<Self, Self::Error> {
        BmsProcessor::parse::<crate::bms::command::channel::mapper::KeyLayoutBeat>(&bms)
    }
}

impl Process for Bms {
    type Error = PlayingError;

    fn process(self) -> Result<Chart, Self::Error> {
        BmsProcessor::parse::<crate::bms::command::channel::mapper::KeyLayoutBeat>(&self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bms::command::StringValue;
    use crate::bms::command::channel::mapper::KeyLayoutBeat;

    /// Test that parsing fails when BPM value is invalid (non-numeric string)
    #[test]
    fn test_parse_invalid_bpm() {
        // Create a BMS object with an invalid BPM value
        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new("invalid_bpm"));

        // Try to parse, should return InvalidBpm error
        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(result.is_err());
        match result {
            Err(PlayingError::InvalidBpm { raw, error }) => {
                assert_eq!(raw, "invalid_bpm");
                // Verify error message contains details
                assert!(error.contains("invalid") || error.contains("digit") || !error.is_empty());
            }
            _ => panic!("Expected PlayingError::InvalidBpm, got: {result:?}"),
        }
    }

    /// Test that parsing fails when BPM value is an empty string
    #[test]
    fn test_parse_empty_bpm() {
        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new(""));

        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(result.is_err());
        match result {
            Err(PlayingError::InvalidBpm { raw, .. }) => {
                assert_eq!(raw, "");
            }
            _ => panic!("Expected PlayingError::InvalidBpm for empty BPM, got: {result:?}"),
        }
    }

    /// Test that parsing fails when BPM value is NaN-like
    #[test]
    fn test_parse_nan_bpm() {
        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new("NaN"));

        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(result.is_err());
        match result {
            Err(PlayingError::InvalidBpm { raw, .. }) => {
                assert_eq!(raw, "NaN");
            }
            _ => panic!("Expected PlayingError::InvalidBpm for NaN BPM, got: {result:?}"),
        }
    }

    /// Test that parsing succeeds with default BPM (120) when no BPM is defined
    #[test]
    fn test_parse_missing_bpm_uses_default() {
        // Create a BMS object without BPM definition
        let bms = Bms::default();

        // Parse should succeed with default BPM (120)
        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(
            result.is_ok(),
            "Parse should succeed with missing BPM: {result:?}"
        );
        let chart = result.unwrap();
        assert_eq!(chart.init_bpm, DEFAULT_BPM, "Should use default BPM of 120");
    }

    /// Test that parsing succeeds with valid BPM value
    #[test]
    fn test_parse_valid_bpm() {
        const TEST_BPM_150_5: PositiveF64 = PositiveF64::new_const(150.5);

        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new("150.5"));

        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(
            result.is_ok(),
            "Parse should succeed with valid BPM: {result:?}"
        );
        let chart = result.unwrap();
        assert_eq!(chart.init_bpm, TEST_BPM_150_5);
    }

    /// Test that parsing succeeds with BPM value containing special characters
    #[test]
    fn test_parse_bpm_with_special_chars() {
        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new("abc123!@#"));

        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        assert!(result.is_err());
        match result {
            Err(PlayingError::InvalidBpm { raw, .. }) => {
                assert_eq!(raw, "abc123!@#");
            }
            _ => {
                panic!("Expected PlayingError::InvalidBpm for special characters, got: {result:?}")
            }
        }
    }

    /// Test that error information is preserved correctly
    #[test]
    fn test_error_contains_raw_value() {
        let invalid_value = "not_a_number";
        let mut bms = Bms::default();
        bms.bpm.bpm = Some(StringValue::new(invalid_value));

        let result = BmsProcessor::parse::<KeyLayoutBeat>(&bms);

        match result {
            Err(PlayingError::InvalidBpm { raw, .. }) => {
                assert_eq!(raw, invalid_value);
            }
            _ => panic!("Expected PlayingError::InvalidBpm"),
        }
    }
}