~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/vamp-plugins/Onset.cpp

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 
2
 
 
3
/*
 
4
    Vamp feature extraction plugins using Paul Brossier's Aubio library.
 
5
 
 
6
    Centre for Digital Music, Queen Mary, University of London.
 
7
    This file copyright 2006 Chris Cannam.
 
8
    
 
9
    This program is free software; you can redistribute it and/or
 
10
    modify it under the terms of the GNU General Public License as
 
11
    published by the Free Software Foundation; either version 2 of the
 
12
    License, or (at your option) any later version.  See the file
 
13
    COPYING included with this distribution for more information.
 
14
 
 
15
*/
 
16
 
 
17
#include <math.h>
 
18
#include "Onset.h"
 
19
 
 
20
using std::string;
 
21
using std::vector;
 
22
using std::cerr;
 
23
using std::endl;
 
24
 
 
25
Onset::Onset(float inputSampleRate) :
 
26
    Plugin(inputSampleRate),
 
27
    m_ibuf(0),
 
28
    m_fftgrain(0),
 
29
    m_onset(0),
 
30
    m_pv(0),
 
31
    m_peakpick(0),
 
32
    m_onsetdet(0),
 
33
    m_onsettype(aubio_onset_complex),
 
34
    m_threshold(0.3),
 
35
    m_silence(-90),
 
36
    m_channelCount(1)
 
37
{
 
38
}
 
39
 
 
40
Onset::~Onset()
 
41
{
 
42
    if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
 
43
    if (m_ibuf) del_fvec(m_ibuf);
 
44
    if (m_onset) del_fvec(m_onset);
 
45
    if (m_fftgrain) del_cvec(m_fftgrain);
 
46
    if (m_pv) del_aubio_pvoc(m_pv);
 
47
    if (m_peakpick) del_aubio_peakpicker(m_peakpick);
 
48
}
 
49
 
 
50
string
 
51
Onset::getIdentifier() const
 
52
{
 
53
    return "aubioonset";
 
54
}
 
55
 
 
56
string
 
57
Onset::getName() const
 
58
{
 
59
    return "Aubio Onset Detector";
 
60
}
 
61
 
 
62
string
 
63
Onset::getDescription() const
 
64
{
 
65
    return "Estimate note onset times";
 
66
}
 
67
 
 
68
string
 
69
Onset::getMaker() const
 
70
{
 
71
    return "Paul Brossier (plugin by Chris Cannam)";
 
72
}
 
73
 
 
74
int
 
75
Onset::getPluginVersion() const
 
76
{
 
77
    return 1;
 
78
}
 
79
 
 
80
string
 
81
Onset::getCopyright() const
 
82
{
 
83
    return "GPL";
 
84
}
 
85
 
 
86
bool
 
87
Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
 
88
{
 
89
    m_channelCount = channels;
 
90
    m_stepSize = stepSize;
 
91
    m_blockSize = blockSize;
 
92
 
 
93
    m_ibuf = new_fvec(stepSize, channels);
 
94
    m_onset = new_fvec(1, channels);
 
95
    m_fftgrain = new_cvec(blockSize, channels);
 
96
    m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
 
97
    m_peakpick = new_aubio_peakpicker(m_threshold);
 
98
 
 
99
    m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
 
100
    
 
101
    m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
 
102
                                             lrintf(m_inputSampleRate));
 
103
 
 
104
    m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
 
105
 
 
106
    return true;
 
107
}
 
108
 
 
109
void
 
110
Onset::reset()
 
111
{
 
112
}
 
113
 
 
114
size_t
 
115
Onset::getPreferredStepSize() const
 
116
{
 
117
    return 512;
 
118
}
 
119
 
 
120
size_t
 
121
Onset::getPreferredBlockSize() const
 
122
{
 
123
    return 2 * getPreferredStepSize();
 
124
}
 
125
 
 
126
Onset::ParameterList
 
127
Onset::getParameterDescriptors() const
 
128
{
 
129
    ParameterList list;
 
130
    
 
131
    ParameterDescriptor desc;
 
132
    desc.identifier = "onsettype";
 
133
    desc.name = "Onset Detection Function Type";
 
134
    desc.minValue = 0;
 
135
    desc.maxValue = 6;
 
136
    desc.defaultValue = (int)aubio_onset_complex;
 
137
    desc.isQuantized = true;
 
138
    desc.quantizeStep = 1;
 
139
    desc.valueNames.push_back("Energy Based");
 
140
    desc.valueNames.push_back("Spectral Difference");
 
141
    desc.valueNames.push_back("High-Frequency Content");
 
142
    desc.valueNames.push_back("Complex Domain");
 
143
    desc.valueNames.push_back("Phase Deviation");
 
144
    desc.valueNames.push_back("Kullback-Liebler");
 
145
    desc.valueNames.push_back("Modified Kullback-Liebler");
 
146
    list.push_back(desc);
 
147
 
 
148
    desc = ParameterDescriptor();
 
149
    desc.identifier = "peakpickthreshold";
 
150
    desc.name = "Peak Picker Threshold";
 
151
    desc.minValue = 0;
 
152
    desc.maxValue = 1;
 
153
    desc.defaultValue = 0.3;
 
154
    desc.isQuantized = false;
 
155
    list.push_back(desc);
 
156
 
 
157
    desc = ParameterDescriptor();
 
158
    desc.identifier = "silencethreshold";
 
159
    desc.name = "Silence Threshold";
 
160
    desc.minValue = -120;
 
161
    desc.maxValue = 0;
 
162
    desc.defaultValue = -90;
 
163
    desc.unit = "dB";
 
164
    desc.isQuantized = false;
 
165
    list.push_back(desc);
 
166
 
 
167
    return list;
 
168
}
 
