baken-export 4.4.0

Bake'n Deck device export: writes a rekordbox USB export (export.pdb + ANLZ) from collection.xml
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
//! `collection.xml` (rekordbox "Export Collection in xml format") to a typed model.

use anyhow::{Context, Result};
use quick_xml::events::{BytesStart, Event};
use quick_xml::reader::Reader;
use std::path::Path;

#[derive(Debug, Clone, Default)]
pub struct Tempo {
    pub inizio: f64,
    pub bpm: f64,
    pub metro: String,
    pub battito: u32,
}

#[derive(Debug, Clone, Default)]
pub struct Cue {
    pub name: String,
    /// rekordbox `Type`: 0 cue, 1 fade-in, 2 fade-out, 3 load, 4 loop.
    pub kind: u32,
    pub start: f64,
    pub end: Option<f64>,
    /// -1 memory cue, 0..=7 hot cue A..H.
    pub num: i32,
    pub rgb: Option<(u8, u8, u8)>,
    /// `Name` started with [`ACTIVE_LOOP_MARKER`], which is not kept in `name`.
    pub marked: bool,
}

impl Cue {
    pub fn is_hot(&self) -> bool {
        self.num >= 0
    }
    pub fn is_loop(&self) -> bool {
        self.kind == 4 || self.end.is_some()
    }
    pub fn is_memory_loop(&self) -> bool {
        !self.is_hot() && self.is_loop()
    }
}

/// The XML has no field for an active loop (a memory loop the player engages
/// when playback reaches it), so a memory loop whose `Name` starts with this,
/// in any case, is one (issue #210).
pub const ACTIVE_LOOP_MARKER: &str = "[active]";

fn split_marker(name: String) -> (String, bool) {
    let trimmed = name.trim_start();
    match trimmed.get(..ACTIVE_LOOP_MARKER.len()) {
        Some(head) if head.eq_ignore_ascii_case(ACTIVE_LOOP_MARKER) => (
            trimmed[ACTIVE_LOOP_MARKER.len()..].trim_start().to_string(),
            true,
        ),
        _ => (name, false),
    }
}

/// The track's active loop: players take one, so the earliest marked memory loop.
pub fn active_loop(cues: &[Cue]) -> Option<&Cue> {
    cues.iter()
        .filter(|c| c.marked && c.is_memory_loop())
        .min_by(|a, b| a.start.total_cmp(&b.start))
}

/// A track whose [`ACTIVE_LOOP_MARKER`]s do not name exactly one memory loop.
#[derive(Debug, Clone, PartialEq)]
pub enum ActiveLoopWarning {
    /// Several memory loops are marked; only the one at `used` seconds is active.
    Several {
        track: String,
        count: usize,
        used: f64,
    },
    /// Only cues that are not memory loops are marked, so none is active.
    NotAMemoryLoop { track: String },
}

impl ActiveLoopWarning {
    pub fn check(track: &Track) -> Option<Self> {
        let count = track
            .cues
            .iter()
            .filter(|c| c.marked && c.is_memory_loop())
            .count();
        match active_loop(&track.cues) {
            Some(used) if count > 1 => Some(Self::Several {
                track: track.name.clone(),
                count,
                used: used.start,
            }),
            None if track.cues.iter().any(|c| c.marked) => Some(Self::NotAMemoryLoop {
                track: track.name.clone(),
            }),
            _ => None,
        }
    }
}

