use crate::{
check_init, ffi,
vec::{CVec, FVecMut},
Result, Smpl, Status,
};
pub struct MFCC {
mfcc: *mut ffi::aubio_mfcc_t,
buf_size: usize,
n_coeffs: usize,
}
impl Drop for MFCC {
fn drop(&mut self) {
unsafe { ffi::del_aubio_mfcc(self.mfcc) }
}
}
impl MFCC {
pub fn new(
buf_size: usize,
n_filters: usize,
n_coeffs: usize,
sample_rate: u32,
) -> Result<Self> {
let mfcc = unsafe {
ffi::new_aubio_mfcc(
buf_size as ffi::uint_t,
n_filters as ffi::uint_t,
n_coeffs as ffi::uint_t,
sample_rate as ffi::uint_t,
)
};
check_init(mfcc)?;
Ok(Self {
mfcc,
buf_size,
n_coeffs,
})
}
pub fn with_power(mut self, power: Smpl) -> Self {
self.set_power(power);
self
}
pub fn with_scale(mut self, scale: Smpl) -> Self {
self.set_scale(scale);
self
}
pub fn with_mel_coeffs(mut self, fmin: Smpl, fmax: Smpl) -> Self {
self.set_mel_coeffs(fmin, fmax);
self
}
pub fn with_mel_coeffs_htk(mut self, fmin: Smpl, fmax: Smpl) -> Self {
self.set_mel_coeffs_htk(fmin, fmax);
self
}
pub fn with_mel_coeffs_slaney(mut self) -> Self {
self.set_mel_coeffs_slaney();
self
}
pub fn do_<'i, 'o, I, O>(&mut self, input: I, output: O) -> Status
where
I: Into<CVec<'i>>,
O: Into<FVecMut<'o>>,
{
let input = input.into();
let mut output = output.into();
input.check_size(self.buf_size)?;
output.check_size(self.n_coeffs)?;
unsafe { ffi::aubio_mfcc_do(self.mfcc, input.as_ptr(), output.as_mut_ptr()) }
Ok(())
}
pub fn set_power(&mut self, power: Smpl) {
unsafe {
ffi::aubio_mfcc_set_power(self.mfcc, power);
}
}
pub fn get_power(&self) -> Smpl {
unsafe { ffi::aubio_mfcc_get_power(self.mfcc) }
}
pub fn set_scale(&mut self, scale: Smpl) {
unsafe {
ffi::aubio_mfcc_set_scale(self.mfcc, scale);
}
}
pub fn get_scale(&self) -> Smpl {
unsafe { ffi::aubio_mfcc_get_scale(self.mfcc) }
}
pub fn set_mel_coeffs(&mut self, fmin: Smpl, fmax: Smpl) {
unsafe {
ffi::aubio_mfcc_set_mel_coeffs(self.mfcc, fmin, fmax);
}
}
pub fn set_mel_coeffs_htk(&mut self, fmin: Smpl, fmax: Smpl) {
unsafe {
ffi::aubio_mfcc_set_mel_coeffs_htk(self.mfcc, fmin, fmax);
}
}
pub fn set_mel_coeffs_slaney(&mut self) {
unsafe {
ffi::aubio_mfcc_set_mel_coeffs_slaney(self.mfcc);
}
}
}