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
//! Microquad BMS/BMSON Chart Player
//!
//! A simple BMS/BMSON chart player supporting 7+1k key layout.
//! Uses the microquad framework for visualization and audio playback.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::Duration;

use bms_rs::bms::prelude::*;
use bms_rs::chart::prelude::*;
use clap::Parser;
use gametime::{TimeSpan, TimeStamp};
use kira::{
    AudioManager, AudioManagerSettings, Capacities, DefaultBackend,
    sound::static_sound::StaticSoundData,
};
use macroquad::prelude::Color;
use macroquad::prelude::*;
use rayon::prelude::*;
use strict_num_extended::{FinF64, PositiveF64};

/// Default BPM value
const DEFAULT_BPM: PositiveF64 = PositiveF64::new_const(120.0);

fn window_conf() -> Conf {
    Conf {
        window_title: "BMS Player".to_owned(),
        platform: miniquad::conf::Platform {
            linux_backend: miniquad::conf::LinuxBackend::WaylandWithX11Fallback,
            ..Default::default()
        },
        ..Default::default()
    }
}

#[macroquad::main(window_conf)]
async fn main() -> Result<(), String> {
    // 1. Parse command line arguments
    let config = Config::parse();

    if !config.chart_path.exists() {
        return Err(format!("File not found: {}", config.chart_path.display()));
    }

    println!("Loading chart: {}", config.chart_path.display());

    // 2. Load chart
    let (chart, base_bpm) = load_chart(&config.chart_path)?;
    println!("Chart loaded successfully");

    // 3. Extract base path (for audio file path resolution)
    let base_path = config.chart_path.parent().unwrap_or_else(|| Path::new("."));

    // 4. Calculate VisibleRangePerBpm
    let reaction_time = TimeSpan::from_duration(Duration::from_millis(config.reaction_time_ms));
    let visible_range = VisibleRangePerBpm::new(base_bpm.value(), reaction_time);

    // 5. Preload audio in parallel using rayon
    println!("Loading audio files...");
    let audio_data_map = load_audio_files_parallel(chart.resources().wav_files(), base_path);
    println!(
        "Audio loading completed: {} files loaded",
        audio_data_map.len()
    );

    // 6. Start ChartPlayer
    let start_time = TimeStamp::now();
    let mut chart_player = ChartPlayer::start(&chart, visible_range, start_time);
    // Set visibility range to [-0.5, 1.0) to show events past judgment line
    chart_player.set_visibility_range(FinF64::NEG_HALF..FinF64::ONE);
    println!("Player started");

    // 6.5. Initialize audio playback system
    let mut audio_manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings {
        capacities: Capacities {
            sub_track_capacity: 512,
            send_track_capacity: 16,
            clock_capacity: 8,
            modulator_capacity: 16,
            listener_capacity: 8,
        },
        internal_buffer_size: 256,
        ..Default::default()
    })
    .map_err(|e| format!("Failed to initialize audio: {e}"))?;
    println!("Audio system initialized");

    // Track played events to prevent duplicate audio playback
    let mut played_events = HashSet::new();

    // 7. Main loop
    println!("Starting playback...");
    let mut next_print_time = start_time;
    let mut missed_sounds = 0u32;
    loop {
        // Update playback state
        let now = TimeStamp::now();
        let events = chart_player.update(now);

        // Print playback status once per second
        if now >= next_print_time {
            let state = chart_player.playback_state();
            let elapsed = now
                .checked_elapsed_since(start_time)
                .unwrap_or(TimeSpan::ZERO);
            println!(
                "[Playback] Time: {:.1}s | BPM: {:.1} | Y: {:.2} | Speed: {:.2} | Scroll: {:.2} | Missed: {}",
                elapsed.as_secs_f64(),
                state.current_bpm.as_f64(),
                state.progressed_y().as_f64(),
                state.current_speed.as_f64(),
                state.current_scroll.as_f64(),
                missed_sounds,
            );
            next_print_time += TimeSpan::SECOND;
        }

        // Process all triggered events (play audio once per event)
        for event in &events {
            let wav_id = match event.event() {
                ChartEvent::Note { wav_id, .. } | ChartEvent::Bgm { wav_id } => wav_id,
                _ => continue,
            };

            let Some(id) = wav_id else { continue };
            let Some(audio) = audio_data_map.get(id) else {
                continue;
            };

            // Skip if this event has already been played
            if played_events.contains(&event.id()) {
                continue;
            }

            if let Err(e) = audio_manager.play(audio.clone()) {
                if matches!(e, kira::PlaySoundError::SoundLimitReached) {
                    missed_sounds += 1;
                } else {
                    eprintln!("Failed to play audio: {e}");
                }
            } else {
                // Mark as played only on success
                played_events.insert(event.id());
            }
        }

        // Rendering
        // Clear background
        macroquad::prelude::clear_background(COLOR_BG);

        // Render tracks and judgment line
        render_tracks();

        // Render visible notes
        render_notes(&mut chart_player, &events);

        // Render info text (BPM, progress, etc.)
        render_info(&chart_player);

        macroquad::prelude::next_frame().await;
    }
}

