[88199ce] | 1 | /* |
---|
| 2 | Copyright (C) 2006 Amaury Hazan |
---|
| 3 | Ported to aubio from LibXtract |
---|
| 4 | http://libxtract.sourceforge.net/ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | This program 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 2 of the License, or |
---|
| 10 | (at your option) any later version. |
---|
| 11 | |
---|
| 12 | This program 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 this program; if not, write to the Free Software |
---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 20 | |
---|
| 21 | */ |
---|
| 22 | |
---|
[7c6c806d] | 23 | #include "aubio_priv.h" |
---|
[6c7d49b] | 24 | #include "fvec.h" |
---|
| 25 | #include "cvec.h" |
---|
[32d6958] | 26 | #include "spectral/fft.h" |
---|
| 27 | #include "spectral/filterbank.h" |
---|
| 28 | #include "spectral/mfcc.h" |
---|
[88199ce] | 29 | |
---|
[8708556] | 30 | /** Internal structure for mfcc object **/ |
---|
[88199ce] | 31 | |
---|
[8708556] | 32 | struct aubio_mfcc_t_{ |
---|
[34d72f0] | 33 | uint_t win_s; /** grain length */ |
---|
| 34 | uint_t samplerate; /** sample rate (needed?) */ |
---|
| 35 | uint_t channels; /** number of channels */ |
---|
[7a46bf6] | 36 | uint_t n_filters; /** number of *filters */ |
---|
| 37 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
[34d72f0] | 38 | smpl_t lowfreq; /** lowest frequency for filters */ |
---|
| 39 | smpl_t highfreq; /** highest frequency for filters */ |
---|
| 40 | aubio_filterbank_t * fb; /** filter bank */ |
---|
| 41 | fvec_t * in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
[7873363] | 42 | aubio_fft_t * fft_dct; /** fft object for dct */ |
---|
[34d72f0] | 43 | cvec_t * fftgrain_dct; /** output buffer for dct */ |
---|
[8708556] | 44 | }; |
---|
| 45 | |
---|
| 46 | |
---|
[7a46bf6] | 47 | aubio_mfcc_t * new_aubio_mfcc (uint_t win_s, uint_t samplerate, uint_t n_filters, uint_t n_coefs, smpl_t lowfreq, smpl_t highfreq, uint_t channels){ |
---|
[8708556] | 48 | /** allocating space for mfcc object */ |
---|
| 49 | aubio_mfcc_t * mfcc = AUBIO_NEW(aubio_mfcc_t); |
---|
[cfe4038] | 50 | |
---|
| 51 | //we need (n_coefs-1)*2 filters to obtain n_coefs coefficients after dct |
---|
[7a46bf6] | 52 | //uint_t n_filters = (n_coefs-1)*2; |
---|
[8708556] | 53 | |
---|
| 54 | mfcc->win_s=win_s; |
---|
| 55 | mfcc->samplerate=samplerate; |
---|
| 56 | mfcc->channels=channels; |
---|
[7a46bf6] | 57 | mfcc->n_filters=n_filters; |
---|
[8708556] | 58 | mfcc->n_coefs=n_coefs; |
---|
| 59 | mfcc->lowfreq=lowfreq; |
---|
| 60 | mfcc->highfreq=highfreq; |
---|
| 61 | |
---|
[f72ceeb] | 62 | |
---|
[8708556] | 63 | /** filterbank allocation */ |
---|
[9170e4c] | 64 | mfcc->fb = new_aubio_filterbank_mfcc(n_filters, mfcc->win_s, samplerate, lowfreq, highfreq); |
---|
[8708556] | 65 | |
---|
| 66 | /** allocating space for fft object (used for dct) */ |
---|
[7873363] | 67 | mfcc->fft_dct=new_aubio_fft(n_filters, 1); |
---|
[8708556] | 68 | |
---|
| 69 | /** allocating buffers */ |
---|
| 70 | mfcc->in_dct=new_fvec(mfcc->win_s, 1); |
---|
| 71 | |
---|
[cfe4038] | 72 | mfcc->fftgrain_dct=new_cvec(n_filters, 1); |
---|
[8708556] | 73 | |
---|
| 74 | return mfcc; |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | void del_aubio_mfcc(aubio_mfcc_t *mf){ |
---|
| 78 | /** deleting filterbank */ |
---|
| 79 | del_aubio_filterbank(mf->fb); |
---|
[7873363] | 80 | /** deleting fft object */ |
---|
| 81 | del_aubio_fft(mf->fft_dct); |
---|
[8708556] | 82 | /** deleting buffers */ |
---|
| 83 | del_fvec(mf->in_dct); |
---|
| 84 | del_cvec(mf->fftgrain_dct); |
---|
| 85 | |
---|
| 86 | /** deleting mfcc object */ |
---|
| 87 | AUBIO_FREE(mf); |
---|
[88199ce] | 88 | } |
---|
| 89 | |
---|
[177f09a] | 90 | |
---|
| 91 | /** intermediate dct involved in aubio_mfcc_do |
---|
| 92 | |
---|
| 93 | \param mf mfcc object as returned by new_aubio_mfcc |
---|
| 94 | \param in input spectrum (n_filters long) |
---|
| 95 | \param out output mel coefficients buffer (n_filters/2 +1 long) |
---|
| 96 | |
---|
| 97 | */ |
---|
| 98 | void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out); |
---|
| 99 | |
---|
[8708556] | 100 | void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){ |
---|
[b276dee] | 101 | // compute filterbank |
---|
| 102 | aubio_filterbank_do(mf->fb, in, mf->in_dct); |
---|
[7c6c806d] | 103 | //TODO: check that zero padding |
---|
[8708556] | 104 | // the following line seems useless since the in_dct buffer has the correct size |
---|
| 105 | //for(n = filter + 1; n < N; n++) result[n] = 0; |
---|
[97886fa] | 106 | |
---|
[8708556] | 107 | aubio_dct_do(mf, mf->in_dct, out); |
---|
[97886fa] | 108 | |
---|
[cfe4038] | 109 | return; |
---|
[88199ce] | 110 | } |
---|
[97886fa] | 111 | |
---|
[8708556] | 112 | void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){ |
---|
[b276dee] | 113 | uint_t i; |
---|
[97886fa] | 114 | //compute mag spectrum |
---|
[7873363] | 115 | aubio_fft_do (mf->fft_dct, in, mf->fftgrain_dct); |
---|
[97886fa] | 116 | //extract real part of fft grain |
---|
[7a46bf6] | 117 | for(i=0; i<mf->n_coefs ;i++){ |
---|
| 118 | //for(i=0; i<out->length;i++){ |
---|
[b276dee] | 119 | out->data[0][i]= mf->fftgrain_dct->norm[0][i] |
---|
| 120 | *COS(mf->fftgrain_dct->phas[0][i]); |
---|
[97886fa] | 121 | } |
---|
[cfe4038] | 122 | return; |
---|
[71d3bf0] | 123 | } |
---|
[cfe4038] | 124 | |
---|