2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #define AUBIO_UNSTABLE 1 // for fvec_median_channel
31 aubio_pitch_t *pitchdet;
33 fvec_t *note_buffer = NULL;
34 fvec_t *note_buffer2 = NULL;
39 unsigned int pos = 0; /*frames%dspblocksize*/
41 aubio_pitch_t *pitchdet;
46 /** append new note candidate to the note_buffer and return filtered value. we
47 * need to copy the input array as fvec_median destroy its input data.*/
48 void note_append (fvec_t * note_buffer, smpl_t curnote);
49 uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2);
51 static int aubio_process(smpl_t **input, smpl_t **output, int nframes) {
52 unsigned int i; /*channels*/
53 unsigned int j; /*frames*/
54 for (j=0;j<(unsigned)nframes;j++) {
56 for (i=0;i<channels;i++) {
57 /* write input to datanew */
58 fvec_write_sample(ibuf, input[i][j], i, pos);
59 /* put synthnew in output */
60 output[i][j] = fvec_read_sample(obuf, i, pos);
64 if (pos == overlap_size-1) {
66 aubio_onset_do(o, ibuf, onset);
68 aubio_pitch_do (pitchdet, ibuf, pitch_obuf);
69 pitch = fvec_read_sample(pitch_obuf, 0, 0);
71 note_append(note_buffer, pitch);
74 /* curlevel is negatif or 1 if silence */
75 curlevel = aubio_level_detection(ibuf, silence);
76 if (fvec_read_sample(onset, 0, 0)) {
77 /* test for silence */
79 if (median) isready = 0;
81 send_noteon(curnote,0);
87 send_noteon(curnote,0);
88 /* get and send new one */
89 send_noteon(pitch,127+(int)floor(curlevel));
93 for (pos = 0; pos < overlap_size; pos++){
94 obuf->data[0][pos] = woodblock->data[0][pos];
101 if (isready == median)
104 send_noteon(curnote,0);
105 newnote = get_note(note_buffer, note_buffer2);
107 /* get and send new one */
109 send_noteon(curnote,127+(int)floor(curlevel));
113 for (pos = 0; pos < overlap_size; pos++)
114 obuf->data[0][pos] = 0.;
116 /* end of block loop */
117 pos = -1; /* so it will be zero next j loop */
124 static void process_print (void) {
125 if (verbose) outmsg("%f\n",pitch);
129 note_append (fvec_t * note_buffer, smpl_t curnote)
132 for (i = 0; i < note_buffer->length - 1; i++) {
133 note_buffer->data[0][i] = note_buffer->data[0][i + 1];
135 note_buffer->data[0][note_buffer->length - 1] = curnote;
140 get_note (fvec_t * note_buffer, fvec_t * note_buffer2)
143 for (i = 0; i < note_buffer->length; i++) {
144 note_buffer2->data[0][i] = note_buffer->data[0][i];
146 return fvec_median_channel (note_buffer2, 0);
149 int main(int argc, char **argv) {
150 examples_common_init(argc,argv);
152 o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels,
154 if (threshold != 0.) aubio_onset_set_threshold (o, threshold);
155 onset = new_fvec (1, channels);
157 pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4,
158 overlap_size, channels, samplerate);
159 aubio_pitch_set_tolerance (pitchdet, 0.7);
160 pitch_obuf = new_fvec (1, channels);
162 note_buffer = new_fvec (median, 1);
163 note_buffer2 = new_fvec (median, 1);
166 examples_common_process(aubio_process, process_print);
168 send_noteon (curnote, 0);
169 del_aubio_pitch (pitchdet);
171 del_fvec (note_buffer);
172 del_fvec (note_buffer2);
174 del_fvec (pitch_obuf);
176 examples_common_del();
177 debug("End of program.\n");