source: src/onset/peakpick.c @ 56ef7e1

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

Change peakpicker to match API specs, make quadint per channel

  • src/mathutils.c
    • add per channel mean and median
    • update moving thres and adapt_thres accordingly
    • change quadint unused span argument to a channel argument
  • src/onset/onset.c:
    • make wasonset a vector for multi channel, use new peakpicker
  • src/onset/peakpick.c:
    • update peakpicker do for multi channeling
  • src/pitch/: update use to fvec_quadint
  • src/tempo/beattracking.c: update calls to fvec_quadint
  • src/tempo/tempo.c: update peakpicker usage
  • tests/src/test-peakpick.c: update peakpicker usage
  • Property mode set to 100644
File size: 5.5 KB
Line 
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 "fvec.h"
22#include "mathutils.h"
23#include "temporal/biquad.h"
24#include "onset/peakpick.h"
25
26/* peak picking parameters, default values in brackets
27 *
28 *         [<----post----|--pre-->]
29 *      .................|.............
30 *      time->           ^now
31 */
32struct _aubio_peakpicker_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;                               
39        /** threshfn: name or handle of fn for computing adaptive threshold [median]  */
40        aubio_thresholdfn_t thresholdfn;
41        /** picker: name or handle of fn for picking event times [peakpick] */
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;
50        /** peak picked window [3] */
51        fvec_t * onset_peek;
52        /** scratch pad for biquad and median */
53        fvec_t * scratch;
54
55  /** number of channels to analyse */
56  uint_t channels;
57
58        /** \bug should be used to calculate filter coefficients */
59        /* cutoff: low-pass filter cutoff [0.34, 1] */
60        /* smpl_t cutoff; */
61
62        /* not used anymore */
63        /* time precision [512/44100  winlength/samplerate, fs/buffer_size */
64        /* smpl_t tau; */
65        /* alpha: normalisation exponent [9] */
66        /* smpl_t alpha; */
67};
68
69
70/** modified version for real time, moving mean adaptive threshold this method
71 * is slightly more permissive than the offline one, and yelds to an increase
72 * of false positives. best  */
73void
74aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out)
75{
76  fvec_t *onset_keep = p->onset_keep;
77  fvec_t *onset_proc = p->onset_proc;
78  fvec_t *onset_peek = p->onset_peek;
79  fvec_t *scratch = p->scratch;
80  smpl_t mean = 0., median = 0.;
81  uint_t length = p->win_post + p->win_pre + 1;
82  uint_t i, j = 0;
83
84  for (i = 0; i < p->channels; i++) {
85    /* store onset in onset_keep */
86    /* shift all elements but last, then write last */
87    for (j = 0; j < length - 1; j++) {
88      onset_keep->data[i][j] = onset_keep->data[i][j + 1];
89      onset_proc->data[i][j] = onset_keep->data[i][j];
90    }
91    onset_keep->data[i][length - 1] = onset->data[i][0];
92    onset_proc->data[i][length - 1] = onset->data[i][0];
93  }
94
95  /* filter onset_proc */
96  /** \bug filtfilt calculated post+pre times, should be only once !? */
97  //aubio_biquad_do_filtfilt(p->biquad,onset_proc,scratch);
98
99  for (i = 0; i < p->channels; i++) {
100    /* calculate mean and median for onset_proc */
101    mean = fvec_mean_channel (onset_proc, i);
102    /* copy to scratch */
103    for (j = 0; j < length; j++)
104      scratch->data[i][j] = onset_proc->data[i][j];
105    median = p->thresholdfn (scratch, i);
106
107    /* shift peek array */
108    for (j = 0; j < 3 - 1; j++)
109      onset_peek->data[i][j] = onset_peek->data[i][j + 1];
110    /* calculate new peek value */
111    onset_peek->data[i][2] =
112        onset_proc->data[i][p->win_post] - median - mean * p->threshold;
113    out->data[i][0] = (p->pickerfn) (onset_peek, 1);
114    if (out->data[i][0]) {
115      out->data[i][0] = fvec_quadint (onset_peek, 1, i);
116    }
117  }
118}
119
120/** this method returns the current value in the pick peaking buffer
121 * after smoothing
122 */
123smpl_t aubio_peakpicker_get_thresholded_input(aubio_peakpicker_t * p) 
124{
125        return p->onset_peek->data[0][1];
126}
127
128uint_t aubio_peakpicker_set_threshold(aubio_peakpicker_t * p, smpl_t threshold) {
129    p->threshold = threshold;
130        return AUBIO_OK;
131}
132
133smpl_t aubio_peakpicker_get_threshold(aubio_peakpicker_t * p) {
134        return p->threshold;
135}
136
137uint_t aubio_peakpicker_set_thresholdfn(aubio_peakpicker_t * p, aubio_thresholdfn_t thresholdfn) {
138        p->thresholdfn = thresholdfn;
139        return AUBIO_OK;
140}
141
142aubio_thresholdfn_t aubio_peakpicker_get_thresholdfn(aubio_peakpicker_t * p) {
143        return (aubio_thresholdfn_t) (p->thresholdfn);
144}
145
146aubio_peakpicker_t * new_aubio_peakpicker(uint_t channels) {
147        aubio_peakpicker_t * t = AUBIO_NEW(aubio_peakpicker_t);
148        t->threshold = 0.1; /* 0.0668; 0.33; 0.082; 0.033; */
149        t->win_post  = 5;
150        t->win_pre   = 1;
151  //channels = 1;
152  t->channels = channels;
153
154        t->thresholdfn = (aubio_thresholdfn_t)(fvec_median_channel); /* (fvec_mean); */
155        t->pickerfn = (aubio_pickerfn_t)(fvec_peakpick);
156
157        t->scratch = new_fvec(t->win_post+t->win_pre+1, channels);
158        t->onset_keep = new_fvec(t->win_post+t->win_pre+1, channels);
159        t->onset_proc = new_fvec(t->win_post+t->win_pre+1, channels);
160        t->onset_peek = new_fvec(3, channels);
161
162        /* cutoff: low-pass filter cutoff [0.34, 1] */
163        /* t->cutoff=0.34; */
164        t->biquad = new_aubio_biquad(0.1600,0.3200,0.1600,-0.5949,0.2348);
165        return t;
166}
167
168void del_aubio_peakpicker(aubio_peakpicker_t * p) {
169        del_aubio_biquad(p->biquad);
170        del_fvec(p->onset_keep);
171        del_fvec(p->onset_proc);
172        del_fvec(p->onset_peek);
173        del_fvec(p->scratch);
174        AUBIO_FREE(p);
175}
Note: See TracBrowser for help on using the repository browser.