source: src/notes/notes.c @ f761f06

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since f761f06 was f761f06, checked in by Paul Brossier <piem@piem.org>, 5 years ago

src/notes/notes.c: emit note-off earlier (see #203)

  • emit note-off when the level drops by a given threshold after onset
  • default drop threshold is set to 10dB
  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2  Copyright (C) 2014 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#include "aubio_priv.h"
22#include "fvec.h"
23#include "pitch/pitch.h"
24#include "onset/onset.h"
25#include "notes/notes.h"
26
27#define AUBIO_DEFAULT_NOTES_SILENCE -70.
28#define AUBIO_DEFAULT_NOTES_LEVEL_DROP 10.
29// increase to 10. for .1  cent precision
30//      or to 100. for .01 cent precision
31#define AUBIO_DEFAULT_CENT_PRECISION 1.
32#define AUBIO_DEFAULT_NOTES_MINIOI_MS 30.
33
34struct _aubio_notes_t {
35
36  uint_t onset_buf_size;
37  uint_t pitch_buf_size;
38  uint_t hop_size;
39
40  uint_t samplerate;
41
42  uint_t median;
43  fvec_t *note_buffer;
44  fvec_t *note_buffer2;
45
46  aubio_pitch_t *pitch;
47  fvec_t *pitch_output;
48  smpl_t pitch_tolerance;
49
50  aubio_onset_t *onset;
51  fvec_t *onset_output;
52  smpl_t onset_threshold;
53
54  smpl_t curnote;
55  smpl_t newnote;
56
57  smpl_t silence_threshold;
58
59  uint_t isready;
60
61  smpl_t last_onset_level;
62  smpl_t level_delta_db;
63};
64
65aubio_notes_t * new_aubio_notes (const char_t * method,
66    uint_t buf_size, uint_t hop_size, uint_t samplerate) {
67  aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
68
69  const char_t * onset_method = "default";
70  const char_t * pitch_method = "default";
71
72  o->onset_buf_size = buf_size;
73  o->pitch_buf_size = buf_size * 4;
74  o->hop_size = hop_size;
75
76  o->onset_threshold = 0.;
77  o->pitch_tolerance = 0.;
78
79  o->samplerate = samplerate;
80
81  o->median = 6;
82
83  o->isready = 0;
84
85  o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate);
86  if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold);
87  o->onset_output = new_fvec (1);
88
89  o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate);
90  if (o->pitch == NULL) goto fail;
91  if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance);
92  aubio_pitch_set_unit (o->pitch, "midi");
93  o->pitch_output = new_fvec (1);
94
95  if (strcmp(method, "default") != 0) {
96    AUBIO_ERR("notes: unknown notes detection method \"%s\"\n", method);
97    goto fail;
98  }
99  o->note_buffer = new_fvec(o->median);
100  o->note_buffer2 = new_fvec(o->median);
101
102  o->curnote = -1.;
103  o->newnote = 0.;
104
105  aubio_notes_set_silence(o, AUBIO_DEFAULT_NOTES_SILENCE);
106  aubio_notes_set_minioi_ms (o, AUBIO_DEFAULT_NOTES_MINIOI_MS);
107
108  o->last_onset_level = AUBIO_DEFAULT_NOTES_SILENCE;
109  o->level_delta_db = AUBIO_DEFAULT_NOTES_LEVEL_DROP;
110
111  return o;
112
113fail:
114  del_aubio_notes(o);
115  return NULL;
116}
117
118uint_t aubio_notes_set_silence(aubio_notes_t *o, smpl_t silence)
119{
120  uint_t err = AUBIO_OK;
121  if (aubio_pitch_set_silence(o->pitch, silence) != AUBIO_OK) {
122    err = AUBIO_FAIL;
123  }
124  if (aubio_onset_set_silence(o->onset, silence) != AUBIO_OK) {
125    err = AUBIO_FAIL;
126  }
127  o->silence_threshold = silence;
128  return err;
129}
130
131smpl_t aubio_notes_get_silence(const aubio_notes_t *o)
132{
133  return aubio_pitch_get_silence(o->pitch);
134}
135
136uint_t aubio_notes_set_minioi_ms (aubio_notes_t *o, smpl_t minioi_ms)
137{
138  uint_t err = AUBIO_OK;
139  if (!o->onset || (aubio_onset_set_minioi_ms(o->onset, minioi_ms) != 0)) {
140    err = AUBIO_FAIL;
141  }
142  return err;
143}
144
145smpl_t aubio_notes_get_minioi_ms(const aubio_notes_t *o)
146{
147  return aubio_onset_get_minioi_ms(o->onset);
148}
149
150/** append new note candidate to the note_buffer and return filtered value. we
151 * need to copy the input array as fvec_median destroy its input data.*/
152static void
153note_append (fvec_t * note_buffer, smpl_t curnote)
154{
155  uint_t i = 0;
156  for (i = 0; i < note_buffer->length - 1; i++) {
157    note_buffer->data[i] = note_buffer->data[i + 1];
158  }
159  //note_buffer->data[note_buffer->length - 1] = ROUND(10.*curnote)/10.;
160  note_buffer->data[note_buffer->length - 1] = ROUND(AUBIO_DEFAULT_CENT_PRECISION*curnote);
161  return;
162}
163
164static smpl_t
165aubio_notes_get_latest_note (aubio_notes_t *o)
166{
167  fvec_copy(o->note_buffer, o->note_buffer2);
168  return fvec_median (o->note_buffer2) / AUBIO_DEFAULT_CENT_PRECISION;
169}
170
171
172void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes)
173{
174  smpl_t new_pitch, curlevel;
175  fvec_zeros(notes);
176  aubio_onset_do(o->onset, input, o->onset_output);
177
178  aubio_pitch_do (o->pitch, input, o->pitch_output);
179  new_pitch = o->pitch_output->data[0];
180  if(o->median){
181    note_append(o->note_buffer, new_pitch);
182  }
183
184  /* curlevel is negatif or 1 if silence */
185  curlevel = aubio_level_detection(input, o->silence_threshold);
186  if (o->onset_output->data[0] != 0) {
187    /* test for silence */
188    if (curlevel == 1.) {
189      if (o->median) o->isready = 0;
190      /* send note off */
191      //send_noteon(o->curnote,0);
192      //notes->data[0] = o->curnote;
193      //notes->data[1] = 0.;
194      //AUBIO_WRN("notes: sending note-off at onset, not enough level\n");
195      notes->data[2] = o->curnote;
196    } else {
197      if (o->median) {
198        o->isready = 1;
199      } else {
200        /* kill old note */
201        //send_noteon(o->curnote,0, o->samplerate);
202        //AUBIO_WRN("notes: sending note-off at onset, new onset detected\n");
203        notes->data[2] = o->curnote;
204        /* get and send new one */
205        //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate);
206        notes->data[0] = new_pitch;
207        notes->data[1] = 127 + (int)floor(curlevel);
208        o->curnote = new_pitch;
209      }
210      o->last_onset_level = curlevel;
211    }
212  } else {
213    if (curlevel < o->last_onset_level - o->level_delta_db)
214    {
215      // send note off
216      //AUBIO_WRN("notes: sending note-off, release detected\n");
217      notes->data[0] = 0;
218      notes->data[1] = 0;
219      notes->data[2] = o->curnote;
220      // reset last_onset_level to silence_threshold
221      o->last_onset_level = o->silence_threshold;
222      o->curnote = 0;
223    }
224    else if (o->median)
225    {
226      if (o->isready > 0)
227        o->isready++;
228      if (o->isready == o->median)
229      {
230        /* kill old note */
231        //send_noteon(curnote,0);
232        if (o->curnote != 0)
233        {
234          //AUBIO_WRN("notes: sending note-off, new note detected\n");
235          notes->data[2] = o->curnote;
236        }
237        o->newnote = aubio_notes_get_latest_note(o);
238        o->curnote = o->newnote;
239        /* get and send new one */
240        if (o->curnote>45){
241          //send_noteon(curnote,127+(int)floor(curlevel));
242          notes->data[0] = o->curnote;
243          notes->data[1] = 127 + (int) floor(curlevel);
244        }
245      }
246    } // if median
247  }
248}
249
250void del_aubio_notes (aubio_notes_t *o) {
251  if (o->note_buffer) del_fvec(o->note_buffer);
252  if (o->note_buffer2) del_fvec(o->note_buffer2);
253  if (o->pitch_output) del_fvec(o->pitch_output);
254  if (o->pitch) del_aubio_pitch(o->pitch);
255  if (o->onset_output) del_fvec(o->onset_output);
256  if (o->onset) del_aubio_onset(o->onset);
257  AUBIO_FREE(o);
258}
Note: See TracBrowser for help on using the repository browser.