impl std::fmt::Display for ActiveLoopWarning {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Several { track, count, used } => write!(
                f,
                "{track}: {count} memory loops are named {ACTIVE_LOOP_MARKER}, but a player takes one active loop per track; only the one at {used:.3} s is written as active"
            ),
            Self::NotAMemoryLoop { track } => write!(
                f,
                "{track}: {ACTIVE_LOOP_MARKER} is on a cue that is not a memory loop, so the track gets no active loop"
            ),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct Track {
    pub id: u64,
    pub name: String,
    pub artist: String,
    pub composer: String,
    pub album: String,
    pub grouping: String,
    pub genre: String,
    pub kind: String,
    pub size: u64,
    pub total_time: u32,
    pub disc_number: u32,
    pub track_number: u32,
    pub year: u32,
    pub average_bpm: f64,
    pub date_added: String,
    pub bit_rate: u32,
    pub sample_rate: u32,
    pub comments: String,
    pub play_count: u32,
    /// 0, 51, 102, 153, 204, 255 for 0..=5 stars.
    pub rating: u32,
    /// Decoded filesystem path.
    pub location: String,
    pub remixer: String,
    pub tonality: String,
    pub label: String,
    pub mix: String,
    /// `0xRRGGBB`.
    pub colour: Option<u32>,
    pub tempos: Vec<Tempo>,
    pub cues: Vec<Cue>,
}

impl Track {
    pub fn file_name(&self) -> &str {
        self.location.rsplit('/').next().unwrap_or(&self.location)
    }
    pub fn stars(&self) -> u8 {
        (self.rating / 51).min(5) as u8
    }
    /// BPM of the first grid segment, or `AverageBpm` without a grid; loop
    /// lengths in the cue sections are counted in these beats.
    pub fn grid_bpm(&self) -> f64 {
        self.tempos.first().map_or(self.average_bpm, |t| t.bpm)
    }
}

#[derive(Debug, Clone, Default)]
pub struct Playlist {
    pub name: String,
    /// `Folder/Sub/Name` under ROOT.
    pub path: String,
    pub is_folder: bool,
    /// `KeyType` for playlists; only "0" (TrackID references) is exportable.
    pub key_type: String,
    /// Index into `Library::playlists` of the containing folder.
    pub parent: Option<usize>,
    pub track_ids: Vec<u64>,
}

#[derive(Debug, Clone, Default)]
pub struct Library {
    pub tracks: Vec<Track>,
    /// Document order, folders before their children.
    pub playlists: Vec<Playlist>,
}

impl Library {
    pub fn load(path: &Path) -> Result<Self> {
        let data = std::fs::read(path).with_context(|| format!("reading {}", path.display()))?;
        Self::parse(&data)
    }

    pub fn parse(data: &[u8]) -> Result<Self> {
        let mut reader = Reader::from_reader(data);
        reader.config_mut().trim_text(true);
        let mut lib = Library::default();
        let mut in_collection = false;
        let mut in_playlists = false;
        let mut current: Option<Track> = None;
        // stack of playlist indices for the NODE nesting (ROOT excluded)
        let mut node_stack: Vec<Option<usize>> = Vec::new();
        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Eof => break,
                Event::Start(e) => match e.name().as_ref() {
                    "COLLECTION" => in_collection = true,
                    "PLAYLISTS" => in_playlists = true,
                    "TRACK" if in_collection => current = Some(track_from(&e)?),
                    "NODE" if in_playlists => {
                        let idx = lib.push_node(&e, &node_stack)?;
                        node_stack.push(idx);
                    }
                    _ => {}
                },
                Event::Empty(e) => match e.name().as_ref() {
                    "TRACK" if in_collection => lib.tracks.push(track_from(&e)?),
                    "TRACK" if in_playlists => {
                        if let (Some(Some(idx)), Some(key)) = (node_stack.last(), attr(&e, "Key")?)
                        {
                            lib.playlists[*idx].track_ids.push(key.trim().parse()?);
                        }
                    }
                    "TEMPO" => {
                        if let Some(t) = current.as_mut() {
                            t.tempos.push(Tempo {
                                inizio: num(&e, "Inizio")?,
                                bpm: num(&e, "Bpm")?,
                                metro: attr(&e, "Metro")?.unwrap_or_default(),
                                battito: num(&e, "Battito")? as u32,
                            });
                        }
                    }
                    "POSITION_MARK" => {
                        if let Some(t) = current.as_mut() {
                            let rgb =
                                match (attr(&e, "Red")?, attr(&e, "Green")?, attr(&e, "Blue")?) {
                                    (Some(r), Some(g), Some(b)) => {
                                        Some((r.parse()?, g.parse()?, b.parse()?))
                                    }
                                    _ => None,
                                };
                            let (name, marked) =
                                split_marker(attr(&e, "Name")?.unwrap_or_default());
                            t.cues.push(Cue {
                                name,
                                marked,
                                kind: num(&e, "Type")? as u32,
                                start: num(&e, "Start")?,
                                end: attr(&e, "End")?.map(|v| v.trim().parse()).transpose()?,
                                num: attr(&e, "Num")?
                                    .map(|v| v.trim().parse())
                                    .transpose()?
                                    .unwrap_or(-1),
                                rgb,
                            });
                        }
                    }
                    "NODE" if in_playlists => {
                        lib.push_node(&e, &node_stack)?;
                    }
                    _ => {}
                },
                Event::End(e) => match e.name().as_ref() {
                    "COLLECTION" => in_collection = false,
                    "PLAYLISTS" => in_playlists = false,
                    "TRACK" if in_collection => {
                        if let Some(t) = current.take() {
                            lib.tracks.push(t);
                        }
                    }
                    "NODE" if in_playlists => {
                        node_stack.pop();
                    }
                    _ => {}
                },
                _ => {}
            }
            buf.clear();
        }
        Ok(lib)
    }

    fn push_node(&mut self, e: &BytesStart, stack: &[Option<usize>]) -> Result<Option<usize>> {
        let name = attr(e, "Name")?.unwrap_or_default();
        let ty = attr(e, "Type")?.unwrap_or_default();
        if stack.is_empty() && ty == "0" {
            return Ok(None); // ROOT
        }
        let parent = stack.last().copied().flatten();
        let path = match parent {
            Some(p) => format!("{}/{}", self.playlists[p].path, name),
            None => name.clone(),
        };
        self.playlists.push(Playlist {
            name,
            path,
            is_folder: ty == "0",
            key_type: attr(e, "KeyType")?.unwrap_or_default(),
            parent,
            track_ids: Vec::new(),
        });
        Ok(Some(self.playlists.len() - 1))
    }

    pub fn track(&self, id: u64) -> Option<&Track> {
        self.tracks.iter().find(|t| t.id == id)
    }

    /// Playlist by `Folder/Name` path.
    pub fn playlist(&self, path: &str) -> Option<&Playlist> {
        self.playlists
            .iter()
            .find(|p| p.path == path && !p.is_folder)
    }
}