/// Configuration parameters
#[derive(Parser, Debug)]
#[command(name = "microquad_player")]
#[command(about = "A simple BMS/BMSON chart player", long_about = None)]
struct Config {
    /// Chart file path
    #[arg(value_name = "FILE")]
    chart_path: PathBuf,

    /// Reaction time (milliseconds)
    #[arg(short, long, default_value = "550", value_name = "MILLISECONDS")]
    reaction_time_ms: u64,
}

/// Load chart file
///
/// Automatically detect format (BMS/BMSON) based on file extension and parse.
///
/// # Arguments
///
/// * `path` - Chart file path
///
/// # Returns
///
/// Returns parsed `Chart` and base BPM value.
fn load_chart(path: &Path) -> Result<(Chart, BaseBpm), String> {
    // Read file content
    // First read as bytes
    let bytes = std::fs::read(path).map_err(|e| format!("Failed to read file: {e}"))?;

    // Use Shift-JIS encoding
    let content = {
        let bytes: &[u8] = &bytes;
        encoding_rs::SHIFT_JIS.decode(bytes).0.to_string()
    };

    // Determine format based on extension
    let extension = path
        .extension()
        .and_then(|e| e.to_str())
        .ok_or("Invalid file extension")?;

    let (chart, base_bpm) = match extension.to_lowercase().as_str() {
        "bms" | "bme" | "bml" | "pms" => {
            // Parse using BmsProcessor
            let output = parse_bms(&content, default_config());
            let bms = output.bms.map_err(|e| format!("Parse error: {e:?}"))?;

            // First generate base BPM from BMS
            let base_bpm = StartBpmGenerator
                .generate(&bms)
                .unwrap_or(BaseBpm::new(DEFAULT_BPM));

            // Use KeyLayoutBeat mapper (supports 7+1k)
            let chart = BmsProcessor::parse::<KeyLayoutBeat>(&bms)
                .map_err(|e| format!("Failed to parse chart: {e}"))?;
            (chart, base_bpm)
        }
        "bmson" => {
            // BMSON format
            #[cfg(feature = "bmson")]
            {
                let bmson =
                    serde_json::from_str(&content).map_err(|e| format!("JSON parse error: {e}"))?;

                // First generate base BPM from BMSON
                let base_bpm = StartBpmGenerator
                    .generate(&bmson)
                    .unwrap_or(BaseBpm::new(DEFAULT_BPM));

                let chart = BmsonProcessor::parse(&bmson);
                (chart, base_bpm)
            }
            #[cfg(not(feature = "bmson"))]
            return Err("BMSON feature not enabled".to_string());
        }
        _ => return Err(format!("Unsupported format: {extension}")),
    };

    Ok((chart, base_bpm))
}

/// Find audio file with extension fallback support
///
/// # Arguments
///
/// * `path` - Original path (with or without extension)
/// * `extensions` - List of extensions to try
///
/// # Returns
///
/// Found file path, or None if not found
fn find_audio_with_extensions(path: &Path, extensions: &[&str]) -> Option<PathBuf> {
    // Get path without extension
    let stem = path.with_extension("");

    // Try each extension in order
    for ext in extensions {
        let candidate = stem.with_extension(ext);
        if candidate.exists() {
            return Some(candidate);
        }
    }

    None
}

