resampling using rubato

This commit is contained in:
mii443
2024-02-15 11:32:26 +09:00
parent 215b498f84
commit 2c536a3135
4 changed files with 45 additions and 0 deletions

22
Cargo.lock generated
View File

@ -495,6 +495,7 @@ dependencies = [
"cpal", "cpal",
"num", "num",
"num-traits", "num-traits",
"rubato",
"rustfft", "rustfft",
"serde", "serde",
"serde_yaml", "serde_yaml",
@ -844,6 +845,15 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "realfft"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953d9f7e5cdd80963547b456251296efc2626ed4e3cbf36c869d9564e0220571"
dependencies = [
"rustfft",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.4.1" version = "0.4.1"
@ -882,6 +892,18 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rubato"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6dd52e80cfc21894deadf554a5673002938ae4625f7a283e536f9cf7c17b0d5"
dependencies = [
"num-complex",
"num-integer",
"num-traits",
"realfft",
]
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"

View File

@ -13,3 +13,4 @@ num-traits = "0.2.18"
rustfft = "6.2.0" rustfft = "6.2.0"
serde = { version = "1.0.196", features = ["derive"] } serde = { version = "1.0.196", features = ["derive"] }
serde_yaml = "0.9.31" serde_yaml = "0.9.31"
rubato = "0.14.1"

View File

@ -0,0 +1 @@
pub mod resampling;

View File

@ -0,0 +1,21 @@
use rubato::{Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction};
pub fn resampling(current_sample_rate: u32, target_sample_rate: u32, data: Vec<Vec<f32>>) -> Vec<Vec<f32>> {
let params = SincInterpolationParameters {
sinc_len: 256,
f_cutoff: 0.95,
interpolation: SincInterpolationType::Linear,
oversampling_factor: 128,
window: WindowFunction::BlackmanHarris2,
};
let mut resampler = SincFixedIn::<f32>::new(
current_sample_rate as f64 / target_sample_rate as f64,
2.0,
params,
data[0].len(),
data.len()
).unwrap();
resampler.process(&data, None).unwrap()
}