shithub: aubio

Download patch

ref: 4621cd6431bcc8ddc7312dc0d98482cfeabaa17a
parent: c17a0ee55f9441312e444ded19ad5e8fd01f117a
author: Paul Brossier <piem@piem.org>
date: Thu Dec 3 20:46:40 EST 2009

examples: switch to mono

--- a/examples/aubiomfcc.c
+++ b/examples/aubiomfcc.c
@@ -32,17 +32,14 @@
 unsigned int pos = 0; /*frames%dspblocksize*/
 
 static int aubio_process(smpl_t **input, smpl_t **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++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
     if (pos == overlap_size-1) {         
@@ -49,7 +46,7 @@
       /* block loop */
       
       //compute mag spectrum
-      aubio_pvoc_do (pv,ibuf, fftgrain);
+      aubio_pvoc_do (pv, ibuf, fftgrain);
      
       //compute mfccs
       aubio_mfcc_do(mfcc, fftgrain, mfcc_out);
@@ -71,7 +68,7 @@
       if (output_filename == NULL) {
         outmsg("%f\t",frames*overlap_size/(float)samplerate);
         for (coef_cnt = 0; coef_cnt < n_coefs; coef_cnt++) {
-            outmsg("%f ", fvec_read_sample (mfcc_out, 0, coef_cnt) );
+            outmsg("%f ", fvec_read_sample (mfcc_out, coef_cnt) );
         }
         outmsg("\n");
       }
@@ -85,14 +82,14 @@
   examples_common_init(argc,argv);
 
   /* phase vocoder */
-  pv = new_aubio_pvoc (buffer_size, overlap_size, channels);
+  pv = new_aubio_pvoc (buffer_size, overlap_size);
 
-  fftgrain = new_cvec (buffer_size, channels);
+  fftgrain = new_cvec (buffer_size);
 
   //populating the filter
   mfcc = new_aubio_mfcc(buffer_size, n_filters, n_coefs, samplerate);
   
-  mfcc_out = new_fvec(n_coefs,channels);
+  mfcc_out = new_fvec(n_coefs);
   
   //process
   examples_common_process(aubio_process,process_print);
--- a/examples/aubionotes.c
+++ b/examples/aubionotes.c
@@ -18,7 +18,7 @@
 
 */
 
-#define AUBIO_UNSTABLE 1 // for fvec_median_channel
+#define AUBIO_UNSTABLE 1 // for fvec_median
 
 #include "utils.h"
 
@@ -49,16 +49,13 @@
 uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2);
 
 static int aubio_process(smpl_t **input, smpl_t **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++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
     if (pos == overlap_size-1) {         
@@ -66,7 +63,7 @@
       aubio_onset_do(o, ibuf, onset);
       
       aubio_pitch_do (pitchdet, ibuf, pitch_obuf);
-      pitch = fvec_read_sample(pitch_obuf, 0, 0);
+      pitch = fvec_read_sample(pitch_obuf, 0);
       if(median){
               note_append(note_buffer, pitch);
       }
@@ -73,7 +70,7 @@
 
       /* curlevel is negatif or 1 if silence */
       curlevel = aubio_level_detection(ibuf, silence);
-      if (fvec_read_sample(onset, 0, 0)) {
+      if (fvec_read_sample(onset, 0)) {
               /* test for silence */
               if (curlevel == 1.) {
                       if (median) isready = 0;
@@ -91,7 +88,7 @@
                       }
 
                       for (pos = 0; pos < overlap_size; pos++){
-                              obuf->data[0][pos] = woodblock->data[0][pos];
+                              obuf->data[pos] = woodblock->data[pos];
                       }
               }
       } else {
@@ -111,7 +108,7 @@
                       }
               } // if median
         for (pos = 0; pos < overlap_size; pos++)
-          obuf->data[0][pos] = 0.;
+          obuf->data[pos] = 0.;
       }
       /* end of block loop */
       pos = -1; /* so it will be zero next j loop */
@@ -130,9 +127,9 @@
 {
   uint_t i = 0;
   for (i = 0; i < note_buffer->length - 1; i++) {
-    note_buffer->data[0][i] = note_buffer->data[0][i + 1];
+    note_buffer->data[i] = note_buffer->data[i + 1];
   }
-  note_buffer->data[0][note_buffer->length - 1] = curnote;
+  note_buffer->data[note_buffer->length - 1] = curnote;
   return;
 }
 
@@ -141,26 +138,25 @@
 {
   uint_t i;
   for (i = 0; i < note_buffer->length; i++) {
-    note_buffer2->data[0][i] = note_buffer->data[0][i];
+    note_buffer2->data[i] = note_buffer->data[i];
   }
-  return fvec_median_channel (note_buffer2, 0);
+  return fvec_median (note_buffer2);
 }
 
 int main(int argc, char **argv) {
   examples_common_init(argc,argv);
 
-  o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels,
-          samplerate);
+  o = new_aubio_onset (onset_mode, buffer_size, overlap_size, samplerate);
   if (threshold != 0.) aubio_onset_set_threshold (o, threshold);
-  onset = new_fvec (1, channels);
+  onset = new_fvec (1);
 
   pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4,
-          overlap_size, channels, samplerate);
+          overlap_size, samplerate);
   aubio_pitch_set_tolerance (pitchdet, 0.7);
