oximedia-audio 0.1.3

Audio codec implementations 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
//! Fingerprint matching algorithm.

use super::hash::Hash;
use super::Fingerprint;
use std::collections::HashMap;

/// Fingerprint matcher.
pub struct FingerprintMatcher {
    /// Minimum number of matching hashes required.
    min_matches: usize,
    /// Time tolerance for offset alignment (seconds).
    time_tolerance: f64,
    /// Enable partial matching.
    allow_partial: bool,
}

impl FingerprintMatcher {
    /// Create a new fingerprint matcher.
    #[must_use]
    pub const fn new(min_matches: usize, time_tolerance: f64, allow_partial: bool) -> Self {
        Self {
            min_matches,
            time_tolerance,
            allow_partial,
        }
    }

    /// Match a query fingerprint against a reference.
    #[must_use]
    pub fn match_fingerprint(
        &self,
        query: &Fingerprint,
        reference: &Fingerprint,
    ) -> Option<MatchResult> {
        // Build hash lookup table for reference
        let mut hash_table: HashMap<Hash, Vec<f64>> = HashMap::new();
        for (hash, time) in &reference.hashes {
            hash_table.entry(*hash).or_default().push(*time);
        }

        // Find matching hashes and compute time offsets
        let mut offset_histogram: HashMap<i64, Vec<(Hash, f64, f64)>> = HashMap::new();

        for (query_hash, query_time) in &query.hashes {
            if let Some(ref_times) = hash_table.get(query_hash) {
                for &ref_time in ref_times {
                    // Compute time offset (quantized to 10ms bins)
                    let offset = ((ref_time - query_time) * 100.0).round() as i64;

                    offset_histogram.entry(offset).or_default().push((
                        *query_hash,
                        *query_time,
                        ref_time,
                    ));
                }
            }
        }

        // Find the offset with most matches
        let (best_offset, matches) = offset_histogram
            .iter()
            .max_by_key(|(_, matches)| matches.len())?;

        let match_count = matches.len();

        // Check minimum match threshold
        if match_count < self.min_matches {
            if !self.allow_partial {
                return None;
            }
        }

        // Calculate confidence
        let confidence = self.calculate_confidence(match_count, query, reference);

        // Convert offset back to seconds
        let time_offset = *best_offset as f64 / 100.0;

        Some(MatchResult {
            match_count,
            total_query_hashes: query.hashes.len(),
            total_reference_hashes: reference.hashes.len(),
            confidence,
            time_offset,
            matches: matches.clone(),
        })
    }

