From: Paul Brossier Date: Fri, 16 Oct 2009 21:03:08 +0000 (+0200) Subject: examples/: make use of aubio_onset in aubioonset and aubionotes, simplify, keep only... X-Git-Tag: bzr2git~110 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d4c5de7b1235d18d93df4de76c2e1e88e7e3d03c;p=aubio.git examples/: make use of aubio_onset in aubioonset and aubionotes, simplify, keep only general stuff in utils --- diff --git a/examples/aubiomfcc.c b/examples/aubiomfcc.c index 27a92f09..d7d1ebf3 100644 --- a/examples/aubiomfcc.c +++ b/examples/aubiomfcc.c @@ -22,15 +22,15 @@ /* mfcc objects */ fvec_t * mfcc_out; aubio_mfcc_t * mfcc; +aubio_pvoc_t *pv; +cvec_t *fftgrain; uint_t n_filters = 40; uint_t n_coefs = 13; unsigned int pos = 0; /*frames%dspblocksize*/ -uint_t usepitch = 0; -int aubio_process(smpl_t **input, smpl_t **output, int nframes); -int aubio_process(smpl_t **input, smpl_t **output, int nframes) { +static int aubio_process(smpl_t **input, smpl_t **output, int nframes) { unsigned int i; /*channels*/ unsigned int j; /*frames*/ @@ -61,8 +61,7 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { return 1; } -void process_print (void); -void process_print (void) { +static void process_print (void) { /* output times in seconds write extracted mfccs */ @@ -83,16 +82,23 @@ int main(int argc, char **argv) { overlap_size = 256; examples_common_init(argc,argv); - mfcc_out = new_fvec(n_coefs,channels); - - + + /* phase vocoder */ + pv = new_aubio_pvoc (buffer_size, overlap_size, channels); + + fftgrain = new_cvec (buffer_size, channels); + //populating the filter - mfcc = new_aubio_mfcc(buffer_size, samplerate, n_filters, n_coefs); + mfcc = new_aubio_mfcc(buffer_size, n_filters, n_coefs, samplerate); + + mfcc_out = new_fvec(n_coefs,channels); //process examples_common_process(aubio_process,process_print); //destroying mfcc + del_aubio_pvoc (pv); + del_cvec (fftgrain); del_aubio_mfcc(mfcc); del_fvec(mfcc_out); diff --git a/examples/aubionotes.c b/examples/aubionotes.c index 7017bd98..2335d753 100644 --- a/examples/aubionotes.c +++ b/examples/aubionotes.c @@ -18,11 +18,33 @@ #include "utils.h" +/* pitch objects */ +smpl_t pitch = 0.; + +uint_t median = 6; +smpl_t curlevel = 0.; + +aubio_pitchdetection_t *pitchdet; + +fvec_t *note_buffer = NULL; +fvec_t *note_buffer2 = NULL; + +smpl_t curnote = 0.; +smpl_t newnote = 0.; +uint_t isready = 0; unsigned int pos = 0; /*frames%dspblocksize*/ -uint_t usepitch = 1; -int aubio_process(smpl_t **input, smpl_t **output, int nframes); -int aubio_process(smpl_t **input, smpl_t **output, int nframes) { +aubio_pitchdetection_t *pitchdet; +aubio_onset_t *o; +fvec_t *onset; +fvec_t *pitch_obuf; + +/** append new note candidate to the note_buffer and return filtered value. we + * need to copy the input array as fvec_median destroy its input data.*/ +void note_append (fvec_t * note_buffer, smpl_t curnote); +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++) { @@ -37,9 +59,7 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /*time for fft*/ if (pos == overlap_size-1) { /* block loop */ - aubio_pvoc_do (pv,ibuf, fftgrain); - aubio_onsetdetection_do(o,fftgrain, onset); - isonset = aubio_peakpicker_do(parms, onset); + aubio_onset_do(o, ibuf, onset); aubio_pitchdetection_do (pitchdet, ibuf, pitch_obuf); pitch = fvec_read_sample(pitch_obuf, 0, 0); @@ -49,10 +69,9 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /* curlevel is negatif or 1 if silence */ curlevel = aubio_level_detection(ibuf, silence); - if (isonset) { + if (fvec_read_sample(onset, 0, 0)) { /* test for silence */ if (curlevel == 1.) { - isonset=0; if (median) isready = 0; /* send note off */ send_noteon(curnote,0); @@ -98,14 +117,57 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { return 1; } -void process_print (void); -void process_print (void) { +static void process_print (void) { if (verbose) outmsg("%f\n",pitch); } +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]; + } + 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); + onset = new_fvec (1, channels); + + pitchdet = new_aubio_pitchdetection (pitch_mode, buffer_size * 4, + overlap_size, channels, samplerate); + aubio_pitchdetection_set_tolerance (pitchdet, 0.7); + pitch_obuf = new_fvec (1, channels); + if (median) { + note_buffer = new_fvec (median, 1); + note_buffer2 = new_fvec (median, 1); + } + examples_common_process(aubio_process, process_print); + + send_noteon (curnote, 0); + del_aubio_pitchdetection (pitchdet); + if (median) { + del_fvec (note_buffer); + del_fvec (note_buffer2); + } + del_fvec (pitch_obuf); + examples_common_del(); debug("End of program.\n"); fflush(stderr); diff --git a/examples/aubioonset.c b/examples/aubioonset.c index 1af5b3ea..2c1a186a 100644 --- a/examples/aubioonset.c +++ b/examples/aubioonset.c @@ -19,10 +19,11 @@ #include "utils.h" unsigned int pos = 0; /*frames%dspblocksize*/ -uint_t usepitch = 0; -int aubio_process(smpl_t **input, smpl_t **output, int nframes); -int aubio_process(smpl_t **input, smpl_t **output, int nframes) { +aubio_onset_t *o; +fvec_t *onset; + +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++) { @@ -37,20 +38,15 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /*time for fft*/ if (pos == overlap_size-1) { /* block loop */ - aubio_pvoc_do (pv,ibuf, fftgrain); - aubio_onsetdetection_do (o,fftgrain, onset); - isonset = aubio_peakpicker_do(parms, onset); - if (isonset) { - /* test for silence */ - if (aubio_silence_detection(ibuf, silence)==1) - isonset=0.; - else - for (pos = 0; pos < overlap_size; pos++){ - obuf->data[0][pos] = woodblock->data[0][pos]; - } + aubio_onset_do (o, ibuf, onset); + if (fvec_read_sample(onset, 0, 0)) { + for (pos = 0; pos < overlap_size; pos++){ + obuf->data[0][pos] = woodblock->data[0][pos]; + } } else { - for (pos = 0; pos < overlap_size; pos++) + for (pos = 0; pos < overlap_size; pos++) { obuf->data[0][pos] = 0.; + } } /* end of block loop */ pos = -1; /* so it will be zero next j loop */ @@ -60,14 +56,16 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { return 1; } -void process_print (void); -void process_print (void) { +static void process_print (void) { /* output times in seconds, taking back some * delay to ensure the label is _before_ the * actual onset */ - if (isonset && output_filename == NULL) { + 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 + isonset)*overlap_size/(float)samplerate); + outmsg("%f\n",(frames - frames_delay + onset_found) + *overlap_size/(float)samplerate); } else if (frames < frames_delay) { outmsg("%f\n",0.); } @@ -77,7 +75,16 @@ void process_print (void) { int main(int argc, char **argv) { frames_delay = 3; examples_common_init(argc,argv); + + o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels, + samplerate); + onset = new_fvec (1, channels); + examples_common_process(aubio_process,process_print); + + del_aubio_onset (o); + del_fvec (onset); + examples_common_del(); debug("End of program.\n"); fflush(stderr); diff --git a/examples/aubioquiet.c b/examples/aubioquiet.c index 5c162c3e..62fbb033 100644 --- a/examples/aubioquiet.c +++ b/examples/aubioquiet.c @@ -20,7 +20,6 @@ unsigned int pos = 0; /*frames%dspblocksize*/ sint_t wassilence = 1, issilence; -uint_t usepitch = 0; int aubio_process(smpl_t **input, smpl_t **output, int nframes); int aubio_process(smpl_t **input, smpl_t **output, int nframes) { @@ -55,8 +54,7 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { return 1; } -void process_print (void); -void process_print (void) { +static void process_print (void) { int curframes = (frames - 4) > 0 ? frames -4 : 0; if (issilence == -1) { outmsg("NOISY: %f\n",curframes*overlap_size/(float)samplerate); diff --git a/examples/aubiotrack.c b/examples/aubiotrack.c index 233b6b35..a83f0a26 100644 --- a/examples/aubiotrack.c +++ b/examples/aubiotrack.c @@ -19,14 +19,13 @@ #include #include "utils.h" -unsigned int pos = 0; /* frames%dspblocksize */ -uint_t usepitch = 0; -fvec_t * out = NULL; -aubio_tempo_t * bt = NULL; -smpl_t istactus = 0; - -int aubio_process(smpl_t **input, smpl_t **output, int nframes); -int aubio_process(smpl_t **input, smpl_t **output, int nframes) { +uint_t pos = 0; /* frames%dspblocksize */ +fvec_t * tempo_out = NULL; +aubio_tempo_t * bt = NULL; +smpl_t istactus = 0; +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++) { @@ -41,11 +40,15 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { /*time for fft*/ if (pos == overlap_size-1) { /* block loop */ - aubio_tempo_do (bt,ibuf,out); - if (out->data[0][0]>=1) - istactus = out->data[0][0]; + aubio_tempo_do (bt,ibuf,tempo_out); + if (tempo_out->data[0][0]>0) + istactus = tempo_out->data[0][0]; else istactus = 0; + if (tempo_out->data[0][1]>0) + isonset = tempo_out->data[0][0]; + else + isonset = 0; if (istactus) { for (pos = 0; pos < overlap_size; pos++) obuf->data[0][pos] = woodblock->data[0][pos]; @@ -61,8 +64,7 @@ int aubio_process(smpl_t **input, smpl_t **output, int nframes) { return 1; } -void process_print (void); -void process_print (void) { +static void process_print (void) { if (output_filename == NULL) { if (istactus) { outmsg("%f\n",((smpl_t)(frames*overlap_size)+(istactus-1.)*overlap_size)/(smpl_t)samplerate); @@ -79,13 +81,13 @@ int main(int argc, char **argv) { /* override default settings */ examples_common_init(argc,argv); - out = new_fvec(2,channels); - bt = new_aubio_tempo(onset_mode,buffer_size,overlap_size,channels); + tempo_out = new_fvec(2,channels); + bt = new_aubio_tempo(onset_mode,buffer_size,overlap_size,channels, samplerate); examples_common_process(aubio_process,process_print); del_aubio_tempo(bt); - del_fvec(out); + del_fvec(tempo_out); examples_common_del(); diff --git a/examples/utils.c b/examples/utils.c index 22e6b95a..2a648d2d 100644 --- a/examples/utils.c +++ b/examples/utils.c @@ -18,6 +18,14 @@ */ +/** + + 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. + +*/ + #include "utils.h" #ifdef HAVE_LASH @@ -40,10 +48,12 @@ const char *onset_filename = int frames = 0; int verbose = 0; int usejack = 0; -int usedoubled = 1; int frames_delay = 0; +char_t * pitch_unit = "default"; +char_t * pitch_mode = "default"; + /* energy,specdiff,hfc,complexdomain,phase */ char_t * onset_mode = "default"; smpl_t threshold = 0.3; @@ -57,42 +67,18 @@ uint_t samplerate = 44100; aubio_sndfile_t *file = NULL; aubio_sndfile_t *fileout = NULL; -aubio_pvoc_t *pv; fvec_t *ibuf; fvec_t *obuf; -fvec_t *pitch_obuf; -cvec_t *fftgrain; fvec_t *woodblock; -aubio_onsetdetection_t *o; -fvec_t *onset; -fvec_t *onset2; -smpl_t isonset = 0; -aubio_peakpicker_t *parms; - - -/* pitch objects */ -smpl_t pitch = 0.; -aubio_pitchdetection_t *pitchdet; -char_t * pitch_unit = "default"; -char_t * pitch_mode = "default"; -uint_t median = 6; - -fvec_t *note_buffer = NULL; -fvec_t *note_buffer2 = NULL; -smpl_t curlevel = 0.; -smpl_t maxonset = 0.; - -smpl_t curnote = 0.; -smpl_t newnote = 0.; -uint_t isready = 0; - - /* badly redeclare some things */ smpl_t threshold; smpl_t averaging; const char *prog_name; +void flush_process (aubio_process_func_t process_func, + aubio_print_func_t print); + void usage (FILE * stream, int exit_code) { @@ -221,6 +207,7 @@ void examples_common_init (int argc, char **argv) { + uint_t found_wood = 0; aubio_sndfile_t *onsetfile = NULL; /* parse command line arguments */ @@ -229,7 +216,7 @@ examples_common_init (int argc, char **argv) woodblock = new_fvec (buffer_size, 1); if (output_filename || usejack) { /* dummy assignement to keep egcs happy */ - isonset = (onsetfile = new_aubio_sndfile_ro (onset_filename)) || + found_wood = (onsetfile = new_aubio_sndfile_ro (onset_filename)) || (onsetfile = new_aubio_sndfile_ro ("sounds/woodblock.aiff")) || (onsetfile = new_aubio_sndfile_ro ("../sounds/woodblock.aiff")); if (onsetfile == NULL) { @@ -276,25 +263,6 @@ examples_common_init (int argc, char **argv) ibuf = new_fvec (overlap_size, channels); obuf = new_fvec (overlap_size, channels); - fftgrain = new_cvec (buffer_size, channels); - - if (usepitch) { - pitchdet = new_aubio_pitchdetection (pitch_mode, buffer_size * 4, - overlap_size, channels, samplerate); - aubio_pitchdetection_set_tolerance (pitchdet, 0.7); - pitch_obuf = new_fvec (1, channels); - - if (median) { - note_buffer = new_fvec (median, 1); - note_buffer2 = new_fvec (median, 1); - } - } - /* phase vocoder */ - pv = new_aubio_pvoc (buffer_size, overlap_size, channels); - /* onsets */ - parms = new_aubio_peakpicker (threshold); - o = new_aubio_onsetdetection (onset_mode, buffer_size, channels); - onset = new_fvec (1, channels); } @@ -302,22 +270,8 @@ examples_common_init (int argc, char **argv) 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_fvec (pitch_obuf); - } - del_aubio_onsetdetection (o); - del_aubio_peakpicker (parms); - del_aubio_pvoc (pv); - del_fvec (obuf); del_fvec (ibuf); - del_cvec (fftgrain); - del_fvec (onset); + del_fvec (obuf); del_fvec (woodblock); aubio_cleanup (); } @@ -354,7 +308,6 @@ examples_common_process (aubio_process_func_t process_func, while ((signed) overlap_size == aubio_sndfile_read (file, overlap_size, ibuf)) { - isonset = 0; process_func (ibuf->data, obuf->data, overlap_size); print (); if (output_filename != NULL) { @@ -415,27 +368,6 @@ send_noteon (int pitch, int velo) } -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]; - } - return fvec_median (note_buffer2); -} - #if HAVE_LASH void * diff --git a/examples/utils.h b/examples/utils.h index c14ef600..884f94ba 100644 --- a/examples/utils.h +++ b/examples/utils.h @@ -42,11 +42,7 @@ extern int frames; extern int verbose; extern int usejack; -extern int usedoubled; extern int frames_delay; -extern unsigned int median; -extern const char *output_filename; -extern const char *input_filename; /* defined in utils.c */ void usage (FILE * stream, int exit_code); int parse_args (int argc, char **argv); @@ -59,64 +55,23 @@ typedef int (*aubio_process_func_t) #endif void examples_common_process (aubio_process_func_t process_func, aubio_print_func_t print); -void flush_process (aubio_process_func_t process_func, - aubio_print_func_t print); +extern char_t * pitch_unit; +extern char_t * pitch_mode; void send_noteon (int pitch, int velo); -/** append new note candidate to the note_buffer and return filtered value. we - * need to copy the input array as fvec_median destroy its input data.*/ -void note_append (fvec_t * note_buffer, smpl_t curnote); -uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2); extern const char *output_filename; -extern const char *input_filename; -extern const char *onset_filename; -extern int verbose; -extern int usejack; -extern int usedoubled; - - -/* energy,specdiff,hfc,complexdomain,phase */ extern char_t * onset_mode; extern smpl_t threshold; extern smpl_t silence; +extern int verbose; +extern int usejack; extern uint_t buffer_size; extern uint_t overlap_size; extern uint_t channels; extern uint_t samplerate; - -extern aubio_sndfile_t *file; -extern aubio_sndfile_t *fileout; - -extern aubio_pvoc_t *pv; extern fvec_t *ibuf; extern fvec_t *obuf; -extern fvec_t *pitch_obuf; -extern cvec_t *fftgrain; extern fvec_t *woodblock; -extern aubio_onsetdetection_t *o; -extern aubio_onsetdetection_t *o2; -extern fvec_t *onset; -extern fvec_t *onset2; -extern smpl_t isonset; -extern aubio_peakpicker_t *parms; - - -/* pitch objects */ -extern smpl_t pitch; -extern aubio_pitchdetection_t *pitchdet; -extern uint_t median; - -extern fvec_t *note_buffer; -extern fvec_t *note_buffer2; -extern smpl_t curlevel; -extern smpl_t maxonset; - -extern smpl_t curnote; -extern smpl_t newnote; -extern uint_t isready; - -/* per example param */ -extern uint_t usepitch;