This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tracklist.rs
291 lines (248 loc) · 9.65 KB
/
tracklist.rs
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
// cue_sheet
// Copyright (C) 2017 Leonardo Schwarz <mail@leoschwarz.com>
//
// 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, either version 3 of the License, or
// (at your option) any later version.
//
// 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 <http://www.gnu.org/licenses/>.
//! Generate a tracklist from a cue file.
// TODO don't swallow errors in parsing but use Result and Option where appropriate.
use errors::Error;
use parser::{self, Command, FileFormat, Time, TrackType};
/// A tracklist provides a more useful representation of the information of a cue sheet.
#[derive(Clone, Debug)]
pub struct Tracklist {
/// Files described by the tracklist.
pub files: Vec<TrackFile>,
/// Performer of the tracklist.
pub performer: Option<String>,
/// Title of the tracklist.
pub title: Option<String>,
}
impl Tracklist {
/// Parse a cue sheet (content provided as `source`) into a `Tracklist`.
pub fn parse(source: &str) -> Result<Tracklist, Error> {
let mut commands = parser::parse_cue(source)?;
let mut performer = None;
let mut title = None;
while commands.len() > 0 {
match commands[0].clone() {
Command::Performer(p) => {
performer = Some(p);
commands.remove(0);
}
Command::Title(t) => {
title = Some(t);
commands.remove(0);
}
Command::Rem(_, _) => {
commands.remove(0);
}
_ => {
break;
}
}
}
let mut files = Vec::new();
while commands.len() > 0 {
if let Ok(file) = TrackFile::consume(&mut commands) {
files.push(file);
} else {
break;
}
}
Ok(Tracklist {
files: files,
performer: performer,
title: title,
})
}
}
/// One file described by a tracklist.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TrackFile {
/// List of tracks contained in the file.
pub tracks: Vec<Track>,
/// The filename.
pub name: String,
/// The format of the file.
pub format: FileFormat,
}
impl TrackFile {
fn consume(commands: &mut Vec<Command>) -> Result<Self, Error> {
if let Command::File(name, format) = commands.remove(0) {
let mut tracks: Vec<Track> = Vec::new();
let mut last_time: Option<Time> = None;
while commands.len() > 0 {
if let Ok(track) = Track::consume(commands) {
if track.index.len() > 0 {
let time = track.index[track.index.len() - 1].clone();
if let Some(start) = last_time {
let stop = track.index[0].clone().1;
let duration = stop - start;
let track_n = tracks.len();
if let Some(last_track) = tracks.get_mut(track_n - 1) {
(*last_track).duration = Some(duration);
}
}
last_time = Some(time.1);
} else {
last_time = None;
}
tracks.push(track);
} else {
break;
}
}
Ok(TrackFile {
tracks: tracks,
name: name,
format: format,
})
} else {
Err("TrackFile::consume called but no Track command found.".into())
}
}
}
/// One track described by a tracklist.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Track {
/// Title of the track.
pub title: Option<String>,
/// Type of the track.
pub track_type: TrackType,
/// Duration of the track, if it was possible to determine it.
///
/// This is only possible if tracks have index commands attached to them.
/// Also note that with just a cue file it is usually not possible to determine the duration of
/// the last track in the list.
pub duration: Option<Time>,
/// Index commands attached to this track (if any).
pub index: Vec<Index>,
/// Track number as provided in the cue sheet.
pub number: u32,
/// The performer of the track if any was stated.
pub performer: Option<String>,
}
type Index = (u32, Time);
impl Track {
fn consume(commands: &mut Vec<Command>) -> Result<Track, Error> {
if let Command::Track(track_num, track_type) = commands.remove(0) {
let mut title = None;
let mut performer = None;
let mut index = Vec::new();
while commands.len() > 0 {
match commands[0].clone() {
Command::Performer(p) => {
performer = Some(p);
commands.remove(0);
}
Command::Title(t) => {
title = Some(t);
commands.remove(0);
}
Command::Pregap(time) => {
let next_command = commands
.get(1)
.ok_or("Pregap is the last command in the track!".to_owned())?
.to_owned();
let first_index;
match next_command {
Command::Index(_, time) => first_index = time,
_ => {
return Err("Pregap is not followed by an index!".into());
}
}
let diff = first_index.total_frames() - time.total_frames();
index.push((0, Time::from_frames(diff)));
commands.remove(0);
}
Command::Index(i, time) => {
index.push((i, time));
commands.remove(0);
}
_ => break,
}
}
Ok(Track {
title: title,
track_type: track_type,
duration: None,
index: index,
number: track_num,
performer: performer,
})
} else {
Err("Track::consume called but no Track command found.".into())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sample() {
let source = r#"REM GENRE Alternative
REM DATE 1991
REM DISCID 860B640B
REM COMMENT "ExactAudioCopy v0.95b4"
PERFORMER "My Bloody Valentine"
TITLE "Loveless"
FILE "My Bloody Valentine - Loveless.wav" WAVE
TRACK 01 AUDIO
TITLE "Only Shallow"
PERFORMER "My Bloody Valentine"
INDEX 01 00:00:00
TRACK 02 AUDIO
TITLE "Loomer"
PERFORMER "My Bloody Valentine"
INDEX 01 04:17:52"#;
let tracklist = Tracklist::parse(source).unwrap();
assert_eq!(tracklist.title.unwrap(), "Loveless".to_string());
let files = tracklist.files;
assert_eq!(files.len(), 1);
let ref f = files[0];
assert_eq!(f.name, "My Bloody Valentine - Loveless.wav".to_string());
assert_eq!(f.format, FileFormat::Wave);
let ref tracks = f.tracks;
assert_eq!(tracks.len(), 2);
assert_eq!(tracks[0].clone().title.unwrap(), "Only Shallow".to_string());
assert_eq!(tracks[0].track_type, TrackType::Audio);
assert_eq!(tracks[0].duration, Some(Time::new(4, 17, 52)));
assert_eq!(tracks[0].number, 1);
assert_eq!(tracks[0].performer, Some("My Bloody Valentine".to_string()));
assert_eq!(tracks[1].clone().title.unwrap(), "Loomer".to_string());
assert_eq!(tracks[1].track_type, TrackType::Audio);
assert_eq!(tracks[1].duration, None);
assert_eq!(tracks[1].number, 2);
assert_eq!(tracks[1].performer, Some("My Bloody Valentine".to_string()));
}
#[test]
fn pregap() {
let src = r#"FILE "disc.img" BINARY
TRACK 01 MODE1/2352
INDEX 01 00:00:00
TRACK 02 AUDIO
PREGAP 00:02:00
INDEX 01 58:41:36
TRACK 03 AUDIO
INDEX 00 61:06:08
INDEX 01 61:08:08"#;
let tracklist = Tracklist::parse(src).unwrap();
let ref f = tracklist.files[0];
let ref tracks = f.tracks;
assert_eq!(tracks[0].index[0], (1, Time::new(0, 0, 0)));
assert_eq!(tracks[1].index[0], (0, Time::new(58, 39, 36)));
assert_eq!(tracks[1].index[1], (1, Time::new(58, 41, 36)));
assert_eq!(tracks[2].index[0], (0, Time::new(61, 06, 08)));
assert_eq!(tracks[2].index[1], (1, Time::new(61, 08, 08)));
}
}