    /// Match against multiple reference fingerprints.
    #[must_use]
    pub fn match_multiple<'a>(
        &self,
        query: &Fingerprint,
        references: &'a [(String, Fingerprint)],
    ) -> Vec<(&'a str, MatchResult)> {
        let mut results = Vec::new();

        for (id, reference) in references {
            if let Some(result) = self.match_fingerprint(query, reference) {
                results.push((id.as_str(), result));
            }
        }

        // Sort by confidence
        results.sort_by(|a, b| {
            b.1.confidence
                .partial_cmp(&a.1.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        results
    }

    /// Calculate match confidence (0-1).
    fn calculate_confidence(
        &self,
        match_count: usize,
        query: &Fingerprint,
        reference: &Fingerprint,
    ) -> f64 {
        if query.hashes.is_empty() || reference.hashes.is_empty() {
            return 0.0;
        }

        // Jaccard similarity
        let jaccard =
            match_count as f64 / (query.hashes.len() + reference.hashes.len() - match_count) as f64;

        // Match rate relative to query
        let query_coverage = match_count as f64 / query.hashes.len() as f64;

        // Combined confidence
        (jaccard * 0.5 + query_coverage * 0.5).min(1.0)
    }

    /// Verify match using temporal consistency.
    #[must_use]
    pub fn verify_match(&self, result: &MatchResult) -> bool {
        if result.matches.len() < self.min_matches {
            return false;
        }

        // Check temporal consistency of matches
        let mut time_diffs = Vec::new();
        for i in 1..result.matches.len() {
            let (_, q1, r1) = result.matches[i - 1];
            let (_, q2, r2) = result.matches[i];

            let query_diff = q2 - q1;
            let ref_diff = r2 - r1;
            let diff_error = (query_diff - ref_diff).abs();

            time_diffs.push(diff_error);
        }

        if time_diffs.is_empty() {
            return true;
        }

        // Check if most time differences are within tolerance
        let consistent_count = time_diffs
            .iter()
            .filter(|&&d| d <= self.time_tolerance)
            .count();

        let consistency_ratio = consistent_count as f64 / time_diffs.len() as f64;

        consistency_ratio >= 0.8
    }

    /// Find all matches above confidence threshold.
    #[must_use]
    pub fn find_matches<'a>(
        &self,
        query: &Fingerprint,
        references: &'a [(String, Fingerprint)],
        min_confidence: f64,
    ) -> Vec<(&'a str, MatchResult)> {
        self.match_multiple(query, references)
            .into_iter()
            .filter(|(_, result)| result.confidence >= min_confidence)
            .filter(|(_, result)| self.verify_match(result))
            .collect()
    }
}

impl Default for FingerprintMatcher {
    fn default() -> Self {
        Self::new(10, 0.1, false)
    }
}

/// Result of a fingerprint match.
#[derive(Clone, Debug)]
pub struct MatchResult {
    /// Number of matching hashes.
    pub match_count: usize,
    /// Total hashes in query fingerprint.
    pub total_query_hashes: usize,
    /// Total hashes in reference fingerprint.
    pub total_reference_hashes: usize,
    /// Confidence score (0-1).
    pub confidence: f64,
    /// Time offset (seconds, reference - query).
    pub time_offset: f64,
    /// Individual matches (hash, query_time, ref_time).
    pub matches: Vec<(Hash, f64, f64)>,
}

impl MatchResult {
    /// Get match coverage relative to query.
    #[must_use]
    pub fn query_coverage(&self) -> f64 {
        if self.total_query_hashes > 0 {
            self.match_count as f64 / self.total_query_hashes as f64
        } else {
            0.0
        }
    }

    /// Get match coverage relative to reference.
    #[must_use]
    pub fn reference_coverage(&self) -> f64 {
        if self.total_reference_hashes > 0 {
            self.match_count as f64 / self.total_reference_hashes as f64
        } else {
            0.0
        }
    }

    /// Check if match is strong (high confidence and coverage).
    #[must_use]
    pub fn is_strong_match(&self) -> bool {
        self.confidence >= 0.7 && self.query_coverage() >= 0.5
    }

    /// Check if match is likely a duplicate.
    #[must_use]
    pub fn is_likely_duplicate(&self) -> bool {
        self.confidence >= 0.9 && self.query_coverage() >= 0.8
    }

    /// Get temporal spread of matches.
    #[must_use]
    pub fn temporal_spread(&self) -> f64 {
        if self.matches.len() < 2 {
            return 0.0;
        }

        let query_times: Vec<f64> = self.matches.iter().map(|(_, q, _)| *q).collect();

        let min_time = query_times
            .iter()
            .copied()
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(0.0);

        let max_time = query_times
            .iter()
            .copied()
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(0.0);

        max_time - min_time
    }

    /// Get match density (matches per second).
    #[must_use]
    pub fn match_density(&self) -> f64 {
        let spread = self.temporal_spread();
        if spread > 0.0 {
            self.match_count as f64 / spread
        } else {
            0.0
        }
    }
}

/// Advanced matching strategies.
pub struct AdvancedMatcher;

impl AdvancedMatcher {
    /// Sliding window matching for long audio.
    #[must_use]
    #[allow(
        clippy::cast_precision_loss,
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        clippy::similar_names
    )]
    pub fn sliding_window_match(
        query: &Fingerprint,
        reference: &Fingerprint,
        window_size: f64,
        hop_size: f64,
    ) -> Vec<WindowMatch> {
        let mut matches = Vec::new();
        let matcher = FingerprintMatcher::default();

        let mut window_start = 0.0;
        while window_start + window_size <= query.duration {
            // Extract window from query
            let window_hashes: Vec<(Hash, f64)> = query
                .hashes
                .iter()
                .filter(|(_, t)| *t >= window_start && *t < window_start + window_size)
                .map(|(h, t)| (*h, *t - window_start))
                .collect();

            if !window_hashes.is_empty() {
                let window_fp = Fingerprint::new(
                    window_hashes,
                    query.sample_rate,
                    window_size,
                    query.config.clone(),
                );

                if let Some(result) = matcher.match_fingerprint(&window_fp, reference) {
                    matches.push(WindowMatch {
                        window_start,
                        window_end: window_start + window_size,
                        result,
                    });
                }
            }

            window_start += hop_size;
        }

        matches
    }

    /// Multi-scale matching (different time resolutions).
    #[must_use]
    #[allow(clippy::similar_names)]
    pub fn multiscale_match(
        query: &Fingerprint,
        reference: &Fingerprint,
        scales: &[f64],
    ) -> Vec<ScaleMatch> {
        let matcher = FingerprintMatcher::default();
        let mut matches = Vec::new();

        for &scale in scales {
            if let Some(result) = matcher.match_fingerprint(query, reference) {
                matches.push(ScaleMatch { scale, result });
            }
        }

        matches.sort_by(|a, b| {
            b.result
                .confidence
                .partial_cmp(&a.result.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        matches
    }

    /// Fuzzy matching with tolerance for variations.
    #[must_use]
    pub fn fuzzy_match(
        query: &Fingerprint,
        reference: &Fingerprint,
        hash_tolerance: u32,
    ) -> Option<MatchResult> {
        let mut offset_histogram: HashMap<i64, Vec<(Hash, f64, f64)>> = HashMap::new();

        // Build hash table with fuzzy matching
        let mut hash_table: HashMap<Hash, Vec<f64>> = HashMap::new();
        for (hash, time) in &reference.hashes {
            hash_table.entry(*hash).or_default().push(*time);
        }

        // Match with tolerance
        for (query_hash, query_time) in &query.hashes {
            // Exact match
            if let Some(ref_times) = hash_table.get(query_hash) {
                for &ref_time in ref_times {
                    let offset = ((ref_time - query_time) * 100.0).round() as i64;
                    offset_histogram.entry(offset).or_default().push((
                        *query_hash,
                        *query_time,
                        ref_time,
                    ));
                }
            }

            // Fuzzy match (check similar hashes)
            if hash_tolerance > 0 {
                for (ref_hash, ref_times) in &hash_table {
                    if super::hash::HashComparison::are_similar(
                        *query_hash,
                        *ref_hash,
                        hash_tolerance,
                    ) {
                        for &ref_time in ref_times {
                            let offset = ((ref_time - query_time) * 100.0).round() as i64;
                            offset_histogram.entry(offset).or_default().push((
                                *query_hash,
                                *query_time,
                                ref_time,
                            ));
                        }
                    }
                }
            }
        }

        // Find best offset
        let (best_offset, matches) = offset_histogram
            .iter()
            .max_by_key(|(_, matches)| matches.len())?;

        let match_count = matches.len();
        let confidence = if query.hashes.is_empty() || reference.hashes.is_empty() {
            0.0
        } else {
            let jaccard = match_count as f64
                / (query.hashes.len() + reference.hashes.len() - match_count) as f64;
            let coverage = match_count as f64 / query.hashes.len() as f64;
            (jaccard * 0.5 + coverage * 0.5).min(1.0)
        };

        Some(MatchResult {
            match_count,
            total_query_hashes: query.hashes.len(),
            total_reference_hashes: reference.hashes.len(),
            confidence,
            time_offset: *best_offset as f64 / 100.0,
            matches: matches.clone(),
        })
    }
}

/// Window match result.
#[derive(Clone, Debug)]
pub struct WindowMatch {
    /// Window start time (seconds).
    pub window_start: f64,
    /// Window end time (seconds).
    pub window_end: f64,
    /// Match result.
    pub result: MatchResult,
}

/// Scale match result.
#[derive(Clone, Debug)]
pub struct ScaleMatch {
    /// Time scale factor.
    pub scale: f64,
    /// Match result.
    pub result: MatchResult,
}