source: examples/utils.c @ 9682d9d

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

examples/aubioonset.c: add minioi option, in millisecond

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