[96fb8ad] | 1 | /* |
---|
| 2 | Copyright (C) 2003 Paul Brossier |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 17 | |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include "aubio_priv.h" |
---|
| 21 | #include "sample.h" |
---|
| 22 | #include "mathutils.h" |
---|
| 23 | #include "biquad.h" |
---|
| 24 | #include "peakpick.h" |
---|
| 25 | |
---|
| 26 | /* peak picking parameters, default values in brackets |
---|
| 27 | * |
---|
| 28 | * [<----post----|--pre-->] |
---|
| 29 | * .................|............. |
---|
| 30 | * time-> ^now |
---|
| 31 | */ |
---|
| 32 | struct _aubio_pickpeak_t { |
---|
| 33 | /** thresh: offset threshold [0.033 or 0.01] */ |
---|
| 34 | smpl_t threshold; |
---|
| 35 | /** win_post: median filter window length (causal part) [8] */ |
---|
| 36 | uint_t win_post; |
---|
| 37 | /** pre: median filter window (anti-causal part) [post-1] */ |
---|
| 38 | uint_t win_pre; |
---|
[4b1e7e7] | 39 | /** threshfn: name or handle of fn for computing adaptive threshold [median] */ |
---|
[6efdc83] | 40 | aubio_thresholdfn_t thresholdfn; |
---|
[4b1e7e7] | 41 | /** picker: name or handle of fn for picking event times [peakpick] */ |
---|
[96fb8ad] | 42 | aubio_pickerfn_t pickerfn; |
---|
| 43 | |
---|
| 44 | /** biquad lowpass filter */ |
---|
| 45 | aubio_biquad_t * biquad; |
---|
| 46 | /** original onsets */ |
---|
| 47 | fvec_t * onset_keep; |
---|
| 48 | /** modified onsets */ |
---|
| 49 | fvec_t * onset_proc; |
---|
[6efdc83] | 50 | /** peak picked window [3] */ |
---|
[96fb8ad] | 51 | fvec_t * onset_peek; |
---|
| 52 | /** scratch pad for biquad and median */ |
---|
| 53 | fvec_t * scratch; |
---|
| 54 | |
---|
| 55 | /** \bug should be used to calculate filter coefficients */ |
---|
| 56 | /* cutoff: low-pass filter cutoff [0.34, 1] */ |
---|
| 57 | /* smpl_t cutoff; */ |
---|
| 58 | |
---|
| 59 | /* not used anymore */ |
---|
| 60 | /* time precision [512/44100 winlength/samplerate, fs/buffer_size */ |
---|
| 61 | /* smpl_t tau; */ |
---|
| 62 | /* alpha: normalisation exponent [9] */ |
---|
| 63 | /* smpl_t alpha; */ |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | /** modified version for real time, moving mean adaptive threshold this method |
---|
| 68 | * is slightly more permissive than the offline one, and yelds to an increase |
---|
| 69 | * of false positives. best */ |
---|
| 70 | uint_t aubio_peakpick_pimrt(fvec_t * onset, aubio_pickpeak_t * p) { |
---|
| 71 | fvec_t * onset_keep = (fvec_t *)p->onset_keep; |
---|
| 72 | fvec_t * onset_proc = (fvec_t *)p->onset_proc; |
---|
| 73 | fvec_t * onset_peek = (fvec_t *)p->onset_peek; |
---|
| 74 | fvec_t * scratch = (fvec_t *)p->scratch; |
---|
| 75 | smpl_t mean = 0., median = 0.; |
---|
| 76 | uint_t length = p->win_post + p->win_pre + 1; |
---|
| 77 | uint_t i = 0, j; |
---|
| 78 | |
---|
| 79 | /* store onset in onset_keep */ |
---|
| 80 | /* shift all elements but last, then write last */ |
---|
| 81 | /* for (i=0;i<channels;i++) { */ |
---|
| 82 | for (j=0;j<length-1;j++) { |
---|
| 83 | onset_keep->data[i][j] = onset_keep->data[i][j+1]; |
---|
| 84 | onset_proc->data[i][j] = onset_keep->data[i][j]; |
---|
| 85 | } |
---|
| 86 | onset_keep->data[i][length-1] = onset->data[i][0]; |
---|
| 87 | onset_proc->data[i][length-1] = onset->data[i][0]; |
---|
| 88 | /* } */ |
---|
| 89 | |
---|
| 90 | /* filter onset_proc */ |
---|
| 91 | /** \bug filtfilt calculated post+pre times, should be only once !? */ |
---|
| 92 | aubio_biquad_do_filtfilt(p->biquad,onset_proc,scratch); |
---|
| 93 | |
---|
| 94 | /* calculate mean and median for onset_proc */ |
---|
| 95 | /* for (i=0;i<onset_proc->channels;i++) { */ |
---|
| 96 | mean = vec_mean(onset_proc); |
---|
| 97 | /* copy to scratch */ |
---|
| 98 | for (j = 0; j < length; j++) |
---|
| 99 | scratch->data[i][j] = onset_proc->data[i][j]; |
---|
[6efdc83] | 100 | median = p->thresholdfn(scratch); |
---|
[96fb8ad] | 101 | /* } */ |
---|
| 102 | |
---|
| 103 | /* for (i=0;i<onset->channels;i++) { */ |
---|
| 104 | /* shift peek array */ |
---|
| 105 | for (j=0;j<3-1;j++) |
---|
| 106 | onset_peek->data[i][j] = onset_peek->data[i][j+1]; |
---|
| 107 | /* calculate new peek value */ |
---|
| 108 | onset_peek->data[i][2] = |
---|
| 109 | onset_proc->data[i][p->win_post] - median - mean * p->threshold; |
---|
| 110 | /* } */ |
---|
| 111 | //AUBIO_DBG("%f\n", onset_peek->data[0][2]); |
---|
| 112 | return (p->pickerfn)(onset_peek,1); |
---|
| 113 | } |
---|
| 114 | |
---|
[f2adb86] | 115 | /** this method returns the current value in the pick peaking buffer |
---|
| 116 | * after smoothing |
---|
| 117 | */ |
---|
| 118 | smpl_t aubio_peakpick_pimrt_getval(aubio_pickpeak_t * p) |
---|
| 119 | { |
---|
| 120 | uint_t i = 0; |
---|
| 121 | return p->onset_peek->data[i][1]; |
---|
| 122 | } |
---|
| 123 | |
---|
[96fb8ad] | 124 | /** function added by Miguel Ramirez to return the onset detection amplitude in peakval */ |
---|
[32669d9] | 125 | uint_t aubio_peakpick_pimrt_wt(fvec_t * onset, aubio_pickpeak_t * p, smpl_t* peakval) |
---|
[96fb8ad] | 126 | { |
---|
[f2adb86] | 127 | uint_t isonset = 0; |
---|
[b7eb9a5] | 128 | isonset = aubio_peakpick_pimrt(onset,p); |
---|
[96fb8ad] | 129 | |
---|
[b78805a] | 130 | //if ( isonset && peakval != NULL ) |
---|
| 131 | if ( peakval != NULL ) |
---|
[f2adb86] | 132 | *peakval = aubio_peakpick_pimrt_getval(p); |
---|
[96fb8ad] | 133 | |
---|
| 134 | return isonset; |
---|
| 135 | } |
---|
| 136 | |
---|
[6efdc83] | 137 | void aubio_peakpicker_set_threshold(aubio_pickpeak_t * p, smpl_t threshold) { |
---|
| 138 | p->threshold = threshold; |
---|
| 139 | return; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | smpl_t aubio_peakpicker_get_threshold(aubio_pickpeak_t * p) { |
---|
| 143 | return p->threshold; |
---|
| 144 | } |
---|
[96fb8ad] | 145 | |
---|
[6efdc83] | 146 | void aubio_peakpicker_set_thresholdfn(aubio_pickpeak_t * p, aubio_thresholdfn_t thresholdfn) { |
---|
| 147 | p->thresholdfn = thresholdfn; |
---|
| 148 | return; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | aubio_thresholdfn_t aubio_peakpicker_get_thresholdfn(aubio_pickpeak_t * p) { |
---|
| 152 | return (aubio_thresholdfn_t) (p->thresholdfn); |
---|
| 153 | } |
---|
[96fb8ad] | 154 | |
---|
| 155 | aubio_pickpeak_t * new_aubio_peakpicker(smpl_t threshold) { |
---|
| 156 | aubio_pickpeak_t * t = AUBIO_NEW(aubio_pickpeak_t); |
---|
| 157 | t->threshold = 0.1; /* 0.0668; 0.33; 0.082; 0.033; */ |
---|
| 158 | if (threshold > 0. && threshold < 10.) |
---|
| 159 | t->threshold = threshold; |
---|
| 160 | t->win_post = 5; |
---|
| 161 | t->win_pre = 1; |
---|
| 162 | |
---|
[6efdc83] | 163 | t->thresholdfn = (aubio_thresholdfn_t)(vec_median); /* (vec_mean); */ |
---|
[96fb8ad] | 164 | t->pickerfn = (aubio_pickerfn_t)(vec_peakpick); |
---|
| 165 | |
---|
| 166 | t->scratch = new_fvec(t->win_post+t->win_pre+1,1); |
---|
| 167 | t->onset_keep = new_fvec(t->win_post+t->win_pre+1,1); |
---|
| 168 | t->onset_proc = new_fvec(t->win_post+t->win_pre+1,1); |
---|
| 169 | t->onset_peek = new_fvec(3,1); |
---|
| 170 | |
---|
| 171 | /* cutoff: low-pass filter cutoff [0.34, 1] */ |
---|
| 172 | /* t->cutoff=0.34; */ |
---|
| 173 | t->biquad = new_aubio_biquad(0.1600,0.3200,0.1600,-0.5949,0.2348); |
---|
| 174 | return t; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void del_aubio_peakpicker(aubio_pickpeak_t * p) { |
---|
[72b649d] | 178 | del_aubio_biquad(p->biquad); |
---|
[96fb8ad] | 179 | del_fvec(p->onset_keep); |
---|
| 180 | del_fvec(p->onset_proc); |
---|
| 181 | del_fvec(p->onset_peek); |
---|
| 182 | del_fvec(p->scratch); |
---|
[72b649d] | 183 | AUBIO_FREE(p); |
---|
[96fb8ad] | 184 | } |
---|