c@35
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@69
|
2 /*
|
c@69
|
3 Constant-Q library
|
c@69
|
4 Copyright (c) 2013-2014 Queen Mary, University of London
|
c@69
|
5
|
c@69
|
6 Permission is hereby granted, free of charge, to any person
|
c@69
|
7 obtaining a copy of this software and associated documentation
|
c@69
|
8 files (the "Software"), to deal in the Software without
|
c@69
|
9 restriction, including without limitation the rights to use, copy,
|
c@69
|
10 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@69
|
11 of the Software, and to permit persons to whom the Software is
|
c@69
|
12 furnished to do so, subject to the following conditions:
|
c@69
|
13
|
c@69
|
14 The above copyright notice and this permission notice shall be
|
c@69
|
15 included in all copies or substantial portions of the Software.
|
c@69
|
16
|
c@69
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@69
|
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@69
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@69
|
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
c@69
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@69
|
22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@69
|
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@69
|
24
|
c@69
|
25 Except as contained in this notice, the names of the Centre for
|
c@69
|
26 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@69
|
27 shall not be used in advertising or otherwise to promote the sale,
|
c@69
|
28 use or other dealings in this Software without prior written
|
c@69
|
29 authorization.
|
c@69
|
30 */
|
c@35
|
31
|
c@35
|
32 #include "CQVamp.h"
|
c@35
|
33
|
c@121
|
34 #include "Pitch.h"
|
c@55
|
35
|
c@58
|
36 #include <algorithm>
|
c@58
|
37 #include <cstdio>
|
c@58
|
38
|
c@35
|
39 using std::string;
|
c@35
|
40 using std::vector;
|
c@35
|
41 using std::cerr;
|
c@35
|
42 using std::endl;
|
c@35
|
43
|
c@109
|
44 // The plugin offers either MIDI pitch or frequency range parameters,
|
c@109
|
45 // depending on the midiPitchParameters option given to the
|
c@109
|
46 // constructor. It never offers both. So they can have different
|
c@109
|
47 // defaults; if we're using MIDI pitch, the min and max frequencies
|
c@109
|
48 // will come from those rather than from the m_minFrequency and
|
c@109
|
49 // m_maxFrequency members.
|
c@109
|
50 static const int defaultMinMIDIPitch = 36;
|
c@109
|
51 static const int defaultMaxMIDIPitch = 96;
|
c@109
|
52 static const int defaultBPO = 36;
|
c@109
|
53 static const float defaultTuningFrequency = 440.f;
|
c@109
|
54
|
c@109
|
55 CQVamp::CQVamp(float inputSampleRate, bool midiPitchParameters) :
|
c@35
|
56 Vamp::Plugin(inputSampleRate),
|
c@109
|
57 m_midiPitchParameters(midiPitchParameters),
|
c@109
|
58 m_minMIDIPitch(defaultMinMIDIPitch),
|
c@109
|
59 m_maxMIDIPitch(defaultMaxMIDIPitch),
|
c@109
|
60 m_tuningFrequency(defaultTuningFrequency),
|
c@109
|
61 m_bpo(defaultBPO),
|
c@90
|
62 m_interpolation(CQSpectrogram::InterpolateLinear),
|
c@35
|
63 m_cq(0),
|
c@109
|
64 m_maxFrequency(14080),
|
c@109
|
65 m_minFrequency(100),
|
c@53
|
66 m_haveStartTime(false),
|
c@53
|
67 m_columnCount(0)
|
c@35
|
68 {
|
c@35
|
69 }
|
c@35
|
70
|
c@35
|
71 CQVamp::~CQVamp()
|
c@35
|
72 {
|
c@35
|
73 delete m_cq;
|
c@35
|
74 }
|
c@35
|
75
|
c@35
|
76 string
|
c@35
|
77 CQVamp::getIdentifier() const
|
c@35
|
78 {
|
c@109
|
79 if (m_midiPitchParameters) {
|
c@109
|
80 return "cqvampmidi";
|
c@109
|
81 } else {
|
c@109
|
82 return "cqvamp";
|
c@109
|
83 }
|
c@35
|
84 }
|
c@35
|
85
|
c@35
|
86 string
|
c@35
|
87 CQVamp::getName() const
|
c@35
|
88 {
|
c@109
|
89 if (m_midiPitchParameters) {
|
c@109
|
90 return "Constant-Q Spectrogram (MIDI pitch range)";
|
c@109
|
91 } else {
|
c@109
|
92 return "Constant-Q Spectrogram (Hz range)";
|
c@109
|
93 }
|
c@35
|
94 }
|
c@35
|
95
|
c@35
|
96 string
|
c@35
|
97 CQVamp::getDescription() const
|
c@35
|
98 {
|
c@109
|
99 if (m_midiPitchParameters) {
|
c@109
|
100 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in MIDI pitch units.";
|
c@109
|
101 } else {
|
c@109
|
102 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in Hz.";
|
c@109
|
103 }
|
c@35
|
104 }
|
c@35
|
105
|
c@35
|
106 string
|
c@35
|
107 CQVamp::getMaker() const
|
c@35
|
108 {
|
c@35
|
109 return "Queen Mary, University of London";
|
c@35
|
110 }
|
c@35
|
111
|
c@35
|
112 int
|
c@35
|
113 CQVamp::getPluginVersion() const
|
c@35
|
114 {
|
c@35
|
115 return 1;
|
c@35
|
116 }
|
c@35
|
117
|
c@35
|
118 string
|
c@35
|
119 CQVamp::getCopyright() const
|
c@35
|
120 {
|
c@35
|
121 return "Plugin by Chris Cannam. Method by Christian Schörkhuber and Anssi Klapuri. Copyright (c) 2013 QMUL";
|
c@35
|
122 }
|
c@35
|
123
|
c@35
|
124 CQVamp::ParameterList
|
c@35
|
125 CQVamp::getParameterDescriptors() const
|
c@35
|
126 {
|
c@35
|
127 ParameterList list;
|
c@35
|
128
|
c@55
|
129 ParameterDescriptor desc;
|
c@55
|
130
|
c@109
|
131 if (m_midiPitchParameters) {
|
c@55
|
132
|
c@109
|
133 desc.identifier = "minpitch";
|
c@109
|
134 desc.name = "Minimum Pitch";
|
c@109
|
135 desc.unit = "MIDI units";
|
c@109
|
136 desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)";
|
c@109
|
137 desc.minValue = 0;
|
c@109
|
138 desc.maxValue = 127;
|
c@109
|
139 desc.defaultValue = 36;
|
c@109
|
140 desc.isQuantized = true;
|
c@109
|
141 desc.quantizeStep = 1;
|
c@109
|
142 list.push_back(desc);
|
c@109
|
143
|
c@109
|
144 desc.identifier = "maxpitch";
|
c@109
|
145 desc.name = "Maximum Pitch";
|
c@109
|
146 desc.unit = "MIDI units";
|
c@109
|
147 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
|
c@109
|
148 desc.minValue = 0;
|
c@109
|
149 desc.maxValue = 127;
|
c@109
|
150 desc.defaultValue = 84;
|
c@109
|
151 desc.isQuantized = true;
|
c@109
|
152 desc.quantizeStep = 1;
|
c@109
|
153 list.push_back(desc);
|
c@109
|
154
|
c@109
|
155 desc.identifier = "tuning";
|
c@109
|
156 desc.name = "Tuning Frequency";
|
c@109
|
157 desc.unit = "Hz";
|
c@109
|
158 desc.description = "Frequency of concert A";
|
c@109
|
159 desc.minValue = 360;
|
c@109
|
160 desc.maxValue = 500;
|
c@109
|
161 desc.defaultValue = 440;
|
c@109
|
162 desc.isQuantized = false;
|
c@109
|
163 list.push_back(desc);
|
c@109
|
164
|
c@109
|
165 } else {
|
c@109
|
166
|
c@109
|
167 desc.identifier = "minfreq";
|
c@109
|
168 desc.name = "Minimum Frequency";
|
c@109
|
169 desc.unit = "Hz";
|
c@109
|
170 desc.description = "Lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)";
|
c@109
|
171 desc.minValue = 1;
|
c@109
|
172 desc.maxValue = m_inputSampleRate / 2;
|
c@109
|
173 desc.defaultValue = 100;
|
c@109
|
174 desc.isQuantized = false;
|
c@109
|
175 list.push_back(desc);
|
c@109
|
176
|
c@109
|
177 desc.identifier = "maxfreq";
|
c@109
|
178 desc.name = "Maximum Frequency";
|
c@109
|
179 desc.unit = "Hz";
|
c@109
|
180 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
|
c@109
|
181 desc.minValue = 1;
|
c@109
|
182 desc.maxValue = m_inputSampleRate / 2;
|
c@109
|
183 desc.defaultValue = 14080;
|
c@109
|
184 desc.isQuantized = false;
|
c@109
|
185 list.push_back(desc);
|
c@109
|
186 }
|
c@35
|
187
|
c@35
|
188 desc.identifier = "bpo";
|
c@35
|
189 desc.name = "Bins per Octave";
|
c@35
|
190 desc.unit = "bins";
|
c@35
|
191 desc.description = "Number of constant-Q transform bins per octave";
|
c@35
|
192 desc.minValue = 2;
|
c@35
|
193 desc.maxValue = 480;
|
c@110
|
194 desc.defaultValue = defaultBPO;
|
c@35
|
195 desc.isQuantized = true;
|
c@35
|
196 desc.quantizeStep = 1;
|
c@35
|
197 list.push_back(desc);
|
c@35
|
198
|
c@75
|
199 desc.identifier = "interpolation";
|
c@75
|
200 desc.name = "Interpolation";
|
c@75
|
201 desc.unit = "";
|
c@75
|
202 desc.description = "Interpolation method used to fill empty cells in lower octaves";
|
c@75
|
203 desc.minValue = 0;
|
c@75
|
204 desc.maxValue = 2;
|
c@75
|
205 desc.defaultValue = 2;
|
c@75
|
206 desc.isQuantized = true;
|
c@75
|
207 desc.quantizeStep = 1;
|
c@90
|
208 desc.valueNames.push_back("None, leave as zero");
|
c@75
|
209 desc.valueNames.push_back("None, repeat prior value");
|
c@75
|
210 desc.valueNames.push_back("Linear interpolation");
|
c@75
|
211 list.push_back(desc);
|
c@75
|
212
|
c@35
|
213 return list;
|
c@35
|
214 }
|
c@35
|
215
|
c@35
|
216 float
|
c@35
|
217 CQVamp::getParameter(std::string param) const
|
c@35
|
218 {
|
c@109
|
219 if (param == "minpitch" && m_midiPitchParameters) {
|
c@55
|
220 return m_minMIDIPitch;
|
c@55
|
221 }
|
c@109
|
222 if (param == "maxpitch" && m_midiPitchParameters) {
|
c@55
|
223 return m_maxMIDIPitch;
|
c@55
|
224 }
|
c@109
|
225 if (param == "tuning" && m_midiPitchParameters) {
|
c@55
|
226 return m_tuningFrequency;
|
c@55
|
227 }
|
c@35
|
228 if (param == "bpo") {
|
c@35
|
229 return m_bpo;
|
c@35
|
230 }
|
c@75
|
231 if (param == "interpolation") {
|
c@75
|
232 return (float)m_interpolation;
|
c@75
|
233 }
|
c@109
|
234 if (param == "minfreq" && !m_midiPitchParameters) {
|
c@109
|
235 return m_minFrequency;
|
c@109
|
236 }
|
c@109
|
237 if (param == "maxfreq" && !m_midiPitchParameters) {
|
c@109
|
238 return m_maxFrequency;
|
c@109
|
239 }
|
c@35
|
240 std::cerr << "WARNING: CQVamp::getParameter: unknown parameter \""
|
c@35
|
241 << param << "\"" << std::endl;
|
c@35
|
242 return 0.0;
|
c@35
|
243 }
|
c@35
|
244
|
c@35
|
245 void
|
c@35
|
246 CQVamp::setParameter(std::string param, float value)
|
c@35
|
247 {
|
c@109
|
248 if (param == "minpitch" && m_midiPitchParameters) {
|
c@55
|
249 m_minMIDIPitch = lrintf(value);
|
c@109
|
250 } else if (param == "maxpitch" && m_midiPitchParameters) {
|
c@55
|
251 m_maxMIDIPitch = lrintf(value);
|
c@109
|
252 } else if (param == "tuning" && m_midiPitchParameters) {
|
c@55
|
253 m_tuningFrequency = value;
|
c@75
|
254 } else if (param == "bpo") {
|
c@35
|
255 m_bpo = lrintf(value);
|
c@75
|
256 } else if (param == "interpolation") {
|
c@90
|
257 m_interpolation = (CQSpectrogram::Interpolation)lrintf(value);
|
c@109
|
258 } else if (param == "minfreq" && !m_midiPitchParameters) {
|
c@109
|
259 m_minFrequency = value;
|
c@109
|
260 } else if (param == "maxfreq" && !m_midiPitchParameters) {
|
c@109
|
261 m_maxFrequency = value;
|
c@35
|
262 } else {
|
c@35
|
263 std::cerr << "WARNING: CQVamp::setParameter: unknown parameter \""
|
c@35
|
264 << param << "\"" << std::endl;
|
c@35
|
265 }
|
c@35
|
266 }
|
c@35
|
267
|
c@35
|
268 bool
|
c@35
|
269 CQVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@35
|
270 {
|
c@35
|
271 if (m_cq) {
|
c@35
|
272 delete m_cq;
|
c@75
|
273 m_cq = 0;
|
c@35
|
274 }
|
c@35
|
275
|
c@35
|
276 if (channels < getMinChannelCount() ||
|
c@35
|
277 channels > getMaxChannelCount()) return false;
|
c@35
|
278
|
c@35
|
279 m_stepSize = stepSize;
|
c@35
|
280 m_blockSize = blockSize;
|
c@35
|
281
|
c@109
|
282 if (m_midiPitchParameters) {
|
c@109
|
283 m_minFrequency = Pitch::getFrequencyForPitch
|
c@109
|
284 (m_minMIDIPitch, 0, m_tuningFrequency);
|
c@109
|
285 m_maxFrequency = Pitch::getFrequencyForPitch
|
c@109
|
286 (m_maxMIDIPitch, 0, m_tuningFrequency);
|
c@109
|
287 }
|
c@55
|
288
|
c@90
|
289 m_cq = new CQSpectrogram
|
c@75
|
290 (m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo,
|
c@75
|
291 m_interpolation);
|
c@35
|
292
|
c@35
|
293 return true;
|
c@35
|
294 }
|
c@35
|
295
|
c@35
|
296 void
|
c@35
|
297 CQVamp::reset()
|
c@35
|
298 {
|
c@35
|
299 if (m_cq) {
|
c@35
|
300 delete m_cq;
|
c@90
|
301 m_cq = new CQSpectrogram
|
c@75
|
302 (m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo,
|
c@75
|
303 m_interpolation);
|
c@35
|
304 }
|
c@53
|
305 m_haveStartTime = false;
|
c@53
|
306 m_columnCount = 0;
|
c@35
|
307 }
|
c@35
|
308
|
c@35
|
309 size_t
|
c@35
|
310 CQVamp::getPreferredStepSize() const
|
c@35
|
311 {
|
c@35
|
312 return 0;
|
c@35
|
313 }
|
c@35
|
314
|
c@35
|
315 size_t
|
c@35
|
316 CQVamp::getPreferredBlockSize() const
|
c@35
|
317 {
|
c@35
|
318 return 0;
|
c@35
|
319 }
|
c@35
|
320
|
c@109
|
321 std::string
|
c@109
|
322 CQVamp::noteName(int i) const
|
c@109
|
323 {
|
c@109
|
324 static const char *names[] = {
|
c@109
|
325 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
|
c@109
|
326 };
|
c@109
|
327
|
c@109
|
328 const char *n = names[i % 12];
|
c@109
|
329 int oct = i / 12 - 1;
|
c@109
|
330 char buf[20];
|
c@110
|
331 sprintf(buf, "%d %s%d", i, n, oct);
|
c@109
|
332
|
c@109
|
333 return buf;
|
c@109
|
334 }
|
c@109
|
335
|
c@35
|
336 CQVamp::OutputList
|
c@35
|
337 CQVamp::getOutputDescriptors() const
|
c@35
|
338 {
|
c@35
|
339 OutputList list;
|
c@35
|
340
|
c@35
|
341 OutputDescriptor d;
|
c@35
|
342 d.identifier = "constantq";
|
c@35
|
343 d.name = "Constant-Q Spectrogram";
|
c@35
|
344 d.unit = "";
|
c@35
|
345 d.description = "Output of constant-Q transform, as a single vector per process block";
|
c@35
|
346 d.hasFixedBinCount = true;
|
c@35
|
347 d.binCount = (m_cq ? m_cq->getTotalBins() : (9 * 24));
|
c@58
|
348
|
c@58
|
349 if (m_cq) {
|
c@58
|
350 char name[20];
|
c@94
|
351 for (int i = 0; i < (int)d.binCount; ++i) {
|
c@58
|
352 float freq = m_cq->getBinFrequency(i);
|
c@58
|
353 sprintf(name, "%.1f Hz", freq);
|
c@110
|
354 int note = Pitch::getPitchForFrequency(freq, 0, m_tuningFrequency);
|
c@110
|
355 float nearestFreq =
|
c@110
|
356 Pitch::getFrequencyForPitch(note, 0, m_tuningFrequency);
|
c@110
|
357 if (fabs(freq - nearestFreq) < 0.01) {
|
c@110
|
358 d.binNames.push_back(name + std::string(" ") + noteName(note));
|
c@109
|
359 } else {
|
c@109
|
360 d.binNames.push_back(name);
|
c@109
|
361 }
|
c@58
|
362 }
|
c@58
|
363 }
|
c@58
|
364
|
c@35
|
365 d.hasKnownExtents = false;
|
c@35
|
366 d.isQuantized = false;
|
c@35
|
367 d.sampleType = OutputDescriptor::FixedSampleRate;
|
c@35
|
368 d.sampleRate = m_inputSampleRate / (m_cq ? m_cq->getColumnHop() : 256);
|
c@35
|
369 list.push_back(d);
|
c@35
|
370
|
c@35
|
371 return list;
|
c@35
|
372 }
|
c@35
|
373
|
c@35
|
374 CQVamp::FeatureSet
|
c@35
|
375 CQVamp::process(const float *const *inputBuffers,
|
c@53
|
376 Vamp::RealTime timestamp)
|
c@35
|
377 {
|
c@35
|
378 if (!m_cq) {
|
c@35
|
379 cerr << "ERROR: CQVamp::process: "
|
c@35
|
380 << "Plugin has not been initialised"
|
c@35
|
381 << endl;
|
c@35
|
382 return FeatureSet();
|
c@35
|
383 }
|
c@35
|
384
|
c@53
|
385 if (!m_haveStartTime) {
|
c@53
|
386 m_startTime = timestamp;
|
c@53
|
387 m_haveStartTime = true;
|
c@53
|
388 }
|
c@53
|
389
|
c@35
|
390 vector<double> data;
|
c@35
|
391 for (int i = 0; i < m_blockSize; ++i) data.push_back(inputBuffers[0][i]);
|
c@35
|
392
|
c@35
|
393 vector<vector<double> > cqout = m_cq->process(data);
|
c@36
|
394 return convertToFeatures(cqout);
|
c@36
|
395 }
|
c@35
|
396
|
c@36
|
397 CQVamp::FeatureSet
|
c@36
|
398 CQVamp::getRemainingFeatures()
|
c@36
|
399 {
|
c@90
|
400 vector<vector<double> > cqout = m_cq->getRemainingOutput();
|
c@36
|
401 return convertToFeatures(cqout);
|
c@36
|
402 }
|
c@36
|
403
|
c@36
|
404 CQVamp::FeatureSet
|
c@36
|
405 CQVamp::convertToFeatures(const vector<vector<double> > &cqout)
|
c@36
|
406 {
|
c@35
|
407 FeatureSet returnFeatures;
|
c@35
|
408
|
c@75
|
409 int width = cqout.size();
|
c@75
|
410 int height = m_cq->getTotalBins();
|
c@35
|
411
|
c@75
|
412 for (int i = 0; i < width; ++i) {
|
c@36
|
413
|
c@75
|
414 vector<float> column(height, 0.f);
|
c@75
|
415 int thisHeight = cqout[i].size();
|
c@75
|
416 for (int j = 0; j < thisHeight; ++j) {
|
c@35
|
417 column[j] = cqout[i][j];
|
c@35
|
418 }
|
c@36
|
419
|
c@58
|
420 // put low frequencies at the start
|
c@58
|
421 std::reverse(column.begin(), column.end());
|
c@58
|
422
|
c@35
|
423 Feature feature;
|
c@53
|
424 feature.hasTimestamp = true;
|
c@53
|
425 feature.timestamp = m_startTime + Vamp::RealTime::frame2RealTime
|
c@53
|
426 (m_columnCount * m_cq->getColumnHop() - m_cq->getLatency(),
|
c@53
|
427 m_inputSampleRate);
|
c@35
|
428 feature.values = column;
|
c@35
|
429 feature.label = "";
|
c@53
|
430
|
c@56
|
431 // cerr << "timestamp = " << feature.timestamp << " (start time = " << m_startTime << ", column count = " << m_columnCount << ", latency = " << m_cq->getLatency() << ", sample rate " << m_inputSampleRate << ")" << endl;
|
c@53
|
432
|
c@53
|
433 if (feature.timestamp >= m_startTime) {
|
c@53
|
434 returnFeatures[0].push_back(feature);
|
c@53
|
435 }
|
c@53
|
436
|
c@53
|
437 ++m_columnCount;
|
c@35
|
438 }
|
c@35
|
439
|
c@35
|
440 return returnFeatures;
|
c@35
|
441 }
|
c@35
|
442
|