examples/utils.c: do not add . at end of path
[aubio.git] / examples / utils.c
index 6741840..0987531 100644 (file)
+/*
+  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
 
-#include "aubio.h"
-
-#ifndef JACK_SUPPORT
-#define JACK_SUPPORT 0
-#endif
-
-#include <getopt.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h> /* for isfinite */
-#include "utils.h"
-
-/* not supported yet */
-#ifdef LADCCA_SUPPORT
-#include <ladcca/ladcca.h>
-cca_client_t * aubio_cca_client;
-#endif /* LADCCA_SUPPORT */
-
-/* settings */
-const char * output_filename = NULL;
-const char * input_filename  = NULL;
-const char * onset_filename  = "/usr/share/sounds/aubio/woodblock.aiff";
-int frames = 0;
-int verbose = 0;
-int usejack = 0;
-int usedoubled = 1;
+  This file is part of aubio.
 
+  aubio 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 3 of the License, or
+  (at your option) any later version.
 
-/* energy,specdiff,hfc,complexdomain,phase */
-aubio_onsetdetection_type type_onset  = hfc;
-aubio_onsetdetection_type type_onset2 = complexdomain;
-smpl_t threshold                      = 0.3;
-smpl_t threshold2                     = -90.;
-uint_t buffer_size                    = 1024;
-uint_t overlap_size                   = 512;
-uint_t channels                       = 1;
-uint_t samplerate                     = 44100;
+  aubio 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 aubio.  If not, see <http://www.gnu.org/licenses/>.
 
-aubio_file_t * file = NULL;
-aubio_file_t * fileout = NULL;
+*/
 
-aubio_pvoc_t * pv;
-fvec_t * ibuf;
-fvec_t * obuf;
-cvec_t * fftgrain;
-fvec_t * woodblock;
-aubio_onsetdetection_t *o;
-aubio_onsetdetection_t *o2;
-fvec_t *onset;
-fvec_t *onset2;
-int isonset = 0;
-aubio_pickpeak_t * parms;
+/**
 
+  This file includes some tools common to all examples. Code specific to the
+  algorithm performed by each program should go in the source file of that
+  program instead.
 
-/* pitch objects */
-smpl_t pitch               = 0.;
-aubio_pitchdetection_t * pitchdet;
-aubio_pitchdetection_type mode = yin; // mcomb
-uint_t median         = 6;
+*/
 
-fvec_t * note_buffer  = NULL;
-fvec_t * note_buffer2 = NULL;
-smpl_t curlevel       = 0.;
-smpl_t maxonset       = 0.;
-
-/* midi objects */
-aubio_midi_player_t * mplay; 
-aubio_midi_driver_t * mdriver; 
-aubio_midi_event_t  * event;
+#include "utils.h"
+#ifdef HAVE_JACK
+#include "jackio.h"
+#endif /* HAVE_JACK */
 
-smpl_t curnote = 0.;
-smpl_t newnote = 0.;
-uint_t isready = 0;
+int verbose = 0;
+int usejack = 0;
+// input / output
+char_t *sink_uri = NULL;
+char_t *source_uri = NULL;
+// general stuff
+uint_t samplerate = 0;
+uint_t buffer_size = 512;
+uint_t hop_size = 256;
+// onset stuff
+char_t * onset_method = "default";
+smpl_t onset_threshold = 0.0; // will be set if != 0.
+// pitch stuff
+char_t * pitch_unit = "default";
+char_t * pitch_method = "default";
+smpl_t pitch_tolerance = 0.0; // will be set if != 0.
+// tempo stuff
+char_t * tempo_method = "default";
+// more general stuff
+smpl_t silence = -90.;
+uint_t mix_input = 0;
+
+//
+// internal memory stuff
+aubio_source_t *this_source = NULL;
+aubio_sink_t *this_sink = NULL;
+fvec_t *ibuf;
+fvec_t *obuf;
 
+/* settings */
+int blocks = 0;
 
