oximedia-mir 0.1.2

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
#![allow(dead_code)]

//! Harmonic-percussive source separation (HPSS) for music analysis.
//!
//! Separates an audio spectrogram into harmonic (tonal) and percussive
//! (transient) components using median filtering along time and frequency axes.

use std::cmp::Ordering;

/// Default kernel size for median filtering along the time axis (harmonic).
const DEFAULT_HARMONIC_KERNEL: usize = 31;

/// Default kernel size for median filtering along the frequency axis (percussive).
const DEFAULT_PERCUSSIVE_KERNEL: usize = 31;

/// Configuration for HPSS.
#[derive(Debug, Clone)]
pub struct HpssConfig {
    /// Kernel size for harmonic median filter (time axis), must be odd.
    pub harmonic_kernel: usize,
    /// Kernel size for percussive median filter (frequency axis), must be odd.
    pub percussive_kernel: usize,
    /// Power exponent for soft mask (higher = harder mask). Typical: 2.0.
    pub mask_power: f64,
    /// Margin for harmonic component (>= 1.0).
    pub harmonic_margin: f64,
    /// Margin for percussive component (>= 1.0).
    pub percussive_margin: f64,
}

impl Default for HpssConfig {
    fn default() -> Self {
        Self {
            harmonic_kernel: DEFAULT_HARMONIC_KERNEL,
            percussive_kernel: DEFAULT_PERCUSSIVE_KERNEL,
            mask_power: 2.0,
            harmonic_margin: 1.0,
            percussive_margin: 1.0,
        }
    }
}

impl HpssConfig {
    /// Ensure kernel sizes are odd (round up if even).
    #[must_use]
    pub fn validated(mut self) -> Self {
        if self.harmonic_kernel % 2 == 0 {
            self.harmonic_kernel += 1;
        }
        if self.percussive_kernel % 2 == 0 {
            self.percussive_kernel += 1;
        }
        self.harmonic_kernel = self.harmonic_kernel.max(3);
        self.percussive_kernel = self.percussive_kernel.max(3);
        self.mask_power = self.mask_power.max(0.1);
        self.harmonic_margin = self.harmonic_margin.max(1.0);
        self.percussive_margin = self.percussive_margin.max(1.0);
        self
    }
}

/// Result of harmonic-percussive separation.
#[derive(Debug, Clone)]
pub struct HpssResult {
    /// Harmonic component spectrogram (n_freq x n_time).
    pub harmonic: Vec<Vec<f64>>,
    /// Percussive component spectrogram (n_freq x n_time).
    pub percussive: Vec<Vec<f64>>,
    /// Number of frequency bins.
    pub n_freq: usize,
    /// Number of time frames.
    pub n_time: usize,
}

impl HpssResult {
    /// Compute the harmonic-to-percussive ratio per frame.
    #[must_use]
    pub fn hp_ratio(&self) -> Vec<f64> {
        let mut ratios = Vec::with_capacity(self.n_time);
        for t in 0..self.n_time {
            let h_energy: f64 = (0..self.n_freq).map(|f| self.harmonic[f][t]).sum();
            let p_energy: f64 = (0..self.n_freq).map(|f| self.percussive[f][t]).sum();
            let ratio = if p_energy > 1e-12 {
                h_energy / p_energy
            } else {
                0.0
            };
            ratios.push(ratio);
        }
        ratios
    }

    /// Compute the total harmonic energy.
    #[must_use]
    pub fn total_harmonic_energy(&self) -> f64 {
        self.harmonic
            .iter()
            .flat_map(|row| row.iter())
            .sum()
    }

    /// Compute the total percussive energy.
    #[must_use]
    pub fn total_percussive_energy(&self) -> f64 {
        self.percussive
            .iter()
            .flat_map(|row| row.iter())
            .sum()
    }
}

/// Harmonic-percussive source separator.
#[derive(Debug)]
pub struct HpssSeparator {
    config: HpssConfig,
}

impl HpssSeparator {
    /// Create a new separator with the given configuration.
    #[must_use]
    pub fn new(config: HpssConfig) -> Self {
        Self {
            config: config.validated(),
        }
    }

    /// Create a separator with default configuration.
    #[must_use]
    pub fn default_separator() -> Self {
        Self::new(HpssConfig::default())
    }

