examples/: move parse_args to parse_args.h, clean up, remove lash and old frames_delay
[aubio.git] / examples / parse_args.h
1 /*
2   Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 extern int verbose;
22 // input / output
23 extern int usejack;
24 extern char_t *source_uri;
25 extern char_t *sink_uri;
26 // general stuff
27 extern uint_t samplerate;
28 extern uint_t buffer_size;
29 extern uint_t overlap_size;
30 // onset stuff
31 extern char_t * onset_method;
32 extern smpl_t onset_threshold;
33 // pitch stuff
34 extern char_t * pitch_method;
35 extern char_t * pitch_unit;
36 extern smpl_t pitch_tolerance;
37 // tempo stuff
38 extern char_t * tempo_method;
39 // more general stuff
40 extern smpl_t silence;
41 extern uint_t mix_input;
42
43 // functions defined in utils.c
44 extern void examples_common_init (int argc, char **argv);
45 extern void examples_common_del (void);
46 extern void examples_common_process (aubio_process_func_t process_func,
47     aubio_print_func_t print);
48
49 // internal stuff
50 extern int frames;
51
52 extern fvec_t *ibuf;
53 extern fvec_t *obuf;
54
55
56 const char *prog_name;
57
58 void
59 usage (FILE * stream, int exit_code)
60 {
61   fprintf (stream, "usage: %s [ options ] \n", prog_name);
62   fprintf (stream,
63       "       -h      --help             display this message\n"
64       "       -v      --verbose          be verbose\n"
65 #ifdef HAVE_JACK
66       "       -j      --jack             use Jack\n"
67 #endif
68       "       -i      --input            input type\n"
69       "       -o      --output           output type\n"
70       "       -r      --samplerate       select samplerate\n"
71       "       -B      --bufsize          set buffer size\n"
72       "       -H      --hopsize          set hopsize\n"
73 #ifdef PROG_HAS_ONSET
74       "       -O      --onset            select onset detection algorithm\n"
75       "       -t      --onset-threshold  set onset detection threshold\n"
76 #endif /* PROG_HAS_ONSET */
77 #ifdef PROG_HAS_PITCH
78       "       -p      --pitch            select pitch detection algorithm\n"
79       "       -u      --pitch-unit       select pitch output unit\n"
80       "       -l      --pitch-tolerance  select pitch tolerance\n"
81 #endif /* PROG_HAS_PITCH */
82       "       -s      --silence          select silence threshold\n"
83       "       -m      --mix-input        mix input signal with output signal\n"
84       );
85   exit (exit_code);
86 }
87
88 int
89 parse_args (int argc, char **argv)
90 {
91   const char *options = "hv"
92 #ifdef HAVE_JACK
93     "j"
94 #endif
95     "i:o:r:B:H:"
96 #ifdef PROG_HAS_ONSET
97     "O:t:"
98 #endif /* PROG_HAS_ONSET */
99 #ifdef PROG_HAS_PITCH
100     "p:u:l:"
101 #endif /* PROG_HAS_PITCH */
102     "s:m";
103   int next_option;
104   struct option long_options[] = {
105     {"help",                  0, NULL, 'h'},
106     {"verbose",               0, NULL, 'v'},
107 #ifdef HAVE_JACK
108     {"jack",                  0, NULL, 'j'},
109 #endif
110     {"input",                 1, NULL, 'i'},
111     {"output",                1, NULL, 'o'},
112     {"samplerate",            1, NULL, 'r'},
113     {"bufsize",               1, NULL, 'B'},
114     {"hopsize",               1, NULL, 'H'},
115 #ifdef PROG_HAS_ONSET
116     {"onset",                 1, NULL, 'O'},
117     {"onset-threshold",       1, NULL, 't'},
118 #endif /* PROG_HAS_ONSET */
119 #ifdef PROG_HAS_PITCH
120     {"pitch",                 1, NULL, 'p'},
121     {"pitch-unit",            1, NULL, 'u'},
122     {"pitch-tolerance",       1, NULL, 'l'},
123 #endif /* PROG_HAS_PITCH */
124     {"silence",               1, NULL, 's'},
125     {"mix-input",             0, NULL, 'm'},
126     {NULL,                    0, NULL, 0}
127   };
128   prog_name = argv[0];
129   if (argc < 1) {
130     usage (stderr, 1);
131     return -1;
132   }
133   do {
134     next_option = getopt_long (argc, argv, options, long_options, NULL);
135     switch (next_option) {
136       case 'h':                /* help */
137         usage (stdout, 0);
138         return -1;
139       case 'v':                /* verbose */
140         verbose = 1;
141         break;
142       case 'j':
143         usejack = 1;
144         break;
145       case 'i':
146         source_uri = optarg;
147         break;
148       case 'o':
149         sink_uri = optarg;
150         break;
151       case 'r':
152         samplerate = atoi (optarg);
153         break;
154       case 'B':
155         buffer_size = atoi (optarg);
156         break;
157       case 'H':
158         overlap_size = atoi (optarg);
159         break;
160       case 'O':                /*onset type */
161         onset_method = optarg;
162         break;
163       case 't':                /* threshold value for onset */
164         onset_threshold = (smpl_t) atof (optarg);
165         break;
166       case 'p':
167         pitch_method = optarg;
168         break;
169       case 'u':
170         pitch_unit = optarg;
171         break;
172       case 'l':
173         pitch_tolerance = (smpl_t) atof (optarg);
174         break;
175       case 's':                /* silence threshold */
176         silence = (smpl_t) atof (optarg);
177         break;
178       case 'm':                /* mix_input flag */
179         mix_input = 1;
180         break;
181       case '?':                /* unknown options */
182         usage (stderr, 1);
183         break;
184       case -1:                 /* done with options */
185         break;
186       default:                 /*something else unexpected */
187         fprintf (stderr, "Error parsing option '%c'\n", next_option);
188         abort ();
189     }
190   }
191   while (next_option != -1);
192
193   if ( source_uri == NULL ) {
194     if (argc - optind == 1) {
195       source_uri = argv[optind];
196     } else if ( argc - optind > 1 ) {
197       errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]);
198       usage ( stderr, 1 );
199     }
200   } else if ( argc - optind > 0 ) {
201     errmsg ("Error: extra non-option argument %s\n", argv[optind]);
202     usage ( stderr, 1 );
203   }
204
205   if (source_uri != NULL) {
206     debug ("Input file : %s\n", source_uri);
207   } else if (source_uri != NULL && sink_uri != NULL) {
208     debug ("Input file : %s\n", source_uri);
209     debug ("Output file : %s\n", sink_uri);
210   } else {
211 #if HAVE_JACK
212     debug ("Jack input output\n");
213     usejack = 1;
214 #else
215     errmsg("Error: no arguments given (and no available audio input)\n");
216     usage ( stderr, 1 );
217 #endif
218   }
219
220   return 0;
221 }