+extern void usage (FILE * stream, int exit_code);
+extern int parse_args (int argc, char **argv);
 
-/* badly redeclare some things */
-aubio_onsetdetection_type type_onset;
-smpl_t threshold;
-smpl_t averaging;
-const char * prog_name;
+#if HAVE_JACK
+aubio_jack_t *jack_setup;
+#endif
 
-void usage (FILE * stream, int exit_code)
+void
+examples_common_init (int argc, char **argv)
 {
-       fprintf(stream, "usage: %s [ options ] \n", prog_name);
-       fprintf(stream, 
-                       "       -j      --jack          Use Jack.\n"
-                       "       -o      --output        Output type.\n"
-                       "       -i      --input         Input type.\n"
-                       "       -h      --help          Display this message.\n"
-                       "       -v      --verbose       Print verbose message.\n"
-                       );
-       exit(exit_code);
-}
-
-int parse_args (int argc, char **argv) {
-       const char *options = "hvjo:i:O:t:a";
-       int next_option;
-       struct option long_options[] =
-       {
-               {"help",                0, NULL, 'h'},
-               {"verbose",     0, NULL, 'v'},
-               {"jack",                0, NULL, 'j'},
-               {"output",      0, NULL, 'o'},
-               {"input",       0, NULL, 'i'},
-               {"onset",       0, NULL, 'O'},
-               {"threshold",   0, NULL, 't'},
-               {"averaging",   0, NULL, 'a'},
-               {NULL,                  0, NULL, 0}
-       };
-       prog_name = argv[0];    
-       if( argc < 1 ) {
-               usage (stderr, 1);
-               return -1;
-       }
-       do {
-               next_option = getopt_long (argc, argv, options, 
-                                                                       long_options, NULL);
-               switch (next_option) {
-                       case 'o':
-                               output_filename = optarg;
-                               break;
-                       case 'i':
-                               input_filename = optarg;
-                               break;
-                       case 'h':       /* help */
-                               usage (stdout, 0);
-                               return -1;
-                       case 'v':               /* verbose */
-                               verbose = 1;
-                               break;
-                       case 'j':               /* verbose */
-                               usejack = 1;
-                               break;
-      case 'O':   /*onset type*/
-        if (strcmp(optarg,"energy") == 0) 
-          type_onset = energy;
-        else if (strcmp(optarg,"specdiff") == 0) 
-          type_onset = specdiff;
-        else if (strcmp(optarg,"hfc") == 0) 
-          type_onset = hfc;
-        else if (strcmp(optarg,"complexdomain") == 0) 
-          type_onset = complexdomain;
-        else if (strcmp(optarg,"phase") == 0) 
-          type_onset = phase;
-        else {
-          debug("could not get onset type.\n");
-          abort();
-        }
-        usedoubled = 0;
-        break;
-      case 't':   /* threshold value for onset */
-        threshold = (smpl_t)atof(optarg);
-        /*
-        if (!isfinite(threshold)) {
-          debug("could not get threshold.\n");
-          abort();
-        }
-        */
-        break;
-      case 'a':
-        averaging = 1;
-        break; 
-                       case '?':       /* unknown options */
-                               usage(stderr, 1);
-        break;
-                       case -1:                /* done with options */
-                               break;
-                       default:                /*something else unexpected */
-                               abort ();
-               }
-       }
-       while (next_option != -1);
-
-       if (input_filename != NULL) {
-               debug ("Input file : %s\n", input_filename );
-       } else if (input_filename != NULL && output_filename != NULL) {
-               debug ("Input file : %s\n", input_filename );
-               debug ("Output file : %s\n", output_filename );
-       } else {
-               if (JACK_SUPPORT)
-               {
-                       debug ("Jack input output\n");
-                       usejack = 1;
-               } else {
-                       debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
-                       exit(1);
-               }
-       }       
-       return 0;
-}
-
-void examples_common_init(int argc,char ** argv) {
-
 
-  aubio_file_t * onsetfile = new_file_ro(onset_filename);
   /* parse command line arguments */
-  parse_args(argc, argv);
-
-  if(!usejack)
-  {
-    debug("Opening files ...\n");
-    file = new_file_ro (input_filename);
-    if (verbose) file_info(file);
-    channels = aubio_file_channels(file);
-    samplerate = aubio_file_samplerate(file);
-    if (output_filename != NULL)
-      fileout = new_file_wo(file, output_filename);
+  parse_args (argc, argv);
+
+  if (!usejack) {
+    debug ("Opening files ...\n");
+    this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size);
+    if (this_source == NULL) {
+      outmsg ("Could not open input file %s\n", source_uri);
+      exit (1);
+    }
+    if (samplerate == 0) {
+      samplerate = aubio_source_get_samplerate(this_source);
+    }
+    if (sink_uri != NULL) {
+      this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
+      if (this_sink == NULL) {
+        outmsg ("Could not open output file %s\n", sink_uri);
+        exit (1);
+      }
+    }
+#ifdef HAVE_JACK
+  } else {
+    debug ("Jack init ...\n");
+    jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1);
+    samplerate = aubio_jack_get_samplerate (jack_setup);
+    source_uri = "jack";
+#endif
   }