    /// Perform HPSS on a magnitude spectrogram.
    ///
    /// The spectrogram is stored as `spectrogram[freq_bin][time_frame]`.
    ///
    /// # Arguments
    ///
    /// * `spectrogram` - 2D magnitude spectrogram (n_freq x n_time)
    ///
    /// # Returns
    ///
    /// Separated harmonic and percussive spectrograms.
    #[must_use]
    pub fn separate(&self, spectrogram: &[Vec<f64>]) -> HpssResult {
        let n_freq = spectrogram.len();
        if n_freq == 0 {
            return HpssResult {
                harmonic: vec![],
                percussive: vec![],
                n_freq: 0,
                n_time: 0,
            };
        }
        let n_time = spectrogram[0].len();

        // Median filter along time axis -> harmonic enhanced
        let harmonic_enhanced = self.median_filter_time(spectrogram, n_freq, n_time);

        // Median filter along frequency axis -> percussive enhanced
        let percussive_enhanced = self.median_filter_freq(spectrogram, n_freq, n_time);

        // Compute soft masks
        let p = self.config.mask_power;
        let hm = self.config.harmonic_margin;
        let pm = self.config.percussive_margin;

        let mut harmonic = vec![vec![0.0; n_time]; n_freq];
        let mut percussive = vec![vec![0.0; n_time]; n_freq];

        for f in 0..n_freq {
            for t in 0..n_time {
                let h_val = (harmonic_enhanced[f][t] * hm).powf(p);
                let p_val = (percussive_enhanced[f][t] * pm).powf(p);
                let total = h_val + p_val;
                if total > 1e-12 {
                    let h_mask = h_val / total;
                    let p_mask = p_val / total;
                    harmonic[f][t] = spectrogram[f][t] * h_mask;
                    percussive[f][t] = spectrogram[f][t] * p_mask;
                }
            }
        }

        HpssResult {
            harmonic,
            percussive,
            n_freq,
            n_time,
        }
    }

    /// Median filter along the time axis for each frequency bin.
    fn median_filter_time(
        &self,
        spec: &[Vec<f64>],
        n_freq: usize,
        n_time: usize,
    ) -> Vec<Vec<f64>> {
        let half = self.config.harmonic_kernel / 2;
        let mut out = vec![vec![0.0; n_time]; n_freq];
        for f in 0..n_freq {
            for t in 0..n_time {
                let start = t.saturating_sub(half);
                let end = (t + half + 1).min(n_time);
                out[f][t] = median_of_slice(&spec[f][start..end]);
            }
        }
        out
    }

    /// Median filter along the frequency axis for each time frame.
    fn median_filter_freq(
        &self,
        spec: &[Vec<f64>],
        n_freq: usize,
        n_time: usize,
    ) -> Vec<Vec<f64>> {
        let half = self.config.percussive_kernel / 2;
        let mut out = vec![vec![0.0; n_time]; n_freq];
        for t in 0..n_time {
            for f in 0..n_freq {
                let start = f.saturating_sub(half);
                let end = (f + half + 1).min(n_freq);
                let col: Vec<f64> = (start..end).map(|fi| spec[fi][t]).collect();
                out[f][t] = median_of_slice(&col);
            }
        }
        out
    }
}

/// Compute the median of a slice of f64 values.
fn median_of_slice(data: &[f64]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    let mut sorted: Vec<f64> = data.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
    sorted[sorted.len() / 2]
}

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

    #[test]
    fn test_hpss_config_default() {
        let cfg = HpssConfig::default();
        assert_eq!(cfg.harmonic_kernel, 31);
        assert_eq!(cfg.percussive_kernel, 31);
        assert!((cfg.mask_power - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_hpss_config_validated_odd() {
        let cfg = HpssConfig {
            harmonic_kernel: 10,
            percussive_kernel: 8,
            ..HpssConfig::default()
        }
        .validated();
        assert_eq!(cfg.harmonic_kernel % 2, 1);
        assert_eq!(cfg.percussive_kernel % 2, 1);
    }

    #[test]
    fn test_hpss_config_validated_minimum() {
        let cfg = HpssConfig {
            harmonic_kernel: 1,
            percussive_kernel: 1,
            mask_power: -5.0,
            ..HpssConfig::default()
        }
        .validated();
        assert!(cfg.harmonic_kernel >= 3);
        assert!(cfg.percussive_kernel >= 3);
        assert!(cfg.mask_power >= 0.1);
    }

    #[test]
    fn test_median_of_slice() {
        assert!((median_of_slice(&[1.0, 3.0, 2.0]) - 2.0).abs() < f64::EPSILON);
        assert!((median_of_slice(&[5.0]) - 5.0).abs() < f64::EPSILON);
        assert!((median_of_slice(&[]) - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_separate_empty() {
        let sep = HpssSeparator::default_separator();
        let result = sep.separate(&[]);
        assert_eq!(result.n_freq, 0);
        assert_eq!(result.n_time, 0);
    }

    #[test]
    fn test_separate_uniform() {
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 3,
            percussive_kernel: 3,
            mask_power: 2.0,
            harmonic_margin: 1.0,
            percussive_margin: 1.0,
        });
        // 4 freq bins x 8 time frames, all value 1.0
        let spec = vec![vec![1.0; 8]; 4];
        let result = sep.separate(&spec);
        assert_eq!(result.n_freq, 4);
        assert_eq!(result.n_time, 8);
        // For a uniform spectrogram, harmonic + percussive should roughly equal original
        for f in 0..4 {
            for t in 0..8 {
                let total = result.harmonic[f][t] + result.percussive[f][t];
                assert!((total - 1.0).abs() < 0.01);
            }
        }
    }

    #[test]
    fn test_hp_ratio_uniform() {
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 3,
            percussive_kernel: 3,
            ..HpssConfig::default()
        });
        let spec = vec![vec![1.0; 8]; 4];
        let result = sep.separate(&spec);
        let ratios = result.hp_ratio();
        assert_eq!(ratios.len(), 8);
        // For uniform input, ratio should be close to 1.0
        for &r in &ratios {
            assert!((r - 1.0).abs() < 0.5);
        }
    }

    #[test]
    fn test_total_energy_conservation() {
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 3,
            percussive_kernel: 3,
            ..HpssConfig::default()
        });
        let spec = vec![vec![2.0; 4]; 3];
        let result = sep.separate(&spec);
        let original_energy: f64 = spec.iter().flat_map(|r| r.iter()).sum();
        let separated_energy =
            result.total_harmonic_energy() + result.total_percussive_energy();
        assert!((original_energy - separated_energy).abs() < 0.5);
    }

    #[test]
    fn test_harmonic_dominance_horizontal_stripes() {
        // Horizontal stripe pattern = constant across time = harmonic
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 5,
            percussive_kernel: 5,
            ..HpssConfig::default()
        });
        let mut spec = vec![vec![0.0; 10]; 8];
        // Set frequency bin 3 to be strong across all time
        for t in 0..10 {
            spec[3][t] = 10.0;
        }
        let result = sep.separate(&spec);
        // Harmonic component at bin 3 should dominate
        let h_energy: f64 = (0..10).map(|t| result.harmonic[3][t]).sum();
        let p_energy: f64 = (0..10).map(|t| result.percussive[3][t]).sum();
        assert!(h_energy > p_energy);
    }

    #[test]
    fn test_percussive_dominance_vertical_stripes() {
        // Vertical stripe pattern = constant across freq = percussive
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 5,
            percussive_kernel: 5,
            ..HpssConfig::default()
        });
        let mut spec = vec![vec![0.0; 10]; 8];
        // Set time frame 5 to be strong across all frequencies
        for f in 0..8 {
            spec[f][5] = 10.0;
        }
        let result = sep.separate(&spec);
        let h_energy: f64 = (0..8).map(|f| result.harmonic[f][5]).sum();
        let p_energy: f64 = (0..8).map(|f| result.percussive[f][5]).sum();
        assert!(p_energy > h_energy);
    }

    #[test]
    fn test_single_bin_single_frame() {
        let sep = HpssSeparator::new(HpssConfig {
            harmonic_kernel: 3,
            percussive_kernel: 3,
            ..HpssConfig::default()
        });
        let spec = vec![vec![5.0]];
        let result = sep.separate(&spec);
        assert_eq!(result.n_freq, 1);
        assert_eq!(result.n_time, 1);
        let total = result.harmonic[0][0] + result.percussive[0][0];
        assert!((total - 5.0).abs() < 0.1);
    }

    #[test]
    fn test_zero_spectrogram() {
        let sep = HpssSeparator::default_separator();
        let spec = vec![vec![0.0; 5]; 3];
        let result = sep.separate(&spec);
        assert!((result.total_harmonic_energy() - 0.0).abs() < f64::EPSILON);
        assert!((result.total_percussive_energy() - 0.0).abs() < f64::EPSILON);
    }
}