From: Amaury Hazan Date: Thu, 6 Sep 2007 00:33:11 +0000 (+0200) Subject: small adds X-Git-Tag: 0.4.0-beta1~969^2~31^2~3 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=71d3bf0fb3571cbb616a0b1138e73a39fc41ffd1;p=aubio.git small adds --- diff --git a/examples/aubiomfcc.c b/examples/aubiomfcc.c index 3dcd2ab9..7366b3cc 100644 --- a/examples/aubiomfcc.c +++ b/examples/aubiomfcc.c @@ -40,11 +40,21 @@ int aubio_process(float **input, float **output, int nframes) { //compute mag spectrum aubio_pvoc_do (pv,ibuf, fftgrain); - + + uint_t coef_cnt; + uint_t n_filters=20; + smpl_t outbuf[20]; + + for (coef_cnt=0; coef_cntnorm, nframes, filterbank, outbuf); + for (coef_cnt=0; coef_cntfilters[n] = (smpl_t *)getbytes((buffer_size/2+1) * sizeof(smpl_t)); //populating the filter - new_aubio_mfcc(buffer_size, nyquist, XTRACT_EQUAL_GAIN, 80.0f, 18000.0f, mf->n_filters, mf->filters); + aubio_mfcc_init(buffer_size, nyquist, XTRACT_EQUAL_GAIN, lowfreq, highfreq, mf->n_filters, mf->filters); //process examples_common_process(aubio_process,process_print); diff --git a/src/Makefile.am b/src/Makefile.am index 1ca5d68f..bd952e21 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,8 @@ pkginclude_HEADERS = aubio.h \ beattracking.h \ onset.h \ tempo.h \ - filter.h + filter.h + nodist_pkginclude_HEADERS = config.h lib_LTLIBRARIES = libaubio.la @@ -68,7 +69,7 @@ libaubio_la_SOURCES = aubio.h \ tempo.c \ tempo.h \ filter.c \ - filter.h + filter.h \ AM_CFLAGS = @AUBIO_CFLAGS@ @FFTWLIB_CFLAGS@ @SAMPLERATE_CFLAGS@ libaubio_la_LIBADD = @FFTWLIB_LIBS@ @SAMPLERATE_LIBS@ @LTLIBOBJS@ diff --git a/src/aubiofilterbank.c b/src/aubiofilterbank.c deleted file mode 100644 index 6039998e..00000000 --- a/src/aubiofilterbank.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - Copyright (C) 2007 Amaury Hazan - Ported to aubio from LibXtract - http://libxtract.sourceforge.net/ - - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#include "aubiofilterbank.h" - -// Initialization - -int aubio_mfcc_init(int N, smpl_t nyquist, int style, smpl_t freq_min, smpl_t freq_max, int freq_bands, smpl_t **fft_tables){ - - int n, i, k, *fft_peak, M, next_peak; - smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val, - freq_bw_mel, *mel_peak, *height_norm, *lin_peak; - - mel_peak = height_norm = lin_peak = NULL; - fft_peak = NULL; - norm = 1; - - mel_freq_max = 1127 * log(1 + freq_max / 700); - mel_freq_min = 1127 * log(1 + freq_min / 700); - freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands; - - mel_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); - /* +2 for zeros at start and end */ - lin_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); - fft_peak = (int *)malloc((freq_bands + 2) * sizeof(int)); - height_norm = (smpl_t *)malloc(freq_bands * sizeof(smpl_t)); - - if(mel_peak == NULL || height_norm == NULL || - lin_peak == NULL || fft_peak == NULL) - return XTRACT_MALLOC_FAILED; - - M = N >> 1; - - mel_peak[0] = mel_freq_min; - lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1); - fft_peak[0] = lin_peak[0] / nyquist * M; - - - for (n = 1; n <= freq_bands; n++){ - /*roll out peak locations - mel, linear and linear on fft window scale */ - mel_peak[n] = mel_peak[n - 1] + freq_bw_mel; - lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1); - fft_peak[n] = lin_peak[n] / nyquist * M; - } - - for (n = 0; n < freq_bands; n++){ - /*roll out normalised gain of each peak*/ - if (style == XTRACT_EQUAL_GAIN){ - height = 1; - norm_fact = norm; - } - else{ - height = 2 / (lin_peak[n + 2] - lin_peak[n]); - norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0])); - } - height_norm[n] = height * norm_fact; - } - - i = 0; - - for(n = 0; n < freq_bands; n++){ - - /*calculate the rise increment*/ - if(n > 0) - inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]); - else - inc = height_norm[n] / fft_peak[n]; - val = 0; - - /*zero the start of the array*/ - for(k = 0; k < i; k++) - fft_tables[n][k] = 0.f; - - /*fill in the rise */ - for(; i <= fft_peak[n]; i++){ - fft_tables[n][i] = val; - val += inc; - } - - /*calculate the fall increment */ - inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]); - - val = 0; - next_peak = fft_peak[n + 1]; - - /*reverse fill the 'fall' */ - for(i = next_peak; i > fft_peak[n]; i--){ - fft_tables[n][i] = val; - val += inc; - } - - /*zero the rest of the array*/ - for(k = next_peak + 1; k < N; k++) - fft_tables[n][k] = 0.f; - } - - free(mel_peak); - free(lin_peak); - free(height_norm); - free(fft_peak); - - return XTRACT_SUCCESS; - -} diff --git a/src/aubiofilterbank.h b/src/aubiofilterbank.h deleted file mode 100644 index 8a8fecf8..00000000 --- a/src/aubiofilterbank.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright (C) 2007 Amaury Hazan - Ported to aubio from LibXtract - http://libxtract.sourceforge.net/ - - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#ifndef AUBIOFILTERBANK_H -#define AUBIOFILTERBANK_H - - -// Struct Declaration - -/** \brief A structure to store a set of n_filters Mel filters */ -typedef struct aubio_mel_filter_ { - int n_filters; - smpl_t **filters; -} aubio_mel_filter; - -// Initialization - -/** \brief A function to initialise a mel filter bank - * - * It is up to the caller to pass in a pointer to memory allocated for freq_bands arrays of length N. This function populates these arrays with magnitude coefficients representing the mel filterbank on a linear scale - */ -int aubio_mfcc_init(int N, float nyquist, int style, float freq_min, float freq_max, int freq_bands, smpl_t ** fft_tables); - -#endif diff --git a/src/filterbank.c b/src/filterbank.c new file mode 100644 index 00000000..d6aa1e8d --- /dev/null +++ b/src/filterbank.c @@ -0,0 +1,123 @@ +/* + Copyright (C) 2007 Amaury Hazan + Ported to aubio from LibXtract + http://libxtract.sourceforge.net/ + + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "filterbank.h" + +// Initialization + +int aubio_mfcc_init(int N, smpl_t nyquist, int style, smpl_t freq_min, smpl_t freq_max, int freq_bands, smpl_t **fft_tables){ + + int n, i, k, *fft_peak, M, next_peak; + smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val, + freq_bw_mel, *mel_peak, *height_norm, *lin_peak; + + mel_peak = height_norm = lin_peak = NULL; + fft_peak = NULL; + norm = 1; + + mel_freq_max = 1127 * log(1 + freq_max / 700); + mel_freq_min = 1127 * log(1 + freq_min / 700); + freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands; + + mel_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); + /* +2 for zeros at start and end */ + lin_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); + fft_peak = (int *)malloc((freq_bands + 2) * sizeof(int)); + height_norm = (smpl_t *)malloc(freq_bands * sizeof(smpl_t)); + + if(mel_peak == NULL || height_norm == NULL || + lin_peak == NULL || fft_peak == NULL) + return XTRACT_MALLOC_FAILED; + + M = N >> 1; + + mel_peak[0] = mel_freq_min; + lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1); + fft_peak[0] = lin_peak[0] / nyquist * M; + + + for (n = 1; n <= freq_bands; n++){ + /*roll out peak locations - mel, linear and linear on fft window scale */ + mel_peak[n] = mel_peak[n - 1] + freq_bw_mel; + lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1); + fft_peak[n] = lin_peak[n] / nyquist * M; + } + + for (n = 0; n < freq_bands; n++){ + /*roll out normalised gain of each peak*/ + if (style == XTRACT_EQUAL_GAIN){ + height = 1; + norm_fact = norm; + } + else{ + height = 2 / (lin_peak[n + 2] - lin_peak[n]); + norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0])); + } + height_norm[n] = height * norm_fact; + } + + i = 0; + + for(n = 0; n < freq_bands; n++){ + + /*calculate the rise increment*/ + if(n > 0) + inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]); + else + inc = height_norm[n] / fft_peak[n]; + val = 0; + + /*zero the start of the array*/ + for(k = 0; k < i; k++) + fft_tables[n][k] = 0.f; + + /*fill in the rise */ + for(; i <= fft_peak[n]; i++){ + fft_tables[n][i] = val; + val += inc; + } + + /*calculate the fall increment */ + inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]); + + val = 0; + next_peak = fft_peak[n + 1]; + + /*reverse fill the 'fall' */ + for(i = next_peak; i > fft_peak[n]; i--){ + fft_tables[n][i] = val; + val += inc; + } + + /*zero the rest of the array*/ + for(k = next_peak + 1; k < N; k++) + fft_tables[n][k] = 0.f; + } + + free(mel_peak); + free(lin_peak); + free(height_norm); + free(fft_peak); + + return XTRACT_SUCCESS; + +} diff --git a/src/filterbank.h b/src/filterbank.h new file mode 100644 index 00000000..8a8fecf8 --- /dev/null +++ b/src/filterbank.h @@ -0,0 +1,42 @@ +/* + Copyright (C) 2007 Amaury Hazan + Ported to aubio from LibXtract + http://libxtract.sourceforge.net/ + + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef AUBIOFILTERBANK_H +#define AUBIOFILTERBANK_H + + +// Struct Declaration + +/** \brief A structure to store a set of n_filters Mel filters */ +typedef struct aubio_mel_filter_ { + int n_filters; + smpl_t **filters; +} aubio_mel_filter; + +// Initialization + +/** \brief A function to initialise a mel filter bank + * + * It is up to the caller to pass in a pointer to memory allocated for freq_bands arrays of length N. This function populates these arrays with magnitude coefficients representing the mel filterbank on a linear scale + */ +int aubio_mfcc_init(int N, float nyquist, int style, float freq_min, float freq_max, int freq_bands, smpl_t ** fft_tables); + +#endif diff --git a/src/mfcc.c b/src/mfcc.c index 4205b2c5..305125b0 100644 --- a/src/mfcc.c +++ b/src/mfcc.c @@ -22,7 +22,6 @@ #include "mffc.h" -#include "aubiofilterbank.h" // Computation @@ -59,4 +58,4 @@ int aubio_dct_do(const float *data, const int N, const void *argv, float *result fftwf_destroy_plan(plan); return XTRACT_SUCCESS; -} \ No newline at end of file +} diff --git a/src/mfcc.h b/src/mfcc.h index 856f77c7..c9251349 100644 --- a/src/mfcc.h +++ b/src/mfcc.h @@ -23,9 +23,7 @@ #ifndef MFCC_H #define MFCC_H -#include "aubiofilterbank.h" - -#define NYQUIST 22050.f +#include "filterbank.h" //libXtract enums // TODO: remove them