lib.rs (1054B)
1 use anyhow::{bail, Context, Result}; 2 3 const NUM_MEL_BINS: usize = 128; 4 5 pub fn compute_fbank(samples: &[f32]) -> Result<Vec<[f32; NUM_MEL_BINS]>> { 6 if samples.is_empty() { 7 bail!("The samples array is empty. No features to compute.") 8 } 9 10 let mut result = unsafe { 11 knf_rs_sys::ComputeFbank( 12 samples.as_ptr(), 13 samples.len().try_into().context("samples len")?, 14 ) 15 }; 16 17 let frames_slice = unsafe { 18 std::slice::from_raw_parts( 19 result.frames, 20 (result.num_frames * NUM_MEL_BINS as i32) as usize, 21 ) 22 }; 23 24 let features: Vec<[f32; NUM_MEL_BINS]> = frames_slice 25 .chunks_exact(NUM_MEL_BINS) 26 .map(|chunk| chunk.try_into().unwrap()) 27 .collect(); 28 29 unsafe { 30 knf_rs_sys::DestroyFbankResult(&mut result); 31 } 32 33 Ok(features) 34 } 35 36 pub fn convert_integer_to_float_audio(samples: &[i16], output: &mut [f32]) { 37 for (input, output) in samples.iter().zip(output.iter_mut()) { 38 *output = *input as f32 / 32768.0; 39 } 40 }