aubio_rs/mfcc.rs
1use crate::{
2 check_init, ffi,
3 vec::{CVec, FVecMut},
4 Result, Smpl, Status,
5};
6
7/**
8 * MFCC object
9 *
10 * Mel-Frequency Cepstrum Coefficients object.
11 *
12 * This object computes MFCC coefficients on an input CVec.
13 *
14 * The implementation follows the specifications established by Malcolm Slaney in its Auditory Toolbox, available online at the following address (see file mfcc.m):
15 *
16 * [https://engineering.purdue.edu/~malcolm/interval/1998-010/](https://engineering.purdue.edu/~malcolm/interval/1998-010/)
17 */
18pub struct MFCC {
19 mfcc: *mut ffi::aubio_mfcc_t,
20 buf_size: usize,
21 n_coeffs: usize,
22}
23
24impl Drop for MFCC {
25 fn drop(&mut self) {
26 unsafe { ffi::del_aubio_mfcc(self.mfcc) }
27 }
28}
29
30impl MFCC {
31 /**
32 * Create MFCC object
33 *
34 * - `buf_size` Size of analysis buffer (and length the FFT transform)
35 * - `n_filters` Number of desired filters
36 * - `n_coeffs` Number of desired coefficients
37 * - `samplerate` Audio sampling rate
38 */
39 pub fn new(
40 buf_size: usize,
41 n_filters: usize,
42 n_coeffs: usize,
43 sample_rate: u32,
44 ) -> Result<Self> {
45 let mfcc = unsafe {
46 ffi::new_aubio_mfcc(
47 buf_size as ffi::uint_t,
48 n_filters as ffi::uint_t,
49 n_coeffs as ffi::uint_t,
50 sample_rate as ffi::uint_t,
51 )
52 };
53
54 check_init(mfcc)?;
55
56 Ok(Self {
57 mfcc,
58 buf_size,
59 n_coeffs,
60 })
61 }
62
63 /**
64 * Set power parameter
65 */
66 pub fn with_power(mut self, power: Smpl) -> Self {
67 self.set_power(power);
68 self
69 }
70
71 /**
72 * Set scaling parameter
73 */
74 pub fn with_scale(mut self, scale: Smpl) -> Self {
75 self.set_scale(scale);
76 self
77 }
78
79 /**
80 * Mel filterbank initialization
81 *
82 * - `fmin` Start frequency, in Hz
83 * - `fmax` End frequency, in Hz
84 *
85 * The filterbank will be initialized with bands linearly spaced in the mel scale, from `fmin` to `fmax`.
86 */
87 pub fn with_mel_coeffs(mut self, fmin: Smpl, fmax: Smpl) -> Self {
88 self.set_mel_coeffs(fmin, fmax);
89 self
90 }
91
92 /**
93 * Mel filterbank initialization
94 *
95 * - `fmin` Start frequency, in Hz
96 * - `fmax` End frequency, in Hz
97 *
98 * The bank of filters will be initalized to to cover linearly spaced bands in the Htk mel scale, from `fmin` to `fmax`.
99 */
100 pub fn with_mel_coeffs_htk(mut self, fmin: Smpl, fmax: Smpl) -> Self {
101 self.set_mel_coeffs_htk(fmin, fmax);
102 self
103 }
104
105 /**
106 * Mel filterbank initialization (Auditory Toolbox's parameters)
107 *
108 * The filter coefficients are built to match exactly Malcolm Slaney's Auditory Toolbox implementation. The number of filters should be 40.
109 *
110 * This is the default filterbank when mf was created with `n_filters = 40`.
111 */
112 pub fn with_mel_coeffs_slaney(mut self) -> Self {
113 self.set_mel_coeffs_slaney();
114 self
115 }
116
117 /**
118 * MFCC object processing
119 *
120 * - `in` Input spectrum (`buf_size` long)
121 * - `out` Output mel coefficients buffer (`n_coeffs` long)
122 */
123 pub fn do_<'i, 'o, I, O>(&mut self, input: I, output: O) -> Status
124 where
125 I: Into<CVec<'i>>,
126 O: Into<FVecMut<'o>>,
127 {
128 let input = input.into();
129 let mut output = output.into();
130
131 input.check_size(self.buf_size)?;
132 output.check_size(self.n_coeffs)?;
133
134 unsafe { ffi::aubio_mfcc_do(self.mfcc, input.as_ptr(), output.as_mut_ptr()) }
135 Ok(())
136 }
137
138 /**
139 * Set power parameter
140 */
141 pub fn set_power(&mut self, power: Smpl) {
142 unsafe {
143 ffi::aubio_mfcc_set_power(self.mfcc, power);
144 }
145 }
146
147 /**
148 * Get power parameter
149 */
150 pub fn get_power(&self) -> Smpl {
151 unsafe { ffi::aubio_mfcc_get_power(self.mfcc) }
152 }
153
154 /**
155 * Set scaling parameter
156 */
157 pub fn set_scale(&mut self, scale: Smpl) {
158 unsafe {
159 ffi::aubio_mfcc_set_scale(self.mfcc, scale);
160 }
161 }
162
163 /**
164 * Get scaling parameter
165 */
166 pub fn get_scale(&self) -> Smpl {
167 unsafe { ffi::aubio_mfcc_get_scale(self.mfcc) }
168 }
169
170 /**
171 * Mel filterbank initialization
172 *
173 * - `fmin` Start frequency, in Hz
174 * - `fmax` End frequency, in Hz
175 *
176 * The filterbank will be initialized with bands linearly spaced in the mel scale, from `fmin` to `fmax`.
177 */
178 pub fn set_mel_coeffs(&mut self, fmin: Smpl, fmax: Smpl) {
179 unsafe {
180 ffi::aubio_mfcc_set_mel_coeffs(self.mfcc, fmin, fmax);
181 }
182 }
183
184 /**
185 * Mel filterbank initialization
186 *
187 * - `fmin` Start frequency, in Hz
188 * - `fmax` End frequency, in Hz
189 *
190 * The bank of filters will be initalized to to cover linearly spaced bands in the Htk mel scale, from `fmin` to `fmax`.
191 */
192 pub fn set_mel_coeffs_htk(&mut self, fmin: Smpl, fmax: Smpl) {
193 unsafe {
194 ffi::aubio_mfcc_set_mel_coeffs_htk(self.mfcc, fmin, fmax);
195 }
196 }
197
198 /**
199 * Mel filterbank initialization (Auditory Toolbox's parameters)
200 *
201 * The filter coefficients are built to match exactly Malcolm Slaney's Auditory Toolbox implementation. The number of filters should be 40.
202 *
203 * This is the default filterbank when mf was created with `n_filters = 40`.
204 */
205 pub fn set_mel_coeffs_slaney(&mut self) {
206 unsafe {
207 ffi::aubio_mfcc_set_mel_coeffs_slaney(self.mfcc);
208 }
209 }
210}