ym2149-replayer-cli 0.9.1

Command-line player for YM chiptune files
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
//! Playlist management for directory-based playback.
//!
//! This module provides:
//! - Recursive directory scanning for music files
//! - Metadata extraction for playlist display
//! - Song selection and loading

use std::fs;
use std::path::{Path, PathBuf};

use ym2149_arkos_replayer::load_aks;
use ym2149_ay_replayer::AyPlayer;
use ym2149_sndh_replayer::{SndhPlayer, is_sndh_data};
use ym2149_ym_replayer::load_song;

/// Supported file extensions
const SUPPORTED_EXTENSIONS: &[&str] = &["ym", "aks", "ay", "sndh"];

/// Entry in the playlist with metadata
#[derive(Clone, Debug)]
pub struct PlaylistEntry {
    /// Full path to the file
    pub path: PathBuf,
    /// Song title (from metadata or filename)
    pub title: String,
    /// Song author (from metadata)
    pub author: String,
    /// Duration in seconds (if known)
    pub duration_secs: Option<f32>,
    /// File format (YM, AKS, AY, SNDH)
    pub format: String,
}

impl PlaylistEntry {
    /// Get display string for the playlist UI
    pub fn display_string(&self) -> String {
        let duration_str = self
            .duration_secs
            .filter(|d| d.is_finite() && *d >= 0.0)
            .map(|d| {
                let clamped = d.min(5999.0); // Cap at 99:59
                let mins = (clamped / 60.0) as u32;
                let secs = (clamped % 60.0) as u32;
                format!(" ({mins:02}:{secs:02})")
            })
            .unwrap_or_default();

        if self.title.is_empty() || self.title == "(unknown)" {
            // Fall back to filename
            let filename = self
                .path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("???");
            format!("{filename}{duration_str}")
        } else if self.author.is_empty() || self.author == "(unknown)" {
            format!("{}{duration_str}", self.title)
        } else {
            format!("{} - {}{duration_str}", self.author, self.title)
        }
    }
}

/// Playlist containing all discovered songs
#[derive(Default)]
pub struct Playlist {
    /// All playlist entries
    pub entries: Vec<PlaylistEntry>,
    /// Currently selected index
    pub selected: usize,
    /// Current search query for type-ahead
    pub search_query: String,
}

impl Playlist {
    /// Scan a directory recursively for music files
    pub fn scan_directory(path: &Path) -> std::io::Result<Self> {
        let mut entries = Vec::new();
        scan_directory_recursive(path, &mut entries)?;

        // Sort by display string for consistent ordering
        entries.sort_by_key(|e| e.display_string().to_lowercase());

        Ok(Self {
            entries,
            selected: 0,
            search_query: String::new(),
        })
    }

    /// Check if playlist is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get number of entries
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Move selection up
    pub fn select_previous(&mut self) {
        if !self.entries.is_empty() {
            if self.selected == 0 {
                self.selected = self.entries.len() - 1;
            } else {
                self.selected -= 1;
            }
        }
    }

    /// Move selection down
    pub fn select_next(&mut self) {
        if !self.entries.is_empty() {
            self.selected = (self.selected + 1) % self.entries.len();
        }
    }

    /// Page up (10 items)
    pub fn page_up(&mut self) {
        if !self.entries.is_empty() {
            self.selected = self.selected.saturating_sub(10);
        }
    }

    /// Page down (10 items)
    pub fn page_down(&mut self) {
        if !self.entries.is_empty() {
            self.selected = (self.selected + 10).min(self.entries.len() - 1);
        }
    }

    /// Get currently selected entry
    pub fn selected_entry(&self) -> Option<&PlaylistEntry> {
        self.entries.get(self.selected)
    }

    /// Get path of selected entry
    pub fn selected_path(&self) -> Option<&Path> {
        self.selected_entry().map(|e| e.path.as_path())
    }

    /// Add a character to the search query and jump to first match
    pub fn search_append(&mut self, c: char) {
        self.search_query.push(c);
        self.jump_to_search_match();
    }

