Skip to content

Commit

Permalink
move resample() into lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotflakes committed Jul 30, 2023
1 parent 34bbbeb commit aba720e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
25 changes: 1 addition & 24 deletions examples/time_stretch2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod wav;

use voiche::{api, overlapping_flatten::OverlappingFlattenTrait, windows};
use voiche::{api, overlapping_flatten::OverlappingFlattenTrait, resample, windows};

fn main() {
let window_size = 1024;
Expand All @@ -26,26 +26,3 @@ fn main() {
.collect()
});
}

pub fn resample<T: voiche::float::Float>(buf: &[T], rate: T) -> Vec<T> {
let mut output = vec![
T::zero();
(T::from(buf.len()).unwrap() * rate)
.ceil()
.to_usize()
.unwrap()
];

for i in 0..output.len() {
let p = T::from(i).unwrap() / rate;
let j = p.to_usize().unwrap();

let x = buf[j];
let y = buf.get(j + 1).copied().unwrap_or(T::zero());
let z = buf.get(j + 2).copied().unwrap_or(T::zero());

output[i] = y - (x - z) * p.fract() / T::from(4.0).unwrap();
}

output
}
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ pub fn apply_window_with_scale<'a, T: rustfft::num_traits::Float>(
) -> impl Iterator<Item = T> + 'a {
iter.zip(window.iter()).map(move |(x, &y)| x * y * scale)
}

/// Resample with quadratic interpolation.
pub fn resample<T: float::Float>(buf: &[T], rate: T) -> Vec<T> {
let mut output = vec![
T::zero();
(T::from(buf.len()).unwrap() * rate)
.ceil()
.to_usize()
.unwrap()
];

for i in 0..output.len() {
let p = T::from(i).unwrap() / rate;
let j = p.to_usize().unwrap();

let x = buf[j];
let y = buf.get(j + 1).copied().unwrap_or(T::zero());
let z = buf.get(j + 2).copied().unwrap_or(T::zero());

output[i] = y - (x - z) * p.fract() / T::from(4.0).unwrap();
}

output
}

0 comments on commit aba720e

Please sign in to comment.