fn attr(e: &BytesStart, name: &str) -> Result<Option<String>> {
    for a in e.attributes() {
        let a = a?;
        if a.key.as_ref() == name {
            #[allow(deprecated)]
            return Ok(Some(a.unescape_value()?.into_owned()));
        }
    }
    Ok(None)
}

fn num(e: &BytesStart, name: &str) -> Result<f64> {
    Ok(attr(e, name)?
        .map(|v| v.trim().parse::<f64>())
        .transpose()?
        .unwrap_or(0.0))
}

fn track_from(e: &BytesStart) -> Result<Track> {
    let mut t = Track::default();
    for a in e.attributes() {
        let a = a?;
        #[allow(deprecated)]
        let v = a.unescape_value()?.into_owned();
        let n = || v.trim().parse::<u64>().unwrap_or(0);
        match a.key.as_ref() {
            "TrackID" => t.id = n(),
            "Name" => t.name = v,
            "Artist" => t.artist = v,
            "Composer" => t.composer = v,
            "Album" => t.album = v,
            "Grouping" => t.grouping = v,
            "Genre" => t.genre = v,
            "Kind" => t.kind = v,
            "Size" => t.size = n(),
            "TotalTime" => t.total_time = n() as u32,
            "DiscNumber" => t.disc_number = n() as u32,
            "TrackNumber" => t.track_number = n() as u32,
            "Year" => t.year = n() as u32,
            "AverageBpm" => t.average_bpm = v.trim().parse().unwrap_or(0.0),
            "DateAdded" => t.date_added = v,
            "BitRate" => t.bit_rate = n() as u32,
            "SampleRate" => t.sample_rate = n() as u32,
            "Comments" => t.comments = v,
            "PlayCount" => t.play_count = n() as u32,
            "Rating" => t.rating = n() as u32,
            "Location" => t.location = baken_core::cdjsafe::decode_location(&v)?,
            "Remixer" => t.remixer = v,
            "Tonality" => t.tonality = v,
            "Label" => t.label = v,
            "Mix" => t.mix = v,
            "Colour" => {
                t.colour = u32::from_str_radix(v.trim().trim_start_matches("0x"), 16).ok();
            }
            _ => {}
        }
    }
    Ok(t)
}

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

    const SAMPLE: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<DJ_PLAYLISTS Version="1.0.0">
  <PRODUCT Name="rekordbox" Version="7.2.18" Company="AlphaTheta"/>
  <COLLECTION Entries="2">
    <TRACK TrackID="352" Name="Unreal" Artist="XamarA" Album="" Genre="Techno" Kind="MP3 File" Size="18920358" TotalTime="472" DiscNumber="0" TrackNumber="0" Year="0" AverageBpm="128.00" DateAdded="2023-09-06" BitRate="320" SampleRate="48000" Comments="" PlayCount="1" Rating="153" Location="file://localhost/Volumes/X/Music/Unreal.mp3" Remixer="" Tonality="1A" Label="" Mix="" Colour="0xFFFF00">
      <TEMPO Inizio="0.281" Bpm="128.00" Metro="4/4" Battito="1"/>
      <POSITION_MARK Name="" Type="0" Start="0.281" Num="-1"/>
      <POSITION_MARK Name="" Type="0" Start="15.280" Num="1" Red="40" Green="226" Blue="20"/>
      <POSITION_MARK Name="[active] lp" Type="4" Start="30.000" End="33.750" Num="-1"/>
    </TRACK>
    <TRACK TrackID="2" Name="&amp;" Artist="A" Kind="FLAC File" Size="1" TotalTime="1" AverageBpm="0" Location="file://localhost/V/22.%20%E7%9B%BE.flac" Tonality=""/>
  </COLLECTION>
  <PLAYLISTS>
    <NODE Type="0" Name="ROOT" Count="2">
      <NODE Name="Sets" Type="0" Count="1">
        <NODE Name="Friday" Type="1" KeyType="0" Entries="2">
          <TRACK Key="352"/>
          <TRACK Key="2"/>
        </NODE>
      </NODE>
      <NODE Name="Empty" Type="1" KeyType="0" Entries="0"/>
    </NODE>
  </PLAYLISTS>
