[88199ce] | 1 | /* |
---|
[7a84b21] | 2 | Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org> |
---|
| 3 | and Amaury Hazan <ahazan@iua.upf.edu> |
---|
[88199ce] | 4 | |
---|
[1c2e186] | 5 | This file is part of aubio. |
---|
[88199ce] | 6 | |
---|
[1c2e186] | 7 | aubio is free software: you can redistribute it and/or modify |
---|
[7a84b21] | 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. |
---|
[88199ce] | 11 | |
---|
[1c2e186] | 12 | aubio is distributed in the hope that it will be useful, |
---|
[7a84b21] | 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 |
---|
[1c2e186] | 18 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
[88199ce] | 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
[7c6c806] | 22 | #include "aubio_priv.h" |
---|
[6c7d49b] | 23 | #include "fvec.h" |
---|
[d95ff38] | 24 | #include "fmat.h" |
---|
[6c7d49b] | 25 | #include "cvec.h" |
---|
[d37400c] | 26 | #include "mathutils.h" |
---|
[d0dca26] | 27 | #include "vecutils.h" |
---|
[32d6958] | 28 | #include "spectral/fft.h" |
---|
[d90db7d4] | 29 | #include "spectral/filterbank.h" |
---|
[06cae6c] | 30 | #include "spectral/filterbank_mel.h" |
---|
[d8b1161] | 31 | #include "spectral/dct.h" |
---|
[32d6958] | 32 | #include "spectral/mfcc.h" |
---|
[88199ce] | 33 | |
---|
[8428a52] | 34 | #ifdef HAVE_NOOPT |
---|
| 35 | #define HAVE_SLOW_DCT 1 |
---|
| 36 | #endif |
---|
[d8b1161] | 37 | |
---|
[7a84b21] | 38 | /** Internal structure for mfcc object */ |
---|
[88199ce] | 39 | |
---|
[d99d819] | 40 | struct _aubio_mfcc_t |
---|
[7a84b21] | 41 | { |
---|
[34d72f0] | 42 | uint_t win_s; /** grain length */ |
---|
| 43 | uint_t samplerate; /** sample rate (needed?) */ |
---|
[f2a0769] | 44 | uint_t n_filters; /** number of filters */ |
---|
[7a46bf6] | 45 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
[7a84b21] | 46 | aubio_filterbank_t *fb; /** filter bank */ |
---|
| 47 | fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
[d8b1161] | 48 | #if defined(HAVE_SLOW_DCT) |
---|
[d95ff38] | 49 | fmat_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */ |
---|
[d8b1161] | 50 | #else |
---|
| 51 | aubio_dct_t *dct; |
---|
| 52 | fvec_t *output; |
---|
| 53 | #endif |
---|
[d66d2ac] | 54 | smpl_t scale; |
---|
[8708556] | 55 | }; |
---|
| 56 | |
---|
| 57 | |
---|
[7a84b21] | 58 | aubio_mfcc_t * |
---|
[41bf913] | 59 | new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs, |
---|
| 60 | uint_t samplerate) |
---|
[7a84b21] | 61 | { |
---|
| 62 | |
---|
| 63 | /* allocate space for mfcc object */ |
---|
| 64 | aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); |
---|
[d8b1161] | 65 | #if defined(HAVE_SLOW_DCT) |
---|
[78c21cf] | 66 | smpl_t scaling; |
---|
[c185ebb] | 67 | |
---|
| 68 | uint_t i, j; |
---|
[d8b1161] | 69 | #endif |
---|
[c185ebb] | 70 | |
---|
[7a84b21] | 71 | mfcc->win_s = win_s; |
---|
| 72 | mfcc->samplerate = samplerate; |
---|
| 73 | mfcc->n_filters = n_filters; |
---|
| 74 | mfcc->n_coefs = n_coefs; |
---|
[cfe4038] | 75 | |
---|
[7a84b21] | 76 | /* filterbank allocation */ |
---|
| 77 | mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); |
---|
[1c1dae7] | 78 | if (n_filters == 40) |
---|
| 79 | aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); |
---|
| 80 | else |
---|
| 81 | aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate, |
---|
| 82 | 0, samplerate/2.); |
---|
[8708556] | 83 | |
---|
[7a84b21] | 84 | /* allocating buffers */ |
---|
[d95ff38] | 85 | mfcc->in_dct = new_fvec (n_filters); |
---|
[8708556] | 86 | |
---|
[d8b1161] | 87 | #if defined(HAVE_SLOW_DCT) |
---|
[c02a1cc] | 88 | mfcc->dct_coeffs = new_fmat (n_coefs, n_filters); |
---|
[c185ebb] | 89 | |
---|
[c02a1cc] | 90 | /* compute DCT transform dct_coeffs[j][i] as |
---|
[7a84b21] | 91 | cos ( j * (i+.5) * PI / n_filters ) */ |
---|
[78c21cf] | 92 | scaling = 1. / SQRT (n_filters / 2.); |
---|
[7a84b21] | 93 | for (i = 0; i < n_filters; i++) { |
---|
[c185ebb] | 94 | for (j = 0; j < n_coefs; j++) { |
---|
[c02a1cc] | 95 | mfcc->dct_coeffs->data[j][i] = |
---|
[7a84b21] | 96 | scaling * COS (j * (i + 0.5) * PI / n_filters); |
---|
[c185ebb] | 97 | } |
---|
[c02a1cc] | 98 | mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.; |
---|
[c185ebb] | 99 | } |
---|
[d8b1161] | 100 | #else |
---|
| 101 | mfcc->dct = new_aubio_dct (n_filters); |
---|
| 102 | mfcc->output = new_fvec (n_filters); |
---|
| 103 | #endif |
---|
[8708556] | 104 | |
---|
[d66d2ac] | 105 | mfcc->scale = 1.; |
---|
| 106 | |
---|
[8708556] | 107 | return mfcc; |
---|
[163d159] | 108 | } |
---|
[8708556] | 109 | |
---|
[7a84b21] | 110 | void |
---|
| 111 | del_aubio_mfcc (aubio_mfcc_t * mf) |
---|
| 112 | { |
---|
| 113 | |
---|
| 114 | /* delete filterbank */ |
---|
| 115 | del_aubio_filterbank (mf->fb); |
---|
| 116 | |
---|
| 117 | /* delete buffers */ |
---|
| 118 | del_fvec (mf->in_dct); |
---|
[d8b1161] | 119 | #if defined(HAVE_SLOW_DCT) |
---|
[fc759f3] | 120 | del_fmat (mf->dct_coeffs); |
---|
[d8b1161] | 121 | #else |
---|
| 122 | del_aubio_dct (mf->dct); |
---|
| 123 | del_fvec (mf->output); |
---|
| 124 | #endif |
---|
[7a84b21] | 125 | |
---|
| 126 | /* delete mfcc object */ |
---|
| 127 | AUBIO_FREE (mf); |
---|
[88199ce] | 128 | } |
---|
| 129 | |
---|
[177f09a] | 130 | |
---|
[7a84b21] | 131 | void |
---|
[feb694b] | 132 | aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out) |
---|
[7a84b21] | 133 | { |
---|
[3aa60b2] | 134 | #ifndef HAVE_SLOW_DCT |
---|
| 135 | fvec_t tmp; |
---|
| 136 | #endif |
---|
[d66d2ac] | 137 | |
---|
[7a84b21] | 138 | /* compute filterbank */ |
---|
| 139 | aubio_filterbank_do (mf->fb, in, mf->in_dct); |
---|
[c185ebb] | 140 | |
---|
[d37400c] | 141 | /* compute log10 */ |
---|
| 142 | fvec_log10 (mf->in_dct); |
---|
| 143 | |
---|
[d66d2ac] | 144 | if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale); |
---|
[d37400c] | 145 | |
---|
[73e8f65] | 146 | /* compute mfccs */ |
---|
[d8b1161] | 147 | #if defined(HAVE_SLOW_DCT) |
---|
[73e8f65] | 148 | fmat_vecmul(mf->dct_coeffs, mf->in_dct, out); |
---|
[d8b1161] | 149 | #else |
---|
| 150 | aubio_dct_do(mf->dct, mf->in_dct, mf->output); |
---|
| 151 | // copy only first n_coeffs elements |
---|
| 152 | // TODO assert mf->output->length == n_coeffs |
---|
| 153 | tmp.data = mf->output->data; |
---|
| 154 | tmp.length = out->length; |
---|
| 155 | fvec_copy(&tmp, out); |
---|
| 156 | #endif |
---|
[cfe4038] | 157 | |
---|
[c185ebb] | 158 | return; |
---|
| 159 | } |
---|
[517630f] | 160 | |
---|
| 161 | uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power) |
---|
| 162 | { |
---|
| 163 | return aubio_filterbank_set_power(mf->fb, power); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf) |
---|
| 167 | { |
---|
| 168 | return aubio_filterbank_get_power(mf->fb); |
---|
| 169 | } |
---|
| 170 | |
---|
[d66d2ac] | 171 | uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale) |
---|
| 172 | { |
---|
| 173 | mf->scale = scale; |
---|
| 174 | return AUBIO_OK; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | uint_t aubio_mfcc_get_scale (aubio_mfcc_t *mf) |
---|
| 178 | { |
---|
| 179 | return mf->scale; |
---|
| 180 | } |
---|
| 181 | |
---|
[517630f] | 182 | uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
| 183 | smpl_t freq_max) |
---|
| 184 | { |
---|
| 185 | return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate, |
---|
| 186 | freq_min, freq_max); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
| 190 | smpl_t freq_max) |
---|
| 191 | { |
---|
| 192 | return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate, |
---|
| 193 | freq_min, freq_max); |
---|
| 194 | } |
---|
[10fafc2] | 195 | |
---|
[c879811] | 196 | uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf) |
---|
[10fafc2] | 197 | { |
---|
| 198 | return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate); |
---|
| 199 | } |
---|