Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Created August 24, 2024 12:37
Show Gist options
  • Save thewh1teagle/7ea8fe0a666f6350ca6ecfaa0d719c7a to your computer and use it in GitHub Desktop.
Save thewh1teagle/7ea8fe0a666f6350ca6ecfaa0d719c7a to your computer and use it in GitHub Desktop.
fn write_input_data<T, U>(input: &[T], writer: &WavWriterHandle, resampler: &mut SincFixedIn<f64>)
where
T: Sample,
U: Sample + hound::Sample + FromSample<T>,
{
// Convert input to f64
let samples: Vec<f64> = input
.iter()
.map(|&s| U::from_sample(s).to_float_sample().to_sample())
.collect();
let samples_slice: &[f64] = &samples; // Slice of f64
let mut resampled_samples: Vec<f64> = vec![0.0; samples.len()];
// Prepare input and output slices
let indata_slices = vec![samples_slice];
let mut outdata_slices = vec![&mut resampled_samples[..]]; // Mutable slice of f64
let mask = vec![true];
let (nbr_in, nbr_out) = resampler
.process_into_buffer(&indata_slices, &mut outdata_slices, Some(&mask))
.unwrap();
let outdata = &resampled_samples[..nbr_out];
if let Ok(mut guard) = writer.try_lock() {
if let Some(writer) = guard.as_mut() {
for &sample in outdata {
// Convert the f64 sample back to the type expected by the writer
writer.write_sample(sample as f32).ok();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment