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
#![allow(dead_code)]
//! Cover version detection for music information retrieval.
//!
//! Detects whether two recordings are versions of the same underlying
//! composition by comparing tonal sequences (chroma features) that are
//! invariant to tempo, key, and timbral differences.

use std::collections::HashMap;

/// Chroma-based fingerprint of a musical passage.
#[derive(Debug, Clone, PartialEq)]
pub struct ChromaFingerprint {
    /// Sequence of 12-dimensional chroma vectors (one per analysis frame).
    pub frames: Vec<[f64; 12]>,
    /// Duration in seconds represented by this fingerprint.
    pub duration_s: f64,
}

impl ChromaFingerprint {
    /// Create a fingerprint from a sequence of chroma vectors.
    #[must_use]
    pub fn new(frames: Vec<[f64; 12]>, duration_s: f64) -> Self {
        Self { frames, duration_s }
    }

    /// Number of frames.
    #[must_use]
    pub fn len(&self) -> usize {
        self.frames.len()
    }

    /// Whether the fingerprint is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.frames.is_empty()
    }

    /// Transpose all chroma vectors by `semitones` (key normalisation).
    #[must_use]
    pub fn transposed(&self, semitones: i32) -> Self {
        let shift = ((semitones % 12) + 12) as usize % 12;
        let frames = self
            .frames
            .iter()
            .map(|f| {
                let mut out = [0.0; 12];
                for i in 0..12 {
                    out[(i + shift) % 12] = f[i];
                }
                out
            })
            .collect();
        Self {
            frames,
            duration_s: self.duration_s,
        }
    }

    /// Compute average chroma vector across all frames.
    #[must_use]
    pub fn average_chroma(&self) -> [f64; 12] {
        let mut avg = [0.0_f64; 12];
        if self.frames.is_empty() {
            return avg;
        }
        for f in &self.frames {
            for (i, v) in f.iter().enumerate() {
                avg[i] += v;
            }
        }
        #[allow(clippy::cast_precision_loss)]
        let n = self.frames.len() as f64;
        for v in &mut avg {
            *v /= n;
        }
        avg
    }
}

/// A known cover version entry.
#[derive(Debug, Clone)]
pub struct CoverVersion {
    /// Unique identifier.
    pub id: String,
    /// Title of the recording.
    pub title: String,
    /// Artist name.
    pub artist: String,
    /// Chroma fingerprint.
    pub fingerprint: ChromaFingerprint,
    /// Estimated key (0..11, where 0 = C).
    pub estimated_key: u8,
}

impl CoverVersion {
    /// Create a new cover version entry.
    #[must_use]
    pub fn new(
        id: &str,
        title: &str,
        artist: &str,
        fingerprint: ChromaFingerprint,
        key: u8,
    ) -> Self {
        Self {
            id: id.to_string(),
            title: title.to_string(),
            artist: artist.to_string(),
            fingerprint,
            estimated_key: key % 12,
        }
    }
}

/// Detection result when comparing two recordings.
#[derive(Debug, Clone, PartialEq)]
pub struct DetectionResult {
    /// Query track ID.
    pub query_id: String,
    /// Reference track ID.
    pub reference_id: String,
    /// Similarity score (0.0..1.0).
    pub similarity: f64,
    /// Best key transposition applied (semitones).
    pub key_shift: i32,
    /// Whether the pair is classified as a cover.
    pub is_cover: bool,
}

/// Database of known tracks for cover detection.
#[derive(Debug, Clone)]
pub struct CoverDatabase {
    /// Stored tracks.
    entries: Vec<CoverVersion>,
    /// Index from ID to position.
    index: HashMap<String, usize>,
}

impl CoverDatabase {
    /// Create an empty database.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            index: HashMap::new(),
        }
    }

    /// Number of entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the database is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Add a track to the database.
    pub fn add(&mut self, entry: CoverVersion) {
        let idx = self.entries.len();
        self.index.insert(entry.id.clone(), idx);
        self.entries.push(entry);
    }

    /// Look up an entry by ID.
    #[must_use]
    pub fn get(&self, id: &str) -> Option<&CoverVersion> {
        self.index.get(id).map(|&i| &self.entries[i])
    }
}

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