</DJ_PLAYLISTS>"#;

    #[test]
    fn parses_tracks_children_and_tree() {
        let lib = Library::parse(SAMPLE.as_bytes()).unwrap();
        assert_eq!(lib.tracks.len(), 2);
        let t = &lib.tracks[0];
        assert_eq!((t.id, t.stars(), t.colour), (352, 3, Some(0xFFFF00)));
        assert_eq!(t.location, "/Volumes/X/Music/Unreal.mp3");
        assert_eq!(t.tempos.len(), 1);
        assert_eq!(t.cues.len(), 3);
        assert!(t.cues[1].is_hot() && t.cues[1].rgb == Some((40, 226, 20)));
        assert!(t.cues[2].is_loop() && t.cues[2].end == Some(33.75));
        assert_eq!((t.cues[2].name.as_str(), t.cues[2].marked), ("lp", true));
        assert!(!t.cues[0].marked);
        assert_eq!(lib.tracks[1].name, "&");
        assert_eq!(lib.tracks[1].file_name(), "22. 盾.flac");
        let paths: Vec<_> = lib
            .playlists
            .iter()
            .map(|p| (p.path.as_str(), p.is_folder))
            .collect();
        assert_eq!(
            paths,
            vec![("Sets", true), ("Sets/Friday", false), ("Empty", false)]
        );
        assert_eq!(lib.playlist("Sets/Friday").unwrap().track_ids, vec![352, 2]);
        assert_eq!(lib.playlists[1].parent, Some(0));
    }

    #[test]
    fn the_active_marker_is_read_and_left_out_of_the_name() {
        let split = |n: &str| split_marker(n.to_string());
        assert_eq!(split("[active] Build"), ("Build".to_string(), true));
        assert_eq!(split(" [Active]Drop"), ("Drop".to_string(), true));
        assert_eq!(split("[ACTIVE]"), (String::new(), true));
        assert_eq!(
            split("Build [active]"),
            ("Build [active]".to_string(), false)
        );
        assert_eq!(split("[act"), ("[act".to_string(), false));
        assert_eq!(split("ワーズワース"), ("ワーズワース".to_string(), false));
    }

    #[test]
    fn active_loop_warnings() {
        let cue = |start: f64, kind: u32, num: i32| Cue {
            start,
            kind,
            end: (kind == 4).then_some(start + 4.0),
            num,
            marked: true,
            ..Default::default()
        };
        let track = |cues: Vec<Cue>| Track {
            name: "T".into(),
            cues,
            ..Default::default()
        };
        // a marked memory loop and its marked hot copy: one active loop
        let one = track(vec![cue(8.0, 4, -1), cue(8.0, 4, 0)]);
        assert_eq!(active_loop(&one.cues).map(|c| c.start), Some(8.0));
        assert_eq!(ActiveLoopWarning::check(&one), None);
        let two = track(vec![cue(40.0, 4, -1), cue(20.0, 4, -1)]);
        assert_eq!(
            ActiveLoopWarning::check(&two),
            Some(ActiveLoopWarning::Several {
                track: "T".into(),
                count: 2,
                used: 20.0
            })
        );
        let cue_only = track(vec![cue(5.0, 0, -1), cue(5.0, 0, 1)]);
        assert_eq!(active_loop(&cue_only.cues).map(|c| c.start), None);
        assert!(matches!(
            ActiveLoopWarning::check(&cue_only),
            Some(ActiveLoopWarning::NotAMemoryLoop { .. })
        ));
        assert_eq!(ActiveLoopWarning::check(&track(Vec::new())), None);
    }
}