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/>.
24 #include <lash/lash.h>
26 lash_client_t *aubio_lash_client;
27 lash_args_t *lash_args;
28 void *lash_thread_main (void *data);
30 void save_data (void);
31 void restore_data (lash_config_t * lash_config);
32 pthread_t lash_thread;
33 #endif /* HAVE_LASH */
36 const char *output_filename = NULL;
37 const char *input_filename = NULL;
38 const char *onset_filename =
39 AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
47 /* energy,specdiff,hfc,complexdomain,phase */
48 aubio_onsetdetection_type type_onset = aubio_onset_kl;
49 aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
50 smpl_t threshold = 0.3;
51 smpl_t silence = -90.;
52 uint_t buffer_size = 512; //1024;
53 uint_t overlap_size = 256; //512;
55 uint_t samplerate = 44100;
58 aubio_sndfile_t *file = NULL;
59 aubio_sndfile_t *fileout = NULL;
66 aubio_onsetdetection_t *o;
67 aubio_onsetdetection_t *o2;
71 aubio_pickpeak_t *parms;
76 aubio_pitchdetection_t *pitchdet;
77 aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
78 aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
81 fvec_t *note_buffer = NULL;
82 fvec_t *note_buffer2 = NULL;
92 /* badly redeclare some things */
93 aubio_onsetdetection_type type_onset;
96 const char *prog_name;
99 usage (FILE * stream, int exit_code)
101 fprintf (stream, "usage: %s [ options ] \n", prog_name);
103 " -h --help Display this message.\n"
104 " -v --verbose Be verbose.\n"
105 " -j --jack Use Jack.\n"
106 " -o --output Output type.\n"
107 " -i --input Input type.\n"
108 " -O --onset Select onset detection algorithm.\n"
109 " -t --threshold Set onset detection threshold.\n"
110 " -s --silence Select silence threshold.\n"
111 " -p --pitch Select pitch detection algorithm.\n"
112 " -B --bufsize Set buffer size.\n"
113 " -H --hopsize Set hopsize.\n"
114 " -a --averaging Use averaging.\n");
119 parse_args (int argc, char **argv)
121 const char *options = "hvjo:i:O:t:s:p:B:H:a";
123 struct option long_options[] = {
124 {"help", 0, NULL, 'h'},
125 {"verbose", 0, NULL, 'v'},
126 {"jack", 0, NULL, 'j'},
127 {"output", 1, NULL, 'o'},
128 {"input", 1, NULL, 'i'},
129 {"onset", 1, NULL, 'O'},
130 {"threshold", 1, NULL, 't'},
131 {"silence", 1, NULL, 's'},
132 {"pitch", 1, NULL, 'p'},
133 {"averaging", 0, NULL, 'a'},
134 {"bufsize", 1, NULL, 'B'},
135 {"hopsize", 1, NULL, 'H'},
139 lash_args = lash_extract_args (&argc, &argv);
140 #endif /* HAVE_LASH */
147 next_option = getopt_long (argc, argv, options, long_options, NULL);
148 switch (next_option) {
150 output_filename = optarg;
153 input_filename = optarg;
158 case 'v': /* verbose */
164 case 'O': /*onset type */
165 if (strcmp (optarg, "energy") == 0)
166 type_onset = aubio_onset_energy;
167 else if (strcmp (optarg, "specdiff") == 0)
168 type_onset = aubio_onset_specdiff;
169 else if (strcmp (optarg, "hfc") == 0)
170 type_onset = aubio_onset_hfc;
171 else if (strcmp (optarg, "complexdomain") == 0)
172 type_onset = aubio_onset_complex;
173 else if (strcmp (optarg, "complex") == 0)
174 type_onset = aubio_onset_complex;
175 else if (strcmp (optarg, "phase") == 0)
176 type_onset = aubio_onset_phase;
177 else if (strcmp (optarg, "mkl") == 0)
178 type_onset = aubio_onset_mkl;
179 else if (strcmp (optarg, "kl") == 0)
180 type_onset = aubio_onset_kl;
181 else if (strcmp (optarg, "specflux") == 0)
182 type_onset = aubio_onset_specflux;
184 errmsg ("unknown onset type.\n");
189 case 's': /* threshold value for onset */
190 silence = (smpl_t) atof (optarg);
192 case 't': /* threshold value for onset */
193 threshold = (smpl_t) atof (optarg);
195 if (!isfinite(threshold)) {
196 debug("could not get threshold.\n");
202 if (strcmp (optarg, "mcomb") == 0)
203 type_pitch = aubio_pitch_mcomb;
204 else if (strcmp (optarg, "yinfft") == 0)
205 type_pitch = aubio_pitch_yin;
206 else if (strcmp (optarg, "yin") == 0)
207 type_pitch = aubio_pitch_yin;
208 else if (strcmp (optarg, "schmitt") == 0)
209 type_pitch = aubio_pitch_schmitt;
210 else if (strcmp (optarg, "fcomb") == 0)
211 type_pitch = aubio_pitch_fcomb;
213 errmsg ("unknown pitch type.\n");
221 buffer_size = atoi (optarg);
224 overlap_size = atoi (optarg);
226 case '?': /* unknown options */
229 case -1: /* done with options */
231 default: /*something else unexpected */
232 fprintf (stderr, "Error parsing option '%c'\n", next_option);
236 while (next_option != -1);
238 if (input_filename != NULL) {
239 debug ("Input file : %s\n", input_filename);
240 } else if (input_filename != NULL && output_filename != NULL) {
241 debug ("Input file : %s\n", input_filename);
242 debug ("Output file : %s\n", output_filename);
245 debug ("Jack input output\n");
249 ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
258 examples_common_init (int argc, char **argv)
262 aubio_sndfile_t *onsetfile = NULL;
263 /* parse command line arguments */
264 parse_args (argc, argv);
266 woodblock = new_fvec (buffer_size, 1);
267 if (output_filename || usejack) {
268 /* dummy assignement to keep egcs happy */
269 isonset = (onsetfile = new_aubio_sndfile_ro (onset_filename)) ||
270 (onsetfile = new_aubio_sndfile_ro ("sounds/woodblock.aiff")) ||
271 (onsetfile = new_aubio_sndfile_ro ("../sounds/woodblock.aiff"));
272 if (onsetfile == NULL) {
273 outmsg ("Could not find woodblock.aiff\n");
278 /* read the output sound once */
279 aubio_sndfile_read (onsetfile, overlap_size, woodblock);
283 debug ("Opening files ...\n");
284 file = new_aubio_sndfile_ro (input_filename);
286 outmsg ("Could not open input file %s.\n", input_filename);
290 aubio_sndfile_info (file);
291 channels = aubio_sndfile_channels (file);
292 samplerate = aubio_sndfile_samplerate (file);
293 if (output_filename != NULL)
294 fileout = new_aubio_sndfile_wo (file, output_filename);
298 aubio_lash_client = lash_init (lash_args, argv[0],
299 LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0));
300 if (!aubio_lash_client) {
301 fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__);
303 /* tell the lash server our client id */
304 if (lash_enabled (aubio_lash_client)) {
305 lash_event_t *event =
306 (lash_event_t *) lash_event_new_with_type (LASH_Client_Name);
307 lash_event_set_string (event, "aubio");
308 lash_send_event (aubio_lash_client, event);
309 pthread_create (&lash_thread, NULL, lash_thread_main, NULL);
312 #endif /* HAVE_LASH */
314 ibuf = new_fvec (overlap_size, channels);
315 obuf = new_fvec (overlap_size, channels);
316 fftgrain = new_cvec (buffer_size, channels);
319 pitchdet = new_aubio_pitchdetection (buffer_size * 4,
320 overlap_size, channels, samplerate, type_pitch, mode_pitch);
321 aubio_pitchdetection_set_yinthresh (pitchdet, 0.7);
324 note_buffer = new_fvec (median, 1);
325 note_buffer2 = new_fvec (median, 1);
329 pv = new_aubio_pvoc (buffer_size, overlap_size, channels);
331 parms = new_aubio_peakpicker (threshold);
332 o = new_aubio_onsetdetection (type_onset, buffer_size, channels);
333 onset = new_fvec (1, channels);
335 o2 = new_aubio_onsetdetection (type_onset2, buffer_size, channels);
336 onset2 = new_fvec (1, channels);
343 examples_common_del (void)
346 send_noteon (curnote, 0);
347 del_aubio_pitchdetection (pitchdet);
349 del_fvec (note_buffer);
350 del_fvec (note_buffer2);
354 del_aubio_onsetdetection (o2);
357 del_aubio_onsetdetection (o);
358 del_aubio_peakpicker (parms);
364 del_fvec (woodblock);
369 aubio_jack_t *jack_setup;
373 examples_common_process (aubio_process_func_t process_func,
374 aubio_print_func_t print)
379 debug ("Jack init ...\n");
380 jack_setup = new_aubio_jack (channels, channels,
381 0, 1, (aubio_process_func_t) process_func);
382 debug ("Jack activation ...\n");
383 aubio_jack_activate (jack_setup);
384 debug ("Processing (Ctrl+C to quit) ...\n");
386 aubio_jack_close (jack_setup);
389 outmsg ("Compiled without jack output, exiting.\n");
394 debug ("Processing ...\n");
398 while ((signed) overlap_size == aubio_sndfile_read (file, overlap_size,
401 process_func (ibuf->data, obuf->data, overlap_size);
403 if (output_filename != NULL) {
404 aubio_sndfile_write (fileout, overlap_size, obuf);
409 debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
411 flush_process (process_func, print);
412 del_aubio_sndfile (file);
414 if (output_filename != NULL)
415 del_aubio_sndfile (fileout);
421 flush_process (aubio_process_func_t process_func, aubio_print_func_t print)
424 for (i = 0; i < channels; i++) {
425 for (j = 0; j < obuf->length; j++) {
426 fvec_write_sample (obuf, 0., i, j);
429 for (i = 0; (signed) i < frames_delay; i++) {
430 process_func (ibuf->data, obuf->data, overlap_size);
436 send_noteon (int pitch, int velo)
438 smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
440 jack_midi_event_t ev;
442 ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
446 ev.buffer[1] = mpitch;
448 ev.buffer[0] = 0x80; /* note off */
450 ev.buffer[0] = 0x90; /* note on */
452 aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
457 outmsg ("%f\n", frames * overlap_size / (float) samplerate);
459 outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate);
466 note_append (fvec_t * note_buffer, smpl_t curnote)
469 for (i = 0; i < note_buffer->length - 1; i++) {
470 note_buffer->data[0][i] = note_buffer->data[0][i + 1];
472 note_buffer->data[0][note_buffer->length - 1] = curnote;
477 get_note (fvec_t * note_buffer, fvec_t * note_buffer2)
480 for (i = 0; i < note_buffer->length; i++) {
481 note_buffer2->data[0][i] = note_buffer->data[0][i];
483 return vec_median (note_buffer2);
489 lash_thread_main (void *data __attribute__ ((unused)))
491 printf ("LASH thread running\n");
493 while (!lash_main ())
496 printf ("LASH thread finished\n");
503 lash_event_t *lash_event;
504 lash_config_t *lash_config;
506 while ((lash_event = lash_get_event (aubio_lash_client))) {
507 switch (lash_event_get_type (lash_event)) {
509 lash_event_destroy (lash_event);
512 case LASH_Restore_Data_Set:
513 lash_send_event (aubio_lash_client, lash_event);
515 case LASH_Save_Data_Set:
517 lash_send_event (aubio_lash_client, lash_event);
519 case LASH_Server_Lost:
522 printf ("%s: received unknown LASH event of type %d",
523 __FUNCTION__, lash_event_get_type (lash_event));
524 lash_event_destroy (lash_event);
529 while ((lash_config = lash_get_config (aubio_lash_client))) {
530 restore_data (lash_config);
531 lash_config_destroy (lash_config);
540 lash_config_t *lash_config;
542 lash_config = lash_config_new_with_key ("threshold");
543 lash_config_set_value_double (lash_config, threshold);
544 lash_send_config (aubio_lash_client, lash_config);
549 restore_data (lash_config_t * lash_config)
551 const char *lash_key;
553 lash_key = lash_config_get_key (lash_config);
555 if (strcmp (lash_key, "threshold") == 0) {
556 threshold = lash_config_get_value_double (lash_config);
562 #endif /* HAVE_LASH */