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 | #undef HAVE_SLOW_DCT |
---|
35 | |
---|
36 | /** Internal structure for mfcc object */ |
---|
37 | |
---|
38 | struct _aubio_mfcc_t |
---|
39 | { |
---|
40 | uint_t win_s; /** grain length */ |
---|
41 | uint_t samplerate; /** sample rate (needed?) */ |
---|
42 | uint_t n_filters; /** number of filters */ |
---|
43 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
44 | aubio_filterbank_t *fb; /** filter bank */ |
---|
45 | fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
46 | #if defined(HAVE_SLOW_DCT) |
---|
47 | fmat_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */ |
---|
48 | #else |
---|
49 | aubio_dct_t *dct; |
---|
50 | fvec_t *output; |
---|
51 | #endif |
---|
52 | }; |
---|
53 | |
---|
54 | |
---|
55 | aubio_mfcc_t * |
---|
56 | new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs, |
---|
57 | uint_t samplerate) |
---|
58 | { |
---|
59 | |
---|
60 | /* allocate space for mfcc object */ |
---|
61 | aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); |
---|
62 | #if defined(HAVE_SLOW_DCT) |
---|
63 | smpl_t scaling; |
---|
64 | |
---|
65 | uint_t i, j; |
---|
66 | #endif |
---|
67 | |
---|
68 | mfcc->win_s = win_s; |
---|
69 | mfcc->samplerate = samplerate; |
---|
70 | mfcc->n_filters = n_filters; |
---|
71 | mfcc->n_coefs = n_coefs; |
---|
72 | |
---|
73 | /* filterbank allocation */ |
---|
74 | mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); |
---|
75 | aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); |
---|
76 | |
---|
77 | /* allocating buffers */ |
---|
78 | mfcc->in_dct = new_fvec (n_filters); |
---|
79 | |
---|
80 | #if defined(HAVE_SLOW_DCT) |
---|
81 | mfcc->dct_coeffs = new_fmat (n_coefs, n_filters); |
---|
82 | |
---|
83 | /* compute DCT transform dct_coeffs[j][i] as |
---|
84 | cos ( j * (i+.5) * PI / n_filters ) */ |
---|
85 | scaling = 1. / SQRT (n_filters / 2.); |
---|
86 | for (i = 0; i < n_filters; i++) { |
---|
87 | for (j = 0; j < n_coefs; j++) { |
---|
88 | mfcc->dct_coeffs->data[j][i] = |
---|
89 | scaling * COS (j * (i + 0.5) * PI / n_filters); |
---|
90 | } |
---|
91 | mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.; |
---|
92 | } |
---|
93 | #else |
---|
94 | mfcc->dct = new_aubio_dct (n_filters); |
---|
95 | mfcc->output = new_fvec (n_filters); |
---|
96 | #endif |
---|
97 | |
---|
98 | return mfcc; |
---|
99 | } |
---|
100 | |
---|
101 | void |
---|
102 | del_aubio_mfcc (aubio_mfcc_t * mf) |
---|
103 | { |
---|
104 | |
---|
105 | /* delete filterbank */ |
---|
106 | del_aubio_filterbank (mf->fb); |
---|
107 | |
---|
108 | /* delete buffers */ |
---|
109 | del_fvec (mf->in_dct); |
---|
110 | #if defined(HAVE_SLOW_DCT) |
---|
111 | del_fmat (mf->dct_coeffs); |
---|
112 | #else |
---|
113 | del_aubio_dct (mf->dct); |
---|
114 | del_fvec (mf->output); |
---|
115 | #endif |
---|
116 | |
---|
117 | /* delete mfcc object */ |
---|
118 | AUBIO_FREE (mf); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | void |
---|
123 | aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out) |
---|
124 | { |
---|
125 | #ifndef HAVE_SLOW_DCT |
---|
126 | fvec_t tmp; |
---|
127 | #endif |
---|
128 | /* compute filterbank */ |
---|
129 | aubio_filterbank_do (mf->fb, in, mf->in_dct); |
---|
130 | |
---|
131 | /* compute log10 */ |
---|
132 | fvec_log10 (mf->in_dct); |
---|
133 | |
---|
134 | /* raise power */ |
---|
135 | //fvec_pow (mf->in_dct, 3.); |
---|
136 | |
---|
137 | /* compute mfccs */ |
---|
138 | #if defined(HAVE_SLOW_DCT) |
---|
139 | fmat_vecmul(mf->dct_coeffs, mf->in_dct, out); |
---|
140 | #else |
---|
141 | aubio_dct_do(mf->dct, mf->in_dct, mf->output); |
---|
142 | // copy only first n_coeffs elements |
---|
143 | // TODO assert mf->output->length == n_coeffs |
---|
144 | tmp.data = mf->output->data; |
---|
145 | tmp.length = out->length; |
---|
146 | fvec_copy(&tmp, out); |
---|
147 | #endif |
---|
148 | |
---|
149 | return; |
---|
150 | } |
---|