[96fb8ad] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[e6a78ea] | 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. |
---|
[96fb8ad] | 10 | |
---|
[e6a78ea] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
[96fb8ad] | 23 | #include "mathutils.h" |
---|
[b01bd4a] | 24 | #include "lvec.h" |
---|
| 25 | #include "temporal/filter.h" |
---|
[32d6958] | 26 | #include "temporal/biquad.h" |
---|
| 27 | #include "onset/peakpick.h" |
---|
[96fb8ad] | 28 | |
---|
| 29 | /* peak picking parameters, default values in brackets |
---|
| 30 | * |
---|
| 31 | * [<----post----|--pre-->] |
---|
| 32 | * .................|............. |
---|
| 33 | * time-> ^now |
---|
| 34 | */ |
---|
[8766cb6] | 35 | struct _aubio_peakpicker_t { |
---|
[96fb8ad] | 36 | /** thresh: offset threshold [0.033 or 0.01] */ |
---|
| 37 | smpl_t threshold; |
---|
| 38 | /** win_post: median filter window length (causal part) [8] */ |
---|
| 39 | uint_t win_post; |
---|
| 40 | /** pre: median filter window (anti-causal part) [post-1] */ |
---|
| 41 | uint_t win_pre; |
---|
[4b1e7e7] | 42 | /** threshfn: name or handle of fn for computing adaptive threshold [median] */ |
---|
[6efdc83] | 43 | aubio_thresholdfn_t thresholdfn; |
---|
[4b1e7e7] | 44 | /** picker: name or handle of fn for picking event times [peakpick] */ |
---|
[96fb8ad] | 45 | aubio_pickerfn_t pickerfn; |
---|
| 46 | |
---|
| 47 | /** biquad lowpass filter */ |
---|
[b01bd4a] | 48 | aubio_filter_t * biquad; |
---|
[96fb8ad] | 49 | /** original onsets */ |
---|
| 50 | fvec_t * onset_keep; |
---|
| 51 | /** modified onsets */ |
---|
| 52 | fvec_t * onset_proc; |
---|
[6efdc83] | 53 | /** peak picked window [3] */ |
---|
[96fb8ad] | 54 | fvec_t * onset_peek; |
---|
| 55 | /** scratch pad for biquad and median */ |
---|
| 56 | fvec_t * scratch; |
---|
| 57 | |
---|
[56ef7e1] | 58 | /** number of channels to analyse */ |
---|
| 59 | uint_t channels; |
---|
| 60 | |
---|
[96fb8ad] | 61 | /** \bug should be used to calculate filter coefficients */ |
---|
| 62 | /* cutoff: low-pass filter cutoff [0.34, 1] */ |
---|
| 63 | /* smpl_t cutoff; */ |
---|
| 64 | |
---|
| 65 | /* not used anymore */ |
---|
| 66 | /* time precision [512/44100 winlength/samplerate, fs/buffer_size */ |
---|
| 67 | /* smpl_t tau; */ |
---|
| 68 | /* alpha: normalisation exponent [9] */ |
---|
| 69 | /* smpl_t alpha; */ |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** modified version for real time, moving mean adaptive threshold this method |
---|
| 74 | * is slightly more permissive than the offline one, and yelds to an increase |
---|
| 75 | * of false positives. best */ |
---|
[56ef7e1] | 76 | void |
---|
| 77 | aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out) |
---|
| 78 | { |
---|
| 79 | fvec_t *onset_keep = p->onset_keep; |
---|
| 80 | fvec_t *onset_proc = p->onset_proc; |
---|
| 81 | fvec_t *onset_peek = p->onset_peek; |
---|
| 82 | fvec_t *scratch = p->scratch; |
---|
| 83 | smpl_t mean = 0., median = 0.; |
---|
| 84 | uint_t length = p->win_post + p->win_pre + 1; |
---|
| 85 | uint_t i, j = 0; |
---|
| 86 | |
---|
| 87 | for (i = 0; i < p->channels; i++) { |
---|
| 88 | /* store onset in onset_keep */ |
---|
| 89 | /* shift all elements but last, then write last */ |
---|
| 90 | for (j = 0; j < length - 1; j++) { |
---|
| 91 | onset_keep->data[i][j] = onset_keep->data[i][j + 1]; |
---|
| 92 | onset_proc->data[i][j] = onset_keep->data[i][j]; |
---|
| 93 | } |
---|
| 94 | onset_keep->data[i][length - 1] = onset->data[i][0]; |
---|
| 95 | onset_proc->data[i][length - 1] = onset->data[i][0]; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* filter onset_proc */ |
---|
| 99 | /** \bug filtfilt calculated post+pre times, should be only once !? */ |
---|
[b01bd4a] | 100 | aubio_filter_do_filtfilt (p->biquad, onset_proc, scratch); |
---|
[56ef7e1] | 101 | |
---|
| 102 | for (i = 0; i < p->channels; i++) { |
---|
| 103 | /* calculate mean and median for onset_proc */ |
---|
| 104 | mean = fvec_mean_channel (onset_proc, i); |
---|
| 105 | /* copy to scratch */ |
---|
| 106 | for (j = 0; j < length; j++) |
---|
| 107 | scratch->data[i][j] = onset_proc->data[i][j]; |
---|
| 108 | median = p->thresholdfn (scratch, i); |
---|
| 109 | |
---|
| 110 | /* shift peek array */ |
---|
| 111 | for (j = 0; j < 3 - 1; j++) |
---|
| 112 | onset_peek->data[i][j] = onset_peek->data[i][j + 1]; |
---|
| 113 | /* calculate new peek value */ |
---|
| 114 | onset_peek->data[i][2] = |
---|
| 115 | onset_proc->data[i][p->win_post] - median - mean * p->threshold; |
---|
| 116 | out->data[i][0] = (p->pickerfn) (onset_peek, 1); |
---|
| 117 | if (out->data[i][0]) { |
---|
| 118 | out->data[i][0] = fvec_quadint (onset_peek, 1, i); |
---|
| 119 | } |
---|
[0f6f2e6] | 120 | } |
---|
[96fb8ad] | 121 | } |
---|
| 122 | |
---|
[f2adb86] | 123 | /** this method returns the current value in the pick peaking buffer |
---|
| 124 | * after smoothing |
---|
| 125 | */ |
---|
[8766cb6] | 126 | smpl_t aubio_peakpicker_get_thresholded_input(aubio_peakpicker_t * p) |
---|
[f2adb86] | 127 | { |
---|
[422edfb] | 128 | return p->onset_peek->data[0][1]; |
---|
[f2adb86] | 129 | } |
---|
| 130 | |
---|
[f1b925e] | 131 | uint_t aubio_peakpicker_set_threshold(aubio_peakpicker_t * p, smpl_t threshold) { |
---|
[56ef7e1] | 132 | p->threshold = threshold; |
---|
[f1b925e] | 133 | return AUBIO_OK; |
---|
[6efdc83] | 134 | } |
---|
| 135 | |
---|
[8766cb6] | 136 | smpl_t aubio_peakpicker_get_threshold(aubio_peakpicker_t * p) { |
---|
[6efdc83] | 137 | return p->threshold; |
---|
| 138 | } |
---|
[96fb8ad] | 139 | |
---|
[f1b925e] | 140 | uint_t aubio_peakpicker_set_thresholdfn(aubio_peakpicker_t * p, aubio_thresholdfn_t thresholdfn) { |
---|
[6efdc83] | 141 | p->thresholdfn = thresholdfn; |
---|
[f1b925e] | 142 | return AUBIO_OK; |
---|
[6efdc83] | 143 | } |
---|
| 144 | |
---|
[8766cb6] | 145 | aubio_thresholdfn_t aubio_peakpicker_get_thresholdfn(aubio_peakpicker_t * p) { |
---|
[6efdc83] | 146 | return (aubio_thresholdfn_t) (p->thresholdfn); |
---|
| 147 | } |
---|
[96fb8ad] | 148 | |
---|
[56ef7e1] | 149 | aubio_peakpicker_t * new_aubio_peakpicker(uint_t channels) { |
---|
[8766cb6] | 150 | aubio_peakpicker_t * t = AUBIO_NEW(aubio_peakpicker_t); |
---|
[96fb8ad] | 151 | t->threshold = 0.1; /* 0.0668; 0.33; 0.082; 0.033; */ |
---|
| 152 | t->win_post = 5; |
---|
| 153 | t->win_pre = 1; |
---|
[56ef7e1] | 154 | //channels = 1; |
---|
| 155 | t->channels = channels; |
---|
[96fb8ad] | 156 | |
---|
[56ef7e1] | 157 | t->thresholdfn = (aubio_thresholdfn_t)(fvec_median_channel); /* (fvec_mean); */ |
---|
[5c4ec3c] | 158 | t->pickerfn = (aubio_pickerfn_t)(fvec_peakpick); |
---|
[96fb8ad] | 159 | |
---|
[56ef7e1] | 160 | t->scratch = new_fvec(t->win_post+t->win_pre+1, channels); |
---|
| 161 | t->onset_keep = new_fvec(t->win_post+t->win_pre+1, channels); |
---|
| 162 | t->onset_proc = new_fvec(t->win_post+t->win_pre+1, channels); |
---|
| 163 | t->onset_peek = new_fvec(3, channels); |
---|
[96fb8ad] | 164 | |
---|
[b01bd4a] | 165 | /* cutoff: low-pass filter with cutoff reduced frequency at 0.34 |
---|
| 166 | generated with octave butter function: [b,a] = butter(2, 0.34); |
---|
| 167 | */ |
---|
| 168 | t->biquad = new_aubio_filter_biquad (0.15998789, 0.31997577, 0.15998789, |
---|
| 169 | -0.59488894, 0.23484048, channels); |
---|
| 170 | |
---|
[96fb8ad] | 171 | return t; |
---|
| 172 | } |
---|
| 173 | |
---|
[8766cb6] | 174 | void del_aubio_peakpicker(aubio_peakpicker_t * p) { |
---|
[b01bd4a] | 175 | del_aubio_filter(p->biquad); |
---|
[96fb8ad] | 176 | del_fvec(p->onset_keep); |
---|
| 177 | del_fvec(p->onset_proc); |
---|
| 178 | del_fvec(p->onset_peek); |
---|
| 179 | del_fvec(p->scratch); |
---|
[72b649d] | 180 | AUBIO_FREE(p); |
---|
[96fb8ad] | 181 | } |
---|