examples/parse_args.h: make sure samplerate and sizes params are positive, fix help...
[aubio.git] / examples / parse_args.h
index 42a24ce..923125b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
+  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
 
   This file is part of aubio.
 
@@ -26,7 +26,7 @@ extern char_t *sink_uri;
 // general stuff
 extern uint_t samplerate;
 extern uint_t buffer_size;
-extern uint_t overlap_size;
+extern uint_t hop_size;
 // onset stuff
 extern char_t * onset_method;
 extern smpl_t onset_threshold;
@@ -37,9 +37,11 @@ extern smpl_t pitch_tolerance;
 // tempo stuff
 extern char_t * tempo_method;
 // more general stuff
-extern smpl_t silence;
+extern smpl_t silence_threshold;
 extern uint_t mix_input;
 
+extern uint_t force_overwrite;
+
 // functions defined in utils.c
 extern void examples_common_init (int argc, char **argv);
 extern void examples_common_del (void);
@@ -47,12 +49,11 @@ extern void examples_common_process (aubio_process_func_t process_func,
     aubio_print_func_t print);
 
 // internal stuff
-extern int frames;
+extern int blocks;
 
 extern fvec_t *ibuf;
 extern fvec_t *obuf;
 
-
 const char *prog_name;
 
 void
@@ -60,13 +61,10 @@ usage (FILE * stream, int exit_code)
 {
   fprintf (stream, "usage: %s [ options ] \n", prog_name);
   fprintf (stream,
-      "       -h      --help             display this message\n"
-      "       -v      --verbose          be verbose\n"
-#ifdef HAVE_JACK
-      "       -j      --jack             use Jack\n"
-#endif
       "       -i      --input            input type\n"
+#ifdef PROG_HAS_OUTPUT
       "       -o      --output           output type\n"
+#endif
       "       -r      --samplerate       select samplerate\n"
       "       -B      --bufsize          set buffer size\n"
       "       -H      --hopsize          set hopsize\n"
@@ -80,7 +78,15 @@ usage (FILE * stream, int exit_code)
       "       -l      --pitch-tolerance  select pitch tolerance\n"
 #endif /* PROG_HAS_PITCH */
       "       -s      --silence          select silence threshold\n"
+#ifdef PROG_HAS_OUTPUT
       "       -m      --mix-input        mix input signal with output signal\n"
+      "       -f      --force-overwrite  overwrite output file if needed\n"
+#endif
+#ifdef PROG_HAS_JACK
+      "       -j      --jack             use Jack\n"
+#endif
+      "       -v      --verbose          be verbose\n"
+      "       -h      --help             display this message\n"
       );
   exit (exit_code);
 }
