Skip to content

Commit

Permalink
interpolator starts from first two samples instead of 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
B0ney committed Dec 23, 2023
1 parent 4500e70 commit 115cf91
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/widget/waveform_view/wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Local {
pub maxima: f32,
pub minima: f32,
Expand All @@ -53,9 +53,7 @@ pub struct Local {
impl Local {
pub fn check(mut self) -> Self {
if self.minima > self.maxima {
let temp = self.maxima;
self.maxima = self.minima;
self.minima = temp;
std::mem::swap(&mut self.maxima, &mut self.minima);
}

self
Expand All @@ -72,6 +70,13 @@ impl From<(f32, f32)> for Local {
}
}

impl From<Local> for [f32; 2] {
fn from(value: Local) -> Self {
let Local { maxima, minima } = value.check();
[maxima, minima]
}
}

impl From<[f32; 2]> for Local {
fn from(value: [f32; 2]) -> Self {
Self {
Expand Down Expand Up @@ -102,15 +107,18 @@ pub fn interpolate_zoom(wave: &WaveData, factor: f32) -> WaveData {
.iter()
.map(|wave| {
let mut output = Vec::new();
let first = wave[0].into();
let second = wave.get(1).cloned().unwrap_or_default().into();

let signal = wave.iter().map(|f| [f.maxima, f.minima]);
let mut converter = Converter::scale_playback_hz(
signal::from_iter(signal),
Linear::new([0.0, 0.0], [0.0, 0.0]),
Linear::new(first, second),
1.0 / factor as f64,
);
);

while !converter.is_exhausted() {
output.push(converter.next().into())
output.push(Local::from(converter.next()))
}

output
Expand Down

0 comments on commit 115cf91

Please sign in to comment.