1 | /* |
---|
2 | Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org> |
---|
3 | and Amaury Hazan <ahazan@iua.upf.edu> |
---|
4 | |
---|
5 | This file is part of aubio. |
---|
6 | |
---|
7 | aubio is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 3 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | aubio is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include "aubio_priv.h" |
---|
23 | #include "fvec.h" |
---|
24 | #include "fmat.h" |
---|
25 | #include "cvec.h" |
---|
26 | #include "mathutils.h" |
---|
27 | #include "vecutils.h" |
---|
28 | #include "spectral/fft.h" |
---|
29 | #include "spectral/filterbank.h" |
---|
30 | #include "spectral/filterbank_mel.h" |
---|
31 | #include "spectral/dct.h" |
---|
32 | #include "spectral/mfcc.h" |
---|
33 | |
---|
34 | /** Internal structure for mfcc object */ |
---|
35 | |
---|
36 | struct _aubio_mfcc_t |
---|
37 | { |
---|
38 | uint_t win_s; /** grain length */ |
---|
39 | uint_t samplerate; /** sample rate (needed?) */ |
---|
40 | uint_t n_filters; /** number of filters */ |
---|
41 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
42 | aubio_filterbank_t *fb; /** filter bank */ |
---|
43 | fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
44 | aubio_dct_t *dct; /** dct object */ |
---|
45 | fvec_t *output; /** dct output */ |
---|
46 | smpl_t scale; |
---|
47 | }; |
---|
48 | |
---|
49 | |
---|
50 | aubio_mfcc_t * |
---|
51 | new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs, |
---|
52 | uint_t samplerate) |
---|
53 | { |
---|
54 | |
---|
55 | /* allocate space for mfcc object */ |
---|
56 | aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); |
---|
57 | |
---|
58 | mfcc->win_s = win_s; |
---|
59 | mfcc->samplerate = samplerate; |
---|
60 | mfcc->n_filters = n_filters; |
---|
61 | mfcc->n_coefs = n_coefs; |
---|
62 | |
---|
63 | /* filterbank allocation */ |
---|
64 | mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); |
---|
65 | if (n_filters == 40) |
---|
66 | aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); |
---|
67 | else |
---|
68 | aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate, |
---|
69 | 0, samplerate/2.); |
---|
70 | |
---|
71 | /* allocating buffers */ |
---|
72 | mfcc->in_dct = new_fvec (n_filters); |
---|
73 | |
---|
74 | mfcc->dct = new_aubio_dct (n_filters); |
---|
75 | mfcc->output = new_fvec (n_filters); |
---|
76 | |
---|
77 | mfcc->scale = 1.; |
---|
78 | |
---|
79 | return mfcc; |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | del_aubio_mfcc (aubio_mfcc_t * mf) |
---|
84 | { |
---|
85 | |
---|
86 | /* delete filterbank */ |
---|
87 | del_aubio_filterbank (mf->fb); |
---|
88 | |
---|
89 | /* delete buffers */ |
---|
90 | del_fvec (mf->in_dct); |
---|
91 | del_aubio_dct (mf->dct); |
---|
92 | del_fvec (mf->output); |
---|
93 | |
---|
94 | /* delete mfcc object */ |
---|
95 | AUBIO_FREE (mf); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void |
---|
100 | aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out) |
---|
101 | { |
---|
102 | fvec_t tmp; |
---|
103 | |
---|
104 | /* compute filterbank */ |
---|
105 | aubio_filterbank_do (mf->fb, in, mf->in_dct); |
---|
106 | |
---|
107 | /* compute log10 */ |
---|
108 | fvec_log10 (mf->in_dct); |
---|
109 | |
---|
110 | if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale); |
---|
111 | |
---|
112 | /* compute mfccs */ |
---|
113 | aubio_dct_do(mf->dct, mf->in_dct, mf->output); |
---|
114 | // copy only first n_coeffs elements |
---|
115 | // TODO assert mf->output->length == n_coeffs |
---|
116 | tmp.data = mf->output->data; |
---|
117 | tmp.length = out->length; |
---|
118 | fvec_copy(&tmp, out); |
---|
119 | |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power) |
---|
124 | { |
---|
125 | return aubio_filterbank_set_power(mf->fb, power); |
---|
126 | } |
---|
127 | |
---|
128 | uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf) |
---|
129 | { |
---|
130 | return aubio_filterbank_get_power(mf->fb); |
---|
131 | } |
---|
132 | |
---|
133 | uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale) |
---|
134 | { |
---|
135 | mf->scale = scale; |
---|
136 | return AUBIO_OK; |
---|
137 | } |
---|
138 | |
---|
139 | uint_t aubio_mfcc_get_scale (aubio_mfcc_t *mf) |
---|
140 | { |
---|
141 | return mf->scale; |
---|
142 | } |
---|
143 | |
---|
144 | uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
145 | smpl_t freq_max) |
---|
146 | { |
---|
147 | return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate, |
---|
148 | freq_min, freq_max); |
---|
149 | } |
---|
150 | |
---|
151 | uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
152 | smpl_t freq_max) |
---|
153 | { |
---|
154 | return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate, |
---|
155 | freq_min, freq_max); |
---|
156 | } |
---|
157 | |
---|
158 | uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf) |
---|
159 | { |
---|
160 | return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate); |
---|
161 | } |
---|