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