    /// Remove last character from search query
    pub fn search_backspace(&mut self) {
        self.search_query.pop();
        if !self.search_query.is_empty() {
            self.jump_to_search_match();
        }
    }

    /// Clear the search query
    pub fn search_clear(&mut self) {
        self.search_query.clear();
    }

    /// Check if search is active
    pub fn is_searching(&self) -> bool {
        !self.search_query.is_empty()
    }

    /// Get current search query
    pub fn search_query(&self) -> &str {
        &self.search_query
    }

    /// Jump to the first entry matching the search query.
    ///
    /// For single-character queries, prioritizes entries that START with the character
    /// (jump-to-letter behavior). For multi-character queries, matches anywhere.
    fn jump_to_search_match(&mut self) {
        if self.search_query.is_empty() || self.entries.is_empty() {
            return;
        }

        let query_lower = self.search_query.to_lowercase();
        let is_single_char = self.search_query.len() == 1;

        // For single character: prioritize "starts with" matches from the beginning
        if is_single_char {
            // Search from beginning for entries starting with the character
            for (i, entry) in self.entries.iter().enumerate() {
                if entry_starts_with(&query_lower, entry) {
                    self.selected = i;
                    return;
                }
            }
        }

        // Multi-char or no "starts with" match: search for "contains" from current position
        for (i, entry) in self.entries.iter().enumerate().skip(self.selected) {
            if entry_matches(&query_lower, entry) {
                self.selected = i;
                return;
            }
        }

        // If not found, search from the beginning
        for (i, entry) in self.entries.iter().enumerate().take(self.selected) {
            if entry_matches(&query_lower, entry) {
                self.selected = i;
                return;
            }
        }
    }

    /// Jump to next match (for repeated search)
    pub fn search_next(&mut self) {
        if self.search_query.is_empty() || self.entries.is_empty() {
            return;
        }

        let query_lower = self.search_query.to_lowercase();
        let start = (self.selected + 1) % self.entries.len();

        // Search from after current position, wrapping around
        for i in 0..self.entries.len() {
            let idx = (start + i) % self.entries.len();
            if entry_matches(&query_lower, &self.entries[idx]) {
                self.selected = idx;
                return;
            }
        }
    }

    /// Jump to previous match
    pub fn search_previous(&mut self) {
        if self.search_query.is_empty() || self.entries.is_empty() {
            return;
        }

        let query_lower = self.search_query.to_lowercase();
        let start = if self.selected == 0 {
            self.entries.len() - 1
        } else {
            self.selected - 1
        };

        // Search backwards from before current position, wrapping around
        for i in 0..self.entries.len() {
            let idx = if i <= start {
                start - i
            } else {
                self.entries.len() - (i - start)
            };
            if entry_matches(&query_lower, &self.entries[idx]) {
                self.selected = idx;
                return;
            }
        }
    }
}

/// Check if an entry matches the search query (contains)
fn entry_matches(query_lower: &str, entry: &PlaylistEntry) -> bool {
    // Match against title, author, or filename
    let title_lower = entry.title.to_lowercase();
    let author_lower = entry.author.to_lowercase();
    let filename_lower = entry
        .path
        .file_stem()
        .and_then(|s| s.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();

    title_lower.contains(query_lower)
        || author_lower.contains(query_lower)
        || filename_lower.contains(query_lower)
}

/// Check if an entry starts with the search query (for jump-to-letter)
fn entry_starts_with(query_lower: &str, entry: &PlaylistEntry) -> bool {
    // Check display string (which is what user sees in the list)
    let display = entry.display_string().to_lowercase();
    display.starts_with(query_lower)
}

/// Recursively scan directory for music files
fn scan_directory_recursive(path: &Path, entries: &mut Vec<PlaylistEntry>) -> std::io::Result<()> {
    if !path.is_dir() {
        return Ok(());
    }

    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            // Recurse into subdirectory
            scan_directory_recursive(&path, entries)?;
        } else if path.is_file() {
            // Check if it's a supported file
            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                let ext_lower = ext.to_ascii_lowercase();
                if SUPPORTED_EXTENSIONS.contains(&ext_lower.as_str()) {
                    // Try to extract metadata
                    if let Some(entry) = extract_metadata(&path) {
                        entries.push(entry);
                    }
                }
            }
        }
    }

    Ok(())
}

