chdlady-disc 0.2.2

Disc geometries and physical media formats for chdlady
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
//! CUE sheet parser and formatter for CD-ROM disc images.
use crate::error::DiscError;
use crate::types::{CdToc, CdTrack, CdTrackType, DiscType, Msf};
use std::path::Path;

/// Temporary staging structure for tracks while parsing CUE sheets.
struct StagedTrack {
    track_num: u32,
    track_type: CdTrackType,
    index0: Option<u32>,
    index1: u32,
    pregap: u32,
    postgap: u32,
    file_name: String,
}

/// Parser and generator for CDRWIN-compatible CUE sheets.
#[derive(Debug, Default)]
pub struct CueSheet;

impl CueSheet {
    /// Parses a CUE sheet from a string given an optional parent directory for resolving track files.
    pub fn parse(content: &str, parent_dir: Option<&Path>) -> Result<CdToc, DiscError> {
        let mut tracks = Vec::new();
        let mut current_file: Option<String> = None;
        let mut current_track_num: Option<u32> = None;
        let mut current_track_type: Option<CdTrackType> = None;
        let mut current_pregap: u32 = 0;
        let mut current_postgap: u32 = 0;
        let mut current_index0: Option<u32> = None;
        let mut current_index1: Option<u32> = None;
        let mut file_tracks: Vec<StagedTrack> = Vec::new();

        let flush_current_track = |file_tracks: &mut Vec<StagedTrack>,
                                   current_file: &Option<String>,
                                   current_track_num: Option<u32>,
                                   current_track_type: Option<CdTrackType>,
                                   current_index0: Option<u32>,
                                   current_index1: Option<u32>,
                                   current_pregap: u32,
                                   current_postgap: u32|
         -> Result<(), DiscError> {
            if let (Some(track_num), Some(track_type), Some(index1)) =
                (current_track_num, current_track_type, current_index1)
            {
                let file_name = current_file.clone().unwrap_or_default();
                file_tracks.push(StagedTrack {
                    track_num,
                    track_type,
                    index0: current_index0,
                    index1,
                    pregap: current_pregap,
                    postgap: current_postgap,
                    file_name,
                });
            }
            Ok(())
        };

        for raw_line in content.lines() {
            let line = raw_line.trim();
            if line.is_empty() || line.starts_with("REM") {
                continue;
            }

            let tokens = tokenize_line(line);
            if tokens.is_empty() {
                continue;
            }

            match tokens[0].to_uppercase().as_str() {
                "FILE" => {
                    flush_current_track(
                        &mut file_tracks,
                        &current_file,
                        current_track_num,
                        current_track_type,
                        current_index0,
                        current_index1,
                        current_pregap,
                        current_postgap,
                    )?;
                    current_track_num = None;
                    current_track_type = None;
                    current_index0 = None;
                    current_index1 = None;
                    current_pregap = 0;
                    current_postgap = 0;

                    if tokens.len() < 2 {
                        return Err(DiscError::InvalidCue("missing FILE path".to_string()));
                    }
                    crate::types::validate_descriptor_path(&tokens[1])?;
                    current_file = Some(tokens[1].clone());
                }
                "TRACK" => {
                    flush_current_track(
                        &mut file_tracks,
                        &current_file,
                        current_track_num,
                        current_track_type,
                        current_index0,
                        current_index1,
                        current_pregap,
                        current_postgap,
                    )?;
                    current_index0 = None;
                    current_index1 = None;
                    current_pregap = 0;
                    current_postgap = 0;

                    if tokens.len() < 3 {
                        return Err(DiscError::InvalidCue("incomplete TRACK line".to_string()));
                    }
                    let num = tokens[1].parse::<u32>().map_err(|_| {
                        DiscError::InvalidCue(format!("invalid track number: {}", tokens[1]))
                    })?;
                    let t_type = CdTrackType::parse_cue_or_metadata(&tokens[2])?;
                    current_track_num = Some(num);
                    current_track_type = Some(t_type);
                }
                "INDEX" => {
                    if tokens.len() < 3 {
                        return Err(DiscError::InvalidCue("incomplete INDEX line".to_string()));
                    }
                    let idx_num = tokens[1].parse::<u32>().map_err(|_| {
                        DiscError::InvalidCue(format!("invalid INDEX number: {}", tokens[1]))
                    })?;
                    let msf = Msf::parse(&tokens[2])?;
                    let lba = msf.to_lba();
                    if idx_num == 0 {
                        current_index0 = Some(lba);
                    } else if idx_num == 1 {
                        current_index1 = Some(lba);
                    }
                }
                "PREGAP" => {
                    if tokens.len() < 2 {
                        return Err(DiscError::InvalidCue("missing PREGAP value".to_string()));
                    }
                    let msf = Msf::parse(&tokens[1])?;
                    current_pregap += msf.to_lba();
                }
                "POSTGAP" => {
                    if tokens.len() < 2 {
                        return Err(DiscError::InvalidCue("missing POSTGAP value".to_string()));
                    }
                    let msf = Msf::parse(&tokens[1])?;
                    current_postgap += msf.to_lba();
                }
                _ => {}
            }
        }

        flush_current_track(
            &mut file_tracks,
            &current_file,
            current_track_num,
            current_track_type,
            current_index0,
            current_index1,
            current_pregap,
            current_postgap,
        )?;

        if file_tracks.is_empty() {
            return Err(DiscError::InvalidCue(
                "no tracks found in CUE sheet".to_string(),
            ));
        }

        // Measure track durations and file offsets
        for i in 0..file_tracks.len() {
            let staged = &file_tracks[i];
            let mut track_pregap = staged.pregap;
            if let Some(i0) = staged.index0 {
                if staged.index1 > i0 {
                    track_pregap += staged.index1 - i0;
                }
            }

            let is_same_file_as_prev = i > 0 && file_tracks[i - 1].file_name == staged.file_name;
            let is_same_file_as_next =
                i + 1 < file_tracks.len() && file_tracks[i + 1].file_name == staged.file_name;

            let cur_start = staged.index0.unwrap_or(staged.index1);

            let (frames, byte_offset) = if !is_same_file_as_prev && !is_same_file_as_next {
                // Standalone file for this track
                let total_frames = if let Some(parent) = parent_dir {
                    let path = parent.join(&staged.file_name);
                    measure_file_frames(&path, staged.track_type)?
                } else {
                    0
                };
                (total_frames, 0u64)
            } else if is_same_file_as_next {
                // Shared file with next track
                let next_start = file_tracks[i + 1]
                    .index0
                    .unwrap_or(file_tracks[i + 1].index1);
                if next_start < cur_start {
                    return Err(DiscError::InvalidCue(format!(
                        "track {} index overlaps next track in file '{}'",
                        staged.track_num, staged.file_name
                    )));
                }
                let span = next_start - cur_start;
                let offset = cur_start as u64 * staged.track_type.sector_size() as u64;
                (span, offset)
            } else {
                // Last track in a shared file
                let total_frames = if let Some(parent) = parent_dir {
                    let path = parent.join(&staged.file_name);
                    measure_file_frames(&path, staged.track_type)?
                } else {
                    0
                };
                let span = total_frames.saturating_sub(cur_start);
                let offset = cur_start as u64 * staged.track_type.sector_size() as u64;
                (span, offset)
            };

            let mut track = CdTrack::new(staged.track_num, staged.track_type, frames);
            track.pregap_frames = track_pregap;
            track.postgap_frames = staged.postgap;
            track.file_name = Some(staged.file_name.clone());
            track.file_offset = byte_offset;
            tracks.push(track);
        }

        Ok(CdToc {
            disc_type: DiscType::Cd,
            tracks,
        })
    }