@@ -89,29 +95,34 @@ int
 parse_args (int argc, char **argv)
 {
   const char *options = "hv"
-#ifdef HAVE_JACK
+    "i:r:B:H:"
+#ifdef PROG_HAS_JACK
     "j"
-#endif
-    "i:o:r:B:H:"
+#endif /* PROG_HAS_JACK */
+#ifdef PROG_HAS_OUTPUT
+    "o:"
+#endif /* PROG_HAS_OUTPUT */
 #ifdef PROG_HAS_ONSET
     "O:t:"
 #endif /* PROG_HAS_ONSET */
 #ifdef PROG_HAS_PITCH
     "p:u:l:"
 #endif /* PROG_HAS_PITCH */
-    "s:m";
+    "s:mf";
   int next_option;
   struct option long_options[] = {
     {"help",                  0, NULL, 'h'},
     {"verbose",               0, NULL, 'v'},
-#ifdef HAVE_JACK
-    {"jack",                  0, NULL, 'j'},
-#endif
     {"input",                 1, NULL, 'i'},
-    {"output",                1, NULL, 'o'},
     {"samplerate",            1, NULL, 'r'},
     {"bufsize",               1, NULL, 'B'},
     {"hopsize",               1, NULL, 'H'},
+#ifdef PROG_HAS_JACK
+    {"jack",                  0, NULL, 'j'},
+#endif /* PROG_HAS_JACK */
+#ifdef PROG_HAS_OUTPUT
+    {"output",                1, NULL, 'o'},
+#endif /* PROG_HAS_OUTPUT */
 #ifdef PROG_HAS_ONSET
     {"onset",                 1, NULL, 'O'},
     {"onset-threshold",       1, NULL, 't'},
@@ -123,6 +134,7 @@ parse_args (int argc, char **argv)
 #endif /* PROG_HAS_PITCH */
     {"silence",               1, NULL, 's'},
     {"mix-input",             0, NULL, 'm'},
+    {"force-overwrite",       0, NULL, 'f'},
     {NULL,                    0, NULL, 0}
   };
   prog_name = argv[0];
@@ -148,6 +160,9 @@ parse_args (int argc, char **argv)
       case 'o':
         sink_uri = optarg;
         break;
+      case 'f':                /* force_overwrite flag */
+        force_overwrite = 1;
+        break;
       case 'r':
         samplerate = atoi (optarg);
         break;
@@ -155,7 +170,7 @@ parse_args (int argc, char **argv)
         buffer_size = atoi (optarg);
         break;
       case 'H':
-        overlap_size = atoi (optarg);
+        hop_size = atoi (optarg);
         break;
       case 'O':                /*onset type */
         onset_method = optarg;
@@ -173,7 +188,7 @@ parse_args (int argc, char **argv)
         pitch_tolerance = (smpl_t) atof (optarg);
         break;
       case 's':                /* silence threshold */
-        silence = (smpl_t) atof (optarg);
+        silence_threshold = (smpl_t) atof (optarg);
         break;
       case 'm':                /* mix_input flag */
         mix_input = 1;
@@ -190,6 +205,7 @@ parse_args (int argc, char **argv)
   }
   while (next_option != -1);
 
+  // if unique, use the non option argument as the source
   if ( source_uri == NULL ) {
     if (argc - optind == 1) {
       source_uri = argv[optind];
@@ -202,19 +218,37 @@ parse_args (int argc, char **argv)
     usage ( stderr, 1 );
   }
 
-  if (source_uri != NULL) {
-    debug ("Input file : %s\n", source_uri);
-  } else if (source_uri != NULL && sink_uri != NULL) {
-    debug ("Input file : %s\n", source_uri);
-    debug ("Output file : %s\n", sink_uri);
-  } else {
+  // if no source, show a message
+  if (source_uri == NULL) {
+#ifdef PROG_HAS_JACK
 #if HAVE_JACK
-    debug ("Jack input output\n");
+    verbmsg("No input source given, using jack\n");
     usejack = 1;
 #else
     errmsg("Error: no arguments given (and no available audio input)\n");
     usage ( stderr, 1 );
-#endif
+#endif /* HAVE_JACK */
+#else
+    errmsg("Error: no arguments given\n");
+    usage ( stderr, 1 );
+#endif /* PROG_HAS_JACK */
+  }
+
+  if ((sint_t)hop_size < 1) {
+    errmsg("Error: got hop_size %d, but can not be < 1\n", hop_size);
+    usage ( stderr, 1 );
+  } else if ((sint_t)buffer_size < 2) {
+    errmsg("Error: got buffer_size %d, but can not be < 2\n", buffer_size);
+    usage ( stderr, 1 );
+  } else if ((sint_t)buffer_size < (sint_t)hop_size + 1) {
+    errmsg("Error: hop size (%d) is larger than or equal to win size (%d)\n",
+        hop_size, buffer_size);
+    usage ( stderr, 1 );
+  }
+
+  if ((sint_t)samplerate < 0) {
+    errmsg("Error: got samplerate %d, but can not be < 0\n", samplerate);
+    usage ( stderr, 1 );
   }
 
   return 0;