2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
23 This file includes some tools common to all examples. Code specific to the
24 algorithm performed by each program should go in the source file of that
32 #include <lash/lash.h>
34 lash_client_t *aubio_lash_client;
35 lash_args_t *lash_args;
36 void *lash_thread_main (void *data);
38 void save_data (void);
39 void restore_data (lash_config_t * lash_config);
40 pthread_t lash_thread;
41 #endif /* HAVE_LASH */
44 const char *sink_uri = NULL;
45 const char *source_uri = NULL;
52 char_t * pitch_unit = "default";
53 char_t * pitch_mode = "default";
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;
63 aubio_source_t *this_source = NULL;
64 aubio_sink_t *this_sink = NULL;
70 /* badly redeclare some things */
73 const char *prog_name;
75 void flush_process (aubio_process_func_t process_func,
76 aubio_print_func_t print);
79 usage (FILE * stream, int exit_code)
81 fprintf (stream, "usage: %s [ options ] \n", prog_name);
83 " -h --help Display this message.\n"
84 " -v --verbose Be verbose.\n"
85 " -j --jack Use Jack.\n"
86 " -o --output Output type.\n"
87 " -i --input Input type.\n"
88 " -O --onset Select onset detection algorithm.\n"
89 " -t --threshold Set onset detection threshold.\n"
90 " -s --silence Select silence threshold.\n"
91 " -p --pitch Select pitch detection algorithm.\n"
92 " -B --bufsize Set buffer size.\n"
93 " -H --hopsize Set hopsize.\n"
94 " -a --averaging Use averaging.\n");
99 parse_args (int argc, char **argv)
101 const char *options = "hvjo:i:O:t:s:p:B:H:a";
103 struct option long_options[] = {
104 {"help", 0, NULL, 'h'},
105 {"verbose", 0, NULL, 'v'},
106 {"jack", 0, NULL, 'j'},
107 {"output", 1, NULL, 'o'},
108 {"input", 1, NULL, 'i'},
109 {"onset", 1, NULL, 'O'},
110 {"threshold", 1, NULL, 't'},
111 {"silence", 1, NULL, 's'},
112 {"pitch", 1, NULL, 'p'},
113 {"averaging", 0, NULL, 'a'},
114 {"bufsize", 1, NULL, 'B'},
115 {"hopsize", 1, NULL, 'H'},
119 lash_args = lash_extract_args (&argc, &argv);
120 #endif /* HAVE_LASH */
127 next_option = getopt_long (argc, argv, options, long_options, NULL);
128 switch (next_option) {
138 case 'v': /* verbose */
144 case 'O': /*onset type */
147 case 's': /* silence value for onset */
148 silence = (smpl_t) atof (optarg);
150 case 't': /* threshold value for onset */
151 threshold = (smpl_t) atof (optarg);
160 buffer_size = atoi (optarg);
163 overlap_size = atoi (optarg);
165 case '?': /* unknown options */
168 case -1: /* done with options */
170 default: /*something else unexpected */
171 fprintf (stderr, "Error parsing option '%c'\n", next_option);
175 while (next_option != -1);
177 if (source_uri != NULL) {
178 debug ("Input file : %s\n", source_uri);
179 } else if (source_uri != NULL && sink_uri != NULL) {
180 debug ("Input file : %s\n", source_uri);
181 debug ("Output file : %s\n", sink_uri);
184 debug ("Jack input output\n");
187 errmsg("Error: no arguments given (and no available audio input)\n");
196 examples_common_init (int argc, char **argv)
199 /* parse command line arguments */
200 parse_args (argc, argv);
203 debug ("Opening files ...\n");
204 this_source = new_aubio_source ((char_t*)source_uri, 0, overlap_size);
205 if (this_source == NULL) {
206 outmsg ("Could not open input file %s.\n", source_uri);
209 samplerate = aubio_source_get_samplerate(this_source);
210 if (sink_uri != NULL) {
211 this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
212 if (this_sink == NULL) {
213 outmsg ("Could not open output file %s.\n", sink_uri);
220 aubio_lash_client = lash_init (lash_args, argv[0],
221 LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0));
222 if (!aubio_lash_client) {
223 fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__);
225 /* tell the lash server our client id */
226 if (lash_enabled (aubio_lash_client)) {
227 lash_event_t *event =
228 (lash_event_t *) lash_event_new_with_type (LASH_Client_Name);
229 lash_event_set_string (event, "aubio");
230 lash_send_event (aubio_lash_client, event);
231 pthread_create (&lash_thread, NULL, lash_thread_main, NULL);
234 #endif /* HAVE_LASH */
236 woodblock = new_fvec (overlap_size);
237 //TODO create woodblock sound
239 ibuf = new_fvec (overlap_size);
240 obuf = new_fvec (overlap_size);
245 examples_common_del (void)
249 del_fvec (woodblock);
254 aubio_jack_t *jack_setup;
258 examples_common_process (aubio_process_func_t process_func,
259 aubio_print_func_t print)
266 debug ("Jack init ...\n");
267 jack_setup = new_aubio_jack (1, 1,
268 0, 1, (aubio_process_func_t) process_func);
269 debug ("Jack activation ...\n");
270 aubio_jack_activate (jack_setup);
271 debug ("Processing (Ctrl+C to quit) ...\n");
273 aubio_jack_close (jack_setup);
276 outmsg ("Compiled without jack output, exiting.\n");
281 debug ("Processing ...\n");
286 aubio_source_do (this_source, ibuf, &read);
287 process_func (&ibuf->data, &obuf->data, overlap_size);
290 aubio_sink_do (this_sink, obuf, overlap_size);
293 } while (read == overlap_size);
295 debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
297 flush_process (process_func, print);
298 del_aubio_source (this_source);
299 del_aubio_sink (this_sink);
305 flush_process (aubio_process_func_t process_func, aubio_print_func_t print)
309 for (i = 0; (signed) i < frames_delay; i++) {
310 process_func (&ibuf->data, &obuf->data, overlap_size);
316 send_noteon (int pitch, int velo)
318 smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
320 jack_midi_event_t ev;
322 ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
326 ev.buffer[1] = mpitch;
328 ev.buffer[0] = 0x80; /* note off */
330 ev.buffer[0] = 0x90; /* note on */
332 aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
337 outmsg ("%f\n", frames * overlap_size / (float) samplerate);
339 outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate);
348 lash_thread_main (void *data __attribute__ ((unused)))
350 printf ("LASH thread running\n");
352 while (!lash_main ())
355 printf ("LASH thread finished\n");
362 lash_event_t *lash_event;
363 lash_config_t *lash_config;
365 while ((lash_event = lash_get_event (aubio_lash_client))) {
366 switch (lash_event_get_type (lash_event)) {
368 lash_event_destroy (lash_event);
371 case LASH_Restore_Data_Set:
372 lash_send_event (aubio_lash_client, lash_event);
374 case LASH_Save_Data_Set:
376 lash_send_event (aubio_lash_client, lash_event);
378 case LASH_Server_Lost:
381 printf ("%s: received unknown LASH event of type %d",
382 __FUNCTION__, lash_event_get_type (lash_event));
383 lash_event_destroy (lash_event);
388 while ((lash_config = lash_get_config (aubio_lash_client))) {
389 restore_data (lash_config);
390 lash_config_destroy (lash_config);
399 lash_config_t *lash_config;
401 lash_config = lash_config_new_with_key ("threshold");
402 lash_config_set_value_double (lash_config, threshold);
403 lash_send_config (aubio_lash_client, lash_config);
408 restore_data (lash_config_t * lash_config)
410 const char *lash_key;
412 lash_key = lash_config_get_key (lash_config);
414 if (strcmp (lash_key, "threshold") == 0) {
415 threshold = lash_config_get_value_double (lash_config);
421 #endif /* HAVE_LASH */