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 | |
---|
23 | #include "aubio_priv.h" |
---|
24 | #include "sample.h" |
---|
25 | #include "fft.h" |
---|
26 | #include "filterbank.h" |
---|
27 | #include "mfcc.h" |
---|
28 | #include "math.h" |
---|
29 | |
---|
30 | /** Internal structure for mfcc object **/ |
---|
31 | |
---|
32 | struct aubio_mfcc_t_{ |
---|
33 | uint_t win_s; /** grain length */ |
---|
34 | uint_t samplerate; /** sample rate (needed?) */ |
---|
35 | uint_t channels; /** number of channels */ |
---|
36 | uint_t n_coefs; /** number of coefficients (= fb->n_filters/2 +1) */ |
---|
37 | smpl_t lowfreq; /** lowest frequency for filters */ |
---|
38 | smpl_t highfreq; /** highest frequency for filters */ |
---|
39 | aubio_filterbank_t * fb; /** filter bank */ |
---|
40 | fvec_t * in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
41 | aubio_mfft_t * fft_dct; /** fft object for dct */ |
---|
42 | cvec_t * fftgrain_dct; /** output buffer for dct */ |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | aubio_mfcc_t * new_aubio_mfcc (uint_t win_s, uint_t samplerate ,uint_t n_coefs, smpl_t lowfreq, smpl_t highfreq, uint_t channels){ |
---|
47 | /** allocating space for mfcc object */ |
---|
48 | aubio_mfcc_t * mfcc = AUBIO_NEW(aubio_mfcc_t); |
---|
49 | |
---|
50 | //we need (n_coefs-1)*2 filters to obtain n_coefs coefficients after dct |
---|
51 | uint_t n_filters = (n_coefs-1)*2; |
---|
52 | |
---|
53 | mfcc->win_s=win_s; |
---|
54 | mfcc->samplerate=samplerate; |
---|
55 | mfcc->channels=channels; |
---|
56 | mfcc->n_coefs=n_coefs; |
---|
57 | mfcc->lowfreq=lowfreq; |
---|
58 | mfcc->highfreq=highfreq; |
---|
59 | |
---|
60 | /** filterbank allocation */ |
---|
61 | mfcc->fb = new_aubio_filterbank_mfcc(n_filters, mfcc->win_s, samplerate, lowfreq, highfreq); |
---|
62 | |
---|
63 | /** allocating space for fft object (used for dct) */ |
---|
64 | mfcc->fft_dct=new_aubio_mfft(n_filters, 1); |
---|
65 | |
---|
66 | /** allocating buffers */ |
---|
67 | mfcc->in_dct=new_fvec(mfcc->win_s, 1); |
---|
68 | |
---|
69 | mfcc->fftgrain_dct=new_cvec(n_filters, 1); |
---|
70 | |
---|
71 | return mfcc; |
---|
72 | }; |
---|
73 | |
---|
74 | void del_aubio_mfcc(aubio_mfcc_t *mf){ |
---|
75 | /** deleting filterbank */ |
---|
76 | del_aubio_filterbank(mf->fb); |
---|
77 | /** deleting mfft object */ |
---|
78 | del_aubio_mfft(mf->fft_dct); |
---|
79 | /** deleting buffers */ |
---|
80 | del_fvec(mf->in_dct); |
---|
81 | del_cvec(mf->fftgrain_dct); |
---|
82 | |
---|
83 | /** deleting mfcc object */ |
---|
84 | AUBIO_FREE(mf); |
---|
85 | } |
---|
86 | |
---|
87 | void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){ |
---|
88 | // compute filterbank |
---|
89 | aubio_filterbank_do(mf->fb, in, mf->in_dct); |
---|
90 | //TODO: check that zero padding |
---|
91 | // the following line seems useless since the in_dct buffer has the correct size |
---|
92 | //for(n = filter + 1; n < N; n++) result[n] = 0; |
---|
93 | |
---|
94 | aubio_dct_do(mf, mf->in_dct, out); |
---|
95 | |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){ |
---|
100 | uint_t i; |
---|
101 | //compute mag spectrum |
---|
102 | aubio_mfft_do (mf->fft_dct, in, mf->fftgrain_dct); |
---|
103 | //extract real part of fft grain |
---|
104 | //for(i=0; i<mf->n_coefs ;i++){ |
---|
105 | for(i=0; i<out->length;i++){ |
---|
106 | out->data[0][i]= mf->fftgrain_dct->norm[0][i] |
---|
107 | *COS(mf->fftgrain_dct->phas[0][i]); |
---|
108 | } |
---|
109 | return; |
---|
110 | } |
---|
111 | |
---|