oximedia-mir 0.1.8

Music Information Retrieval (MIR) system for OxiMedia
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
//! Music playlist intelligence for `oximedia-mir`.
//!
//! Provides smart playlist generation using BPM transitions, key
//! compatibility (Camelot wheel), energy flow, and other musical constraints.

#![allow(dead_code)]

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// PlaylistTrack
// ---------------------------------------------------------------------------

/// Metadata and musical properties of a single playlist track.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaylistTrack {
    /// Unique track identifier.
    pub id: u64,
    /// Track title.
    pub title: String,
    /// Artist name.
    pub artist: String,
    /// Tempo in BPM.
    pub bpm: f32,
    /// Musical key in Camelot notation (e.g., `"4A"`, `"9B"`) or standard notation.
    pub key: String,
    /// Energy level (0.0–1.0).
    pub energy: f32,
    /// Mood tag (e.g., `"happy"`, `"melancholic"`).
    pub mood: String,
    /// Duration in milliseconds.
    pub duration_ms: u64,
}

impl PlaylistTrack {
    /// Create a new track entry.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: u64,
        title: impl Into<String>,
        artist: impl Into<String>,
        bpm: f32,
        key: impl Into<String>,
        energy: f32,
        mood: impl Into<String>,
        duration_ms: u64,
    ) -> Self {
        Self {
            id,
            title: title.into(),
            artist: artist.into(),
            bpm,
            key: key.into(),
            energy,
            mood: mood.into(),
            duration_ms,
        }
    }
}

// ---------------------------------------------------------------------------
// PlaylistConstraint
// ---------------------------------------------------------------------------

/// A constraint applied when building a smart playlist.
#[derive(Debug, Clone)]
pub enum PlaylistConstraint {
    /// Maximum allowed BPM difference between consecutive tracks.
    MaxBpmChange(f32),
    /// Consecutive tracks must be harmonically compatible (Camelot wheel).
    KeyCompatible,
    /// Consecutive tracks should have similar energy levels.
    SimilarEnergy,
    /// No two consecutive tracks from the same artist.
    NoDuplicateArtist,
}

// ---------------------------------------------------------------------------
// BpmTransition
// ---------------------------------------------------------------------------

/// BPM transition compatibility utilities.
pub struct BpmTransition;

impl BpmTransition {
    /// Compatibility score between two BPM values.
    ///
    /// Returns 1.0 for identical BPMs and decays toward 0 as the difference grows.
    /// Tracks at double/half tempo (harmonic mixing) also get a bonus.
    #[must_use]
    pub fn compatibility(bpm_a: f32, bpm_b: f32) -> f32 {
        if bpm_a <= 0.0 || bpm_b <= 0.0 {
            return 0.0;
        }

        // Direct difference
        let diff = (bpm_a - bpm_b).abs();
        let direct = (-diff / 10.0).exp(); // decays at ~10 BPM scale

        // Harmonic relationship (half/double tempo)
        let diff_half = (bpm_a - bpm_b * 2.0).abs();
        let diff_double = (bpm_a * 2.0 - bpm_b).abs();
        let harmonic = ((-diff_half / 10.0).exp() + (-diff_double / 10.0).exp()) * 0.5;

        direct.max(harmonic)
    }

    /// Whether the BPM difference is within the given maximum.
    #[must_use]
    pub fn within_limit(bpm_a: f32, bpm_b: f32, max_diff: f32) -> bool {
        (bpm_a - bpm_b).abs() <= max_diff
    }
}

// ---------------------------------------------------------------------------
// KeyCompatibility (Camelot wheel)
// ---------------------------------------------------------------------------

/// Camelot wheel key compatibility checker.
pub struct KeyCompatibility;

