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 #define DEFAULT_NOTES_SILENCE -50.
29 struct _aubio_notes_t {
31 uint_t onset_buf_size;
32 uint_t pitch_buf_size;
43 smpl_t pitch_tolerance;
47 smpl_t onset_threshold;
52 smpl_t silence_threshold;
57 aubio_notes_t * new_aubio_notes (const char_t * method,
58 uint_t buf_size, uint_t hop_size, uint_t samplerate) {
59 aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
61 const char_t * onset_method = "default";
62 const char_t * pitch_method = "default";
64 o->onset_buf_size = buf_size;
65 o->pitch_buf_size = buf_size * 4;
66 o->hop_size = hop_size;
68 o->onset_threshold = 0.;
69 o->pitch_tolerance = 0.;
71 o->samplerate = samplerate;
77 o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate);
78 if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold);
79 o->onset_output = new_fvec (1);
81 o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate);
82 if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance);
83 o->pitch_output = new_fvec (1);
85 if (strcmp(method, "default") != 0) {
86 AUBIO_ERR("notes: unknown notes detection method \"%s\"\n", method);
89 o->note_buffer = new_fvec(o->median);
90 o->note_buffer2 = new_fvec(o->median);
95 aubio_notes_set_silence(o, DEFAULT_NOTES_SILENCE);
104 uint_t aubio_notes_set_silence(aubio_notes_t *o, smpl_t silence)
106 uint_t err = AUBIO_OK;
107 if (aubio_pitch_set_silence(o->pitch, silence) != AUBIO_OK) {
110 if (aubio_onset_set_silence(o->onset, silence) != AUBIO_OK) {
116 smpl_t aubio_notes_get_silence(const aubio_notes_t *o)
118 return aubio_pitch_get_silence(o->pitch);
121 /** append new note candidate to the note_buffer and return filtered value. we
122 * need to copy the input array as fvec_median destroy its input data.*/
124 note_append (fvec_t * note_buffer, smpl_t curnote)
127 for (i = 0; i < note_buffer->length - 1; i++) {
128 note_buffer->data[i] = note_buffer->data[i + 1];
130 note_buffer->data[note_buffer->length - 1] = curnote;
135 aubio_notes_get_latest_note (aubio_notes_t *o)
138 for (i = 0; i < o->note_buffer->length; i++) {
139 o->note_buffer2->data[i] = o->note_buffer->data[i];
141 return fvec_median (o->note_buffer2);
145 void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes)
147 smpl_t new_pitch, curlevel;
149 aubio_onset_do(o->onset, input, o->onset_output);
151 aubio_pitch_do (o->pitch, input, o->pitch_output);
152 new_pitch = o->pitch_output->data[0];
154 note_append(o->note_buffer, new_pitch);
157 /* curlevel is negatif or 1 if silence */
158 curlevel = aubio_level_detection(input, o->silence_threshold);
159 if (o->onset_output->data[0] != 0) {
160 /* test for silence */
161 if (curlevel == 1.) {
162 if (o->median) o->isready = 0;
164 //send_noteon(o->curnote,0);
165 //notes->data[0] = o->curnote;
166 //notes->data[1] = 0.;
167 notes->data[2] = o->curnote;
173 //send_noteon(o->curnote,0, o->samplerate);
174 notes->data[2] = o->curnote;
175 /* get and send new one */
176 //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate);
177 notes->data[0] = new_pitch;
178 notes->data[1] = 127 + (int)floor(curlevel);
179 o->curnote = new_pitch;
186 if (o->isready == o->median)
189 //send_noteon(curnote,0);
190 notes->data[2] = o->curnote;
191 o->newnote = aubio_notes_get_latest_note(o);
192 o->curnote = o->newnote;
193 /* get and send new one */
195 //send_noteon(curnote,127+(int)floor(curlevel));
196 notes->data[0] = o->curnote;
197 notes->data[1] = 127 + (int) floor(curlevel);
204 void del_aubio_notes (aubio_notes_t *o) {
205 if (o->note_buffer) del_fvec(o->note_buffer);
206 if (o->note_buffer2) del_fvec(o->note_buffer2);
207 if (o->pitch_output) del_fvec(o->pitch_output);
208 if (o->pitch) del_aubio_pitch(o->pitch);
209 if (o->onset_output) del_fvec(o->onset_output);
210 if (o->onset) del_aubio_onset(o->onset);