81ded93aff93c765a5cacc579fbb708daefeecf1
[aubio.git] / examples / utils.c
1
2 #include "aubio.h"
3
4 #ifndef JACK_SUPPORT
5 #define JACK_SUPPORT 0
6 #endif
7
8 #include <getopt.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <math.h> /* for isfinite */
13 #include "utils.h"
14
15 /* not supported yet */
16 #ifdef LADCCA_SUPPORT
17 #include <ladcca/ladcca.h>
18 cca_client_t * aubio_cca_client;
19 #endif /* LADCCA_SUPPORT */
20
21
22 /* badly redeclare some things */
23 aubio_onsetdetection_type type_onset;
24 smpl_t threshold;
25 smpl_t averaging;
26 const char * prog_name;
27
28 void usage (FILE * stream, int exit_code)
29 {
30         fprintf(stream, "usage: %s [ options ] \n", prog_name);
31         fprintf(stream, 
32                         "       -j      --jack          Use Jack.\n"
33                         "       -o      --output        Output type.\n"
34                         "       -i      --input         Input type.\n"
35                         "       -h      --help          Display this message.\n"
36                         "       -v      --verbose       Print verbose message.\n"
37                         );
38         exit(exit_code);
39 }
40
41 int parse_args (int argc, char **argv) {
42         const char *options = "hvjo:i:O:t:a";
43         int next_option;
44         struct option long_options[] =
45         {
46                 {"help",                0, NULL, 'h'},
47                 {"verbose",     0, NULL, 'v'},
48                 {"jack",                0, NULL, 'j'},
49                 {"output",      0, NULL, 'o'},
50                 {"input",       0, NULL, 'i'},
51                 {"onset",       0, NULL, 'O'},
52                 {"threshold",   0, NULL, 't'},
53                 {"averaging",   0, NULL, 'a'},
54                 {NULL,                  0, NULL, 0}
55         };
56         prog_name = argv[0];    
57         if( argc < 1 ) {
58                 usage (stderr, 1);
59                 return -1;
60         }
61         do {
62                 next_option = getopt_long (argc, argv, options, 
63                                                                         long_options, NULL);
64                 switch (next_option) {
65                         case 'o':
66                                 output_filename = optarg;
67                                 break;
68                         case 'i':
69                                 input_filename = optarg;
70                                 break;
71                         case 'h':       /* help */
72                                 usage (stdout, 0);
73                                 return -1;
74                         case 'v':               /* verbose */
75                                 verbose = 1;
76                                 break;
77                         case 'j':               /* verbose */
78                                 usejack = 1;
79                                 break;
80       case 'O':   /*onset type*/
81         if (strcmp(optarg,"energy") == 0) 
82           type_onset = energy;
83         else if (strcmp(optarg,"specdiff") == 0) 
84           type_onset = specdiff;
85         else if (strcmp(optarg,"hfc") == 0) 
86           type_onset = hfc;
87         else if (strcmp(optarg,"complexdomain") == 0) 
88           type_onset = complexdomain;
89         else if (strcmp(optarg,"phase") == 0) 
90           type_onset = phase;
91         else {
92           debug("could not get onset type.\n");
93           abort();
94         }
95         break;
96       case 't':   /* threshold value for onset */
97         threshold = (smpl_t)atof(optarg);
98         /*
99         if (!isfinite(threshold)) {
100           debug("could not get threshold.\n");
101           abort();
102         }
103         */
104         break;
105       case 'a':
106         averaging = 1;
107         break; 
108                         case '?':       /* unknown options */
109                                 usage(stderr, 1);
110         break;
111                         case -1:                /* done with options */
112                                 break;
113                         default:                /*something else unexpected */
114                                 abort ();
115                 }
116         }
117         while (next_option != -1);
118
119         if (input_filename != NULL) {
120                 errmsg ("Input file : %s\n", input_filename );
121         } else if (input_filename != NULL && output_filename != NULL) {
122                 errmsg ("Input file : %s\n", input_filename );
123                 errmsg ("Output file : %s\n", output_filename );
124         } else {
125                 if (JACK_SUPPORT)
126                 {
127                         errmsg ("Jack input output\n");
128                         usejack = 1;
129                 } else {
130                         errmsg ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
131                         exit(1);
132                 }
133         }       
134         return 0;
135 }
136