impl KeyCompatibility {
    /// Parse a Camelot wheel key string (e.g., `"4A"`, `"9B"`) into
    /// `(number, suffix)` where suffix is `'A'` or `'B'`.
    ///
    /// Returns `None` if the string cannot be parsed.
    #[must_use]
    pub fn parse_camelot(key: &str) -> Option<(u8, char)> {
        let key = key.trim();
        if key.len() < 2 {
            return None;
        }

        let suffix = key.chars().last()?;
        if suffix != 'A' && suffix != 'B' {
            return None;
        }

        let num_str = &key[..key.len() - 1];
        let num: u8 = num_str.parse().ok()?;
        if !(1..=12).contains(&num) {
            return None;
        }

        Some((num, suffix))
    }

    /// Check whether two keys are compatible on the Camelot wheel.
    ///
    /// Compatible means:
    /// - Identical key
    /// - Adjacent number (±1, wrapping 12→1)
    /// - Same number, opposite suffix (relative major/minor)
    #[must_use]
    pub fn check(key_a: &str, key_b: &str) -> bool {
        match (Self::parse_camelot(key_a), Self::parse_camelot(key_b)) {
            (Some((na, sa)), Some((nb, sb))) => {
                if na == nb {
                    // Same number: either exact match or relative swap
                    return true;
                }
                // Adjacent numbers (wrap-around 1..=12)
                let adjacent =
                    na.abs_diff(nb) == 1 || (na == 1 && nb == 12) || (na == 12 && nb == 1);

                adjacent && sa == sb
            }
            _ => {
                // Fall back: if both keys are identical strings, they're compatible
                key_a.eq_ignore_ascii_case(key_b)
            }
        }
    }