-  pitch_obuf = new_fvec (1, channels);
+  pitch_obuf = new_fvec (1);
   if (median) {
-      note_buffer = new_fvec (median, 1);
-      note_buffer2 = new_fvec (median, 1);
+      note_buffer = new_fvec (median);
+      note_buffer2 = new_fvec (median);
   }
 
   examples_common_process(aubio_process, process_print);
--- a/examples/aubioonset.c
+++ b/examples/aubioonset.c
@@ -30,18 +30,16 @@
   unsigned int j;       /*frames*/
   for (j=0;j<(unsigned)nframes;j++) {
     if(usejack) {
-      for (i=0;i<channels;i++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
-    if (pos == overlap_size-1) {         
+    if (pos == overlap_size-1) {
       /* block loop */
       aubio_onset_do (o, ibuf, onset);
-      if (fvec_read_sample(onset, 0, 0)) {
+      if ( fvec_read_sample(onset, 0) ) {
         fvec_copy (woodblock, obuf);
       } else {
         fvec_zeros (obuf);
@@ -54,20 +52,22 @@
   return 1;
 }
 
-static void process_print (void) {
-      /* output times in seconds, taking back some 
-       * delay to ensure the label is _before_ the
-       * actual onset */
-      if (!verbose && usejack) return;
-      smpl_t onset_found = fvec_read_sample(onset, 0, 0);
-      if (onset_found) {
-        if(frames >= 4) {
-          outmsg("%f\n",(frames - frames_delay + onset_found) 
-                  *overlap_size/(float)samplerate);
-        } else if (frames < frames_delay) {
-          outmsg("%f\n",0.);
-        }
-      }
+static void
+process_print (void)
+{
+  /* output times in seconds, taking back some delay to ensure the label is
+   * _before_ the actual onset */
+  if (!verbose && usejack)
+    return;
+  smpl_t onset_found = fvec_read_sample (onset, 0);
+  if (onset_found) {
+    if (frames >= 4) {
+      outmsg ("%f\n", (frames - frames_delay + onset_found)
+          * overlap_size / (float) samplerate);
+    } else if (frames < frames_delay) {
+      outmsg ("%f\n", 0.);
+    }
+  }
 }
 
 int main(int argc, char **argv) {
@@ -74,10 +74,9 @@
   frames_delay = 3;
   examples_common_init(argc,argv);
 
-  o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels,
-          samplerate);
+  o = new_aubio_onset (onset_mode, buffer_size, overlap_size, samplerate);
   if (threshold != 0.) aubio_onset_set_threshold (o, threshold);
-  onset = new_fvec (1, channels);
+  onset = new_fvec (1);
 
   examples_common_process(aubio_process,process_print);
 
--- a/examples/aubiopitch.c
+++ b/examples/aubiopitch.c
@@ -26,22 +26,19 @@
 fvec_t *pitch;
 
 static int aubio_process(smpl_t **input, smpl_t **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++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
     if (pos == overlap_size-1) {         
       /* block loop */
       aubio_pitch_do (o, ibuf, pitch);
-      if (fvec_read_sample(pitch, 0, 0)) {
+      if (fvec_read_sample(pitch, 0)) {
         for (pos = 0; pos < overlap_size; pos++){
           // TODO, play sine at this freq
         }
@@ -58,7 +55,7 @@
 
 static void process_print (void) {
       if (!verbose && usejack) return;
-      smpl_t pitch_found = fvec_read_sample(pitch, 0, 0);
+      smpl_t pitch_found = fvec_read_sample(pitch, 0);
       outmsg("%f %f\n",(frames) 
               *overlap_size/(float)samplerate, pitch_found);
 }
@@ -66,9 +63,8 @@
 int main(int argc, char **argv) {
   examples_common_init(argc,argv);
 
-  o = new_aubio_pitch (onset_mode, buffer_size, overlap_size, channels,
-          samplerate);
-  pitch = new_fvec (1, channels);
+  o = new_aubio_pitch (onset_mode, buffer_size, overlap_size, samplerate);
+  pitch = new_fvec (1);
 
   examples_common_process(aubio_process,process_print);
 
--- a/examples/aubioquiet.c
+++ b/examples/aubioquiet.c
@@ -25,27 +25,24 @@
 
 int aubio_process(smpl_t **input, smpl_t **output, int nframes);
 int aubio_process(smpl_t **input, smpl_t **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++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
     if (pos == overlap_size-1) {         
       /* test for silence */
       if (aubio_silence_detection(ibuf, silence)==1) {
-	if (wassilence==1) issilence = 1;
-	else issilence = 2;
+        if (wassilence==1) issilence = 1;
+        else issilence = 2;
         wassilence=1;
       } else { 
-	if (wassilence<=0) issilence = 0;
-	else issilence = -1;
+        if (wassilence<=0) issilence = 0;
+        else issilence = -1;
         wassilence=0;
       }
       /* end of block loop */
--- a/examples/aubiotrack.c
+++ b/examples/aubiotrack.c
@@ -28,23 +28,20 @@
 smpl_t isonset = 0;
 
 static int aubio_process(smpl_t **input, smpl_t **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++) {
-        /* write input to datanew */
-        fvec_write_sample(ibuf, input[i][j], i, pos);
-        /* put synthnew in output */
-        output[i][j] = fvec_read_sample(obuf, i, pos);
-      }
+      /* write input to datanew */
+      fvec_write_sample(ibuf, input[0][j], pos);
+      /* put synthnew in output */
+      output[0][j] = fvec_read_sample(obuf, pos);
     }
     /*time for fft*/
     if (pos == overlap_size-1) {         
       /* block loop */
       aubio_tempo_do (bt,ibuf,tempo_out);
-      istactus = fvec_read_sample (tempo_out, 0, 0);
-      isonset = fvec_read_sample (tempo_out, 0, 1);
+      istactus = fvec_read_sample (tempo_out, 0);
+      isonset = fvec_read_sample (tempo_out, 1);
       if (istactus > 0.) {
         fvec_copy (woodblock, obuf);
       } else {
@@ -75,8 +72,8 @@
   /* override default settings */
   examples_common_init(argc,argv);
 
-  tempo_out = new_fvec(2,channels);
-  bt = new_aubio_tempo(onset_mode,buffer_size,overlap_size,channels, samplerate);
+  tempo_out = new_fvec(2);
+  bt = new_aubio_tempo(onset_mode,buffer_size,overlap_size, samplerate);
   if (threshold != 0.) aubio_tempo_set_threshold (bt, threshold);
 
   examples_common_process(aubio_process,process_print);
--- a/examples/sndfileio.c
+++ b/examples/sndfileio.c
@@ -95,7 +95,7 @@
 aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * fmodel, const char *outputname) {
         aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
         f->samplerate    = fmodel->samplerate;
-        f->channels      = fmodel->channels;
+        f->channels      = 1; //fmodel->channels;
         f->format        = fmodel->format;
         aubio_sndfile_open_wo(f, outputname);
         return f;
@@ -124,7 +124,7 @@
 
 /* read frames from file in data 
  *  return the number of frames actually read */
-int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t * read) {
+int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t ** read) {
         sf_count_t read_frames;
         int i,j, channels = f->channels;
         int nsamples = frames*channels;
@@ -149,7 +149,7 @@
 
         /* de-interleaving data  */
         for (i=0; i<channels; i++) {
-                pread = (smpl_t *)fvec_get_channel(read,i);
+                pread = (smpl_t *)fvec_get_data(read[i]);
                 for (j=0; j<aread; j++) {
                         pread[j] = (smpl_t)f->tmpdata[channels*j+i];
                 }
@@ -157,10 +157,49 @@
         return aread;
 }
 
+int
+aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read)
+{
+  sf_count_t read_frames;
+  int i, j, channels = f->channels;
+  int nsamples = frames * channels;
+  int aread;
+  smpl_t *pread;
+
+  /* allocate data for de/interleaving reallocated when needed. */
+  if (nsamples >= f->size) {
+    AUBIO_ERR ("Maximum aubio_sndfile_read buffer size exceeded.");
+    return -1;
+    /*
+    AUBIO_FREE(f->tmpdata);
+    f->tmpdata = AUBIO_ARRAY(float,nsamples);
+    */
+  }
+  //f->size = nsamples;
+
+  /* do actual reading */
+  read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
+
+  aread = (int) FLOOR (read_frames / (float) channels);
+
+  /* de-interleaving data  */
+  pread = (smpl_t *) fvec_get_data (read);
+  for (i = 0; i < channels; i++) {
+    for (j = 0; j < aread; j++) {
+      pread[j] += (smpl_t) f->tmpdata[channels * j + i];
+    }
+  }
+  for (j = 0; j < aread; j++) {
+    pread[j] /= (smpl_t)channels;
+  }
+
+  return aread;
+}
+
 /* write 'frames' samples to file from data 
  *   return the number of frames actually written 
  */
-int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t * write) {
+int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t ** write) {
         sf_count_t written_frames = 0;
         int i, j,	channels = f->channels;
         int nsamples = channels*frames;
@@ -179,7 +218,7 @@
 
         /* interleaving data  */
         for (i=0; i<channels; i++) {
-                pwrite = (smpl_t *)fvec_get_channel(write,i);
+                pwrite = (smpl_t *)fvec_get_data(write[i]);
                 for (j=0; j<frames; j++) {
                         f->tmpdata[channels*j+i] = (float)pwrite[j];
                 }
--- a/examples/sndfileio.h
+++ b/examples/sndfileio.h
@@ -48,11 +48,11 @@
 /** 
  * Read frames data from file 
  */
-int aubio_sndfile_read(aubio_sndfile_t * file, int frames, fvec_t * read);
+int aubio_sndfile_read(aubio_sndfile_t * file, int frames, fvec_t ** read);
 /** 
  * Write data of length frames to file
  */
-int aubio_sndfile_write(aubio_sndfile_t * file, int frames, fvec_t * write);
+int aubio_sndfile_write(aubio_sndfile_t * file, int frames, fvec_t ** write);
 /**
  * Close file and delete file object
  */
--- a/examples/utils.c
+++ b/examples/utils.c
@@ -60,7 +60,6 @@
 smpl_t silence = -90.;
 uint_t buffer_size = 512;       //1024;
 uint_t overlap_size = 256;      //512;
-uint_t channels = 1;
 uint_t samplerate = 44100;
 
 
@@ -207,7 +206,7 @@
   /* parse command line arguments */
   parse_args (argc, argv);
 
-  woodblock = new_fvec (buffer_size, 1);
+  woodblock = new_fvec (overlap_size);
   if (output_filename || usejack) {
     /* dummy assignement to keep egcs happy */
     found_wood = (onsetfile = new_aubio_sndfile_ro (onset_filename)) ||
@@ -220,7 +219,7 @@
   }
   if (onsetfile) {
     /* read the output sound once */
-    aubio_sndfile_read (onsetfile, overlap_size, woodblock);
+    aubio_sndfile_read_mono (onsetfile, overlap_size, woodblock);
   }
 
   if (!usejack) {
@@ -232,7 +231,6 @@
     }
     if (verbose)
       aubio_sndfile_info (file);
-    channels = aubio_sndfile_channels (file);
     samplerate = aubio_sndfile_samplerate (file);
     if (output_filename != NULL)
       fileout = new_aubio_sndfile_wo (file, output_filename);
@@ -255,8 +253,9 @@
   }
 #endif /* HAVE_LASH */
 
-  ibuf = new_fvec (overlap_size, channels);
-  obuf = new_fvec (overlap_size, channels);
+  uint_t i;
+  ibuf = new_fvec (overlap_size);
+  obuf = new_fvec (overlap_size);
 
 }
 
@@ -264,6 +263,7 @@
 void
 examples_common_del (void)
 {
+  uint_t i;
   del_fvec (ibuf);
   del_fvec (obuf);
   del_fvec (woodblock);
@@ -282,7 +282,7 @@
 
 #if HAVE_JACK
     debug ("Jack init ...\n");
-    jack_setup = new_aubio_jack (channels, channels,
+    jack_setup = new_aubio_jack (1, 1,
         0, 1, (aubio_process_func_t) process_func);
     debug ("Jack activation ...\n");
     aubio_jack_activate (jack_setup);
@@ -300,12 +300,12 @@
 
     frames = 0;
 
-    while ((signed) overlap_size == aubio_sndfile_read (file, overlap_size,
-            ibuf)) {
-      process_func (ibuf->data, obuf->data, overlap_size);
+    while ((signed) overlap_size ==
+        aubio_sndfile_read_mono (file, overlap_size, ibuf)) {
+      process_func (&ibuf->data, &obuf->data, overlap_size);
       print ();
       if (output_filename != NULL) {
-        aubio_sndfile_write (fileout, overlap_size, obuf);
+        aubio_sndfile_write (fileout, overlap_size, &obuf);
       }
       frames++;
     }
@@ -327,7 +327,7 @@
   uint_t i;
   fvec_zeros(obuf);
   for (i = 0; (signed) i < frames_delay; i++) {
-    process_func (ibuf->data, obuf->data, overlap_size);
+    process_func (&ibuf->data, &obuf->data, overlap_size);
     print ();
   }
 }
--- a/examples/utils.h
+++ b/examples/utils.h
@@ -73,7 +73,6 @@
 extern int usejack;
 extern uint_t buffer_size;
 extern uint_t overlap_size;
-extern uint_t channels;
 extern uint_t samplerate;
 
 extern fvec_t *ibuf;