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 *output_filename = NULL;
45 const char *input_filename = NULL;
46 const char *onset_filename =
47 AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
54 char_t * pitch_unit = "default";
55 char_t * pitch_mode = "default";
57 /* energy,specdiff,hfc,complexdomain,phase */
58 char_t * onset_mode = "default";
59 smpl_t threshold = 0.0; // leave unset, only set as asked
60 smpl_t silence = -90.;
61 uint_t buffer_size = 512; //1024;
62 uint_t overlap_size = 256; //512;
63 uint_t samplerate = 44100;
67 aubio_sndfile_t *file = NULL;
68 aubio_sndfile_t *fileout = NULL;
78 /* badly redeclare some things */
81 const char *prog_name;
83 void flush_process (aubio_process_func_t process_func,
84 aubio_print_func_t print);
87 usage (FILE * stream, int exit_code)
89 fprintf (stream, "usage: %s [ options ] \n", prog_name);
91 " -h --help Display this message.\n"
92 " -v --verbose Be verbose.\n"
93 " -j --jack Use Jack.\n"
94 " -o --output Output type.\n"
95 " -i --input Input type.\n"
96 " -O --onset Select onset detection algorithm.\n"
97 " -t --threshold Set onset detection threshold.\n"
98 " -s --silence Select silence threshold.\n"
99 " -p --pitch Select pitch detection algorithm.\n"
100 " -B --bufsize Set buffer size.\n"
101 " -H --hopsize Set hopsize.\n"
102 " -a --averaging Use averaging.\n");
107 parse_args (int argc, char **argv)
109 const char *options = "hvjo:i:O:t:s:p:B:H:a";
111 struct option long_options[] = {
112 {"help", 0, NULL, 'h'},
113 {"verbose", 0, NULL, 'v'},
114 {"jack", 0, NULL, 'j'},
115 {"output", 1, NULL, 'o'},
116 {"input", 1, NULL, 'i'},
117 {"onset", 1, NULL, 'O'},
118 {"threshold", 1, NULL, 't'},
119 {"silence", 1, NULL, 's'},
120 {"pitch", 1, NULL, 'p'},
121 {"averaging", 0, NULL, 'a'},
122 {"bufsize", 1, NULL, 'B'},
123 {"hopsize", 1, NULL, 'H'},
127 lash_args = lash_extract_args (&argc, &argv);
128 #endif /* HAVE_LASH */
135 next_option = getopt_long (argc, argv, options, long_options, NULL);
136 switch (next_option) {
138 output_filename = optarg;
141 input_filename = optarg;
146 case 'v': /* verbose */
152 case 'O': /*onset type */
155 case 's': /* silence value for onset */
156 silence = (smpl_t) atof (optarg);
158 case 't': /* threshold value for onset */
159 threshold = (smpl_t) atof (optarg);
168 buffer_size = atoi (optarg);
171 overlap_size = atoi (optarg);
173 case '?': /* unknown options */
176 case -1: /* done with options */
178 default: /*something else unexpected */
179 fprintf (stderr, "Error parsing option '%c'\n", next_option);
183 while (next_option != -1);
185 if (input_filename != NULL) {
186 debug ("Input file : %s\n", input_filename);
187 } else if (input_filename != NULL && output_filename != NULL) {
188 debug ("Input file : %s\n", input_filename);
189 debug ("Output file : %s\n", output_filename);
192 debug ("Jack input output\n");
196 ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
207 examples_common_init (int argc, char **argv)
210 uint_t found_wood = 0;
212 aubio_sndfile_t *onsetfile = NULL;
213 /* parse command line arguments */
214 parse_args (argc, argv);
217 debug ("Opening files ...\n");
218 file = new_aubio_sndfile_ro (input_filename);
220 outmsg ("Could not open input file %s.\n", input_filename);
224 aubio_sndfile_info (file);
225 samplerate = aubio_sndfile_samplerate (file);
226 if (output_filename != NULL)
227 fileout = new_aubio_sndfile_wo (file, output_filename);
231 aubio_lash_client = lash_init (lash_args, argv[0],
232 LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0));
233 if (!aubio_lash_client) {
234 fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__);
236 /* tell the lash server our client id */
237 if (lash_enabled (aubio_lash_client)) {
238 lash_event_t *event =
239 (lash_event_t *) lash_event_new_with_type (LASH_Client_Name);
240 lash_event_set_string (event, "aubio");
241 lash_send_event (aubio_lash_client, event);
242 pthread_create (&lash_thread, NULL, lash_thread_main, NULL);
245 #endif /* HAVE_LASH */
247 woodblock = new_fvec (overlap_size);
248 if (output_filename || usejack) {
249 /* dummy assignement to keep egcs happy */
250 found_wood = (onsetfile = new_aubio_sndfile_ro (onset_filename)) ||
251 (onsetfile = new_aubio_sndfile_ro ("sounds/woodblock.aiff")) ||
252 (onsetfile = new_aubio_sndfile_ro ("../sounds/woodblock.aiff"));
253 if (onsetfile == NULL) {
254 outmsg ("Could not find woodblock.aiff\n");
259 /* read the output sound once */
260 aubio_sndfile_read_mono (onsetfile, overlap_size, woodblock);
263 ibuf = new_fvec (overlap_size);
264 obuf = new_fvec (overlap_size);
268 #else /* HAVE_SNDFILE */
271 examples_common_init (int argc, char **argv)
273 outmsg ("Error, compiled without sndfile, nothing to do for now!\n");
277 #endif /* HAVE_SNDFILE */
281 examples_common_del (void)
286 del_fvec (woodblock);
292 aubio_jack_t *jack_setup;
298 examples_common_process (aubio_process_func_t process_func,
299 aubio_print_func_t print)
304 debug ("Jack init ...\n");
305 jack_setup = new_aubio_jack (1, 1,
306 0, 1, (aubio_process_func_t) process_func);
307 debug ("Jack activation ...\n");
308 aubio_jack_activate (jack_setup);
309 debug ("Processing (Ctrl+C to quit) ...\n");
311 aubio_jack_close (jack_setup);
314 outmsg ("Compiled without jack output, exiting.\n");
319 debug ("Processing ...\n");
323 while ((signed) overlap_size ==
324 aubio_sndfile_read_mono (file, overlap_size, ibuf)) {
325 process_func (&ibuf->data, &obuf->data, overlap_size);
327 if (output_filename != NULL) {
328 aubio_sndfile_write (fileout, overlap_size, &obuf);
333 debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
335 flush_process (process_func, print);
336 del_aubio_sndfile (file);
338 if (output_filename != NULL)
339 del_aubio_sndfile (fileout);
344 #else /* HAVE_SNDFILE */
347 examples_common_process (aubio_process_func_t process_func,
348 aubio_print_func_t print)
352 #endif /* HAVE_SNDFILE */
355 flush_process (aubio_process_func_t process_func, aubio_print_func_t print)
359 for (i = 0; (signed) i < frames_delay; i++) {
360 process_func (&ibuf->data, &obuf->data, overlap_size);
366 send_noteon (int pitch, int velo)
368 smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
370 jack_midi_event_t ev;
372 ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
376 ev.buffer[1] = mpitch;
378 ev.buffer[0] = 0x80; /* note off */
380 ev.buffer[0] = 0x90; /* note on */
382 aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
387 outmsg ("%f\n", frames * overlap_size / (float) samplerate);
389 outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate);
398 lash_thread_main (void *data __attribute__ ((unused)))
400 printf ("LASH thread running\n");
402 while (!lash_main ())
405 printf ("LASH thread finished\n");
412 lash_event_t *lash_event;
413 lash_config_t *lash_config;
415 while ((lash_event = lash_get_event (aubio_lash_client))) {
416 switch (lash_event_get_type (lash_event)) {
418 lash_event_destroy (lash_event);
421 case LASH_Restore_Data_Set:
422 lash_send_event (aubio_lash_client, lash_event);
424 case LASH_Save_Data_Set:
426 lash_send_event (aubio_lash_client, lash_event);
428 case LASH_Server_Lost:
431 printf ("%s: received unknown LASH event of type %d",
432 __FUNCTION__, lash_event_get_type (lash_event));
433 lash_event_destroy (lash_event);
438 while ((lash_config = lash_get_config (aubio_lash_client))) {
439 restore_data (lash_config);
440 lash_config_destroy (lash_config);
449 lash_config_t *lash_config;
451 lash_config = lash_config_new_with_key ("threshold");
452 lash_config_set_value_double (lash_config, threshold);
453 lash_send_config (aubio_lash_client, lash_config);
458 restore_data (lash_config_t * lash_config)
460 const char *lash_key;
462 lash_key = lash_config_get_key (lash_config);
464 if (strcmp (lash_key, "threshold") == 0) {
465 threshold = lash_config_get_value_double (lash_config);
471 #endif /* HAVE_LASH */