Skip to content

Commit

Permalink
copy-paste issue 23
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Sep 12, 2022
1 parent 53fd8c6 commit fb2863b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/detector/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,34 @@ pub fn autocorrelation<T>(
inv_fft.process_with_scratch(signal_complex, scratch_complex);
copy_complex_to_real(signal_complex, result, ComplexComponent::Re);
}
pub fn pitch_from_peaks(
input: &[T],
sample_rate: usize,
clarity_threshold: T,
pick_threshold: T,
correction: PeakCorrection,
) -> Option<Pitch>
where
T: Float,
{
let sample_rate = T::from_usize(sample_rate).unwrap();
let peaks = detect_peaks(input);
let peaks2 = detect_peaks(input);
let maxpeak = peaks2.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or_else(|| Ordering::Equal));
let thresh2 = match maxpeak {
None => T::from(0).unwrap(),
Some(p) => p.1 * pick_threshold,
};
choose_peak(peaks, thresh2, clarity_threshold)
.map(|peak| correct_peak(peak, input, correction))
.map(|peak| Pitch {
frequency: sample_rate / peak.0,
clarity: peak.1 / input[0],
})
}


pub fn pitch_from_peaks<T>(
pub fn OLD_pitch_from_peaks<T>(
input: &[T],
sample_rate: usize,
clarity_threshold: T,
Expand Down
8 changes: 8 additions & 0 deletions src/utils/peak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ pub fn detect_peaks<'a, T: Float>(arr: &'a [T]) -> impl Iterator<Item = (usize,
pub fn choose_peak<I: Iterator<Item = (usize, T)>, T: Float>(
mut peaks: I,
threshold: T,
threshold2: T,
) -> Option<(usize, T)> {
peaks.find(|p| p.1 > threshold && p.1 > threshold2)
}

pub fn OLD_choose_peak<I: Iterator<Item = (usize, T)>, T: Float>(
mut peaks: I,
threshold: T,
) -> Option<(usize, T)> {
peaks.find(|p| p.1 > threshold)
}
Expand Down

0 comments on commit fb2863b

Please sign in to comment.