source: examples/utils.c @ 6627c87

feature/crepe
Last change on this file since 6627c87 was 982629c, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[examples] add quiet mode for performance measurements

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[50bc5f2]1/*
[466dff3]2  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[50bc5f2]4  This file is part of aubio.
[96fb8ad]5
[50bc5f2]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*/
[96fb8ad]20
[9430dfd]21/**
[d4c5de7]22
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
25  program instead.
26
27*/
28
[96fb8ad]29#include "utils.h"
[1b25a70]30#ifdef HAVE_JACK
31#include "jackio.h"
32#endif /* HAVE_JACK */
[96fb8ad]33
[bd2f2ab]34int verbose = 0;
[982629c]35int quiet = 0;
[466dff3]36int usejack = 0;
[1b25a70]37// input / output
38char_t *sink_uri = NULL;
39char_t *source_uri = NULL;
40// general stuff
41uint_t samplerate = 0;
42uint_t buffer_size = 512;
[466dff3]43uint_t hop_size = 256;
[1b25a70]44// onset stuff
45char_t * onset_method = "default";
46smpl_t onset_threshold = 0.0; // will be set if != 0.
[ab7212f]47smpl_t onset_minioi = 0.0; // will be set if != 0.
[1b25a70]48// pitch stuff
[d4c5de7]49char_t * pitch_unit = "default";
[1b25a70]50char_t * pitch_method = "default";
51smpl_t pitch_tolerance = 0.0; // will be set if != 0.
[340cb93]52// time stuff
[df7be43]53uint_t time_format = 0; // for "seconds", 1 for "ms", 2 for "samples"
[1b25a70]54// tempo stuff
[a559796]55char_t * tempo_method = "default";
[1b25a70]56// more general stuff
[ce6186a]57smpl_t silence_threshold = -90.;
[a07fdb4]58smpl_t release_drop = 10.;
[1b25a70]59uint_t mix_input = 0;
[50bc5f2]60
[0a509c6]61uint_t force_overwrite = 0;
62
[1b25a70]63//
64// internal memory stuff
[5a66677]65aubio_source_t *this_source = NULL;
66aubio_sink_t *this_sink = NULL;
[faf2702]67fvec_t *input_buffer;
68fvec_t *output_buffer;
[96fb8ad]69
[2293d6a]70smpl_t miditap_note = 69.;
71smpl_t miditap_velo = 65.;
72
[1b25a70]73/* settings */
[466dff3]74int blocks = 0;
[50bc5f2]75
[1b25a70]76extern void usage (FILE * stream, int exit_code);
77extern int parse_args (int argc, char **argv);
[96fb8ad]78
[466dff3]79#if HAVE_JACK
[f7771c6]80#define MAX_MIDI_EVENTS 128
[8ba682a]81#define MAX_MIDI_EVENT_SIZE 3
[466dff3]82aubio_jack_t *jack_setup;
[8f0db97]83jack_midi_event_t ev;
[8ba682a]84jack_midi_data_t midi_data[MAX_MIDI_EVENTS * MAX_MIDI_EVENT_SIZE];
[f7771c6]85size_t midi_event_count = 0;
[7585822]86#endif /* HAVE_JACK */
[466dff3]87
[d389e23]88void examples_common_init (int argc, char **argv);
89void examples_common_del (void);
90void examples_common_process (aubio_process_func_t process_func,
91    aubio_print_func_t print);
92
93void examples_common_init (int argc, char **argv)
[50bc5f2]94{
[bd2f2ab]95
96  /* parse command line arguments */
[50bc5f2]97  parse_args (argc, argv);
[bd2f2ab]98
[50bc5f2]99  if (!usejack) {
100    debug ("Opening files ...\n");
[466dff3]101    this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size);
[5a66677]102    if (this_source == NULL) {
[4d733b5]103      errmsg ("Error: could not open input file %s\n", source_uri);
[50bc5f2]104      exit (1);
[9af07aa]105    }
[466dff3]106    if (samplerate == 0) {
107      samplerate = aubio_source_get_samplerate(this_source);
108    }
[5a66677]109    if (sink_uri != NULL) {
[0a509c6]110      uint_t sink_exists = (access(sink_uri, F_OK) == 0 );
111      if (!force_overwrite && sink_exists) {
[4d733b5]112        errmsg ("Error: output file %s already exists, use -f to overwrite.\n",
[0a509c6]113            sink_uri);
114        exit (1);
115      }
[5a66677]116      this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
[88fc249]117      if (this_sink == NULL) {
[4d733b5]118        errmsg ("Error: could not create output file %s\n", sink_uri);
[88fc249]119        exit (1);
120      }
[5a66677]121    }
[466dff3]122#ifdef HAVE_JACK
123  } else {
124    debug ("Jack init ...\n");
125    jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1);
126    samplerate = aubio_jack_get_samplerate (jack_setup);
127    source_uri = "jack";
[7585822]128#endif /* HAVE_JACK */
[bd2f2ab]129  }
[faf2702]130  input_buffer = new_fvec (hop_size);
131  output_buffer = new_fvec (hop_size);
[bd2f2ab]132
133}
134
[d389e23]135void examples_common_del (void)
[50bc5f2]136{
[faf2702]137  del_fvec (input_buffer);
138  del_fvec (output_buffer);
[50bc5f2]139  aubio_cleanup ();
[466dff3]140  fflush(stderr);
141  fflush(stdout);
[bd2f2ab]142}
143
[d389e23]144void examples_common_process (aubio_process_func_t process_func,
[50bc5f2]145    aubio_print_func_t print)
146{
[5a66677]147
148  uint_t read = 0;
[50bc5f2]149  if (usejack) {
[ebbf5a0]150
[7585822]151#ifdef HAVE_JACK
[8ba682a]152    ev.size = MAX_MIDI_EVENT_SIZE;
[8f0db97]153    ev.time = 0; // send it now
[50bc5f2]154    debug ("Jack activation ...\n");
[466dff3]155    aubio_jack_activate (jack_setup, process_func);
[50bc5f2]156    debug ("Processing (Ctrl+C to quit) ...\n");
157    pause ();
158    aubio_jack_close (jack_setup);
[7585822]159#else /* HAVE_JACK */
[50bc5f2]160    usage (stderr, 1);
161    outmsg ("Compiled without jack output, exiting.\n");
[7585822]162#endif /* HAVE_JACK */
[bd2f2ab]163
164  } else {
[5cc88f7]165
[466dff3]166    uint_t total_read = 0;
[fe87823]167    blocks = 0;
[bd2f2ab]168
[5a66677]169    do {
[faf2702]170      aubio_source_do (this_source, input_buffer, &read);
171      process_func (input_buffer, output_buffer);
[466dff3]172      // print to console if verbose or no output given
[982629c]173      if ((verbose || sink_uri == NULL) && !quiet) {
[466dff3]174        print();
175      }
[5a66677]176      if (this_sink) {
[faf2702]177        aubio_sink_do (this_sink, output_buffer, hop_size);
[bd2f2ab]178      }
[466dff3]179      blocks++;
180      total_read += read;
181    } while (read == hop_size);
[bd2f2ab]182
[2c31bb6]183    verbmsg ("read %.2fs (%d samples in %d blocks of %d) from %s at %dHz\n",
184        total_read * 1. / samplerate,
[466dff3]185        total_read, blocks, hop_size, source_uri, samplerate);
[e24378a]186
[5a66677]187    del_aubio_source (this_source);
[fef012a]188    if (this_sink)
189      del_aubio_sink   (this_sink);
[bd2f2ab]190
191  }
192}
193
[50bc5f2]194void
[9562f08]195send_noteon (smpl_t pitch, smpl_t velo)
[bd2f2ab]196{
[7585822]197#ifdef HAVE_JACK
[50bc5f2]198  if (usejack) {
[8ba682a]199    ev.buffer = midi_data + midi_event_count++ * MAX_MIDI_EVENT_SIZE;
[f7771c6]200    if (midi_event_count >= MAX_MIDI_EVENTS) {
201      midi_event_count = 0;
202    }
[ebbf5a0]203    ev.buffer[2] = velo;
[9562f08]204    ev.buffer[1] = pitch;
[50bc5f2]205    if (velo == 0) {
[ebbf5a0]206      ev.buffer[0] = 0x80;      /* note off */
[50bc5f2]207    } else {
[ebbf5a0]208      ev.buffer[0] = 0x90;      /* note on */
[50bc5f2]209    }
[ebbf5a0]210    aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
[50bc5f2]211  } else
[bd2f2ab]212#endif
[466dff3]213  if (velo == 0) {
[340cb93]214    print_time (blocks * hop_size);
215    outmsg ("\n");
[466dff3]216  } else {
[9562f08]217    outmsg ("%f\t", pitch);
[340cb93]218    print_time (blocks * hop_size);
219    outmsg ("\t");
[50bc5f2]220  }
[bd2f2ab]221}
222
[340cb93]223void print_time (uint_t time_in_samples) {
224  /* output times in selected format */
[df7be43]225  if (time_format == 2) {
[340cb93]226    outmsg ("%d", time_in_samples);
[df7be43]227  } else if (time_format == 1) {
[340cb93]228    outmsg ("%f", 1000. * time_in_samples / (float) samplerate);
229  } else {
230    outmsg ("%f", time_in_samples / (float) samplerate);
231  }
232}
Note: See TracBrowser for help on using the repository browser.