source: src/notes/notes.c @ b236757

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

src/notes/notes.c: bail out if pitch creation failed (see #188)

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