Changeset 1b25a70 for examples/utils.c


Ignore:
Timestamp:
Dec 7, 2013, 12:14:50 AM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
44e94f3c
Parents:
cc6b221
Message:

examples/: move parse_args to parse_args.h, clean up, remove lash and old frames_delay

File:
1 edited

Legend:

Unmodified
Added
Removed
  • examples/utils.c

    rcc6b221 r1b25a70  
    2828
    2929#include "utils.h"
     30#ifdef HAVE_JACK
     31#include "jackio.h"
     32#endif /* HAVE_JACK */
    3033
    31 #ifdef HAVE_LASH
    32 #include <lash/lash.h>
    33 #include <pthread.h>
    34 lash_client_t *aubio_lash_client;
    35 lash_args_t *lash_args;
    36 void *lash_thread_main (void *data);
    37 int lash_main (void);
    38 void save_data (void);
    39 void restore_data (lash_config_t * lash_config);
    40 pthread_t lash_thread;
    41 #endif /* HAVE_LASH */
     34int verbose = 0;
     35// input / output
     36char_t *sink_uri = NULL;
     37char_t *source_uri = NULL;
     38// general stuff
     39uint_t samplerate = 0;
     40uint_t buffer_size = 512;
     41uint_t overlap_size = 256;
     42// onset stuff
     43char_t * onset_method = "default";
     44smpl_t onset_threshold = 0.0; // will be set if != 0.
     45// pitch stuff
     46char_t * pitch_unit = "default";
     47char_t * pitch_method = "default";
     48smpl_t pitch_tolerance = 0.0; // will be set if != 0.
     49// tempo stuff
     50char_t * tempo_method = "default";
     51// more general stuff
     52smpl_t silence = -90.;
     53uint_t mix_input = 0;
     54
     55//
     56// internal memory stuff
     57aubio_source_t *this_source = NULL;
     58aubio_sink_t *this_sink = NULL;
     59fvec_t *ibuf;
     60fvec_t *obuf;
     61
    4262
    4363/* settings */
    44 const char *sink_uri = NULL;
    45 const char *source_uri = NULL;
    4664int frames = 0;
    47 int verbose = 0;
    4865int usejack = 0;
    4966int frames_delay = 0;
    5067
    51 
    52 char_t * pitch_unit = "default";
    53 char_t * pitch_mode = "default";
    54 
    55 /* energy,specdiff,hfc,complexdomain,phase */
    56 char_t * onset_mode = "default";
    57 smpl_t threshold = 0.0;         // leave unset, only set as asked
    58 smpl_t silence = -90.;
    59 uint_t buffer_size = 512;       //1024;
    60 uint_t overlap_size = 256;      //512;
    61 uint_t samplerate = 44100;
    62 
    63 aubio_source_t *this_source = NULL;
    64 aubio_sink_t *this_sink = NULL;
    65 
    66 fvec_t *ibuf;
    67 fvec_t *obuf;
    68 
    69 /* badly redeclare some things */
    70 smpl_t threshold;
    71 smpl_t averaging;
    72 const char *prog_name;
    73 
    74 void flush_process (aubio_process_func_t process_func,
    75     aubio_print_func_t print);
    76 
    77 void
    78 usage (FILE * stream, int exit_code)
    79 {
    80   fprintf (stream, "usage: %s [ options ] \n", prog_name);
    81   fprintf (stream,
    82       "       -h      --help          Display this message.\n"
    83       "       -v      --verbose       Be verbose.\n"
    84       "       -j      --jack          Use Jack.\n"
    85       "       -o      --output        Output type.\n"
    86       "       -i      --input         Input type.\n"
    87       "       -O      --onset         Select onset detection algorithm.\n"
    88       "       -t      --threshold     Set onset detection threshold.\n"
    89       "       -s      --silence       Select silence threshold.\n"
    90       "       -p      --pitch         Select pitch detection algorithm.\n"
    91       "       -B      --bufsize       Set buffer size.\n"
    92       "       -H      --hopsize       Set hopsize.\n"
    93       "       -a      --averaging     Use averaging.\n");
    94   exit (exit_code);
    95 }
    96 
    97 int
    98 parse_args (int argc, char **argv)
    99 {
    100   const char *options = "hvjo:i:O:t:s:p:B:H:a";
    101   int next_option;
    102   struct option long_options[] = {
    103     {"help", 0, NULL, 'h'},
    104     {"verbose", 0, NULL, 'v'},
    105     {"jack", 0, NULL, 'j'},
    106     {"output", 1, NULL, 'o'},
    107     {"input", 1, NULL, 'i'},
    108     {"onset", 1, NULL, 'O'},
    109     {"threshold", 1, NULL, 't'},
    110     {"silence", 1, NULL, 's'},
    111     {"pitch", 1, NULL, 'p'},
    112     {"averaging", 0, NULL, 'a'},
    113     {"bufsize", 1, NULL, 'B'},
    114     {"hopsize", 1, NULL, 'H'},
    115     {NULL, 0, NULL, 0}
    116   };
    117 #ifdef HAVE_LASH
    118   lash_args = lash_extract_args (&argc, &argv);
    119 #endif /* HAVE_LASH */
    120   prog_name = argv[0];
    121   if (argc < 1) {
    122     usage (stderr, 1);
    123     return -1;
    124   }
    125   do {
    126     next_option = getopt_long (argc, argv, options, long_options, NULL);
    127     switch (next_option) {
    128       case 'o':
    129         sink_uri = optarg;
    130         break;
    131       case 'i':
    132         source_uri = optarg;
    133         break;
    134       case 'h':                /* help */
    135         usage (stdout, 0);
    136         return -1;
    137       case 'v':                /* verbose */
    138         verbose = 1;
    139         break;
    140       case 'j':
    141         usejack = 1;
    142         break;
    143       case 'O':                /*onset type */
    144         onset_mode = optarg;
    145         break;
    146       case 's':                /* silence value for onset */
    147         silence = (smpl_t) atof (optarg);
    148         break;
    149       case 't':                /* threshold value for onset */
    150         threshold = (smpl_t) atof (optarg);
    151         break;
    152       case 'p':
    153         pitch_mode = optarg;
    154         break;
    155       case 'a':
    156         averaging = 1;
    157         break;
    158       case 'B':
    159         buffer_size = atoi (optarg);
    160         break;
    161       case 'H':
    162         overlap_size = atoi (optarg);
    163         break;
    164       case '?':                /* unknown options */
    165         usage (stderr, 1);
    166         break;
    167       case -1:                 /* done with options */
    168         break;
    169       default:                 /*something else unexpected */
    170         fprintf (stderr, "Error parsing option '%c'\n", next_option);
    171         abort ();
    172     }
    173   }
    174   while (next_option != -1);
    175 
    176   if ( source_uri == NULL ) {
    177     if (argc - optind == 1) {
    178       source_uri = argv[optind];
    179     } else if ( argc - optind > 1 ) {
    180       errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]);
    181       usage ( stderr, 1 );
    182     }
    183   } else if ( argc - optind > 0 ) {
    184     errmsg ("Error: extra non-option argument %s\n", argv[optind]);
    185     usage ( stderr, 1 );
    186   }
    187 
    188   if (source_uri != NULL) {
    189     debug ("Input file : %s\n", source_uri);
    190   } else if (source_uri != NULL && sink_uri != NULL) {
    191     debug ("Input file : %s\n", source_uri);
    192     debug ("Output file : %s\n", sink_uri);
    193   } else {
    194 #if HAVE_JACK
    195     debug ("Jack input output\n");
    196     usejack = 1;
    197 #else
    198     errmsg("Error: no arguments given (and no available audio input)\n");
    199     usage ( stderr, 1 );
    200 #endif
    201   }
    202 
    203   return 0;
    204 }
     68extern void usage (FILE * stream, int exit_code);
     69extern int parse_args (int argc, char **argv);
    20570
    20671void
     
    22792    }
    22893  }
    229 #ifdef HAVE_LASH
    230   else {
    231     aubio_lash_client = lash_init (lash_args, argv[0],
    232         LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0));
    233     if (!aubio_lash_client) {
    234       fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__);
    235     }
    236     /* tell the lash server our client id */
    237     if (lash_enabled (aubio_lash_client)) {
    238       lash_event_t *event =
    239           (lash_event_t *) lash_event_new_with_type (LASH_Client_Name);
    240       lash_event_set_string (event, "aubio");
    241       lash_send_event (aubio_lash_client, event);
    242       pthread_create (&lash_thread, NULL, lash_thread_main, NULL);
    243     }
    244   }
    245 #endif /* HAVE_LASH */
    246 
    24794  ibuf = new_fvec (overlap_size);
    24895  obuf = new_fvec (overlap_size);
     
    302149    debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
    303150
    304     flush_process (process_func, print);
    305151    del_aubio_source (this_source);
    306152    del_aubio_sink   (this_sink);
    307153
    308   }
    309 }
    310 
    311 void
    312 flush_process (aubio_process_func_t process_func, aubio_print_func_t print)
    313 {
    314   uint_t i;
    315   fvec_zeros(obuf);
    316   for (i = 0; (signed) i < frames_delay; i++) {
    317     process_func (&ibuf->data, &obuf->data, overlap_size);
    318     print ();
    319154  }
    320155}
     
    349184}
    350185
    351 
    352 #if HAVE_LASH
    353 
    354 void *
    355 lash_thread_main (void *data __attribute__ ((unused)))
    356 {
    357   printf ("LASH thread running\n");
    358 
    359   while (!lash_main ())
    360     usleep (1000);
    361 
    362   printf ("LASH thread finished\n");
    363   return NULL;
    364 }
    365 
    366 int
    367 lash_main (void)
    368 {
    369   lash_event_t *lash_event;
    370   lash_config_t *lash_config;
    371 
    372   while ((lash_event = lash_get_event (aubio_lash_client))) {
    373     switch (lash_event_get_type (lash_event)) {
    374       case LASH_Quit:
    375         lash_event_destroy (lash_event);
    376         exit (1);
    377         return 1;
    378       case LASH_Restore_Data_Set:
    379         lash_send_event (aubio_lash_client, lash_event);
    380         break;
    381       case LASH_Save_Data_Set:
    382         save_data ();
    383         lash_send_event (aubio_lash_client, lash_event);
    384         break;
    385       case LASH_Server_Lost:
    386         return 1;
    387       default:
    388         printf ("%s: received unknown LASH event of type %d",
    389             __FUNCTION__, lash_event_get_type (lash_event));
    390         lash_event_destroy (lash_event);
    391         break;
    392     }
    393   }
    394 
    395   while ((lash_config = lash_get_config (aubio_lash_client))) {
    396     restore_data (lash_config);
    397     lash_config_destroy (lash_config);
    398   }
    399 
    400   return 0;
    401 }
    402 
    403 void
    404 save_data ()
    405 {
    406   lash_config_t *lash_config;
    407 
    408   lash_config = lash_config_new_with_key ("threshold");
    409   lash_config_set_value_double (lash_config, threshold);
    410   lash_send_config (aubio_lash_client, lash_config);
    411 
    412 }
    413 
    414 void
    415 restore_data (lash_config_t * lash_config)
    416 {
    417   const char *lash_key;
    418 
    419   lash_key = lash_config_get_key (lash_config);
    420 
    421   if (strcmp (lash_key, "threshold") == 0) {
    422     threshold = lash_config_get_value_double (lash_config);
    423     return;
    424   }
    425 
    426 }
    427 
    428 #endif /* HAVE_LASH */
Note: See TracChangeset for help on using the changeset viewer.