    /// Generates CUE sheet text for a single binary file.
    pub fn format_single_bin(toc: &CdToc, bin_filename: &str) -> String {
        let mut out = String::new();
        out.push_str(&format!("FILE \"{}\" BINARY\n", bin_filename));

        let mut cumulative_lba: u32 = 0;
        for track in &toc.tracks {
            out.push_str(&format!(
                "  TRACK {:02} {}\n",
                track.track_num,
                track.track_type.cue_mode_str()
            ));

            if track.pregap_frames > 0 {
                let idx0 = Msf::from_lba(cumulative_lba);
                let idx1 = Msf::from_lba(cumulative_lba + track.pregap_frames);
                out.push_str(&format!("    INDEX 00 {}\n", idx0));
                out.push_str(&format!("    INDEX 01 {}\n", idx1));
            } else {
                let idx1 = Msf::from_lba(cumulative_lba);
                out.push_str(&format!("    INDEX 01 {}\n", idx1));
            }

            cumulative_lba += track.frames + track.postgap_frames;
        }

        out
    }

    /// Generates CUE sheet text for split-binary files (one file per track).
    pub fn format_split_bin(toc: &CdToc, base_name: &str) -> String {
        let mut out = String::new();

        for track in &toc.tracks {
            let track_bin = format!("{} (Track {}).bin", base_name, track.track_num);
            out.push_str(&format!("FILE \"{}\" BINARY\n", track_bin));
            out.push_str(&format!(
                "  TRACK {:02} {}\n",
                track.track_num,
                track.track_type.cue_mode_str()
            ));

            if track.pregap_frames > 0 {
                let idx0 = Msf::from_lba(0);
                let idx1 = Msf::from_lba(track.pregap_frames);
                out.push_str(&format!("    INDEX 00 {}\n", idx0));
                out.push_str(&format!("    INDEX 01 {}\n", idx1));
            } else {
                out.push_str("    INDEX 01 00:00:00\n");
            }
        }

        out
    }
}

