src/onset/onset.c: add get_last_onset
[aubio.git] / src / onset / onset.c
1 /*
2   Copyright (C) 2006-2009 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 "cvec.h"
24 #include "spectral/specdesc.h"
25 #include "spectral/phasevoc.h"
26 #include "onset/peakpicker.h"
27 #include "mathutils.h"
28 #include "onset/onset.h"
29
30 /** structure to store object state */
31 struct _aubio_onset_t {
32   aubio_pvoc_t * pv;            /**< phase vocoder */
33   aubio_specdesc_t * od;        /**< onset description function */
34   aubio_peakpicker_t * pp;      /**< peak picker */
35   cvec_t * fftgrain;            /**< phase vocoder output */
36   fvec_t * of;                  /**< onset detection function */
37   smpl_t threshold;             /**< onset peak picking threshold */
38   smpl_t silence;               /**< silence threhsold */
39   uint_t minioi;                /**< minimum inter onset interval */
40   uint_t delay;                 /**< constant delay, in samples, removed from detected onset times */
41   fvec_t * wasonset;            /**< number of blocks since last onset */
42   uint_t samplerate;            /**< sampling rate of the input signal */
43   uint_t hop_size;              /**< number of samples between two runs */
44
45   uint_t total_frames;          /**< total number of frames processed since the beginning */
46   uint_t last_onset;            /**< last detected onset location, in frames */
47 };
48
49 /* execute onset detection function on iput buffer */
50 void aubio_onset_do (aubio_onset_t *o, fvec_t * input, fvec_t * onset)
51 {
52   smpl_t isonset = 0;
53   smpl_t wasonset = 0;
54   aubio_pvoc_do (o->pv,input, o->fftgrain);
55   aubio_specdesc_do (o->od,o->fftgrain, o->of);
56   aubio_peakpicker_do(o->pp, o->of, onset);
57   isonset = onset->data[0];
58   wasonset = o->wasonset->data[0];
59   if (isonset > 0.) {
60     if (aubio_silence_detection(input, o->silence)==1) {
61       isonset  = 0;
62       wasonset++;
63     } else {
64       if (wasonset > o->minioi) {
65         wasonset = 0;
66         o->last_onset = o->total_frames + isonset * o->hop_size;
67       } else {
68         isonset  = 0;
69         wasonset++;
70       }
71     }
72   } else {
73     if (wasonset == -1 && aubio_silence_detection(input, o->silence) == 0) {
74       //AUBIO_MSG("beginning of file is not silent, marking as onset\n",
75       //  wasonset, aubio_silence_detection(input, o->silence));
76       isonset = o->delay / o->hop_size;
77       o->last_onset = o->delay;
78       wasonset = 0;
79     }
80     wasonset++;
81   }
82   o->wasonset->data[0] = wasonset;
83   //onset->data[0] = isonset * o->hop_size - o->delay;
84   onset->data[0] = isonset;
85   // also keep a copy of the offset for use in get_last_onset
86   o->total_frames += o->hop_size;
87   return;
88 }
89
90 smpl_t aubio_onset_get_last_onset (aubio_onset_t *o)
91 {
92   return o->last_onset - o->delay;
93 }
94
95 smpl_t aubio_onset_get_last_onset_s (aubio_onset_t *o)
96 {
97   return aubio_onset_get_last_onset (o) / (smpl_t) (o->samplerate);
98 }
99
100 smpl_t aubio_onset_get_last_onset_ms (aubio_onset_t *o)
101 {
102   return aubio_onset_get_last_onset_s (o) / 1000.;
103 }
104
105 smpl_t aubio_onset_get_descriptor(aubio_onset_t * o) {
106   return o->of->data[0];
107 }
108
109 smpl_t aubio_onset_get_thresholded_descriptor(aubio_onset_t * o) {
110   fvec_t * thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
111   return thresholded->data[0];
112 }
113
114 uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
115   o->silence = silence;
116   return AUBIO_OK;
117 }
118
119 uint_t aubio_onset_set_threshold(aubio_onset_t * o, smpl_t threshold) {
120   o->threshold = threshold;
121   aubio_peakpicker_set_threshold(o->pp, o->threshold);
122   return AUBIO_OK;
123 }
124
125 uint_t aubio_onset_set_minioi(aubio_onset_t * o, uint_t minioi) {
126   o->minioi = FLOOR(minioi / 1000. * o->samplerate / o->hop_size);
127   return AUBIO_OK;
128 }
129
130 uint_t aubio_onset_get_minioi(aubio_onset_t * o) {
131   return o->minioi;
132 }
133
134 uint_t aubio_onset_set_delay(aubio_onset_t * o, uint_t delay) {
135   o->delay = delay;
136   return AUBIO_OK;
137 }
138
139 uint_t aubio_onset_get_delay(aubio_onset_t * o) {
140   return o->delay;
141 }
142
143 uint_t aubio_onset_set_delay_s(aubio_onset_t * o, smpl_t delay) {
144   return aubio_onset_set_delay (o, delay * o->samplerate);
145 }
146
147 smpl_t aubio_onset_get_delay_s(aubio_onset_t * o) {
148   return aubio_onset_get_delay (o) / (smpl_t) o->samplerate;
149 }
150
151 uint_t aubio_onset_set_delay_ms(aubio_onset_t * o, smpl_t delay) {
152   return aubio_onset_set_delay_s (o, delay / 1000.);
153 }
154
155 smpl_t aubio_onset_get_delay_ms(aubio_onset_t * o) {
156   return aubio_onset_get_delay_s (o) * 1000.;
157 }
158
159 /* Allocate memory for an onset detection */
160 aubio_onset_t * new_aubio_onset (char_t * onset_mode, 
161     uint_t buf_size, uint_t hop_size, uint_t samplerate)
162 {
163   aubio_onset_t * o = AUBIO_NEW(aubio_onset_t);
164   /** set some default parameter */
165   o->last_onset = 0;
166   o->threshold = 0.3;
167   o->delay     = 4.3 * hop_size;
168   o->minioi    = 5;
169   o->silence   = -70;
170   o->wasonset  = new_fvec(1);
171   o->wasonset->data[0] = -1.;
172   o->total_frames = 0;
173   o->samplerate = samplerate;
174   o->hop_size = hop_size;
175   o->pv = new_aubio_pvoc(buf_size, hop_size);
176   o->pp = new_aubio_peakpicker();
177   aubio_peakpicker_set_threshold (o->pp, o->threshold);
178   o->od = new_aubio_specdesc(onset_mode,buf_size);
179   o->fftgrain = new_cvec(buf_size);
180   o->of = new_fvec(1);
181   /*if (usedoubled)    {
182     o2 = new_aubio_specdesc(onset_type2,buffer_size);
183     onset2 = new_fvec(1);
184   }*/
185   return o;
186 }
187
188 void del_aubio_onset (aubio_onset_t *o)
189 {
190   del_aubio_specdesc(o->od);
191   del_aubio_peakpicker(o->pp);
192   del_aubio_pvoc(o->pv);
193   del_fvec(o->of);
194   del_fvec(o->wasonset);
195   del_cvec(o->fftgrain);
196   AUBIO_FREE(o);
197 }