source: examples/utils.c @ c82a034

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since c82a034 was 36c302a, checked in by Paul Brossier <piem@piem.org>, 10 years ago

examples/: use outmsg to print notes (fixes #8)

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