shithub: aubio

Download patch

ref: fdf39bad6a0efebca0fe1dba5f44861e89df20bd
parent: 97886fac26f2f105871453d8dd8ae2c489c710dc
parent: 7c6c806dda27c76b03afac7bfeecdaa88371a0ed
author: Amaury Hazan <[email protected]>
date: Fri Sep 7 06:49:45 EDT 2007

merged from mtg

--- a/examples/aubiomfcc.c
+++ b/examples/aubiomfcc.c
@@ -25,6 +25,7 @@
 int aubio_process(float **input, float **output, int nframes) {
   unsigned int i;       /*channels*/
   unsigned int j;       /*frames*/
+  
   for (j=0;j<(unsigned)nframes;j++) {
     if(usejack) {
       for (i=0;i<channels;i++) {
@@ -41,18 +42,18 @@
       //compute mag spectrum
       aubio_pvoc_do (pv,ibuf, fftgrain);
      
+      uint_t n_coefs= n_filters/2 +1;
       uint_t coef_cnt;
-      uint_t n_filters=20;
-      smpl_t outbuf[20];
+       
 
-      for (coef_cnt=0; coef_cnt<n_filters ; coef_cnt++)
-        outbuf[coef_cnt]=0.f;
+      for (coef_cnt=0; coef_cnt<n_coefs ; coef_cnt++)
+        mfcc_outbuf[coef_cnt]=0.f;
        
       //compute mfccs
-      aubio_mffc_do(fftgrain->norm, nframes, mf, outbuf);
+      aubio_mffc_do(fftgrain->norm, nframes, mf, mfcc_outbuf, fft_dct, fftgrain_dct);
       
-      for (coef_cnt=0; coef_cnt<n_filters ; coef_cnt++)
-        outmsg("%f ",outbuf[coef_cnt]);
+      for (coef_cnt=0; coef_cnt<n_coefs ; coef_cnt++)
+        outmsg("%f ",mfcc_outbuf[coef_cnt]);
       outmsg("\n");
       
       
@@ -84,13 +85,11 @@
   examples_common_init(argc,argv);
   
   //allocate and initialize mel filter bank
-  uint_t n_filters=20;
-  uint_t nyquist= samplerate / 2.; 
-  smpl_t lowfreq=80.f;
-  smpl_t highfreq=18000.f;
+  
 
+  //allocating global mf (in utils.c)
   uint_t banksize = (uint) ( sizeof(aubio_mel_filter));
-  aubio_mel_filter * mf = (aubio_mel_filter *)getbytes(banksize);
+  mf = (aubio_mel_filter *)getbytes(banksize);
 
   mf->n_filters = 20;
   mf->filters = (smpl_t **)getbytes(mf->n_filters * sizeof(smpl_t *));
--- a/examples/utils.c
+++ b/examples/utils.c
@@ -60,7 +60,21 @@
 int isonset = 0;
 aubio_pickpeak_t * parms;
 
+/* mfcc objects */
+//parameters
+uint_t n_filters=20;
+uint_t nyquist= samplerate / 2.; 
+smpl_t lowfreq=80.f;
+smpl_t highfreq=18000.f;
+// filterbank object
+aubio_mel_filter * mf;
 
+// DCT mfft and result storage
+aubio_mfft * fft_dct;
+cvec_t * fftgrain_dct;
+smpl_t mfcc_outbuf[11];
+
+
 /* pitch objects */
 smpl_t pitch               = 0.;
 aubio_pitchdetection_t * pitchdet;
@@ -300,6 +314,9 @@
   obuf      = new_fvec(overlap_size, channels);
   fftgrain  = new_cvec(buffer_size, channels);
 
+  //init for mfcc process
+  fftgrain_dct= new_cvec(n_filters, channels);
+
   if (usepitch) {
     pitchdet = new_aubio_pitchdetection(buffer_size*4, 
         overlap_size, channels, samplerate, type_pitch, mode_pitch);
@@ -312,6 +329,11 @@
   }
   /* phase vocoder */
   pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
+  
+  // dct phase vocoder
+  //TODO: check size
+  fft_dct = new_aubio_mfft(n_filters, channels);
+
   /* onsets */
   parms = new_aubio_peakpicker(threshold);
   o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
@@ -345,6 +367,11 @@
   del_cvec(fftgrain);
   del_fvec(onset);
   del_fvec(woodblock);
+  
+  //mffc related
+  del_aubio_mfft(fft_dct);
+  del_cvec(fftgrain_dct);
+  
   aubio_cleanup();
 }
 
--- a/examples/utils.h
+++ b/examples/utils.h
@@ -97,6 +97,18 @@
 extern int isonset;
 extern aubio_pickpeak_t * parms;
 
+/* mfcc objects */
+// params
+extern uint_t n_filters;
+extern uint_t nyquist; 
+extern smpl_t lowfreq;
+extern smpl_t highfreq;
+// filterbank object
+extern aubio_mel_filter * mf;
+// DCT pvoc and result storage
+extern aubio_mfft_t * fft_dct;
+extern cvec_t * fftgrain_dct;
+extern smpl_t mfcc_outbuf[20];
 
 /* pitch objects */
 extern smpl_t pitch;
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,7 +21,8 @@
 	beattracking.h \
 	onset.h \
 	tempo.h \
-	filter.h 
+	filter.h \
+	mfcc.h
 
 nodist_pkginclude_HEADERS = config.h
 
@@ -70,6 +71,8 @@
 	tempo.h \
 	filter.c \
 	filter.h \
+	mfcc.h \
+	mfcc.c 
 
 AM_CFLAGS = @AUBIO_CFLAGS@ @FFTWLIB_CFLAGS@ @SAMPLERATE_CFLAGS@
 libaubio_la_LIBADD = @FFTWLIB_LIBS@ @SAMPLERATE_LIBS@ @LTLIBOBJS@
--- a/src/filterbank.c
+++ b/src/filterbank.c
@@ -20,7 +20,17 @@
 
 */
 
+#include "aubio_priv.h"
 #include "filterbank.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;
+};
 
 // Initialization
 
--- a/src/filterbank.h
+++ b/src/filterbank.h
@@ -22,15 +22,14 @@
 #ifndef AUBIOFILTERBANK_H
 #define AUBIOFILTERBANK_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-// 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;
 
+typedef struct aubio_mel_filter_ aubio_mel_filter;
+
 // Initialization
 
 /** \brief A function to initialise a mel filter bank 
@@ -37,6 +36,10 @@
  * 
  * 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);
+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);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif
--- a/src/mfcc.c
+++ b/src/mfcc.c
@@ -20,13 +20,25 @@
 
 */
 
+#include "aubio_priv.h"
+#include "sample.h"
+#include "fft.h"
+#include "mfcc.h"
+#include "math.h"
 
-#include "mffc.h"
+/*
+new_aubio_mfcc
+aubio_mfcc_do
+del_aubio_mfcc
+*/
 
 // Computation
+// Added last two arguments to be able to pass from example
 
-int aubio_mfcc_do(const float *data, const int N, const void *argv, float *result){
 
+
+int aubio_mfcc_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t * fft_dct, cvec_t * fftgrain_dct){
+
     aubio_mel_filter *f;
     int n, filter;
 
@@ -37,18 +49,20 @@
         for(n = 0; n < N; n++){
             result[filter] += data[n] * f->filters[filter][n];
         }
-        result[filter] = log(result[filter] < XTRACT_LOG_LIMIT ? XTRACT_LOG_LIMIT : result[filter]);
+        result[filter] = LOG(result[filter] < XTRACT_LOG_LIMIT ? XTRACT_LOG_LIMIT : result[filter]);
     }
 
-    //TODO: check that zero padding
+    //TODO: check that zero padding 
     for(n = filter + 1; n < N; n++) result[n] = 0; 
     
-    aubio_dct_do(result, f->n_filters, NULL, result);
+    aubio_dct_do(result, f->n_filters, NULL, result, fft_dct, fftgrain_dct);
     
     return XTRACT_SUCCESS;
 }
 
-int aubio_dct_do(const float *data, const int N, const void *argv, float *result){
+// Added last two arguments to be able to pass from example
+
+int aubio_dct_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t * fft_dct, cvec_t * fftgrain_dct){
     
     
     //call aubio p_voc in dct setting
@@ -55,23 +69,18 @@
 
     //TODO: fvec as input? Remove data length, N?
 
+    fvec_t * momo = new_fvec(20, 1);
+    momo->data = data;
+    
     //compute mag spectrum
-    aubio_pvoc_do (pv,data, fftgrain);
+    aubio_mfft_do (fft_dct, data, fftgrain_dct);
 
     int i;
     //extract real part of fft grain
     for(i=0; i<N ;i++){
-      result[i]= fftgrain->norm[i]*cos(fftgrain->phase[i]);
+      result[i]= fftgrain_dct->norm[0][i]*COS(fftgrain_dct->phas[0][i]);
     }
-    
-    /*
-    fftwf_plan plan;
-    
-    plan = 
-        fftwf_plan_r2r_1d(N, (float *) data, result, FFTW_REDFT00, FFTW_ESTIMATE);
-    
-    fftwf_execute(plan);
-    fftwf_destroy_plan(plan);*/
+
 
     return XTRACT_SUCCESS;
 }
--- a/src/mfcc.h
+++ b/src/mfcc.h
@@ -23,11 +23,40 @@
 #ifndef MFCC_H 
 #define MFCC_H 
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include "filterbank.h"
 
-//libXtract enums
+//libXtract constants and enums
 // TODO: remove them 
 
+#define XTRACT_SQ(a) ((a) * (a))
+#define XTRACT_MIN(a, b) ((a) < (b) ? (a) : (b))
+#define XTRACT_MAX(a, b) ((a) > (b) ? (a) : (b))
+#define XTRACT_NEEDS_FFTW printf("LibXtract must be compiled with fftw support to use this function.\n")
+#define XTRACT_VERY_SMALL_NUMBER 2e-42
+#define XTRACT_LOG_LIMIT XTRACT_VERY_SMALL_NUMBER
+#define XTRACT_LOG_LIMIT_DB -96.0
+#define XTRACT_DB_SCALE_OFFSET 96.0
+#define XTRACT_VERY_BIG_NUMBER 2e42
+#define XTRACT_SR_UPPER_LIMIT 192000.0
+#define XTRACT_SR_LOWER_LIMIT 22050.0
+#define XTRACT_SR_DEFAULT 44100.0
+#define XTRACT_FUNDAMENTAL_DEFAULT 440.0
+#define XTRACT_CHECK_nyquist if(!nyquist) nyquist = XTRACT_SR_DEFAULT / 2
+#define XTRACT_CHECK_q if(!q) q = XTRACT_SR_DEFAULT / N
+#define XTRACT_IS_ODD(x) (x % 2 != 0 ? 1 : 0) 
+#define XTRACT_SR_LIMIT SR_UPPER_LIMIT
+#define XTRACT_FFT_BANDS_MIN 16
+#define XTRACT_FFT_BANDS_MAX 65536
+#define XTRACT_FFT_BANDS_DEF 1024
+#define XTRACT_SPEC_BW_MIN 0.168 /* Minimum spectral bandwidth \
+            (= SR_LOWER_LIMIT / FFT_BANDS_MAX*/ 
+#define XTRACT_SPEC_BW_MAX 12000.0 /* SR_UPPER_LIMIT / FFT_BANDS_MIN */
+#define XTRACT_SPEC_BW_DEF 43.066 /* SR_DEFAULT / FFT_BANDS_DEF */
+
 /** \brief Enumeration of feature initialisation functions */
 enum xtract_feature_init_ {
     XTRACT_INIT_MFCC = 100,
@@ -131,8 +160,10 @@
  * 
  * The data structure pointed to by *argv must be obtained by first calling xtract_init_mfcc
  */
-int aubio_mfcc_do(const float *data, const int N, const void *argv, float *result);
 
+
+int aubio_mfcc_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t *fft_dct, cvec_t *fftgrain_dct);
+
 /** \brief Extract the Discrete Cosine transform of a time domain signal
  * \param *data: a pointer to the first element in an array of floats representing an audio vector
  * \param N: the number of array elements to be considered
@@ -139,7 +170,10 @@
  * \param *argv: a pointer to NULL 
  * \param *result: a pointer to an array containing resultant dct coefficients
  */
-int aubio_dct_do(const float *data, const int N, const void *argv, float *result);
+int aubio_dct_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t *fft_dct, cvec_t *fftgrain_dct);
 
+#ifdef __cplusplus
+}
+#endif
 
 #endif