use anyhow::{anyhow, ensure, Result};
use crate::runtime::{Model, Tensor};
pub struct MelExtractor<M: Model> {
model: M,
}
impl<M: Model> MelExtractor<M> {
pub fn new(model: M) -> Self {
Self { model }
}
pub fn extract(&mut self, samples: &[f32]) -> Result<Tensor> {
let input = Tensor {
shape: vec![1, samples.len()],
data: samples.to_vec(),
};
let mut outputs = self.model.run(&[("audio_pcm", &input)])?;
let mel = outputs
.remove("mel_spectrogram")
.ok_or_else(|| anyhow!("Model missing 'mel_spectrogram' output"))?;
ensure!(
mel.shape.len() == 3 && mel.shape[0] == 1 && mel.shape[2] == 128,
"Unexpected mel shape: {:?}",
mel.shape
);
Ok(mel)
}
}