c@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@0
|
2
|
c@0
|
3 /*
|
c@0
|
4 QM Vamp Plugin Set
|
c@0
|
5
|
c@0
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@0
|
7 All rights reserved.
|
c@0
|
8 */
|
c@0
|
9
|
c@0
|
10 #include "ChromagramPlugin.h"
|
c@0
|
11
|
c@3
|
12 #include <base/Pitch.h>
|
c@3
|
13 #include <dsp/chromagram/Chromagram.h>
|
c@0
|
14
|
c@0
|
15 using std::string;
|
c@0
|
16 using std::vector;
|
c@0
|
17 using std::cerr;
|
c@0
|
18 using std::endl;
|
c@0
|
19
|
c@0
|
20 ChromagramPlugin::ChromagramPlugin(float inputSampleRate) :
|
c@0
|
21 Vamp::Plugin(inputSampleRate),
|
c@0
|
22 m_chromagram(0),
|
c@0
|
23 m_step(0),
|
c@0
|
24 m_block(0),
|
c@0
|
25 m_stepDelay(0)
|
c@0
|
26 {
|
c@0
|
27 m_minMIDIPitch = 12;
|
c@0
|
28 m_maxMIDIPitch = 96;
|
c@0
|
29 m_tuningFrequency = 440;
|
c@0
|
30 m_normalized = true;
|
c@0
|
31 m_bpo = 12;
|
c@0
|
32
|
c@0
|
33 setupConfig();
|
c@0
|
34 }
|
c@0
|
35
|
c@0
|
36 void
|
c@0
|
37 ChromagramPlugin::setupConfig()
|
c@0
|
38 {
|
c@0
|
39 m_config.FS = lrintf(m_inputSampleRate);
|
c@0
|
40 m_config.min = Pitch::getFrequencyForPitch
|
c@0
|
41 (m_minMIDIPitch, 0, m_tuningFrequency);
|
c@0
|
42 m_config.max = Pitch::getFrequencyForPitch
|
c@0
|
43 (m_maxMIDIPitch, 0, m_tuningFrequency);
|
c@0
|
44 m_config.BPO = m_bpo;
|
c@0
|
45 m_config.CQThresh = 0.0054;
|
c@0
|
46 m_config.isNormalised = m_normalized;
|
c@0
|
47
|
c@0
|
48 m_step = 0;
|
c@0
|
49 m_block = 0;
|
c@0
|
50 }
|
c@0
|
51
|
c@0
|
52 ChromagramPlugin::~ChromagramPlugin()
|
c@0
|
53 {
|
c@0
|
54 delete m_chromagram;
|
c@0
|
55 }
|
c@0
|
56
|
c@0
|
57 string
|
c@0
|
58 ChromagramPlugin::getName() const
|
c@0
|
59 {
|
c@0
|
60 return "chromagram";
|
c@0
|
61 }
|
c@0
|
62
|
c@0
|
63 string
|
c@0
|
64 ChromagramPlugin::getDescription() const
|
c@0
|
65 {
|
c@0
|
66 return "Chromagram";
|
c@0
|
67 }
|
c@0
|
68
|
c@0
|
69 string
|
c@0
|
70 ChromagramPlugin::getMaker() const
|
c@0
|
71 {
|
c@0
|
72 return "QMUL";
|
c@0
|
73 }
|
c@0
|
74
|
c@0
|
75 int
|
c@0
|
76 ChromagramPlugin::getPluginVersion() const
|
c@0
|
77 {
|
c@0
|
78 return 2;
|
c@0
|
79 }
|
c@0
|
80
|
c@0
|
81 string
|
c@0
|
82 ChromagramPlugin::getCopyright() const
|
c@0
|
83 {
|
c@0
|
84 return "Copyright (c) 2006 - All Rights Reserved";
|
c@0
|
85 }
|
c@0
|
86
|
c@0
|
87 ChromagramPlugin::ParameterList
|
c@0
|
88 ChromagramPlugin::getParameterDescriptors() const
|
c@0
|
89 {
|
c@0
|
90 ParameterList list;
|
c@0
|
91
|
c@0
|
92 ParameterDescriptor desc;
|
c@0
|
93 desc.name = "minpitch";
|
c@0
|
94 desc.description = "Minimum Pitch";
|
c@0
|
95 desc.unit = "MIDI units";
|
c@0
|
96 desc.minValue = 0;
|
c@0
|
97 desc.maxValue = 127;
|
c@0
|
98 desc.defaultValue = 12;
|
c@0
|
99 desc.isQuantized = true;
|
c@0
|
100 desc.quantizeStep = 1;
|
c@0
|
101 list.push_back(desc);
|
c@0
|
102
|
c@0
|
103 desc.name = "maxpitch";
|
c@0
|
104 desc.description = "Maximum Pitch";
|
c@0
|
105 desc.unit = "MIDI units";
|
c@0
|
106 desc.minValue = 0;
|
c@0
|
107 desc.maxValue = 127;
|
c@0
|
108 desc.defaultValue = 96;
|
c@0
|
109 desc.isQuantized = true;
|
c@0
|
110 desc.quantizeStep = 1;
|
c@0
|
111 list.push_back(desc);
|
c@0
|
112
|
c@0
|
113 desc.name = "tuning";
|
c@0
|
114 desc.description = "Tuning Frequency";
|
c@0
|
115 desc.unit = "Hz";
|
c@0
|
116 desc.minValue = 420;
|
c@0
|
117 desc.maxValue = 460;
|
c@0
|
118 desc.defaultValue = 440;
|
c@0
|
119 desc.isQuantized = false;
|
c@0
|
120 list.push_back(desc);
|
c@0
|
121
|
c@0
|
122 desc.name = "bpo";
|
c@0
|
123 desc.description = "Bins per Octave";
|
c@0
|
124 desc.unit = "bins";
|
c@0
|
125 desc.minValue = 2;
|
c@0
|
126 desc.maxValue = 36;
|
c@0
|
127 desc.defaultValue = 12;
|
c@0
|
128 desc.isQuantized = true;
|
c@0
|
129 desc.quantizeStep = 1;
|
c@0
|
130 list.push_back(desc);
|
c@0
|
131
|
c@0
|
132 desc.name = "normalized";
|
c@0
|
133 desc.description = "Normalized";
|
c@0
|
134 desc.unit = "";
|
c@0
|
135 desc.minValue = 0;
|
c@0
|
136 desc.maxValue = 1;
|
c@0
|
137 desc.defaultValue = 1;
|
c@0
|
138 desc.isQuantized = true;
|
c@0
|
139 desc.quantizeStep = 1;
|
c@0
|
140 list.push_back(desc);
|
c@0
|
141
|
c@0
|
142 return list;
|
c@0
|
143 }
|
c@0
|
144
|
c@0
|
145 float
|
c@0
|
146 ChromagramPlugin::getParameter(std::string param) const
|
c@0
|
147 {
|
c@0
|
148 if (param == "minpitch") {
|
c@0
|
149 return m_minMIDIPitch;
|
c@0
|
150 }
|
c@0
|
151 if (param == "maxpitch") {
|
c@0
|
152 return m_maxMIDIPitch;
|
c@0
|
153 }
|
c@0
|
154 if (param == "tuning") {
|
c@0
|
155 return m_tuningFrequency;
|
c@0
|
156 }
|
c@0
|
157 if (param == "bpo") {
|
c@0
|
158 return m_bpo;
|
c@0
|
159 }
|
c@0
|
160 if (param == "normalized") {
|
c@0
|
161 return m_normalized;
|
c@0
|
162 }
|
c@0
|
163 std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \""
|
c@0
|
164 << param << "\"" << std::endl;
|
c@0
|
165 return 0.0;
|
c@0
|
166 }
|
c@0
|
167
|
c@0
|
168 void
|
c@0
|
169 ChromagramPlugin::setParameter(std::string param, float value)
|
c@0
|
170 {
|
c@0
|
171 if (param == "minpitch") {
|
c@0
|
172 m_minMIDIPitch = lrintf(value);
|
c@0
|
173 } else if (param == "maxpitch") {
|
c@0
|
174 m_maxMIDIPitch = lrintf(value);
|
c@0
|
175 } else if (param == "tuning") {
|
c@0
|
176 m_tuningFrequency = value;
|
c@0
|
177 } else if (param == "bpo") {
|
c@0
|
178 m_bpo = lrintf(value);
|
c@0
|
179 } else if (param == "normalized") {
|
c@0
|
180 m_normalized = (value > 0.0001);
|
c@0
|
181 } else {
|
c@0
|
182 std::cerr << "WARNING: ChromagramPlugin::setParameter: unknown parameter \""
|
c@0
|
183 << param << "\"" << std::endl;
|
c@0
|
184 }
|
c@0
|
185
|
c@0
|
186 setupConfig();
|
c@0
|
187 }
|
c@0
|
188
|
c@0
|
189
|
c@0
|
190 bool
|
c@0
|
191 ChromagramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@0
|
192 {
|
c@0
|
193 if (m_chromagram) {
|
c@0
|
194 delete m_chromagram;
|
c@0
|
195 m_chromagram = 0;
|
c@0
|
196 }
|
c@0
|
197
|
c@0
|
198 if (channels < getMinChannelCount() ||
|
c@0
|
199 channels > getMaxChannelCount()) return false;
|
c@0
|
200
|
c@0
|
201 if (stepSize != m_step) return false;
|
c@0
|
202 if (blockSize != m_block) return false;
|
c@0
|
203
|
c@0
|
204 // m_stepDelay = (blockSize - stepSize) / 2;
|
c@0
|
205 // m_stepDelay = m_stepDelay / stepSize;
|
c@0
|
206 m_stepDelay = (blockSize - stepSize) / stepSize; //!!! why? seems about right to look at, but...
|
c@0
|
207
|
c@0
|
208 std::cerr << "ChromagramPlugin::initialise: step " << stepSize << ", block "
|
c@0
|
209 << blockSize << ", delay " << m_stepDelay << std::endl;
|
c@0
|
210
|
c@0
|
211 m_chromagram = new Chromagram(m_config);
|
c@0
|
212 return true;
|
c@0
|
213 }
|
c@0
|
214
|
c@0
|
215 void
|
c@0
|
216 ChromagramPlugin::reset()
|
c@0
|
217 {
|
c@0
|
218 if (m_chromagram) {
|
c@0
|
219 delete m_chromagram;
|
c@0
|
220 m_chromagram = new Chromagram(m_config);
|
c@0
|
221 }
|
c@0
|
222 while (!m_pending.empty()) m_pending.pop();
|
c@0
|
223 }
|
c@0
|
224
|
c@0
|
225 size_t
|
c@0
|
226 ChromagramPlugin::getPreferredStepSize() const
|
c@0
|
227 {
|
c@0
|
228 if (!m_step) {
|
c@0
|
229 Chromagram chroma(m_config);
|
c@0
|
230 m_step = chroma.getHopSize();
|
c@0
|
231 m_block = chroma.getFrameSize();
|
c@0
|
232 }
|
c@0
|
233
|
c@0
|
234 return m_step;
|
c@0
|
235 }
|
c@0
|
236
|
c@0
|
237 size_t
|
c@0
|
238 ChromagramPlugin::getPreferredBlockSize() const
|
c@0
|
239 {
|
c@0
|
240 if (!m_block) {
|
c@0
|
241 Chromagram chroma(m_config);
|
c@0
|
242 m_step = chroma.getHopSize();
|
c@0
|
243 m_block = chroma.getFrameSize();
|
c@0
|
244 }
|
c@0
|
245
|
c@0
|
246 return m_block;
|
c@0
|
247 }
|
c@0
|
248
|
c@0
|
249 ChromagramPlugin::OutputList
|
c@0
|
250 ChromagramPlugin::getOutputDescriptors() const
|
c@0
|
251 {
|
c@0
|
252 OutputList list;
|
c@0
|
253
|
c@0
|
254 OutputDescriptor d;
|
c@0
|
255 // d.name = "unnormalized";
|
c@0
|
256 d.name = "chromagram";
|
c@0
|
257 d.unit = "";
|
c@0
|
258 d.description = "Chromagram";
|
c@0
|
259 d.hasFixedBinCount = true;
|
c@0
|
260 d.binCount = m_config.BPO;
|
c@0
|
261
|
c@0
|
262 const char *names[] =
|
c@0
|
263 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
|
c@0
|
264
|
c@0
|
265 if (d.binCount % 12 == 0) {
|
c@0
|
266 for (int i = 0; i < 12; ++i) {
|
c@0
|
267 int ipc = m_minMIDIPitch % 12;
|
c@0
|
268 int index = (i + ipc) % 12;
|
c@0
|
269 d.binNames.push_back(names[index]);
|
c@0
|
270 for (int j = 0; j < d.binCount / 12 - 1; ++j) {
|
c@0
|
271 d.binNames.push_back("");
|
c@0
|
272 }
|
c@0
|
273 }
|
c@0
|
274 } else {
|
c@0
|
275 d.binNames.push_back("C");
|
c@0
|
276 }
|
c@0
|
277
|
c@0
|
278 d.hasKnownExtents = m_normalized;
|
c@0
|
279 d.minValue = 0.0;
|
c@0
|
280 d.maxValue = (m_normalized ? 1.0 : 0.0);
|
c@0
|
281 d.isQuantized = false;
|
c@0
|
282 d.sampleType = OutputDescriptor::OneSamplePerStep;
|
c@0
|
283 list.push_back(d);
|
c@0
|
284 /*
|
c@0
|
285 d.name = "normalized";
|
c@0
|
286 d.description = "Normalized Chromagram";
|
c@0
|
287 d.hasKnownExtents = true;
|
c@0
|
288 d.maxValue = 1.0;
|
c@0
|
289 list.push_back(d);
|
c@0
|
290 */
|
c@0
|
291 return list;
|
c@0
|
292 }
|
c@0
|
293
|
c@0
|
294 ChromagramPlugin::Feature
|
c@0
|
295 ChromagramPlugin::normalize(const Feature &feature)
|
c@0
|
296 {
|
c@0
|
297 float min = 0.0, max = 0.0;
|
c@0
|
298
|
c@0
|
299 for (size_t i = 0; i < feature.values.size(); ++i) {
|
c@0
|
300 if (i == 0 || feature.values[i] < min) min = feature.values[i];
|
c@0
|
301 if (i == 0 || feature.values[i] > max) max = feature.values[i];
|
c@0
|
302 }
|
c@0
|
303
|
c@0
|
304 if (max == 0.0 || max == min) return feature;
|
c@0
|
305
|
c@0
|
306 Feature normalized;
|
c@0
|
307 normalized.hasTimestamp = false;
|
c@0
|
308
|
c@0
|
309 for (size_t i = 0; i < feature.values.size(); ++i) {
|
c@0
|
310 normalized.values.push_back((feature.values[i] - min) / (max - min));
|
c@0
|
311 }
|
c@0
|
312
|
c@0
|
313 return normalized;
|
c@0
|
314 }
|
c@0
|
315
|
c@0
|
316 ChromagramPlugin::FeatureSet
|
c@0
|
317 ChromagramPlugin::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
|
c@0
|
318 {
|
c@0
|
319 if (!m_chromagram) {
|
c@0
|
320 cerr << "ERROR: ChromagramPlugin::process: "
|
c@0
|
321 << "Chromagram has not been initialised"
|
c@0
|
322 << endl;
|
c@0
|
323 return FeatureSet();
|
c@0
|
324 }
|
c@0
|
325
|
c@0
|
326 // convert float* to double*
|
c@0
|
327 double *tempBuffer = new double[m_block];
|
c@0
|
328 for (size_t i = 0; i < m_block; ++i) {
|
c@0
|
329 tempBuffer[i] = inputBuffers[0][i];
|
c@0
|
330 }
|
c@0
|
331
|
c@0
|
332 double *output = m_chromagram->process(tempBuffer);
|
c@0
|
333 delete[] tempBuffer;
|
c@0
|
334
|
c@0
|
335 Feature feature;
|
c@0
|
336 feature.hasTimestamp = false;
|
c@0
|
337 for (size_t i = 0; i < m_config.BPO; ++i) {
|
c@0
|
338 feature.values.push_back(output[i]);
|
c@0
|
339 }
|
c@0
|
340 feature.label = "";
|
c@0
|
341
|
c@0
|
342 FeatureSet returnFeatures;
|
c@0
|
343
|
c@0
|
344 if (m_stepDelay == 0) {
|
c@0
|
345 returnFeatures[0].push_back(feature);
|
c@0
|
346 // returnFeatures[1].push_back(normalize(feature));
|
c@0
|
347 return returnFeatures;
|
c@0
|
348 }
|
c@0
|
349
|
c@0
|
350 if (m_pending.size() == m_stepDelay) {
|
c@0
|
351 returnFeatures[0].push_back(m_pending.front());
|
c@0
|
352 // returnFeatures[1].push_back(normalize(m_pending.front()));
|
c@0
|
353 m_pending.pop();
|
c@0
|
354 } else {
|
c@0
|
355 returnFeatures[0].push_back(Feature());
|
c@0
|
356 // returnFeatures[1].push_back(Feature());
|
c@0
|
357 }
|
c@0
|
358
|
c@0
|
359 m_pending.push(feature);
|
c@0
|
360 return returnFeatures;
|
c@0
|
361 }
|
c@0
|
362
|
c@0
|
363 ChromagramPlugin::FeatureSet
|
c@0
|
364 ChromagramPlugin::getRemainingFeatures()
|
c@0
|
365 {
|
c@0
|
366 FeatureSet returnFeatures;
|
c@0
|
367
|
c@0
|
368 while (!m_pending.empty()) {
|
c@0
|
369 returnFeatures[0].push_back(m_pending.front());
|
c@0
|
370 // returnFeatures[1].push_back(normalize(m_pending.front()));
|
c@0
|
371 m_pending.pop();
|
c@0
|
372 }
|
c@0
|
373
|
c@0
|
374 return returnFeatures;
|
c@0
|
375 }
|
c@0
|
376
|