2 Copyright (C) 2003 Paul Brossier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #define AUBIO_UNSTABLE 1 // for fvec_median_channel
29 aubio_pitch_t *pitchdet;
31 fvec_t *note_buffer = NULL;
32 fvec_t *note_buffer2 = NULL;
37 unsigned int pos = 0; /*frames%dspblocksize*/
39 aubio_pitch_t *pitchdet;
44 /** append new note candidate to the note_buffer and return filtered value. we
45 * need to copy the input array as fvec_median destroy its input data.*/
46 void note_append (fvec_t * note_buffer, smpl_t curnote);
47 uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2);
49 static int aubio_process(smpl_t **input, smpl_t **output, int nframes) {
50 unsigned int i; /*channels*/
51 unsigned int j; /*frames*/
52 for (j=0;j<(unsigned)nframes;j++) {
54 for (i=0;i<channels;i++) {
55 /* write input to datanew */
56 fvec_write_sample(ibuf, input[i][j], i, pos);
57 /* put synthnew in output */
58 output[i][j] = fvec_read_sample(obuf, i, pos);
62 if (pos == overlap_size-1) {
64 aubio_onset_do(o, ibuf, onset);
66 aubio_pitch_do (pitchdet, ibuf, pitch_obuf);
67 pitch = fvec_read_sample(pitch_obuf, 0, 0);
69 note_append(note_buffer, pitch);
72 /* curlevel is negatif or 1 if silence */
73 curlevel = aubio_level_detection(ibuf, silence);
74 if (fvec_read_sample(onset, 0, 0)) {
75 /* test for silence */
77 if (median) isready = 0;
79 send_noteon(curnote,0);
85 send_noteon(curnote,0);
86 /* get and send new one */
87 send_noteon(pitch,127+(int)floor(curlevel));
91 for (pos = 0; pos < overlap_size; pos++){
92 obuf->data[0][pos] = woodblock->data[0][pos];
99 if (isready == median)
102 send_noteon(curnote,0);
103 newnote = get_note(note_buffer, note_buffer2);
105 /* get and send new one */
107 send_noteon(curnote,127+(int)floor(curlevel));
111 for (pos = 0; pos < overlap_size; pos++)
112 obuf->data[0][pos] = 0.;
114 /* end of block loop */
115 pos = -1; /* so it will be zero next j loop */
122 static void process_print (void) {
123 if (verbose) outmsg("%f\n",pitch);
127 note_append (fvec_t * note_buffer, smpl_t curnote)
130 for (i = 0; i < note_buffer->length - 1; i++) {
131 note_buffer->data[0][i] = note_buffer->data[0][i + 1];
133 note_buffer->data[0][note_buffer->length - 1] = curnote;
138 get_note (fvec_t * note_buffer, fvec_t * note_buffer2)
141 for (i = 0; i < note_buffer->length; i++) {
142 note_buffer2->data[0][i] = note_buffer->data[0][i];
144 return fvec_median_channel (note_buffer2, 0);
147 int main(int argc, char **argv) {
148 examples_common_init(argc,argv);
150 o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels,
152 if (threshold != 0.) aubio_onset_set_threshold (o, threshold);
153 onset = new_fvec (1, channels);
155 pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4,
156 overlap_size, channels, samplerate);
157 aubio_pitch_set_tolerance (pitchdet, 0.7);
158 pitch_obuf = new_fvec (1, channels);
160 note_buffer = new_fvec (median, 1);
161 note_buffer2 = new_fvec (median, 1);
164 examples_common_process(aubio_process, process_print);
166 send_noteon (curnote, 0);
167 del_aubio_pitch (pitchdet);
169 del_fvec (note_buffer);
170 del_fvec (note_buffer2);
172 del_fvec (pitch_obuf);
174 examples_common_del();
175 debug("End of program.\n");