    /// Compatibility score: 1.0 = identical, 0.5 = adjacent, 0.0 = incompatible.
    #[must_use]
    pub fn score(key_a: &str, key_b: &str) -> f32 {
        match (Self::parse_camelot(key_a), Self::parse_camelot(key_b)) {
            (Some((na, sa)), Some((nb, sb))) => {
                if na == nb && sa == sb {
                    return 1.0;
                }
                if na == nb {
                    return 0.7; // relative major/minor
                }
                let adjacent =
                    na.abs_diff(nb) == 1 || (na == 1 && nb == 12) || (na == 12 && nb == 1);
                if adjacent && sa == sb {
                    0.5
                } else {
                    0.0
                }
            }
            _ => {
                if key_a.eq_ignore_ascii_case(key_b) {
                    1.0
                } else {
                    0.0
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// EnergyFlow
// ---------------------------------------------------------------------------

/// How energy should evolve across a playlist.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum EnergyFlow {
    /// Tracks should gradually increase in energy.
    Rising,
    /// Tracks should gradually decrease in energy.
    Falling,
    /// Energy should remain roughly constant.
    Steady,
    /// Energy rises and falls in a wave pattern.
    Wave,
}

impl EnergyFlow {
    /// Target energy for the next track given the current energy.
    #[must_use]
    pub fn next_target_energy(self, current: f32) -> f32 {
        match self {
            Self::Rising => (current + 0.15).min(1.0),
            Self::Falling => (current - 0.15).max(0.0),
            Self::Steady => current,
            Self::Wave => {
                // Simple triangle: if below 0.5 rise, else fall
                if current < 0.5 {
                    (current + 0.2).min(1.0)
                } else {
                    (current - 0.2).max(0.0)
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// PlaylistBuilder
// ---------------------------------------------------------------------------

/// Builds smart playlists from a library of tracks.
pub struct PlaylistBuilder {
    tracks: HashMap<u64, PlaylistTrack>,
}

impl PlaylistBuilder {
    /// Create a new empty builder.
    #[must_use]
    pub fn new() -> Self {
        Self {
            tracks: HashMap::new(),
        }
    }

    /// Add a track to the library.
    pub fn add_track(&mut self, track: PlaylistTrack) {
        self.tracks.insert(track.id, track);
    }

    /// Build a smart playlist starting from a seed track.
    ///
    /// Uses greedy selection: at each step, score all remaining candidate
    /// tracks by how well they satisfy the active constraints, then pick the
    /// best one.
    ///
    /// # Arguments
    ///
    /// * `seed_id` — ID of the first track.
    /// * `length` — Desired playlist length (number of tracks).
    /// * `constraints` — List of constraints to apply.
    ///
    /// # Returns
    ///
    /// Ordered list of track IDs forming the playlist.
    #[must_use]
    pub fn build_smart(
        &self,
        seed_id: u64,
        length: u32,
        constraints: &[PlaylistConstraint],
    ) -> Vec<u64> {
        let length = length as usize;
        let mut playlist = Vec::with_capacity(length);

        let Some(seed) = self.tracks.get(&seed_id) else {
            return playlist;
        };

        playlist.push(seed_id);

        let mut used: std::collections::HashSet<u64> = std::collections::HashSet::new();
        used.insert(seed_id);

        let mut current = seed;

        while playlist.len() < length {
            let candidates: Vec<&PlaylistTrack> = self
                .tracks
                .values()
                .filter(|t| !used.contains(&t.id))
                .collect();

            if candidates.is_empty() {
                break;
            }

            let best = candidates
                .into_iter()
                .map(|t| (t, self.score_transition(current, t, constraints)))
                .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

            match best {
                Some((track, _score)) => {
                    used.insert(track.id);
                    playlist.push(track.id);
                    current = track;
                }
                None => break,
            }
        }

        playlist
    }

    /// Score a transition from `from` to `to` given the active constraints.
    fn score_transition(
        &self,
        from: &PlaylistTrack,
        to: &PlaylistTrack,
        constraints: &[PlaylistConstraint],
    ) -> f32 {
        let mut score = 1.0_f32;

        for constraint in constraints {
            match constraint {
                PlaylistConstraint::MaxBpmChange(max_diff) => {
                    if BpmTransition::within_limit(from.bpm, to.bpm, *max_diff) {
                        score += BpmTransition::compatibility(from.bpm, to.bpm) * 0.2;
                    } else {
                        score -= 0.5;
                    }
                }
                PlaylistConstraint::KeyCompatible => {
                    score += KeyCompatibility::score(&from.key, &to.key) * 0.3;
                }
                PlaylistConstraint::SimilarEnergy => {
                    let energy_diff = (from.energy - to.energy).abs();
                    score += (1.0 - energy_diff) * 0.2;
                }
                PlaylistConstraint::NoDuplicateArtist => {
                    if from.artist == to.artist {
                        score -= 1.0;
                    }
                }
            }
        }

        score
    }
}

impl Default for PlaylistBuilder {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn sample_track(id: u64, bpm: f32, key: &str, energy: f32, artist: &str) -> PlaylistTrack {
        PlaylistTrack::new(
            id,
            format!("Track {id}"),
            artist,
            bpm,
            key,
            energy,
            "neutral",
            180_000,
        )
    }

    #[test]
    fn test_bpm_compatibility_identical() {
        assert!((BpmTransition::compatibility(120.0, 120.0) - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_bpm_compatibility_decays() {
        let close = BpmTransition::compatibility(120.0, 125.0);
        let far = BpmTransition::compatibility(120.0, 160.0);
        assert!(close > far, "Closer BPM should have higher compatibility");
    }

    #[test]
    fn test_bpm_within_limit() {
        assert!(BpmTransition::within_limit(120.0, 128.0, 10.0));
        assert!(!BpmTransition::within_limit(120.0, 140.0, 10.0));
    }

    #[test]
    fn test_camelot_parse_valid() {
        assert_eq!(KeyCompatibility::parse_camelot("4A"), Some((4, 'A')));
        assert_eq!(KeyCompatibility::parse_camelot("12B"), Some((12, 'B')));
        assert_eq!(KeyCompatibility::parse_camelot("1A"), Some((1, 'A')));
    }

    #[test]
    fn test_camelot_parse_invalid() {
        assert_eq!(KeyCompatibility::parse_camelot("13A"), None);
        assert_eq!(KeyCompatibility::parse_camelot("4C"), None);
        assert_eq!(KeyCompatibility::parse_camelot("A"), None);
    }

    #[test]
    fn test_camelot_check_identical() {
        assert!(KeyCompatibility::check("4A", "4A"));
    }

    #[test]
    fn test_camelot_check_adjacent() {
        assert!(KeyCompatibility::check("4A", "5A"));
        assert!(KeyCompatibility::check("4A", "3A"));
    }

    #[test]
    fn test_camelot_check_wrap() {
        assert!(KeyCompatibility::check("1A", "12A"));
        assert!(KeyCompatibility::check("12A", "1A"));
    }

    #[test]
    fn test_camelot_check_incompatible() {
        assert!(!KeyCompatibility::check("4A", "8B"));
    }

    #[test]
    fn test_camelot_relative_same_number() {
        // Same number different suffix = relative major/minor → compatible
        assert!(KeyCompatibility::check("4A", "4B"));
    }

    #[test]
    fn test_energy_flow_rising() {
        let next = EnergyFlow::Rising.next_target_energy(0.5);
        assert!((next - 0.65).abs() < 1e-5);
    }

    #[test]
    fn test_energy_flow_falling() {
        let next = EnergyFlow::Falling.next_target_energy(0.5);
        assert!((next - 0.35).abs() < 1e-5);
    }

    #[test]
    fn test_energy_flow_steady() {
        let next = EnergyFlow::Steady.next_target_energy(0.7);
        assert!((next - 0.7).abs() < 1e-5);
    }

    #[test]
    fn test_energy_flow_wave_below_half() {
        let next = EnergyFlow::Wave.next_target_energy(0.3);
        assert!(next > 0.3, "Wave should rise when below 0.5");
    }

    #[test]
    fn test_energy_flow_wave_above_half() {
        let next = EnergyFlow::Wave.next_target_energy(0.8);
        assert!(next < 0.8, "Wave should fall when above 0.5");
    }

    #[test]
    fn test_playlist_builder_basic() {
        let mut builder = PlaylistBuilder::new();
        for i in 0..5 {
            builder.add_track(sample_track(i, 120.0 + i as f32, "4A", 0.7, "Artist A"));
        }
        let playlist = builder.build_smart(0, 3, &[]);
        assert_eq!(playlist.len(), 3);
        assert_eq!(playlist[0], 0); // seed first
    }

    #[test]
    fn test_playlist_builder_missing_seed() {
        let builder = PlaylistBuilder::new();
        let playlist = builder.build_smart(999, 5, &[]);
        assert!(playlist.is_empty());
    }

    #[test]
    fn test_playlist_builder_no_duplicate_artist() {
        let mut builder = PlaylistBuilder::new();
        builder.add_track(sample_track(1, 120.0, "4A", 0.7, "Artist A"));
        builder.add_track(sample_track(2, 121.0, "4A", 0.7, "Artist A"));
        builder.add_track(sample_track(3, 122.0, "5A", 0.6, "Artist B"));
        let constraints = [PlaylistConstraint::NoDuplicateArtist];
        let playlist = builder.build_smart(1, 3, &constraints);
        // Track 3 (Artist B) should be selected before track 2 (Artist A)
        assert!(playlist.contains(&3));
    }

    #[test]
    fn test_playlist_builder_key_compatible() {
        let mut builder = PlaylistBuilder::new();
        builder.add_track(sample_track(1, 120.0, "4A", 0.7, "A"));
        builder.add_track(sample_track(2, 120.0, "5A", 0.7, "B")); // adjacent key
        builder.add_track(sample_track(3, 120.0, "9B", 0.7, "C")); // incompatible key
        let constraints = [PlaylistConstraint::KeyCompatible];
        let playlist = builder.build_smart(1, 3, &constraints);
        assert!(!playlist.is_empty());
    }
}