/// Detector that finds cover versions by comparing chroma fingerprints.
#[derive(Debug)]
pub struct CoverDetector {
    /// Similarity threshold above which a pair is considered a cover.
    pub threshold: f64,
}

impl Default for CoverDetector {
    fn default() -> Self {
        Self { threshold: 0.7 }
    }
}

impl CoverDetector {
    /// Create a detector with the given threshold.
    #[must_use]
    pub fn new(threshold: f64) -> Self {
        Self {
            threshold: threshold.clamp(0.0, 1.0),
        }
    }

    /// Compare two fingerprints, trying all 12 key transpositions.
    #[must_use]
    pub fn compare(&self, a: &ChromaFingerprint, b: &ChromaFingerprint) -> (f64, i32) {
        let mut best_sim = 0.0_f64;
        let mut best_shift = 0_i32;

        for shift in 0..12_i32 {
            let b_transposed = b.transposed(shift);
            let sim = Self::cosine_similarity_sequence(a, &b_transposed);
            if sim > best_sim {
                best_sim = sim;
                best_shift = shift;
            }
        }
        (best_sim, best_shift)
    }

    /// Detect whether a query is a cover of any entry in the database.
    #[must_use]
    pub fn detect(&self, query: &CoverVersion, db: &CoverDatabase) -> Vec<DetectionResult> {
        let mut results = Vec::new();
        for entry in &db.entries {
            if entry.id == query.id {
                continue;
            }
            let (sim, shift) = self.compare(&query.fingerprint, &entry.fingerprint);
            results.push(DetectionResult {
                query_id: query.id.clone(),
                reference_id: entry.id.clone(),
                similarity: sim,
                key_shift: shift,
                is_cover: sim >= self.threshold,
            });
        }
        results.sort_by(|a, b| {
            b.similarity
                .partial_cmp(&a.similarity)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results
    }

    /// Average cosine similarity across aligned frames.
    fn cosine_similarity_sequence(a: &ChromaFingerprint, b: &ChromaFingerprint) -> f64 {
        let n = a.frames.len().min(b.frames.len());
        if n == 0 {
            return 0.0;
        }
        let total: f64 = (0..n)
            .map(|i| Self::cosine_sim_12(&a.frames[i], &b.frames[i]))
            .sum();
        #[allow(clippy::cast_precision_loss)]
        let avg = total / n as f64;
        avg
    }

    /// Cosine similarity of two 12-dimensional vectors.
    fn cosine_sim_12(a: &[f64; 12], b: &[f64; 12]) -> f64 {
        let dot: f64 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let na: f64 = a.iter().map(|x| x * x).sum::<f64>().sqrt();
        let nb: f64 = b.iter().map(|x| x * x).sum::<f64>().sqrt();
        if na < 1e-12 || nb < 1e-12 {
            return 0.0;
        }
        dot / (na * nb)
    }
}

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

    fn make_fingerprint(value: f64, n_frames: usize) -> ChromaFingerprint {
        let frame = [value; 12];
        ChromaFingerprint::new(vec![frame; n_frames], n_frames as f64 * 0.1)
    }

    fn make_c_major_fingerprint(n_frames: usize) -> ChromaFingerprint {
        // C major chord: C=1, E=1, G=1, rest=0
        let mut frame = [0.0_f64; 12];
        frame[0] = 1.0; // C
        frame[4] = 1.0; // E
        frame[7] = 1.0; // G
        ChromaFingerprint::new(vec![frame; n_frames], n_frames as f64 * 0.1)
    }

    #[test]
    fn test_fingerprint_creation() {
        let fp = make_fingerprint(0.5, 10);
        assert_eq!(fp.len(), 10);
        assert!(!fp.is_empty());
    }

    #[test]
    fn test_fingerprint_empty() {
        let fp = ChromaFingerprint::new(vec![], 0.0);
        assert!(fp.is_empty());
    }

    #[test]
    fn test_fingerprint_transpose() {
        let fp = make_c_major_fingerprint(5);
        let transposed = fp.transposed(3); // Transpose up 3 semitones
                                           // C -> Eb, E -> G, G -> Bb
        assert!((transposed.frames[0][3] - 1.0).abs() < f64::EPSILON); // Eb
        assert!((transposed.frames[0][7] - 1.0).abs() < f64::EPSILON); // G
        assert!((transposed.frames[0][10] - 1.0).abs() < f64::EPSILON); // Bb
    }

    #[test]
    fn test_average_chroma() {
        let fp = make_fingerprint(0.5, 10);
        let avg = fp.average_chroma();
        for v in &avg {
            assert!((v - 0.5).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn test_cover_version_creation() {
        let fp = make_fingerprint(0.5, 10);
        let cv = CoverVersion::new("t1", "Song A", "Artist 1", fp, 0);
        assert_eq!(cv.id, "t1");
        assert_eq!(cv.estimated_key, 0);
    }

    #[test]
    fn test_database_operations() {
        let mut db = CoverDatabase::new();
        assert!(db.is_empty());
        let fp = make_fingerprint(0.5, 10);
        db.add(CoverVersion::new("t1", "Song A", "Artist 1", fp, 0));
        assert_eq!(db.len(), 1);
        assert!(db.get("t1").is_some());
        assert!(db.get("nonexistent").is_none());
    }

    #[test]
    fn test_detector_identical() {
        let det = CoverDetector::new(0.7);
        let fp = make_c_major_fingerprint(20);
        let (sim, _) = det.compare(&fp, &fp);
        assert!((sim - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_detector_transposed_cover() {
        let det = CoverDetector::new(0.7);
        let original = make_c_major_fingerprint(20);
        let transposed = original.transposed(5); // up a fourth
        let (sim, shift) = det.compare(&original, &transposed);
        // Should find high similarity at shift=7 (12-5)
        assert!(sim > 0.9, "similarity was {sim}");
        assert!(shift == 7 || shift == 0, "shift was {shift}");
    }

    #[test]
    fn test_detector_different() {
        let det = CoverDetector::new(0.7);
        let a = make_c_major_fingerprint(20);
        // D minor: D=1, F=1, A=1
        let mut frame = [0.0_f64; 12];
        frame[2] = 1.0;
        frame[5] = 1.0;
        frame[9] = 1.0;
        let b = ChromaFingerprint::new(vec![frame; 20], 2.0);
        let (sim, _) = det.compare(&a, &b);
        // Different chord: similarity should be lower
        assert!(sim < 1.0);
    }

    #[test]
    fn test_detect_in_database() {
        let det = CoverDetector::new(0.5);
        let mut db = CoverDatabase::new();
        let fp1 = make_c_major_fingerprint(20);
        db.add(CoverVersion::new(
            "t1",
            "Original",
            "Artist A",
            fp1.clone(),
            0,
        ));
        let fp2 = make_fingerprint(0.1, 20);
        db.add(CoverVersion::new("t2", "Other", "Artist B", fp2, 3));

        let query_fp = fp1.transposed(2);
        let query = CoverVersion::new("q1", "Cover", "Artist C", query_fp, 2);
        let results = det.detect(&query, &db);
        assert_eq!(results.len(), 2);
        // First result should match original
        assert_eq!(results[0].reference_id, "t1");
    }

    #[test]
    fn test_detection_result_fields() {
        let det = CoverDetector::new(0.5);
        let fp = make_c_major_fingerprint(10);
        let mut db = CoverDatabase::new();
        db.add(CoverVersion::new("ref", "Ref", "A", fp.clone(), 0));
        let query = CoverVersion::new("q", "Query", "B", fp, 0);
        let results = det.detect(&query, &db);
        assert_eq!(results.len(), 1);
        assert!(results[0].is_cover);
        assert!(results[0].similarity > 0.9);
    }

    #[test]
    fn test_empty_fingerprint_compare() {
        let det = CoverDetector::new(0.5);
        let a = ChromaFingerprint::new(vec![], 0.0);
        let b = make_c_major_fingerprint(10);
        let (sim, _) = det.compare(&a, &b);
        assert!((sim - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_average_chroma_empty() {
        let fp = ChromaFingerprint::new(vec![], 0.0);
        let avg = fp.average_chroma();
        for v in &avg {
            assert!((*v - 0.0).abs() < f64::EPSILON);
        }
    }
}