source: examples/utils.c @ fc359e0

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

examples/utils.c: allocate note event once

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