MCPcopy Create free account
hub / github.com/evilsocket/cake / upsample

Function upsample

cake-core/src/models/luxtts/vocos.rs:370–386  ·  view source on GitHub ↗

Upsample audio from source_rate to target_rate using linear interpolation.

(samples: &[f32], source_rate: usize, target_rate: usize)

Source from the content-addressed store, hash-verified

368
369/// Upsample audio from source_rate to target_rate using linear interpolation.
370pub fn upsample(samples: &[f32], source_rate: usize, target_rate: usize) -> Vec<f32> {
371 if source_rate == target_rate {
372 return samples.to_vec();
373 }
374 let ratio = target_rate as f64 / source_rate as f64;
375 let new_len = (samples.len() as f64 * ratio) as usize;
376 let mut output = Vec::with_capacity(new_len);
377 for i in 0..new_len {
378 let src_pos = i as f64 / ratio;
379 let idx = src_pos as usize;
380 let frac = (src_pos - idx as f64) as f32;
381 let s0 = samples.get(idx).copied().unwrap_or(0.0);
382 let s1 = samples.get(idx + 1).copied().unwrap_or(s0);
383 output.push(s0 + frac * (s1 - s0));
384 }
385 output
386}
387
388/// Save audio samples as WAV file (16-bit PCM).
389///

Callers 2

generate_speechMethod · 0.85
test_upsampleFunction · 0.85

Calls 3

to_vecMethod · 0.80
getMethod · 0.45
pushMethod · 0.45

Tested by 1

test_upsampleFunction · 0.68