1extern crate noisy_float;
7
8use crate::Feature;
9
10use super::errors::{AnalysisError, AnalysisResult};
11use super::utils::{Normalize, hz_to_octs_inplace, stft};
12use ndarray::{Array, Array1, Array2, Axis, Order, Zip, arr1, arr2, concatenate, s};
13use ndarray_stats::QuantileExt;
14use ndarray_stats::interpolate::Midpoint;
15use noisy_float::prelude::*;
16
17#[derive(Debug, Clone)]
28#[allow(clippy::module_name_repetitions)]
29pub struct ChromaDesc {
30 sample_rate: u32,
31 n_chroma: u32,
32 values_chroma: Array2<f64>,
33}
34
35impl Normalize for ChromaDesc {
36 const MAX_VALUE: Feature = 0.12;
37 const MIN_VALUE: Feature = 0.;
38}
39
40impl ChromaDesc {
41 pub const WINDOW_SIZE: usize = 8192;
42
43 #[must_use]
44 #[inline]
45 pub fn new(sample_rate: u32, n_chroma: u32) -> Self {
46 Self {
47 sample_rate,
48 n_chroma,
49 values_chroma: Array2::zeros((n_chroma as usize, 0)),
50 }
51 }
52
53 #[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
60 #[inline]
61 pub fn do_(&mut self, signal: &[f32]) -> AnalysisResult<()> {
62 let mut stft = stft(signal, Self::WINDOW_SIZE, 2205);
63 let tuning = estimate_tuning(self.sample_rate, &stft, Self::WINDOW_SIZE, 0.01, 12)?;
64 let chroma = chroma_stft(
65 self.sample_rate,
66 &mut stft,
67 Self::WINDOW_SIZE,
68 self.n_chroma,
69 tuning,
70 )?;
71 self.values_chroma = concatenate![Axis(1), self.values_chroma, chroma];
72 Ok(())
73 }
74
75 #[inline]
86 pub fn get_value(&mut self) -> Vec<Feature> {
87 #[allow(clippy::cast_possible_truncation)]
88 chroma_interval_features(&self.values_chroma)
89 .mapv(|x| self.normalize(x as Feature))
90 .to_vec()
91 }
92}
93
94#[allow(
97 clippy::missing_errors_doc,
98 clippy::missing_panics_doc,
99 clippy::module_name_repetitions
100)]
101#[must_use]
102#[inline]
103pub fn chroma_interval_features(chroma: &Array2<f64>) -> Array1<f64> {
104 let chroma = normalize_feature_sequence(&chroma.mapv(|x| (x * 15.).exp()));
105 let templates = arr2(&[
106 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
107 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
108 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
109 [0, 0, 1, 0, 0, 0, 0, 1, 1, 0],
110 [0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
111 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
112 [0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
113 [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
114 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
115 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
116 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
117 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
118 ]);
119 let interval_feature_matrix = extract_interval_features(&chroma, &templates);
120 interval_feature_matrix.mean_axis(Axis(1)).unwrap()
121}
122
123#[must_use]
124#[inline]
125pub fn extract_interval_features(chroma: &Array2<f64>, templates: &Array2<i32>) -> Array2<f64> {
126 let mut f_intervals: Array2<f64> = Array::zeros((chroma.shape()[1], templates.shape()[1]));
127 for (template, mut f_interval) in templates
128 .axis_iter(Axis(1))
129 .zip(f_intervals.axis_iter_mut(Axis(1)))
130 {
131 for shift in 0..12 {
132 let mut vec: Vec<i32> = template.to_vec();
133 vec.rotate_right(shift);
134 let rolled = arr1(&vec);
135 let power = Zip::from(chroma.t())
136 .and_broadcast(&rolled)
137 .map_collect(|&f, &s| f.powi(s))
138 .map_axis_mut(Axis(1), |x| x.product());
139 f_interval += &power;
140 }
141 }
142 f_intervals.t().to_owned()
143}
144
145#[inline]
146pub fn normalize_feature_sequence(feature: &Array2<f64>) -> Array2<f64> {
147 let mut normalized_sequence = feature.to_owned();
148 for mut column in normalized_sequence.columns_mut() {
149 let sum: f64 = column.iter().copied().map(f64::abs).sum();
150 if sum >= 0.0001 {
151 column /= sum;
152 }
153 }
154
155 normalized_sequence
156}
157
158#[allow(
166 clippy::missing_errors_doc,
167 clippy::missing_panics_doc,
168 clippy::module_name_repetitions,
169 clippy::missing_inline_in_public_items
170)]
171pub fn chroma_filter(
172 sample_rate: u32,
173 n_fft: usize,
174 n_chroma: u32,
175 tuning: f64,
176) -> AnalysisResult<Array2<f64>> {
177 let ctroct = 5.0;
178 let octwidth = 2.;
179 let n_chroma_float = f64::from(n_chroma);
180 let n_chroma2 = (n_chroma_float / 2.0).round();
181
182 let frequencies = Array::linspace(0., f64::from(sample_rate), n_fft + 1);
183
184 let mut freq_bins = frequencies;
185 hz_to_octs_inplace(&mut freq_bins, tuning, n_chroma);
186 freq_bins.mapv_inplace(|x| x * n_chroma_float);
187 freq_bins[0] = 1.5f64.mul_add(-n_chroma_float, freq_bins[1]);
188
189 let mut binwidth_bins = Array::ones(freq_bins.raw_dim());
190 binwidth_bins.slice_mut(s![0..freq_bins.len() - 1]).assign(
191 &(&freq_bins.slice(s![1..]) - &freq_bins.slice(s![..-1]))
192 .mapv(|x| if x <= 1. { 1. } else { x }),
193 );
194
195 let mut d: Array2<f64> = Array::zeros((n_chroma as usize, (freq_bins).len()));
196 for (idx, mut row) in d.rows_mut().into_iter().enumerate() {
197 #[allow(clippy::cast_precision_loss)]
198 row.fill(idx as f64);
199 }
200 d = -d + &freq_bins;
201
202 d.mapv_inplace(|x| 10f64.mul_add(n_chroma_float, x + n_chroma2) % n_chroma_float - n_chroma2);
203 d = d / binwidth_bins;
204 d.mapv_inplace(|x| (-0.5 * (2. * x) * (2. * x)).exp());
205
206 let mut wts = d;
207 for mut col in wts.columns_mut() {
209 let mut sum = col.mapv(|x| x * x).sum().sqrt();
210 if sum < f64::MIN_POSITIVE {
211 sum = 1.;
212 }
213 col /= sum;
214 }
215
216 freq_bins.mapv_inplace(|x| (-0.5 * ((x / n_chroma_float - ctroct) / octwidth).powi(2)).exp());
217
218 wts *= &freq_bins;
219
220 let mut b = Array2::zeros(wts.dim());
222 b.slice_mut(s![-3.., ..]).assign(&wts.slice(s![..3, ..]));
223 b.slice_mut(s![..-3, ..]).assign(&wts.slice(s![3.., ..]));
224
225 wts = b;
226 let non_aliased = 1 + n_fft / 2;
227 Ok(wts.slice_move(s![.., ..non_aliased]))
228}
229
230#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
231#[allow(clippy::missing_inline_in_public_items)]
232pub fn pip_track(
233 sample_rate: u32,
234 spectrum: &Array2<f64>,
235 n_fft: usize,
236) -> AnalysisResult<(Vec<f64>, Vec<f64>)> {
237 let sample_rate_float = f64::from(sample_rate);
238 let fmin = 150.0_f64;
239 let fmax = 4000.0_f64.min(sample_rate_float / 2.0);
240 let threshold = 0.1;
241
242 let fft_freqs = Array::linspace(0., sample_rate_float / 2., 1 + n_fft / 2);
243
244 let length = spectrum.len_of(Axis(0));
245
246 let freq_mask = fft_freqs
248 .iter()
249 .map(|&f| (fmin <= f) && (f < fmax))
250 .collect::<Vec<bool>>();
251
252 let ref_value = spectrum.map_axis(Axis(0), |x| {
253 let first: f64 = *x.first().expect("empty spectrum axis");
254 let max = x.fold(first, |acc, &elem| acc.max(elem));
255 threshold * max
256 });
257
258 let taken_columns = freq_mask
260 .iter()
261 .fold(0, |acc, &x| if x { acc + 1 } else { acc });
262 let mut pitches = Vec::with_capacity(taken_columns * length);
263 let mut mags = Vec::with_capacity(taken_columns * length);
264
265 let beginning = freq_mask
266 .iter()
267 .position(|&b| b)
268 .ok_or_else(|| AnalysisError::AnalysisError(String::from("in chroma")))?;
269 let end = freq_mask
270 .iter()
271 .rposition(|&b| b)
272 .ok_or_else(|| AnalysisError::AnalysisError(String::from("in chroma")))?;
273
274 let zipped = Zip::indexed(spectrum.slice(s![beginning..end - 3, ..]))
275 .and(spectrum.slice(s![beginning + 1..end - 2, ..]))
276 .and(spectrum.slice(s![beginning + 2..end - 1, ..]));
277
278 zipped.for_each(|(i, j), &before_elem, &elem, &after_elem| {
281 if elem > ref_value[j] && after_elem <= elem && before_elem < elem {
282 let avg = 0.5 * (after_elem - before_elem);
283 let mut shift = 2f64.mul_add(elem, -after_elem) - before_elem;
284 if shift.abs() < f64::MIN_POSITIVE {
285 shift += 1.;
286 }
287 shift = avg / shift;
288 #[allow(clippy::cast_precision_loss)]
289 pitches.push(((i + beginning + 1) as f64 + shift) * sample_rate_float / n_fft as f64);
290 mags.push((0.5 * avg).mul_add(shift, elem));
291 }
292 });
293
294 Ok((pitches, mags))
295}
296
297#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
299#[inline]
300pub fn pitch_tuning(
301 frequencies: &mut Array1<f64>,
302 resolution: f64,
303 bins_per_octave: u32,
304) -> AnalysisResult<f64> {
305 if frequencies.is_empty() {
306 return Ok(0.0);
307 }
308 hz_to_octs_inplace(frequencies, 0.0, 12);
309 frequencies.mapv_inplace(|x| f64::from(bins_per_octave) * x % 1.0);
310
311 frequencies.mapv_inplace(|x| if x >= 0.5 { x - 1. } else { x });
313
314 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
315 let indexes = ((frequencies.to_owned() - -0.5) / resolution).mapv(|x| x as usize);
316 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
317 let mut counts: Array1<usize> = Array::zeros(((0.5 - -0.5) / resolution) as usize);
318 for &idx in &indexes {
319 counts[idx] += 1;
320 }
321 let max_index = counts
322 .argmax()
323 .map_err(|e| AnalysisError::AnalysisError(format!("in chroma: {e}")))?;
324
325 #[allow(clippy::cast_precision_loss)]
327 Ok((100. * resolution).mul_add(max_index as f64, -50.) / 100.)
328}
329
330#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
331#[inline]
332pub fn estimate_tuning(
333 sample_rate: u32,
334 spectrum: &Array2<f64>,
335 n_fft: usize,
336 resolution: f64,
337 bins_per_octave: u32,
338) -> AnalysisResult<f64> {
339 let (pitch, mag) = pip_track(sample_rate, spectrum, n_fft)?;
340
341 let (filtered_pitch, filtered_mag): (Vec<N64>, Vec<N64>) = pitch
342 .iter()
343 .zip(&mag)
344 .filter(|&(&p, _)| p > 0.)
345 .map(|(x, y)| (n64(*x), n64(*y)))
346 .unzip();
347
348 if pitch.is_empty() {
349 return Ok(0.);
350 }
351
352 let threshold: N64 = Array::from(filtered_mag.clone())
353 .quantile_axis_mut(Axis(0), n64(0.5), &Midpoint)
354 .map_err(|e| AnalysisError::AnalysisError(format!("in chroma: {e}")))?
355 .into_scalar();
356 let mut pitch = filtered_pitch
357 .iter()
358 .zip(&filtered_mag)
359 .filter_map(|(&p, &m)| if m >= threshold { Some(p.into()) } else { None })
360 .collect::<Array1<f64>>();
361 pitch_tuning(&mut pitch, resolution, bins_per_octave)
362}
363
364#[allow(
365 clippy::missing_errors_doc,
366 clippy::missing_panics_doc,
367 clippy::module_name_repetitions
368)]
369#[inline]
370pub fn chroma_stft(
371 sample_rate: u32,
372 spectrum: &mut Array2<f64>, n_fft: usize,
374 n_chroma: u32,
375 tuning: f64,
376) -> AnalysisResult<Array2<f64>> {
377 spectrum.mapv_inplace(|x| x * x);
378 let mut raw_chroma = chroma_filter(sample_rate, n_fft, n_chroma, tuning)?;
379
380 raw_chroma = raw_chroma.dot(spectrum);
381
382 raw_chroma = raw_chroma
385 .to_shape((raw_chroma.dim(), Order::ColumnMajor))
386 .map_err(|_| {
387 AnalysisError::AnalysisError(String::from("in chroma: failed to reorder array"))
388 })?
389 .to_owned();
390
391 for mut row in raw_chroma.columns_mut() {
392 let sum = row.sum(); if sum >= f64::MIN_POSITIVE {
394 row /= sum;
395 }
396 }
397
398 Ok(raw_chroma)
399}
400
401#[cfg(test)]
402mod test {
403 use super::*;
404 use crate::{
405 SAMPLE_RATE,
406 decoder::{Decoder as _, MecompDecoder as Decoder},
407 utils::stft,
408 };
409 use ndarray::{Array2, arr1, arr2};
410 use ndarray_npy::ReadNpyExt as _;
411 use std::{fs::File, path::Path};
412
413 #[test]
414 fn test_chroma_interval_features() {
415 let file = File::open("data/chroma.npy").unwrap();
416 let chroma = Array2::<f64>::read_npy(file).unwrap();
417 let features = chroma_interval_features(&chroma);
418 let expected_features = arr1(&[
419 0.038_602_84,
420 0.021_852_81,
421 0.042_243_79,
422 0.063_852_78,
423 0.073_111_48,
424 0.025_125_66,
425 0.003_198_99,
426 0.003_113_08,
427 0.001_074_33,
428 0.002_418_61,
429 ]);
430 for (expected, actual) in expected_features.iter().zip(&features) {
431 assert!(
432 0.000_000_01 > (expected - actual.abs()),
433 "{expected} !~= {actual}"
434 );
435 }
436 }
437
438 #[test]
439 fn test_extract_interval_features() {
440 let file = File::open("data/chroma-interval.npy").unwrap();
441 let chroma = Array2::<f64>::read_npy(file).unwrap();
442 let templates = arr2(&[
443 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
444 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
445 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
446 [0, 0, 1, 0, 0, 0, 0, 1, 1, 0],
447 [0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
448 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
449 [0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
450 [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
451 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
452 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
453 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
454 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
455 ]);
456
457 let file = File::open("data/interval-feature-matrix.npy").unwrap();
458 let expected_interval_features = Array2::<f64>::read_npy(file).unwrap();
459
460 let interval_features = extract_interval_features(&chroma, &templates);
461 for (expected, actual) in expected_interval_features
462 .iter()
463 .zip(interval_features.iter())
464 {
465 assert!(
466 0.000_000_1 > (expected - actual).abs(),
467 "{expected} !~= {actual}"
468 );
469 }
470 }
471
472 #[test]
473 fn test_normalize_feature_sequence() {
474 let array = arr2(&[[0.1, 0.3, 0.4], [1.1, 0.53, 1.01]]);
475 let expected_array = arr2(&[
476 [0.083_333_33, 0.361_445_78, 0.283_687_94],
477 [0.916_666_67, 0.638_554_22, 0.716_312_06],
478 ]);
479
480 let normalized_array = normalize_feature_sequence(&array);
481
482 assert!(!array.is_empty() && !expected_array.is_empty());
483
484 for (expected, actual) in normalized_array.iter().zip(expected_array.iter()) {
485 assert!(
486 0.000_000_1 > (expected - actual).abs(),
487 "{expected} !~= {actual}"
488 );
489 }
490 }
491
492 #[test]
493 fn test_chroma_desc() {
494 let song = Decoder::new()
495 .unwrap()
496 .decode(Path::new("data/s16_mono_22_5kHz.flac"))
497 .unwrap();
498 let mut chroma_desc = ChromaDesc::new(SAMPLE_RATE, 12);
499 chroma_desc.do_(&song.samples).unwrap();
500 let expected_values = [
501 -0.356_619_36,
502 -0.635_786_53,
503 -0.295_936_82,
504 0.064_213_04,
505 0.218_524_58,
506 -0.581_239,
507 -0.946_683_5,
508 -0.948_115_3,
509 -0.982_094_5,
510 -0.959_689_74,
511 ];
512 for (expected, actual) in expected_values.iter().zip(chroma_desc.get_value().iter()) {
513 let relative_error = (expected - actual).abs() / expected.abs();
515 assert!(
516 relative_error < 0.01,
517 "relative error: {relative_error}, expected: {expected}, actual: {actual}"
518 );
519 }
520 }
521
522 #[test]
523 fn test_chroma_stft_decode() {
524 let signal = Decoder::new()
525 .unwrap()
526 .decode(Path::new("data/s16_mono_22_5kHz.flac"))
527 .unwrap()
528 .samples;
529 let mut stft = stft(&signal, 8192, 2205);
530
531 let file = File::open("data/chroma.npy").unwrap();
532 let expected_chroma = Array2::<f64>::read_npy(file).unwrap();
533
534 let chroma = chroma_stft(22050, &mut stft, 8192, 12, -0.049_999_999_999_999_99).unwrap();
535
536 assert!(!chroma.is_empty() && !expected_chroma.is_empty());
537
538 for (expected, actual) in expected_chroma.iter().zip(chroma.iter()) {
539 let relative_error = (expected - actual).abs() / expected.abs();
541 assert!(
542 relative_error < 0.01,
543 "relative error: {relative_error}, expected: {expected}, actual: {actual}"
544 );
545 }
546 }
547
548 #[test]
549 fn test_estimate_tuning() {
550 let file = File::open("data/spectrum-chroma.npy").unwrap();
551 let arr = Array2::<f64>::read_npy(file).unwrap();
552
553 let tuning = estimate_tuning(22050, &arr, 2048, 0.01, 12).unwrap();
554 assert!(
555 0.000_001 > (-0.099_999_999_999_999_98 - tuning).abs(),
556 "{tuning} !~= -0.09999999999999998"
557 );
558 }
559
560 #[test]
561 fn test_chroma_estimate_tuning_empty_fix() {
562 assert!(0. == estimate_tuning(22050, &Array2::zeros((8192, 1)), 8192, 0.01, 12).unwrap());
563 }
564
565 #[test]
566 fn test_estimate_tuning_decode() {
567 let signal = Decoder::new()
568 .unwrap()
569 .decode(Path::new("data/s16_mono_22_5kHz.flac"))
570 .unwrap()
571 .samples;
572 let stft = stft(&signal, 8192, 2205);
573
574 let tuning = estimate_tuning(22050, &stft, 8192, 0.01, 12).unwrap();
575 assert!(
576 0.000_001 > (-0.049_999_999_999_999_99 - tuning).abs(),
577 "{tuning} !~= -0.04999999999999999"
578 );
579 }
580
581 #[test]
582 fn test_pitch_tuning() {
583 let file = File::open("data/pitch-tuning.npy").unwrap();
584 let mut pitch = Array1::<f64>::read_npy(file).unwrap();
585 let tuned = pitch_tuning(&mut pitch, 0.05, 12).unwrap();
586 assert!(f64::EPSILON > (tuned + 0.1).abs(), "{tuned} != -0.1");
587 }
588
589 #[test]
590 fn test_pitch_tuning_no_frequencies() {
591 let mut frequencies = arr1(&[]);
592 let tuned = pitch_tuning(&mut frequencies, 0.05, 12).unwrap();
593 assert!(f64::EPSILON > tuned.abs(), "{tuned} != 0");
594 }
595
596 #[test]
597 fn test_pip_track() {
598 let file = File::open("data/spectrum-chroma.npy").unwrap();
599 let spectrum = Array2::<f64>::read_npy(file).unwrap();
600
601 let mags_file = File::open("data/spectrum-chroma-mags.npy").unwrap();
602 let expected_mags = Array1::<f64>::read_npy(mags_file).unwrap();
603
604 let pitches_file = File::open("data/spectrum-chroma-pitches.npy").unwrap();
605 let expected_pitches = Array1::<f64>::read_npy(pitches_file).unwrap();
606
607 let (mut pitches, mut mags) = pip_track(22050, &spectrum, 2048).unwrap();
608 pitches.sort_by(|a, b| a.partial_cmp(b).unwrap());
609 mags.sort_by(|a, b| a.partial_cmp(b).unwrap());
610
611 for (expected_pitches, actual_pitches) in expected_pitches.iter().zip(pitches.iter()) {
612 assert!(
613 0.000_000_01 > (expected_pitches - actual_pitches).abs(),
614 "{expected_pitches} !~= {actual_pitches}"
615 );
616 }
617 for (expected_mags, actual_mags) in expected_mags.iter().zip(mags.iter()) {
618 assert!(
619 0.000_000_01 > (expected_mags - actual_mags).abs(),
620 "{expected_mags} !~= {actual_mags}"
621 );
622 }
623 }
624
625 #[test]
626 fn test_chroma_filter() {
627 let file = File::open("data/chroma-filter.npy").unwrap();
628 let expected_filter = Array2::<f64>::read_npy(file).unwrap();
629
630 let filter = chroma_filter(22050, 2048, 12, -0.1).unwrap();
631
632 assert!(filter.iter().all(|&x| x > 0.));
633
634 for (expected, actual) in expected_filter.iter().zip(filter.iter()) {
635 assert!(
636 0.000_000_001 > (expected - actual).abs(),
637 "{expected} !~= {actual}"
638 );
639 }
640 }
641}