/// Preload all audio files in parallel using rayon
///
/// # Arguments
///
/// * `audio_files` - Audio file ID to path mapping
/// * `base_path` - Base path for audio files (usually chart file directory)
///
/// # Returns
///
/// Returns loaded audio mapping, failed audio files will be skipped.
fn load_audio_files_parallel(
    audio_files: &HashMap<WavId, PathBuf>,
    base_path: &Path,
) -> HashMap<WavId, StaticSoundData> {
    audio_files
        .par_iter()
        .filter_map(|(wav_id, path)| {
            let full_path = base_path.join(path);

            // Find file with extension fallback
            let found_path =
                find_audio_with_extensions(&full_path, &["ogg", "flac", "wav", "mp3"])?;

            match StaticSoundData::from_file(&found_path)
                .map_err(|e| format!("Failed to load audio: {e}"))
            {
                Ok(data) => Some((*wav_id, data)),
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to load audio {} (ID: {wav_id:?}): {e}",
                        found_path.display()
                    );
                    None
                }
            }
        })
        .collect()
}

// Screen size configuration
const SCREEN_WIDTH: f32 = 800.0;
const SCREEN_HEIGHT: f32 = 600.0;

// Track configuration
const TRACK_COUNT: usize = 8;
const TRACK_WIDTH: f32 = 60.0;
const TRACK_SPACING: f32 = 5.0;
const TOTAL_TRACKS_WIDTH: f32 = TRACK_COUNT as f32 * (TRACK_WIDTH + TRACK_SPACING);

// Judgment line configuration
const JUDGMENT_LINE_Y: f32 = 500.0;

// Color configuration
const COLOR_BG: Color = Color::from_rgba(30, 30, 30, 255);
const COLOR_TRACK_LINE: Color = Color::from_rgba(100, 100, 100, 255);
const COLOR_JUDGMENT_LINE: Color = Color::from_rgba(255, 255, 255, 255);
const COLOR_BAR_LINE: Color = Color::from_rgba(80, 80, 80, 180);
const COLOR_NOTE_WHITE: Color = Color::from_rgba(255, 255, 255, 255);
const COLOR_NOTE_BLUE: Color = Color::from_rgba(100, 149, 237, 255);
const COLOR_NOTE_SCRATCH: Color = Color::from_rgba(255, 0, 0, 255);
const COLOR_NOTE_MINE: Color = Color::from_rgba(255, 255, 0, 255);

/// Get all supported keys (7+1k layout)
#[must_use]
const fn supported_keys() -> [Key; 8] {
    [
        Key::Scratch(1),
        Key::Key(1),
        Key::Key(2),
        Key::Key(3),
        Key::Key(4),
        Key::Key(5),
        Key::Key(6),
        Key::Key(7),
    ]
}

/// Map Key to track index (0-7)
///
/// Only supports 7+1k: `Key::Key(1..=7)` and `Key::Scratch(1)`
///
/// # Returns
///
/// Returns corresponding track index, or None if not supported.
#[must_use]
const fn key_to_index(key: Key) -> Option<usize> {
    match key {
        Key::Scratch(1) => Some(0),
        Key::Key(1) => Some(1),
        Key::Key(2) => Some(2),
        Key::Key(3) => Some(3),
        Key::Key(4) => Some(4),
        Key::Key(5) => Some(5),
        Key::Key(6) => Some(6),
        Key::Key(7) => Some(7),
        _ => None,
    }
}

/// Calculate track X coordinate
#[must_use]
fn track_x(key: Key) -> Option<f32> {
    let start_x = (SCREEN_WIDTH - TOTAL_TRACKS_WIDTH) / 2.0;
    let index = key_to_index(key)?;
    Some(start_x + index as f32 * (TRACK_WIDTH + TRACK_SPACING))
}

/// Render tracks and judgment line
fn render_tracks() {
    let start_x = (SCREEN_WIDTH - TOTAL_TRACKS_WIDTH) / 2.0;

    for key in supported_keys() {
        let x = track_x(key).expect("supported keys should have valid track position");

        // Draw track lines
        macroquad::prelude::draw_line(x, 0.0, x, SCREEN_HEIGHT, 2.0, COLOR_TRACK_LINE);

        // Draw track right border
        macroquad::prelude::draw_line(
            x + TRACK_WIDTH,
            0.0,
            x + TRACK_WIDTH,
            SCREEN_HEIGHT,
            1.0,
            COLOR_TRACK_LINE,
        );
    }

    // Draw judgment line
    macroquad::prelude::draw_line(
        start_x - 10.0,
        JUDGMENT_LINE_Y,
        start_x + TOTAL_TRACKS_WIDTH + 10.0,
        JUDGMENT_LINE_Y,
        3.0,
        COLOR_JUDGMENT_LINE,
    );
}

