Ignore:
Timestamp:
Apr 11, 2005, 6:37:51 PM (19 years ago)
Author:
Paul Brossier <piem@altern.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
dc27a81
Parents:
84941cb
Message:

merged commmon code between aubionotes.c and aubioonset.c into utils.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • examples/aubionotes.c

    r84941cb rbd2f2ab  
    1717*/
    1818
    19 #include <stdio.h>
    20 #include <stdlib.h>
    21 #include <stdarg.h>
    22 #include <getopt.h>
    23 #include <unistd.h>
    24 #include <math.h>
    25 #include "aubio.h"
    26 #include "aubioext.h"
    2719#include "utils.h"
    2820
    29 /* settings */
    30 const char * output_filename = NULL;
    31 const char * input_filename  = NULL;
    32 const char * onset_filename  = "/usr/share/sounds/aubio/woodblock.aiff";
    33 int verbose = 0;
    34 int usejack = 0;
    35 int usedoubled = 1;
    36 int usemidi = 1;
    37 int median = 0;
    38 
    39 /* energy,specdiff,hfc,complexdomain,phase */
    40 aubio_onsetdetection_type type_onset  = hfc;
    41 aubio_onsetdetection_type type_onset2 = complexdomain;
    42 smpl_t threshold                      = 0.3;
    43 smpl_t threshold2                     = -90.;
    44 uint_t buffer_size                    = 1024;
    45 uint_t overlap_size                   = 512;
    46 uint_t channels                       = 1;
    47 uint_t samplerate                     = 44100;
    48 
    49 /* global objects */
    50 int frames;
    51 aubio_pvoc_t * pv;
    52 fvec_t * ibuf;
    53 fvec_t * obuf;
    54 cvec_t * fftgrain;
    55 fvec_t * woodblock;
    56 aubio_onsetdetection_t *o;
    57 aubio_onsetdetection_t *o2;
    58 fvec_t *onset;
    59 fvec_t *onset2;
    60 int isonset = 0;
    61 aubio_pickpeak_t * parms;
    62 
    63 /* pitch objects */
    64 smpl_t pitch               = 0.;
    65 aubio_pitchdetection_t * pitchdet;
    66 
    67 fvec_t * note_buffer = NULL;
    68 fvec_t * note_buffer2 = NULL;
    69 uint_t medianfiltlen = 6;
    70 smpl_t curlevel = 0.;
    71 smpl_t maxonset = 0.;
    72 
    73 /* midi objects */
    74 aubio_midi_player_t * mplay;
    75 aubio_midi_driver_t * mdriver;
    76 aubio_midi_event_t  * event;
    77 
    7821unsigned int pos = 0; /*frames%dspblocksize*/
    79 
    80 void send_noteon(aubio_midi_driver_t * d, int pitch, int velo);
    81 void send_noteon(aubio_midi_driver_t * d, int pitch, int velo)
    82 {
    83     /* we should check if we use midi here, not jack */
    84 #if ALSA_SUPPORT
    85     if (usejack) {
    86         if (velo==0) {
    87             aubio_midi_event_set_type(event,NOTE_OFF);
    88         } else {
    89             aubio_midi_event_set_type(event,NOTE_ON);
    90         }
    91         aubio_midi_event_set_channel(event,0);
    92         aubio_midi_event_set_pitch(event,pitch);
    93         aubio_midi_event_set_velocity(event,velo);
    94         aubio_midi_direct_output(mdriver,event);
    95     } else
    96 #endif
    97     {
    98         if (velo==0) {
    99             outmsg("%f\n",frames*overlap_size/(float)samplerate);
    100         } else {
    101             outmsg("%d\t%f\t",pitch,frames*overlap_size/(float)samplerate);
    102         }
    103     }
    104 }
    105 
    106 
    107 /** append new note candidate to the note_buffer and return filtered value. we
    108  * need to copy the input array as vec_median destroy its input data.*/
    109 
    110 void note_append(fvec_t * note_buffer, smpl_t curnote);
    111 void note_append(fvec_t * note_buffer, smpl_t curnote) {
    112   uint_t i = 0;
    113   for (i = 0; i < note_buffer->length - 1; i++) {
    114       note_buffer->data[0][i] = note_buffer->data[0][i+1];
    115   }
    116   note_buffer->data[0][note_buffer->length - 1] = curnote;
    117   return;
    118 }
    119 
    120 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2);
    121 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
    122   uint_t i = 0;
    123   for (i = 0; i < note_buffer->length; i++) {
    124       note_buffer2->data[0][i] = note_buffer->data[0][i];
    125   }
    126   return vec_median(note_buffer2);
    127 }
    128 
    129 
    130 smpl_t curnote = 0.;
    131 smpl_t newnote = 0.;
    132 uint_t isready = 0;
     22uint_t usepitch = 1;
    13323
    13424int aubio_process(float **input, float **output, int nframes);
     
    15848      pitch = aubio_pitchdetection(pitchdet,ibuf);
    15949      if(median){
    160       newnote = (FLOOR)(bintomidi(pitch,samplerate,buffer_size*4)+.5);
    161       note_append(note_buffer, newnote);
     50              note_append(note_buffer, pitch);
    16251      }
    16352
     
    16554      curlevel = aubio_level_detection(ibuf, threshold2);
    16655      if (isonset) {
    167         /* test for silence */
    168         if (curlevel == 1.) {
    169           isonset=0;
    170           if (median) isready = 0;
    171           /* send note off */
    172           send_noteon(mdriver,curnote,0);
    173         } else {
    174           if (median) {
    175                   isready = 1;
    176           } else {
    177           /* kill old note */
    178           send_noteon(mdriver,curnote,0);
    179           //curnote = (int)FLOOR(bintomidi(pitch,samplerate,buffer_size*4) + .5);
    180           curnote = (int)FLOOR(freqtomidi(pitch) + .5);
    181           /* get and send new one */
    182           /*if (curnote<45){
    183             send_noteon(mdriver,curnote,0);
    184           } else {*/
    185           if (curnote>45){
    186             send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
    187           }
    188           }
    189          
    190           for (pos = 0; pos < overlap_size; pos++){
    191             obuf->data[0][pos] = woodblock->data[0][pos];
    192           }
    193         }
     56              /* test for silence */
     57              if (curlevel == 1.) {
     58                      isonset=0;
     59                      if (median) isready = 0;
     60                      /* send note off */
     61                      send_noteon(curnote,0);
     62              } else {
     63                      if (median) {
     64                              isready = 1;
     65                      } else {
     66                              /* kill old note */
     67                              send_noteon(curnote,0);
     68                              /* get and send new one */
     69                              send_noteon(pitch,127+(int)FLOOR(curlevel));
     70                              curnote = pitch;
     71                      }
     72
     73                      for (pos = 0; pos < overlap_size; pos++){
     74                              obuf->data[0][pos] = woodblock->data[0][pos];
     75                      }
     76              }
    19477      } else {
    19578              if (median) {
    196 //        if (curlevel != 1) {
    197 //          if (bufpos == note_buffer->length)
    198 //            curnote = aubio_getnote(note_buffer);
    199 //        }
    200 //
    201         if (isready > 0)
    202             isready++;
    203         if (isready == note_buffer->length)
    204         {
    205             //outmsg("%d, %f\n", isready, curnote);
    206             send_noteon(mdriver,curnote,0);
    207         /* kill old note */
    208             newnote = get_note(note_buffer, note_buffer2);
    209             curnote = newnote;
    210         /* get and send new one */
    211         /*if (curnote<45){
    212           send_noteon(mdriver,curnote,0);
    213         } else {*/
    214             if (curnote>45){
    215                 send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
    216             }
    217         }
    218         } // if median
     79                      if (isready > 0)
     80                              isready++;
     81                      if (isready == median)
     82                      {
     83                              /* kill old note */
     84                              send_noteon(curnote,0);
     85                              newnote = get_note(note_buffer, note_buffer2);
     86                              curnote = newnote;
     87                              /* get and send new one */
     88                              if (curnote>45){
     89                                      send_noteon(curnote,127+(int)FLOOR(curlevel));
     90                              }
     91                      }
     92              } // if median
    21993        for (pos = 0; pos < overlap_size; pos++)
    22094          obuf->data[0][pos] = 0.;
     
    228102}
    229103
     104void process_print (void);
     105void process_print (void) {
     106      if (verbose) outmsg("%f\n",pitch);
     107}
     108
    230109int main(int argc, char **argv) {
    231 
    232   aubio_file_t * file = NULL;
    233   aubio_file_t * fileout = NULL;
    234 
    235   aubio_file_t * onsetfile = new_file_ro(onset_filename);
    236   /* parse command line arguments */
    237   parse_args(argc, argv);
    238 
    239   if(!usejack)
    240   {
    241     debug("Opening files ...\n");
    242     file = new_file_ro (input_filename);
    243     file_info(file);
    244     samplerate = aubio_file_samplerate(file);
    245     channels = aubio_file_channels(file);
    246     if (output_filename != NULL)
    247       fileout = new_file_wo(file, output_filename);
    248   }
    249 
    250   ibuf        = new_fvec(overlap_size, channels);
    251   obuf        = new_fvec(overlap_size, channels);
    252   woodblock   = new_fvec(buffer_size,1);
    253   fftgrain    = new_cvec(buffer_size, channels);
    254 
    255   aubio_pitchdetection_type mode = yin; // mcomb
    256   pitchdet = new_aubio_pitchdetection(buffer_size*4, overlap_size, channels, samplerate, mode, freq);
    257  
    258 if (median) {
    259   note_buffer = new_fvec(medianfiltlen, 1);
    260   note_buffer2= new_fvec(medianfiltlen, 1);
    261 }
    262   /* read the output sound once */
    263   file_read(onsetfile, overlap_size, woodblock);
    264   /* phase vocoder */
    265   pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
    266   /* onsets */
    267   parms = new_aubio_peakpicker(threshold);
    268   o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
    269   onset = new_fvec(1, channels);
    270   if (usedoubled)    {
    271     o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
    272     onset2 = new_fvec(1 , channels);
    273   }
    274 
    275   if(usejack) {
    276 #ifdef JACK_SUPPORT
    277     aubio_jack_t * jack_setup;
    278     debug("Midi init ...\n");
    279     debug("Jack init ...\n");
    280     jack_setup = new_aubio_jack(channels, channels,
    281           (aubio_process_func_t)aubio_process);
    282 
    283     mplay = new_aubio_midi_player();
    284 
    285     mdriver = new_aubio_midi_driver("alsa_seq",
    286         (handle_midi_event_func_t)aubio_midi_send_event, mplay);
    287 
    288     event = new_aubio_midi_event();
    289    
    290     debug("Jack activation ...\n");
    291     aubio_jack_activate(jack_setup);
    292     debug("Processing (Ctrl+C to quit) ...\n");
    293     pause();
    294     send_noteon(mdriver,curnote,0);
    295     aubio_jack_close(jack_setup);
    296     del_aubio_midi_driver(mdriver);
    297 #else
    298     usage(stderr, 1);
    299     outmsg("Compiled without jack output, exiting.\n");
    300 #endif
    301 
    302   } else {
    303     /* phasevoc */
    304     debug("Processing ...\n");
    305 
    306     frames = 0;
    307 
    308     while (overlap_size == file_read(file, overlap_size, ibuf))
    309     {
    310       isonset=0;
    311       aubio_process(ibuf->data, obuf->data, overlap_size);
    312       if (output_filename != NULL) {
    313         file_write(fileout,overlap_size,obuf);
    314       }
    315       frames++;
    316     }
    317     send_noteon(mdriver,curnote,0);
    318 
    319     debug("Processed %d frames of %d samples.\n", frames, buffer_size);
    320     del_file(file);
    321 
    322     if (output_filename != NULL)
    323       del_file(fileout);
    324 
    325   }
    326 
    327   del_aubio_pvoc(pv);
    328   del_fvec(obuf);
    329   del_fvec(ibuf);
    330   del_cvec(fftgrain);
    331   del_aubio_pitchdetection(pitchdet);
    332   del_fvec(onset);
    333   if (median) {
    334   del_fvec(note_buffer);
    335   del_fvec(note_buffer2);
    336   }
    337 
     110  examples_common_init(argc,argv);
     111  examples_common_process(aubio_process, process_print);
     112  examples_common_del();
    338113  debug("End of program.\n");
    339114  fflush(stderr);
Note: See TracChangeset for help on using the changeset viewer.