use std::path::Path;
use hound::{SampleFormat, WavReader};
use whisper_rs::convert_integer_to_float_audio;
use crate::error::{AppError, Result};
pub fn load_wav(path: &Path) -> Result<Vec<f32>> {
let reader = WavReader::open(path).map_err(|e| AppError::Audio(e.to_string()))?;
let spec = reader.spec();
if spec.sample_rate != 16_000 {
return Err(AppError::Audio(format!(
"需要 16kHz 采样率,当前为 {}Hz",
spec.sample_rate
)));
}
if spec.channels != 1 {
return Err(AppError::Audio(format!(
"需要单声道,当前为 {} 声道",
spec.channels
)));
}
match spec.sample_format {
SampleFormat::Int => {
let samples: Vec<i16> = reader
.into_samples::<i16>()
.map(|s| s.map_err(|e| AppError::Audio(e.to_string())))
.collect::<Result<_>>()?;
let mut audio = vec![0.0f32; samples.len()];
convert_integer_to_float_audio(&samples, &mut audio)?;
Ok(audio)
}
SampleFormat::Float => {
let samples: Vec<f32> = reader
.into_samples::<f32>()
.map(|s| s.map_err(|e| AppError::Audio(e.to_string())))
.collect::<Result<_>>()?;
Ok(samples)
}
}
}