2 Copyright (C) 2014 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 #include "aubio_priv.h"
23 #include "pitch/pitch.h"
24 #include "onset/onset.h"
25 #include "notes/notes.h"
27 struct _aubio_notes_t {
29 uint_t onset_buf_size;
30 uint_t pitch_buf_size;
41 smpl_t pitch_tolerance;
45 smpl_t onset_threshold;
50 smpl_t silence_threshold;
55 aubio_notes_t * new_aubio_notes (const char_t * method,
56 uint_t buf_size, uint_t hop_size, uint_t samplerate) {
57 aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
59 const char_t * onset_method = "default";
60 const char_t * pitch_method = "default";
62 o->onset_buf_size = buf_size;
63 o->pitch_buf_size = buf_size * 4;
64 o->hop_size = hop_size;
66 o->onset_threshold = 0.;
67 o->pitch_tolerance = 0.;
69 o->samplerate = samplerate;
75 o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate);
76 if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold);
77 o->onset_output = new_fvec (1);
79 o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate);
80 if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance);
81 o->pitch_output = new_fvec (1);
83 if (strcmp(method, "default") != 0) {
84 AUBIO_ERR("unknown notes detection method %s, using default.\n",
88 o->note_buffer = new_fvec(o->median);
89 o->note_buffer2 = new_fvec(o->median);
94 o->silence_threshold = -90.;
103 /** append new note candidate to the note_buffer and return filtered value. we
104 * need to copy the input array as fvec_median destroy its input data.*/
106 note_append (fvec_t * note_buffer, smpl_t curnote)
109 for (i = 0; i < note_buffer->length - 1; i++) {
110 note_buffer->data[i] = note_buffer->data[i + 1];
112 note_buffer->data[note_buffer->length - 1] = curnote;
117 aubio_notes_get_latest_note (aubio_notes_t *o)
120 for (i = 0; i < o->note_buffer->length; i++) {
121 o->note_buffer2->data[i] = o->note_buffer->data[i];
123 return fvec_median (o->note_buffer2);
127 void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes)
129 smpl_t new_pitch, curlevel;
131 aubio_onset_do(o->onset, input, o->onset_output);
133 aubio_pitch_do (o->pitch, input, o->pitch_output);
134 new_pitch = o->pitch_output->data[0];
136 note_append(o->note_buffer, new_pitch);
139 /* curlevel is negatif or 1 if silence */
140 curlevel = aubio_level_detection(input, o->silence_threshold);
141 if (o->onset_output->data[0] != 0) {
142 /* test for silence */
143 if (curlevel == 1.) {
144 if (o->median) o->isready = 0;
146 //send_noteon(o->curnote,0);
147 //notes->data[0] = o->curnote;
148 //notes->data[1] = 0.;
149 notes->data[2] = o->curnote;
155 //send_noteon(o->curnote,0, o->samplerate);
156 notes->data[2] = o->curnote;
157 /* get and send new one */
158 //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate);
159 notes->data[0] = new_pitch;
160 notes->data[1] = 127 + (int)floor(curlevel);
161 o->curnote = new_pitch;
168 if (o->isready == o->median)
171 //send_noteon(curnote,0);
172 notes->data[2] = o->curnote;
174 o->newnote = aubio_notes_get_latest_note(o);
175 o->curnote = o->newnote;
176 /* get and send new one */
178 //send_noteon(curnote,127+(int)floor(curlevel));
179 notes->data[0] = o->curnote;
180 notes->data[1] = 127 + (int) floor(curlevel);
187 void del_aubio_notes (aubio_notes_t *o) {
188 if (o->note_buffer) del_fvec(o->note_buffer);
189 if (o->note_buffer2) del_fvec(o->note_buffer2);
190 if (o->pitch_output) del_fvec(o->pitch_output);
191 if (o->pitch) del_aubio_pitch(o->pitch);
192 if (o->onset_output) del_fvec(o->onset_output);
193 if (o->onset) del_aubio_onset(o->onset);