169
 
 
170
float
 
171
Onset::getParameter(std::string param) const
 
172
{
 
173
    if (param == "onsettype") {
 
174
        return m_onsettype;
 
175
    } else if (param == "peakpickthreshold") {
 
176
        return m_threshold;
 
177
    } else if (param == "silencethreshold") {
 
178
        return m_silence;
 
179
    } else {
 
180
        return 0.0;
 
181
    }
 
182
}
 
183
 
 
184
void
 
185
Onset::setParameter(std::string param, float value)
 
186
{
 
187
    if (param == "onsettype") {
 
188
        switch (lrintf(value)) {
 
189
        case 0: m_onsettype = aubio_onset_energy; break;
 
190
        case 1: m_onsettype = aubio_onset_specdiff; break;
 
191
        case 2: m_onsettype = aubio_onset_hfc; break;
 
192
        case 3: m_onsettype = aubio_onset_complex; break;
 
193
        case 4: m_onsettype = aubio_onset_phase; break;
 
194
        case 5: m_onsettype = aubio_onset_kl; break;
 
195
        case 6: m_onsettype = aubio_onset_mkl; break;
 
196
        }
 
197
    } else if (param == "peakpickthreshold") {
 
198
        m_threshold = value;
 
199
    } else if (param == "silencethreshold") {
 
200
        m_silence = value;
 
201
    }
 
202
}
 
203
 
 
204
Onset::OutputList
 
205
Onset::getOutputDescriptors() const
 
206
{
 
207
    OutputList list;
 
208
 
 
209
    OutputDescriptor d;
 
210
    d.identifier = "onsets";
 
211
    d.name = "Onsets";
 
212
    d.unit = "";
 
213
    d.hasFixedBinCount = true;
 
214
    d.binCount = 0;
 
215
    d.sampleType = OutputDescriptor::VariableSampleRate;
 
216
    d.sampleRate = 0;
 
217
    list.push_back(d);
 
218
 
 
219
    d = OutputDescriptor();
 
220
    d.identifier = "detectionfunction";
 
221
    d.name = "Onset Detection Function";
 
222
    d.unit = "";
 
223
    d.hasFixedBinCount = true;
 
224
    d.binCount = m_channelCount;
 
225
    d.hasKnownExtents = false;
 
226
    d.isQuantized = false;
 
227
    d.sampleType = OutputDescriptor::OneSamplePerStep;
 
228
    list.push_back(d);
 
229
 
 
230
    return list;
 
231
}
 
232
 
 
233
Onset::FeatureSet
 
234
Onset::process(const float *const *inputBuffers,
 
235
               Vamp::RealTime timestamp)
 
236
{
 
237
    for (size_t i = 0; i < m_stepSize; ++i) {
 
238
        for (size_t j = 0; j < m_channelCount; ++j) {
 
239
            fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
 
240
        }
 
241
    }
 
242
 
 
243
    aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
 
244
    aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
 
245
 
 
246
    bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
 
247
 
 
248
    if (isonset) {
 
249
        if (aubio_silence_detection(m_ibuf, m_silence)) {
 
250
            isonset = false;
 
251
        }
 
252
    }
 
253
 
 
254
    FeatureSet returnFeatures;
 
255
 
 
256
    if (isonset) {
 
257
        if (timestamp - m_lastOnset >= m_delay) {
 
258
            Feature onsettime;
 
259
            onsettime.hasTimestamp = true;
 
260
            if (timestamp < m_delay) timestamp = m_delay;
 
261
            onsettime.timestamp = timestamp - m_delay;
 
262
            returnFeatures[0].push_back(onsettime);
 
263
            m_lastOnset = timestamp;
 
264
        }
 
265
    }
 
266
    Feature feature;
 
267
    for (size_t j = 0; j < m_channelCount; ++j) {
 
268
        feature.values.push_back(m_onset->data[j][0]);
 
269
    }
 
270
    returnFeatures[1].push_back(feature);
 
271
 
 
272
    return returnFeatures;
 
273
}
 
274
 
 
275
Onset::FeatureSet
 
276
Onset::getRemainingFeatures()
 
277
{
 
278
    return FeatureSet();
 
279
}
 
280