/// Render visible notes
///
/// # Arguments
///
/// * `player` - `ChartPlayer` instance
fn render_notes(player: &mut ChartPlayer, _events: &[PlayheadEvent]) {
    let visible = player.visible_events();

    for (event, display_range) in visible {
        // Render measure lines
        if matches!(event.event(), ChartEvent::BarLine) {
            let ratio = display_range.start().value().as_f64();
            let y = JUDGMENT_LINE_Y - (ratio as f32 * JUDGMENT_LINE_Y);

            // Draw measure line across all tracks
            let start_x = (SCREEN_WIDTH - TOTAL_TRACKS_WIDTH) / 2.0;
            macroquad::prelude::draw_line(
                start_x,
                y,
                start_x + TOTAL_TRACKS_WIDTH,
                y,
                2.0,
                COLOR_BAR_LINE,
            );
            continue;
        }

        // Only render Note events
        if let ChartEvent::Note { key, kind, .. } = event.event() {
            // Filter non-7+1k keys
            let x = match track_x(*key) {
                Some(x) => x,
                None => continue,
            };

            // DisplayRatio: 0.0 = judgment line, 1.0 = visible area top
            // Convert to screen coordinates: Y = judgment line - (ratio * visible height)
            let ratio_start = display_range.start().value().as_f64();
            let ratio_end = display_range.end().value().as_f64();

            let y_start = JUDGMENT_LINE_Y - (ratio_start as f32 * JUDGMENT_LINE_Y);
            let y_end = JUDGMENT_LINE_Y - (ratio_end as f32 * JUDGMENT_LINE_Y);

            // Select color based on track type
            let color = match key {
                Key::Scratch(_) => COLOR_NOTE_SCRATCH,
                Key::Key(n) if n % 2 == 1 => COLOR_NOTE_WHITE,
                Key::Key(_) => COLOR_NOTE_BLUE,
                _ => COLOR_NOTE_WHITE,
            };

            // Draw notes
            match kind {
                NoteKind::Visible | NoteKind::Invisible => {
                    // Normal note (rectangle)
                    let note_height = 10.0;
                    macroquad::prelude::draw_rectangle(
                        x + 2.0,
                        y_start - note_height,
                        TRACK_WIDTH - 4.0,
                        note_height,
                        color,
                    );
                }
                NoteKind::Long => {
                    // Long note (rectangle from start to end)
                    let height = y_start - y_end;
                    macroquad::prelude::draw_rectangle(
                        x + 2.0,
                        y_end,
                        TRACK_WIDTH - 4.0,
                        height,
                        color,
                    );

                    // Long note head
                    macroquad::prelude::draw_rectangle(
                        x + 2.0,
                        y_start - 5.0,
                        TRACK_WIDTH - 4.0,
                        5.0,
                        color,
                    );
                }
                NoteKind::Landmine => {
                    // Landmine note (circle)
                    macroquad::prelude::draw_circle(
                        x + TRACK_WIDTH / 2.0,
                        y_start,
                        TRACK_WIDTH / 3.0,
                        COLOR_NOTE_MINE,
                    );
                }
            }
        }
    }
}

/// Render info text
///
/// # Arguments
///
/// * `player` - `ChartPlayer` instance
fn render_info(player: &ChartPlayer) {
    let state = player.playback_state();

    // Display BPM
    let bpm = state.current_bpm.as_f64();
    macroquad::prelude::draw_text(
        &format!("BPM: {bpm:.1}"),
        10.0,
        20.0,
        20.0,
        Color::from_rgba(255, 255, 255, 255),
    );

    // Display progress (current Y coordinate)
    let y = state.progressed_y().as_f64();
    macroquad::prelude::draw_text(
        &format!("Position: {y:.2}"),
        10.0,
        50.0,
        20.0,
        Color::from_rgba(255, 255, 255, 255),
    );

    // Display key hints
    macroquad::prelude::draw_text(
        "7+1k Layout: S | 1 | 2 | 3 | 4 | 5 | 6 | 7",
        10.0,
        SCREEN_HEIGHT - 30.0,
        16.0,
        Color::from_rgba(128, 128, 128, 255),
    );
}