/// Extract metadata from a music file
fn extract_metadata(path: &Path) -> Option<PlaylistEntry> {
    let file_data = fs::read(path).ok()?;
    let extension = path
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase())
        .unwrap_or_default();

    let (title, author, duration_secs, format) = match extension.as_str() {
        "aks" => extract_aks_metadata(&file_data)?,
        "ay" => extract_ay_metadata(&file_data)?,
        "sndh" => extract_sndh_metadata(&file_data)?,
        _ => {
            // Try YM format (also handles header-based SNDH detection)
            if is_sndh_data(&file_data) {
                extract_sndh_metadata(&file_data)?
            } else {
                extract_ym_metadata(&file_data)?
            }
        }
    };

    Some(PlaylistEntry {
        path: path.to_path_buf(),
        title,
        author,
        duration_secs,
        format,
    })
}

/// Extract metadata from AKS file
fn extract_aks_metadata(data: &[u8]) -> Option<(String, String, Option<f32>, String)> {
    let song = load_aks(data).ok()?;

    let title = if song.metadata.title.is_empty() {
        "(unknown)".to_string()
    } else {
        song.metadata.title.clone()
    };

    let author = if song.metadata.author.is_empty() {
        "(unknown)".to_string()
    } else {
        song.metadata.author.clone()
    };

    let duration = song
        .subsongs
        .first()
        .map(|s| s.end_position as f32 / s.replay_frequency_hz);

    Some((title, author, duration, "AKS".to_string()))
}

/// Extract metadata from AY file
fn extract_ay_metadata(data: &[u8]) -> Option<(String, String, Option<f32>, String)> {
    let (_, metadata) = AyPlayer::load_from_bytes(data, 0).ok()?;

    let title = if metadata.song_name.is_empty() {
        "(unknown)".to_string()
    } else {
        metadata.song_name.clone()
    };

    let author = if metadata.author.is_empty() {
        "(unknown)".to_string()
    } else {
        metadata.author.clone()
    };

    let duration = metadata.frame_count.map(|frames| frames as f32 / 50.0);

    Some((title, author, duration, "AY".to_string()))
}

/// Extract metadata from SNDH file
fn extract_sndh_metadata(data: &[u8]) -> Option<(String, String, Option<f32>, String)> {
    use ym2149_common::ChiptunePlayer;

    let player = SndhPlayer::new(data, 44100).ok()?;
    let metadata = ChiptunePlayer::metadata(&player);

    let title = if metadata.title.is_empty() {
        "(unknown)".to_string()
    } else {
        metadata.title.to_string()
    };

    let author = if metadata.author.is_empty() {
        "(unknown)".to_string()
    } else {
        metadata.author.to_string()
    };

    // SNDH duration is often unknown
    Some((title, author, None, "SNDH".to_string()))
}

/// Extract metadata from YM file
fn extract_ym_metadata(data: &[u8]) -> Option<(String, String, Option<f32>, String)> {
    let (player, summary) = load_song(data).ok()?;

    let (title, author) = if let Some(info) = player.info() {
        (
            if info.song_name.is_empty() {
                "(unknown)".to_string()
            } else {
                info.song_name.clone()
            },
            if info.author.is_empty() {
                "(unknown)".to_string()
            } else {
                info.author.clone()
            },
        )
    } else {
        ("(unknown)".to_string(), "(unknown)".to_string())
    };

    let duration = Some(summary.total_samples() as f32 / 44100.0);
    let format = summary.format.to_string();

    Some((title, author, duration, format))
}