/// Tokenizes a line handling quoted strings.
fn tokenize_line(line: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;

    for ch in line.chars() {
        match ch {
            '"' => {
                in_quotes = !in_quotes;
            }
            c if c.is_whitespace() && !in_quotes => {
                if !current.is_empty() {
                    tokens.push(current.clone());
                    current.clear();
                }
            }
            c => {
                current.push(c);
            }
        }
    }
    if !current.is_empty() {
        tokens.push(current);
    }
    tokens
}

/// Measures the frame duration of an external file (binary or WAV).
fn measure_file_frames(path: &Path, track_type: CdTrackType) -> Result<u32, DiscError> {
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        if ext.eq_ignore_ascii_case("wav") {
            let reader = hound::WavReader::open(path)?;
            let spec = reader.spec();
            let samples = reader.duration();
            let channels = spec.channels as u32;
            let sample_rate = spec.sample_rate;
            if sample_rate != 44100 || channels != 2 {
                return Err(DiscError::InvalidCue(format!(
                    "WAV file '{}' must be 44100Hz 16-bit stereo (found {}Hz, {} channels)",
                    path.display(),
                    sample_rate,
                    channels
                )));
            }
            // 588 stereo samples per CD audio frame
            return Ok(samples / 588);
        }
    }

    let meta = std::fs::metadata(path)?;
    let bytes = meta.len();
    let sec_size = track_type.sector_size() as u64;
    Ok((bytes / sec_size) as u32)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cue_single_bin_format() {
        let mut toc = CdToc::new(DiscType::Cd);
        let t1 = CdTrack::new(1, CdTrackType::Mode1Raw, 4);
        let mut t2 = CdTrack::new(2, CdTrackType::Audio, 4);
        t2.pregap_frames = 150;
        toc.tracks.push(t1);
        toc.tracks.push(t2);

        let cue = CueSheet::format_single_bin(&toc, "out.bin");
        assert!(cue.contains("FILE \"out.bin\" BINARY"));
        assert!(cue.contains("  TRACK 01 MODE1/2352\n    INDEX 01 00:00:00\n"));
        assert!(cue.contains("  TRACK 02 AUDIO\n    INDEX 00 00:00:04\n    INDEX 01 00:02:04\n"));
    }

    #[test]
    fn test_cue_split_bin_format() {
        let mut toc = CdToc::new(DiscType::Cd);
        let t1 = CdTrack::new(1, CdTrackType::Mode1, 10);
        let t2 = CdTrack::new(2, CdTrackType::Audio, 20);
        toc.tracks.push(t1);
        toc.tracks.push(t2);

        let cue = CueSheet::format_split_bin(&toc, "disc");
        assert!(cue.contains("FILE \"disc (Track 1).bin\" BINARY"));
        assert!(cue.contains("  TRACK 01 MODE1/2048\n    INDEX 01 00:00:00\n"));
        assert!(cue.contains("FILE \"disc (Track 2).bin\" BINARY"));
        assert!(cue.contains("  TRACK 02 AUDIO\n    INDEX 01 00:00:00\n"));
    }

    #[test]
    fn test_cue_single_bin_format_multitrack_with_pregaps() {
        let mut toc = CdToc::new(DiscType::Cd);
        let t1 = CdTrack::new(1, CdTrackType::Audio, 3365);
        let mut t2 = CdTrack::new(2, CdTrackType::Mode1Raw, 8634);
        t2.pregap_frames = 231;
        let mut t3 = CdTrack::new(3, CdTrackType::Audio, 9417);
        t3.pregap_frames = 153;
        let t4 = CdTrack::new(4, CdTrackType::Audio, 9261);

        toc.tracks.push(t1);
        toc.tracks.push(t2);
        toc.tracks.push(t3);
        toc.tracks.push(t4);

        let cue = CueSheet::format_single_bin(&toc, "out.bin");
        assert!(cue.contains("  TRACK 01 AUDIO\n    INDEX 01 00:00:00\n"));
        assert!(cue.contains("  TRACK 02 MODE1/2352\n    INDEX 00 00:44:65\n    INDEX 01 00:47:71\n"));
        assert!(cue.contains("  TRACK 03 AUDIO\n    INDEX 00 02:39:74\n    INDEX 01 02:42:02\n"));
        assert!(cue.contains("  TRACK 04 AUDIO\n    INDEX 01 04:45:41\n"));
    }
}