[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 | |
---|
[7c6c806d] | 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 | |
---|
[7a84b21] | 34 | /** Internal structure for mfcc object */ |
---|
[88199ce] | 35 | |
---|
[d99d819] | 36 | struct _aubio_mfcc_t |
---|
[7a84b21] | 37 | { |
---|
[34d72f0] | 38 | uint_t win_s; /** grain length */ |
---|
| 39 | uint_t samplerate; /** sample rate (needed?) */ |
---|
[f2a0769] | 40 | uint_t n_filters; /** number of filters */ |
---|
[7a46bf6] | 41 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
[7a84b21] | 42 | aubio_filterbank_t *fb; /** filter bank */ |
---|
| 43 | fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
[e744416] | 44 | aubio_dct_t *dct; /** dct object */ |
---|
| 45 | fvec_t *output; /** dct output */ |
---|
[d66d2ac] | 46 | smpl_t scale; |
---|
[8708556] | 47 | }; |
---|
| 48 | |
---|
| 49 | |
---|
[7a84b21] | 50 | aubio_mfcc_t * |
---|
[41bf913] | 51 | new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs, |
---|
| 52 | uint_t samplerate) |
---|
[7a84b21] | 53 | { |
---|
| 54 | |
---|
| 55 | /* allocate space for mfcc object */ |
---|
| 56 | aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); |
---|
[c185ebb] | 57 | |
---|
[5ad5109] | 58 | if ((sint_t)n_coefs <= 0) { |
---|
| 59 | AUBIO_ERR("mfcc: n_coefs should be > 0, got %d\n", n_coefs); |
---|
| 60 | goto failure; |
---|
| 61 | } |
---|
| 62 | if ((sint_t)samplerate <= 0) { |
---|
| 63 | AUBIO_ERR("mfcc: samplerate should be > 0, got %d\n", samplerate); |
---|
| 64 | goto failure; |
---|
| 65 | } |
---|
| 66 | |
---|
[7a84b21] | 67 | mfcc->win_s = win_s; |
---|
| 68 | mfcc->samplerate = samplerate; |
---|
| 69 | mfcc->n_filters = n_filters; |
---|
| 70 | mfcc->n_coefs = n_coefs; |
---|
[cfe4038] | 71 | |
---|
[7a84b21] | 72 | /* filterbank allocation */ |
---|
| 73 | mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); |
---|
[5ad5109] | 74 | |
---|
| 75 | if (!mfcc->fb) |
---|
| 76 | goto failure; |
---|
| 77 | |
---|
[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 | mfcc->dct = new_aubio_dct (n_filters); |
---|
| 88 | mfcc->output = new_fvec (n_filters); |
---|
[8708556] | 89 | |
---|
[5ad5109] | 90 | if (!mfcc->in_dct || !mfcc->dct || !mfcc->output) |
---|
| 91 | goto failure; |
---|
| 92 | |
---|
[d66d2ac] | 93 | mfcc->scale = 1.; |
---|
| 94 | |
---|
[8708556] | 95 | return mfcc; |
---|
[5ad5109] | 96 | |
---|
| 97 | failure: |
---|
| 98 | del_aubio_mfcc(mfcc); |
---|
| 99 | return NULL; |
---|
[163d159] | 100 | } |
---|
[8708556] | 101 | |
---|
[7a84b21] | 102 | void |
---|
| 103 | del_aubio_mfcc (aubio_mfcc_t * mf) |
---|
| 104 | { |
---|
[5ad5109] | 105 | if (mf->fb) |
---|
| 106 | del_aubio_filterbank (mf->fb); |
---|
| 107 | if (mf->in_dct) |
---|
| 108 | del_fvec (mf->in_dct); |
---|
| 109 | if (mf->dct) |
---|
| 110 | del_aubio_dct (mf->dct); |
---|
| 111 | if (mf->output) |
---|
| 112 | del_fvec (mf->output); |
---|
[7a84b21] | 113 | AUBIO_FREE (mf); |
---|
[88199ce] | 114 | } |
---|
| 115 | |
---|
[177f09a] | 116 | |
---|
[7a84b21] | 117 | void |
---|
[feb694b] | 118 | aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out) |
---|
[7a84b21] | 119 | { |
---|
[3aa60b2] | 120 | fvec_t tmp; |
---|
[d66d2ac] | 121 | |
---|
[7a84b21] | 122 | /* compute filterbank */ |
---|
| 123 | aubio_filterbank_do (mf->fb, in, mf->in_dct); |
---|
[c185ebb] | 124 | |
---|
[d37400c] | 125 | /* compute log10 */ |
---|
| 126 | fvec_log10 (mf->in_dct); |
---|
| 127 | |
---|
[d66d2ac] | 128 | if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale); |
---|
[d37400c] | 129 | |
---|
[73e8f65] | 130 | /* compute mfccs */ |
---|
[d8b1161] | 131 | aubio_dct_do(mf->dct, mf->in_dct, mf->output); |
---|
| 132 | // copy only first n_coeffs elements |
---|
| 133 | // TODO assert mf->output->length == n_coeffs |
---|
| 134 | tmp.data = mf->output->data; |
---|
| 135 | tmp.length = out->length; |
---|
| 136 | fvec_copy(&tmp, out); |
---|
[cfe4038] | 137 | |
---|
[c185ebb] | 138 | return; |
---|
| 139 | } |
---|
[517630f] | 140 | |
---|
| 141 | uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power) |
---|
| 142 | { |
---|
| 143 | return aubio_filterbank_set_power(mf->fb, power); |
---|
| 144 | } |
---|
| 145 | |
---|
[8affe8c] | 146 | smpl_t aubio_mfcc_get_power (aubio_mfcc_t *mf) |
---|
[517630f] | 147 | { |
---|
| 148 | return aubio_filterbank_get_power(mf->fb); |
---|
| 149 | } |
---|
| 150 | |
---|
[d66d2ac] | 151 | uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale) |
---|
| 152 | { |
---|
| 153 | mf->scale = scale; |
---|
| 154 | return AUBIO_OK; |
---|
| 155 | } |
---|
| 156 | |
---|
[8affe8c] | 157 | smpl_t aubio_mfcc_get_scale (aubio_mfcc_t *mf) |
---|
[d66d2ac] | 158 | { |
---|
| 159 | return mf->scale; |
---|
| 160 | } |
---|
| 161 | |
---|
[517630f] | 162 | uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
| 163 | smpl_t freq_max) |
---|
| 164 | { |
---|
| 165 | return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate, |
---|
| 166 | freq_min, freq_max); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min, |
---|
| 170 | smpl_t freq_max) |
---|
| 171 | { |
---|
| 172 | return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate, |
---|
| 173 | freq_min, freq_max); |
---|
| 174 | } |
---|
[10fafc2] | 175 | |
---|
[c879811] | 176 | uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf) |
---|
[10fafc2] | 177 | { |
---|
| 178 | return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate); |
---|
| 179 | } |
---|