source: src/notes/notes.c @ 17b08e6

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchsampleryinfft+
Last change on this file since 17b08e6 was 17b08e6, checked in by Paul Brossier <piem@piem.org>, 4 years ago

src/notes/notes.h: add aubio_notes_{get,set}_minioi_ms

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