source: examples/parse_args.h @ 0428150

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 0428150 was 0428150, checked in by Paul Brossier <piem@piem.org>, 10 years ago

examples/parse_args.h: check buffer_size and hop_size

  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[1b25a70]1/*
[466dff3]2  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
[1b25a70]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
21extern int verbose;
22// input / output
23extern int usejack;
24extern char_t *source_uri;
25extern char_t *sink_uri;
26// general stuff
27extern uint_t samplerate;
28extern uint_t buffer_size;
[466dff3]29extern uint_t hop_size;
[1b25a70]30// onset stuff
31extern char_t * onset_method;
32extern smpl_t onset_threshold;
33// pitch stuff
34extern char_t * pitch_method;
35extern char_t * pitch_unit;
36extern smpl_t pitch_tolerance;
37// tempo stuff
38extern char_t * tempo_method;
39// more general stuff
40extern smpl_t silence;
41extern uint_t mix_input;
42
[7f2c515]43typedef int (*aubio_process_func_t)(fvec_t * input, fvec_t * output);
44
[1b25a70]45// functions defined in utils.c
46extern void examples_common_init (int argc, char **argv);
47extern void examples_common_del (void);
48extern void examples_common_process (aubio_process_func_t process_func,
49    aubio_print_func_t print);
50
51// internal stuff
[466dff3]52extern int blocks;
[1b25a70]53
54extern fvec_t *ibuf;
55extern fvec_t *obuf;
56
57const char *prog_name;
58
59void
60usage (FILE * stream, int exit_code)
61{
62  fprintf (stream, "usage: %s [ options ] \n", prog_name);
63  fprintf (stream,
64      "       -h      --help             display this message\n"
65      "       -v      --verbose          be verbose\n"
[3da8187]66#ifdef PROG_HAS_JACK
[1b25a70]67      "       -j      --jack             use Jack\n"
68#endif
69      "       -i      --input            input type\n"
[3da8187]70#ifdef PROG_HAS_OUTPUT
[1b25a70]71      "       -o      --output           output type\n"
[3da8187]72#endif
[1b25a70]73      "       -r      --samplerate       select samplerate\n"
74      "       -B      --bufsize          set buffer size\n"
75      "       -H      --hopsize          set hopsize\n"
76#ifdef PROG_HAS_ONSET
77      "       -O      --onset            select onset detection algorithm\n"
78      "       -t      --onset-threshold  set onset detection threshold\n"
79#endif /* PROG_HAS_ONSET */
80#ifdef PROG_HAS_PITCH
81      "       -p      --pitch            select pitch detection algorithm\n"
82      "       -u      --pitch-unit       select pitch output unit\n"
83      "       -l      --pitch-tolerance  select pitch tolerance\n"
84#endif /* PROG_HAS_PITCH */
85      "       -s      --silence          select silence threshold\n"
[3da8187]86#ifdef PROG_HAS_OUTPUT
[1b25a70]87      "       -m      --mix-input        mix input signal with output signal\n"
[3da8187]88#endif
[1b25a70]89      );
90  exit (exit_code);
91}
92
93int
94parse_args (int argc, char **argv)
95{
96  const char *options = "hv"
[3da8187]97    "i:r:B:H:"
98#ifdef PROG_HAS_JACK
[1b25a70]99    "j"
[3da8187]100#endif /* PROG_HAS_JACK */
101#ifdef PROG_HAS_OUTPUT
102    "o:"
103#endif /* PROG_HAS_OUTPUT */
[1b25a70]104#ifdef PROG_HAS_ONSET
105    "O:t:"
106#endif /* PROG_HAS_ONSET */
107#ifdef PROG_HAS_PITCH
108    "p:u:l:"
109#endif /* PROG_HAS_PITCH */
110    "s:m";
111  int next_option;
112  struct option long_options[] = {
113    {"help",                  0, NULL, 'h'},
114    {"verbose",               0, NULL, 'v'},
115    {"input",                 1, NULL, 'i'},
116    {"samplerate",            1, NULL, 'r'},
117    {"bufsize",               1, NULL, 'B'},
118    {"hopsize",               1, NULL, 'H'},
[3da8187]119#ifdef PROG_HAS_JACK
120    {"jack",                  0, NULL, 'j'},
121#endif /* PROG_HAS_JACK */
122#ifdef PROG_HAS_OUTPUT
123    {"output",                1, NULL, 'o'},
124#endif /* PROG_HAS_OUTPUT */
[1b25a70]125#ifdef PROG_HAS_ONSET
126    {"onset",                 1, NULL, 'O'},
127    {"onset-threshold",       1, NULL, 't'},
128#endif /* PROG_HAS_ONSET */
129#ifdef PROG_HAS_PITCH
130    {"pitch",                 1, NULL, 'p'},
131    {"pitch-unit",            1, NULL, 'u'},
132    {"pitch-tolerance",       1, NULL, 'l'},
133#endif /* PROG_HAS_PITCH */
134    {"silence",               1, NULL, 's'},
135    {"mix-input",             0, NULL, 'm'},
136    {NULL,                    0, NULL, 0}
137  };
138  prog_name = argv[0];
139  if (argc < 1) {
140    usage (stderr, 1);
141    return -1;
142  }
143  do {
144    next_option = getopt_long (argc, argv, options, long_options, NULL);
145    switch (next_option) {
146      case 'h':                /* help */
147        usage (stdout, 0);
148        return -1;
149      case 'v':                /* verbose */
150        verbose = 1;
151        break;
152      case 'j':
153        usejack = 1;
154        break;
155      case 'i':
156        source_uri = optarg;
157        break;
158      case 'o':
159        sink_uri = optarg;
160        break;
161      case 'r':
162        samplerate = atoi (optarg);
163        break;
164      case 'B':
165        buffer_size = atoi (optarg);
166        break;
167      case 'H':
[466dff3]168        hop_size = atoi (optarg);
[1b25a70]169        break;
170      case 'O':                /*onset type */
171        onset_method = optarg;
172        break;
173      case 't':                /* threshold value for onset */
174        onset_threshold = (smpl_t) atof (optarg);
175        break;
176      case 'p':
177        pitch_method = optarg;
178        break;
179      case 'u':
180        pitch_unit = optarg;
181        break;
182      case 'l':
183        pitch_tolerance = (smpl_t) atof (optarg);
184        break;
185      case 's':                /* silence threshold */
186        silence = (smpl_t) atof (optarg);
187        break;
188      case 'm':                /* mix_input flag */
189        mix_input = 1;
190        break;
191      case '?':                /* unknown options */
192        usage (stderr, 1);
193        break;
194      case -1:                 /* done with options */
195        break;
196      default:                 /*something else unexpected */
197        fprintf (stderr, "Error parsing option '%c'\n", next_option);
198        abort ();
199    }
200  }
201  while (next_option != -1);
202
[3da8187]203  // if unique, use the non option argument as the source
[1b25a70]204  if ( source_uri == NULL ) {
205    if (argc - optind == 1) {
206      source_uri = argv[optind];
207    } else if ( argc - optind > 1 ) {
208      errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]);
209      usage ( stderr, 1 );
210    }
211  } else if ( argc - optind > 0 ) {
212    errmsg ("Error: extra non-option argument %s\n", argv[optind]);
213    usage ( stderr, 1 );
214  }
215
[3da8187]216  // if no source, show a message
217  if (source_uri == NULL) {
[8a22fc4]218#ifdef PROG_HAS_JACK
[1b25a70]219#if HAVE_JACK
[3da8187]220    verbmsg("No input source given, using jack\n");
[1b25a70]221    usejack = 1;
222#else
223    errmsg("Error: no arguments given (and no available audio input)\n");
224    usage ( stderr, 1 );
[8a22fc4]225#endif /* HAVE_JACK */
226#else
227    errmsg("Error: no arguments given\n");
228    usage ( stderr, 1 );
229#endif /* PROG_HAS_JACK */
[1b25a70]230  }
231
[0428150]232  if (hop_size < 1) {
233    errmsg("Error: got hop_size %d, but can not be < 1\n", hop_size);
234    usage ( stderr, 1 );
235  } else if (buffer_size < 2) {
236    errmsg("Error: got buffer_size %d, but can not be < 2\n", buffer_size);
237    usage ( stderr, 1 );
238  } else if (buffer_size < hop_size + 1) {
239    errmsg("Error: hop size (%d) is larger than or equal to win size (%d)\n",
240        buffer_size, hop_size);
241    usage ( stderr, 1 );
242  }
243
[1b25a70]244  return 0;
245}
Note: See TracBrowser for help on using the repository browser.