+  ibuf = new_fvec (hop_size);
+  obuf = new_fvec (hop_size);
 
-  ibuf      = new_fvec(overlap_size, channels);
-  obuf      = new_fvec(overlap_size, channels);
-  woodblock = new_fvec(buffer_size,1);
-  fftgrain  = new_cvec(buffer_size, channels);
-
-  if (usepitch) {
-    pitchdet = new_aubio_pitchdetection(buffer_size*4, overlap_size, channels, samplerate, mode, freq);
-  
-  if (median) {
-          note_buffer = new_fvec(median, 1);
-          note_buffer2= new_fvec(median, 1);
-  }
-  }
-  /* read the output sound once */
-  file_read(onsetfile, overlap_size, woodblock);
-  /* phase vocoder */
-  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
-  /* onsets */
-  parms = new_aubio_peakpicker(threshold);
-  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
-  onset = new_fvec(1, channels);
-  if (usedoubled)    {
-    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
-    onset2 = new_fvec(1 , channels);
-  }
+}
 
+void
+examples_common_del (void)
+{
+  del_fvec (ibuf);
+  del_fvec (obuf);
+  aubio_cleanup ();
+  fflush(stderr);
+  fflush(stdout);
 }
 
+void
+examples_common_process (aubio_process_func_t process_func,
+    aubio_print_func_t print)
+{
 
-void examples_common_del(void){
-  if (usepitch) {
-          send_noteon(curnote,0);
-          del_aubio_pitchdetection(pitchdet);
-          if (median) {
-                  del_fvec(note_buffer);
-                  del_fvec(note_buffer2);
-          }
-  }
-  del_aubio_pvoc(pv);
-  del_fvec(obuf);
-  del_fvec(ibuf);
-  del_cvec(fftgrain);
-  del_fvec(onset);
-}
+  uint_t read = 0;
+  if (usejack) {
 
-void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
-  if(usejack) {
-#ifdef JACK_SUPPORT
-    aubio_jack_t * jack_setup;
-    debug("Jack init ...\n");
-    jack_setup = new_aubio_jack(channels, channels,
-          (aubio_process_func_t)process_func);
-    if (usepitch) {
-            debug("Midi init ...\n");
-            mplay = new_aubio_midi_player();
-            mdriver = new_aubio_midi_driver("alsa_seq",
-                            (handle_midi_event_func_t)aubio_midi_send_event, mplay);
-            event = new_aubio_midi_event();
-    }
-    debug("Jack activation ...\n");
-    aubio_jack_activate(jack_setup);
-    debug("Processing (Ctrl+C to quit) ...\n");
-    pause();
-    aubio_jack_close(jack_setup);
-    if (usepitch) {
-            send_noteon(curnote,0);
-            del_aubio_midi_driver(mdriver);
-    }
+#if HAVE_JACK
+    debug ("Jack activation ...\n");
+    aubio_jack_activate (jack_setup, process_func);
+    debug ("Processing (Ctrl+C to quit) ...\n");
+    pause ();
+    aubio_jack_close (jack_setup);
 #else
-    usage(stderr, 1);
-    outmsg("Compiled without jack output, exiting.\n");
+    usage (stderr, 1);
+    outmsg ("Compiled without jack output, exiting.\n");
 #endif
 
   } else {
     /* phasevoc */
-    debug("Processing ...\n");
-
-    frames = 0;
-
-    while (overlap_size == file_read(file, overlap_size, ibuf))
-    {
-      isonset=0;
-      process_func(ibuf->data, obuf->data, overlap_size);
-      print(); 
-      if (output_filename != NULL) {
-        file_write(fileout,overlap_size,obuf);
+    blocks = 0;
+    uint_t total_read = 0;
+
+    do {
+      aubio_source_do (this_source, ibuf, &read);
+      process_func (ibuf, obuf);
+      // print to console if verbose or no output given
+      if (verbose || sink_uri == NULL) {
+        print();
       }
-      frames++;
-    }
+      if (this_sink) {
+        aubio_sink_do (this_sink, obuf, hop_size);
+      }
+      blocks++;
+      total_read += read;
+    } while (read == hop_size);
 
-    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
-    del_file(file);
+    verbmsg ("read %d samples (%d blocks of %d) from %s at %dHz\n",
+        total_read, blocks, hop_size, source_uri, samplerate);
 
-    if (output_filename != NULL)
-      del_file(fileout);
+    del_aubio_source (this_source);
+    del_aubio_sink   (this_sink);
 
   }
 }
 
-
-
-void send_noteon(int pitch, int velo)
+void
+send_noteon (int pitch, int velo)
 {
-    smpl_t mpitch = (FLOOR)(freqtomidi(pitch)+.5);
-    /* we should check if we use midi here, not jack */
-#if ALSA_SUPPORT
-    if (usejack) {
-        if (velo==0) {
-            aubio_midi_event_set_type(event,NOTE_OFF);
-        } else {
-            aubio_midi_event_set_type(event,NOTE_ON);
-        }
-        aubio_midi_event_set_channel(event,0);
-        aubio_midi_event_set_pitch(event,mpitch);
-        aubio_midi_event_set_velocity(event,velo);
-        aubio_midi_direct_output(mdriver,event);
-    } else 
-#endif
-    if (!verbose)
-    {
-        if (velo==0) {
-            outmsg("%f\n",frames*overlap_size/(float)samplerate);
-        } else {
-            outmsg("%f\t%f\t", mpitch,
-                        frames*overlap_size/(float)samplerate);
-        }
+  smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
+#if HAVE_JACK
+  jack_midi_event_t ev;
+  ev.size = 3;
+  ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
+  ev.time = 0;
+  if (usejack) {
+    ev.buffer[2] = velo;
+    ev.buffer[1] = mpitch;
+    if (velo == 0) {
+      ev.buffer[0] = 0x80;      /* note off */
+    } else {
+      ev.buffer[0] = 0x90;      /* note on */
     }
-}
-
-
-void note_append(fvec_t * note_buffer, smpl_t curnote) {
-  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[0][note_buffer->length - 1] = curnote;
-  return;
-}
-
-uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
-  uint_t i = 0;
-  for (i = 0; i < note_buffer->length; i++) { 
-      note_buffer2->data[0][i] = note_buffer->data[0][i];
+    aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
+  } else
+#endif
+  if (velo == 0) {
+    verbmsg ("%f\n", blocks * hop_size / (float) samplerate);
+  } else {
+    verbmsg ("%f\t%f\t", mpitch, blocks * hop_size / (float) samplerate);
   }
-  return vec_median(note_buffer2);
 }