source: src/notes/notes.c @ 4b9443c4

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

src/notes/notes.c: complete implementation, now equivalent to examples/aubionotes.c

  • Property mode set to 100644
File size: 5.2 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
27struct _aubio_notes_t {
28
29  uint_t onset_buf_size;
30  uint_t pitch_buf_size;
31  uint_t hop_size;
32
33  uint_t samplerate;
34
35  uint_t median;
36  fvec_t *note_buffer;
37  fvec_t *note_buffer2;
38
39  aubio_pitch_t *pitch;
40  fvec_t *pitch_output;
41  smpl_t pitch_tolerance;
42
43  aubio_onset_t *onset;
44  fvec_t *onset_output;
45  smpl_t onset_threshold;
46
47  smpl_t curnote;
48  smpl_t newnote;
49
50  smpl_t silence_threshold;
51
52  uint_t isready;
53};
54
55aubio_notes_t * new_aubio_notes (const char_t * notes_method,
56    uint_t buf_size, uint_t hop_size, uint_t samplerate) {
57  aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
58
59  const char_t * onset_method = "default";
60  const char_t * pitch_method = "default";
61
62  o->onset_buf_size = buf_size;
63  o->pitch_buf_size = buf_size * 4;
64  o->hop_size = hop_size;
65
66  o->onset_threshold = 0.;
67  o->pitch_tolerance = 0.;
68
69  o->samplerate = samplerate;
70
71  o->median = 6;
72
73  o->isready = 0;
74
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);
78
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);
82
83  if (strcmp(notes_method, "default") != 0) {
84    AUBIO_ERR("unknown notes detection method %s, using default.\n",
85       notes_method);
86    goto fail;
87  }
88  o->note_buffer = new_fvec(o->median);
89  o->note_buffer2 = new_fvec(o->median);
90
91  o->curnote = -1.;
92  o->newnote = 0.;
93
94  o->silence_threshold = -90.;
95
96  return o;
97
98fail:
99  del_aubio_notes(o);
100  return NULL;
101}
102
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.*/
105static void
106note_append (fvec_t * note_buffer, smpl_t curnote)
107{
108  uint_t i = 0;
109  for (i = 0; i < note_buffer->length - 1; i++) {
110    note_buffer->data[i] = note_buffer->data[i + 1];
111  }
112  note_buffer->data[note_buffer->length - 1] = curnote;
113  return;
114}
115
116static uint_t
117aubio_notes_get_latest_note (aubio_notes_t *o)
118{
119  uint_t i;
120  for (i = 0; i < o->note_buffer->length; i++) {
121    o->note_buffer2->data[i] = o->note_buffer->data[i];
122  }
123  return fvec_median (o->note_buffer2);
124}
125
126
127void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes)
128{
129  smpl_t new_pitch, curlevel;
130  fvec_zeros(notes);
131  aubio_onset_do(o->onset, input, o->onset_output);
132
133  aubio_pitch_do (o->pitch, input, o->pitch_output);
134  new_pitch = o->pitch_output->data[0];
135  if(o->median){
136    note_append(o->note_buffer, new_pitch);
137  }
138
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;
145      /* send note off */
146      //send_noteon(o->curnote,0);
147      //notes->data[0] = o->curnote;
148      //notes->data[1] = 0.;
149      notes->data[2] = o->curnote;
150    } else {
151      if (o->median) {
152        o->isready = 1;
153      } else {
154        /* kill old note */
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;
162      }
163    }
164  } else {
165    if (o->median) {
166      if (o->isready > 0)
167        o->isready++;
168      if (o->isready == o->median)
169      {
170        /* kill old note */
171        //send_noteon(curnote,0);
172        notes->data[2] = o->curnote;
173        notes->data[3] = 0;
174        o->newnote = aubio_notes_get_latest_note(o);
175        o->curnote = o->newnote;
176        /* get and send new one */
177        if (o->curnote>45){
178          //send_noteon(curnote,127+(int)floor(curlevel));
179          notes->data[0] = o->curnote;
180          notes->data[1] = 127 + (int) floor(curlevel);
181        }
182      }
183    } // if median
184  }
185}
186
187void 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);
194  AUBIO_FREE(o);
195}
Note: See TracBrowser for help on using the repository browser.