Chris@23
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
matthiasm@0
|
2
|
Chris@35
|
3 /*
|
Chris@35
|
4 NNLS-Chroma / Chordino
|
Chris@35
|
5
|
Chris@35
|
6 Audio feature extraction plugins for chromagram and chord
|
Chris@35
|
7 estimation.
|
Chris@35
|
8
|
Chris@35
|
9 Centre for Digital Music, Queen Mary University of London.
|
Chris@35
|
10 This file copyright 2008-2010 Matthias Mauch and QMUL.
|
Chris@35
|
11
|
Chris@35
|
12 This program is free software; you can redistribute it and/or
|
Chris@35
|
13 modify it under the terms of the GNU General Public License as
|
Chris@35
|
14 published by the Free Software Foundation; either version 2 of the
|
Chris@35
|
15 License, or (at your option) any later version. See the file
|
Chris@35
|
16 COPYING included with this distribution for more information.
|
Chris@35
|
17 */
|
Chris@35
|
18
|
matthiasm@0
|
19 #include "NNLSChroma.h"
|
Chris@27
|
20
|
Chris@27
|
21 #include "chromamethods.h"
|
Chris@27
|
22
|
Chris@27
|
23 #include <cstdlib>
|
Chris@27
|
24 #include <fstream>
|
matthiasm@0
|
25 #include <cmath>
|
matthiasm@9
|
26
|
Chris@27
|
27 #include <algorithm>
|
matthiasm@0
|
28
|
matthiasm@0
|
29 const bool debug_on = false;
|
matthiasm@0
|
30
|
matthiasm@0
|
31 NNLSChroma::NNLSChroma(float inputSampleRate) :
|
Chris@35
|
32 NNLSBase(inputSampleRate)
|
matthiasm@0
|
33 {
|
Chris@23
|
34 if (debug_on) cerr << "--> NNLSChroma" << endl;
|
matthiasm@0
|
35 }
|
matthiasm@0
|
36
|
matthiasm@0
|
37 NNLSChroma::~NNLSChroma()
|
matthiasm@0
|
38 {
|
Chris@23
|
39 if (debug_on) cerr << "--> ~NNLSChroma" << endl;
|
matthiasm@0
|
40 }
|
matthiasm@0
|
41
|
matthiasm@0
|
42 string
|
matthiasm@0
|
43 NNLSChroma::getIdentifier() const
|
matthiasm@0
|
44 {
|
Chris@23
|
45 if (debug_on) cerr << "--> getIdentifier" << endl;
|
matthiasm@46
|
46 return "nnls-chroma";
|
matthiasm@0
|
47 }
|
matthiasm@0
|
48
|
matthiasm@0
|
49 string
|
matthiasm@0
|
50 NNLSChroma::getName() const
|
matthiasm@0
|
51 {
|
Chris@23
|
52 if (debug_on) cerr << "--> getName" << endl;
|
matthiasm@0
|
53 return "NNLS Chroma";
|
matthiasm@0
|
54 }
|
matthiasm@0
|
55
|
matthiasm@0
|
56 string
|
matthiasm@0
|
57 NNLSChroma::getDescription() const
|
matthiasm@0
|
58 {
|
Chris@23
|
59 if (debug_on) cerr << "--> getDescription" << endl;
|
matthiasm@58
|
60 return "This plugin provides a number of features derived from a DFT-based log-frequency amplitude spectrum: some variants of the log-frequency spectrum, including a semitone spectrum derived from approximate transcription using the NNLS algorithm; and based on this semitone spectrum, different chroma features.";
|
matthiasm@0
|
61 }
|
matthiasm@0
|
62
|
matthiasm@0
|
63 NNLSChroma::OutputList
|
matthiasm@0
|
64 NNLSChroma::getOutputDescriptors() const
|
matthiasm@0
|
65 {
|
Chris@23
|
66 if (debug_on) cerr << "--> getOutputDescriptors" << endl;
|
matthiasm@0
|
67 OutputList list;
|
matthiasm@0
|
68
|
matthiasm@0
|
69 // Make chroma names for the binNames property
|
matthiasm@0
|
70 vector<string> chromanames;
|
matthiasm@0
|
71 vector<string> bothchromanames;
|
matthiasm@0
|
72 for (int iNote = 0; iNote < 24; iNote++) {
|
matthiasm@0
|
73 bothchromanames.push_back(notenames[iNote]);
|
matthiasm@0
|
74 if (iNote < 12) {
|
matthiasm@43
|
75 chromanames.push_back(notenames[iNote+12]);
|
matthiasm@0
|
76 }
|
matthiasm@0
|
77 }
|
matthiasm@0
|
78
|
Chris@35
|
79 int index = 0;
|
matthiasm@0
|
80
|
Chris@23
|
81 OutputDescriptor d1;
|
matthiasm@0
|
82 d1.identifier = "logfreqspec";
|
matthiasm@0
|
83 d1.name = "Log-Frequency Spectrum";
|
matthiasm@0
|
84 d1.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping.";
|
matthiasm@0
|
85 d1.unit = "";
|
matthiasm@0
|
86 d1.hasFixedBinCount = true;
|
matthiasm@0
|
87 d1.binCount = nNote;
|
matthiasm@0
|
88 d1.hasKnownExtents = false;
|
matthiasm@0
|
89 d1.isQuantized = false;
|
matthiasm@0
|
90 d1.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
91 d1.hasDuration = false;
|
matthiasm@0
|
92 d1.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
93 list.push_back(d1);
|
Chris@35
|
94 m_outputLogSpec = index++;
|
matthiasm@0
|
95
|
Chris@23
|
96 OutputDescriptor d2;
|
matthiasm@0
|
97 d2.identifier = "tunedlogfreqspec";
|
matthiasm@0
|
98 d2.name = "Tuned Log-Frequency Spectrum";
|
matthiasm@0
|
99 d2.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping, then its tuned using the estimated tuning frequency.";
|
matthiasm@0
|
100 d2.unit = "";
|
matthiasm@0
|
101 d2.hasFixedBinCount = true;
|
mail@77
|
102 d2.binCount = nNote;
|
matthiasm@0
|
103 d2.hasKnownExtents = false;
|
matthiasm@0
|
104 d2.isQuantized = false;
|
matthiasm@0
|
105 d2.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
106 d2.hasDuration = false;
|
matthiasm@0
|
107 d2.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
108 list.push_back(d2);
|
Chris@35
|
109 m_outputTunedSpec = index++;
|
matthiasm@0
|
110
|
matthiasm@0
|
111 OutputDescriptor d3;
|
matthiasm@0
|
112 d3.identifier = "semitonespectrum";
|
matthiasm@0
|
113 d3.name = "Semitone Spectrum";
|
matthiasm@0
|
114 d3.description = "A semitone-spaced log-frequency spectrum derived from the third-of-a-semitone-spaced tuned log-frequency spectrum.";
|
matthiasm@0
|
115 d3.unit = "";
|
matthiasm@0
|
116 d3.hasFixedBinCount = true;
|
matthiasm@0
|
117 d3.binCount = 84;
|
matthiasm@0
|
118 d3.hasKnownExtents = false;
|
matthiasm@0
|
119 d3.isQuantized = false;
|
matthiasm@0
|
120 d3.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
121 d3.hasDuration = false;
|
matthiasm@0
|
122 d3.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
123 list.push_back(d3);
|
Chris@35
|
124 m_outputSemiSpec = index++;
|
matthiasm@0
|
125
|
matthiasm@0
|
126 OutputDescriptor d4;
|
matthiasm@0
|
127 d4.identifier = "chroma";
|
matthiasm@0
|
128 d4.name = "Chromagram";
|
matthiasm@58
|
129 d4.description = "Tuning-adjusted chromagram from NNLS approximate transcription, with an emphasis on the medium note range.";
|
matthiasm@0
|
130 d4.unit = "";
|
matthiasm@0
|
131 d4.hasFixedBinCount = true;
|
matthiasm@0
|
132 d4.binCount = 12;
|
matthiasm@0
|
133 d4.binNames = chromanames;
|
matthiasm@0
|
134 d4.hasKnownExtents = false;
|
matthiasm@0
|
135 d4.isQuantized = false;
|
matthiasm@0
|
136 d4.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
137 d4.hasDuration = false;
|
matthiasm@0
|
138 d4.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
139 list.push_back(d4);
|
Chris@35
|
140 m_outputChroma = index++;
|
matthiasm@0
|
141
|
matthiasm@0
|
142 OutputDescriptor d5;
|
matthiasm@0
|
143 d5.identifier = "basschroma";
|
matthiasm@0
|
144 d5.name = "Bass Chromagram";
|
matthiasm@58
|
145 d5.description = "Tuning-adjusted bass chromagram from NNLS approximate transcription, with an emphasis on the bass note range.";
|
matthiasm@0
|
146 d5.unit = "";
|
matthiasm@0
|
147 d5.hasFixedBinCount = true;
|
matthiasm@0
|
148 d5.binCount = 12;
|
matthiasm@0
|
149 d5.binNames = chromanames;
|
matthiasm@0
|
150 d5.hasKnownExtents = false;
|
matthiasm@0
|
151 d5.isQuantized = false;
|
matthiasm@0
|
152 d5.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
153 d5.hasDuration = false;
|
matthiasm@0
|
154 d5.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
155 list.push_back(d5);
|
Chris@35
|
156 m_outputBassChroma = index++;
|
matthiasm@0
|
157
|
matthiasm@0
|
158 OutputDescriptor d6;
|
matthiasm@0
|
159 d6.identifier = "bothchroma";
|
matthiasm@0
|
160 d6.name = "Chromagram and Bass Chromagram";
|
matthiasm@58
|
161 d6.description = "Tuning-adjusted chromagram and bass chromagram (stacked on top of each other) from NNLS approximate transcription.";
|
matthiasm@0
|
162 d6.unit = "";
|
matthiasm@0
|
163 d6.hasFixedBinCount = true;
|
matthiasm@0
|
164 d6.binCount = 24;
|
matthiasm@0
|
165 d6.binNames = bothchromanames;
|
matthiasm@0
|
166 d6.hasKnownExtents = false;
|
matthiasm@0
|
167 d6.isQuantized = false;
|
matthiasm@0
|
168 d6.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
169 d6.hasDuration = false;
|
matthiasm@0
|
170 d6.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@0
|
171 list.push_back(d6);
|
Chris@35
|
172 m_outputBothChroma = index++;
|
matthiasm@1
|
173
|
mail@83
|
174 OutputDescriptor d7;
|
mail@83
|
175 d7.identifier = "consonance";
|
mail@83
|
176 d7.name = "Consonance estimate.";
|
mail@83
|
177 d7.description = "A simple consonance value based on the convolution of a consonance profile with the semitone spectrum.";
|
mail@83
|
178 d7.unit = "";
|
mail@83
|
179 d7.hasFixedBinCount = true;
|
mail@83
|
180 d7.binCount = 1;
|
mail@83
|
181 d7.hasKnownExtents = false;
|
mail@83
|
182 d7.isQuantized = false;
|
mail@83
|
183 d7.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@83
|
184 d7.hasDuration = false;
|
mail@83
|
185 d7.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
mail@83
|
186 list.push_back(d7);
|
mail@83
|
187 m_outputConsonance = index++;
|
mail@83
|
188
|
matthiasm@105
|
189 OutputDescriptor monophonicness;
|
matthiasm@105
|
190 monophonicness.identifier = "monophonicness";
|
matthiasm@105
|
191 monophonicness.name = "Monophonicness estimate.";
|
matthiasm@105
|
192 monophonicness.description = ".";
|
matthiasm@105
|
193 monophonicness.unit = "";
|
matthiasm@105
|
194 monophonicness.hasFixedBinCount = true;
|
matthiasm@105
|
195 monophonicness.binCount = 1;
|
matthiasm@105
|
196 monophonicness.hasKnownExtents = true;
|
matthiasm@105
|
197 monophonicness.minValue = 0;
|
matthiasm@105
|
198 monophonicness.maxValue = 1;
|
matthiasm@105
|
199 monophonicness.isQuantized = false;
|
matthiasm@105
|
200 monophonicness.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@105
|
201 monophonicness.hasDuration = false;
|
matthiasm@105
|
202 monophonicness.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
matthiasm@105
|
203 list.push_back(monophonicness);
|
matthiasm@105
|
204 m_outputMonophonicness = index++;
|
matthiasm@105
|
205
|
matthiasm@0
|
206 return list;
|
matthiasm@0
|
207 }
|
matthiasm@0
|
208
|
matthiasm@0
|
209
|
matthiasm@0
|
210 bool
|
matthiasm@0
|
211 NNLSChroma::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
matthiasm@0
|
212 {
|
Chris@23
|
213 if (debug_on) {
|
Chris@23
|
214 cerr << "--> initialise";
|
Chris@23
|
215 }
|
matthiasm@1
|
216
|
Chris@35
|
217 if (!NNLSBase::initialise(channels, stepSize, blockSize)) {
|
Chris@35
|
218 return false;
|
Chris@35
|
219 }
|
matthiasm@1
|
220
|
matthiasm@0
|
221 return true;
|
matthiasm@0
|
222 }
|
matthiasm@0
|
223
|
matthiasm@0
|
224 void
|
matthiasm@0
|
225 NNLSChroma::reset()
|
matthiasm@0
|
226 {
|
Chris@23
|
227 if (debug_on) cerr << "--> reset";
|
Chris@35
|
228 NNLSBase::reset();
|
matthiasm@0
|
229 }
|
matthiasm@0
|
230
|
matthiasm@0
|
231 NNLSChroma::FeatureSet
|
matthiasm@0
|
232 NNLSChroma::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
|
matthiasm@0
|
233 {
|
Chris@23
|
234 if (debug_on) cerr << "--> process" << endl;
|
Chris@35
|
235
|
Chris@35
|
236 NNLSBase::baseProcess(inputBuffers, timestamp);
|
matthiasm@0
|
237
|
Chris@23
|
238 FeatureSet fs;
|
Chris@35
|
239 fs[m_outputLogSpec].push_back(m_logSpectrum[m_logSpectrum.size()-1]);
|
Chris@23
|
240 return fs;
|
matthiasm@0
|
241 }
|
matthiasm@0
|
242
|
matthiasm@0
|
243 NNLSChroma::FeatureSet
|
matthiasm@0
|
244 NNLSChroma::getRemainingFeatures()
|
matthiasm@0
|
245 {
|
mail@100
|
246 static const int nConsonance = 24;
|
mail@100
|
247 float consonancepattern[nConsonance] = {0,-1,-1,1,1,1,-1,1,1,1,-1,-1,1,-1,-1,1,1,1,-1,1,1,1,-1,-1};
|
mail@100
|
248 float consonancemean = 0;
|
mail@100
|
249 for (int i = 0; i< nConsonance; ++i) {
|
mail@100
|
250 consonancemean += consonancepattern[i]/nConsonance;
|
mail@100
|
251 }
|
mail@84
|
252
|
mail@100
|
253 cerr << "consonancemean = " << consonancemean << endl;
|
mail@100
|
254
|
mail@100
|
255 for (int i = 0; i< nConsonance; ++i) {
|
mail@100
|
256 consonancepattern[i] -= consonancemean;
|
mail@100
|
257 }
|
Chris@23
|
258 if (debug_on) cerr << "--> getRemainingFeatures" << endl;
|
Chris@23
|
259 FeatureSet fsOut;
|
Chris@35
|
260 if (m_logSpectrum.size() == 0) return fsOut;
|
Chris@23
|
261 //
|
Chris@23
|
262 /** Calculate Tuning
|
Chris@23
|
263 calculate tuning from (using the angle of the complex number defined by the
|
Chris@23
|
264 cumulative mean real and imag values)
|
Chris@23
|
265 **/
|
mail@80
|
266 float meanTuningImag = 0;
|
mail@80
|
267 float meanTuningReal = 0;
|
mail@80
|
268 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
mail@80
|
269 meanTuningReal += m_meanTunings[iBPS] * cosvalues[iBPS];
|
mail@80
|
270 meanTuningImag += m_meanTunings[iBPS] * sinvalues[iBPS];
|
mail@80
|
271 }
|
Chris@23
|
272 float cumulativetuning = 440 * pow(2,atan2(meanTuningImag, meanTuningReal)/(24*M_PI));
|
Chris@23
|
273 float normalisedtuning = atan2(meanTuningImag, meanTuningReal)/(2*M_PI);
|
Chris@23
|
274 int intShift = floor(normalisedtuning * 3);
|
mail@80
|
275 float floatShift = normalisedtuning * 3 - intShift; // floatShift is a really bad name for this
|
matthiasm@1
|
276
|
Chris@23
|
277 char buffer0 [50];
|
matthiasm@1
|
278
|
Chris@23
|
279 sprintf(buffer0, "estimated tuning: %0.1f Hz", cumulativetuning);
|
matthiasm@1
|
280
|
Chris@23
|
281 // cerr << "normalisedtuning: " << normalisedtuning << '\n';
|
matthiasm@1
|
282
|
Chris@23
|
283 /** Tune Log-Frequency Spectrogram
|
Chris@23
|
284 calculate a tuned log-frequency spectrogram (f2): use the tuning estimated above (kinda f0) to
|
Chris@23
|
285 perform linear interpolation on the existing log-frequency spectrogram (kinda f1).
|
Chris@23
|
286 **/
|
Chris@23
|
287 cerr << endl << "[NNLS Chroma Plugin] Tuning Log-Frequency Spectrogram ... ";
|
matthiasm@13
|
288
|
Chris@23
|
289 float tempValue = 0;
|
Chris@23
|
290 float dbThreshold = 0; // relative to the background spectrum
|
Chris@23
|
291 float thresh = pow(10,dbThreshold/20);
|
Chris@23
|
292 // cerr << "tune local ? " << m_tuneLocal << endl;
|
Chris@23
|
293 int count = 0;
|
mail@77
|
294
|
matthiasm@1
|
295
|
Chris@35
|
296 for (FeatureList::iterator i = m_logSpectrum.begin(); i != m_logSpectrum.end(); ++i) {
|
Chris@23
|
297 Feature f1 = *i;
|
Chris@23
|
298 Feature f2; // tuned log-frequency spectrum
|
Chris@23
|
299 f2.hasTimestamp = true;
|
Chris@23
|
300 f2.timestamp = f1.timestamp;
|
Chris@23
|
301 f2.values.push_back(0.0); f2.values.push_back(0.0); // set lower edge to zero
|
matthiasm@1
|
302
|
matthiasm@85
|
303
|
Chris@23
|
304 if (m_tuneLocal) {
|
Chris@23
|
305 intShift = floor(m_localTuning[count] * 3);
|
mail@80
|
306 floatShift = m_localTuning[count] * 3 - intShift; // floatShift is a really bad name for this
|
Chris@23
|
307 }
|
matthiasm@1
|
308
|
mail@80
|
309 // cerr << intShift << " " << floatShift << endl;
|
matthiasm@1
|
310
|
Chris@23
|
311 for (unsigned k = 2; k < f1.values.size() - 3; ++k) { // interpolate all inner bins
|
mail@80
|
312 tempValue = f1.values[k + intShift] * (1-floatShift) + f1.values[k+intShift+1] * floatShift;
|
Chris@23
|
313 f2.values.push_back(tempValue);
|
Chris@23
|
314 }
|
matthiasm@1
|
315
|
Chris@23
|
316 f2.values.push_back(0.0); f2.values.push_back(0.0); f2.values.push_back(0.0); // upper edge
|
mail@77
|
317
|
Chris@23
|
318 vector<float> runningmean = SpecialConvolution(f2.values,hw);
|
Chris@23
|
319 vector<float> runningstd;
|
mail@77
|
320 for (int i = 0; i < nNote; i++) { // first step: squared values into vector (variance)
|
Chris@23
|
321 runningstd.push_back((f2.values[i] - runningmean[i]) * (f2.values[i] - runningmean[i]));
|
Chris@23
|
322 }
|
Chris@23
|
323 runningstd = SpecialConvolution(runningstd,hw); // second step convolve
|
mail@77
|
324 for (int i = 0; i < nNote; i++) {
|
Chris@23
|
325 runningstd[i] = sqrt(runningstd[i]); // square root to finally have running std
|
Chris@23
|
326 if (runningstd[i] > 0) {
|
Chris@23
|
327 // f2.values[i] = (f2.values[i] / runningmean[i]) > thresh ?
|
mail@41
|
328 // (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_whitening) : 0;
|
Chris@23
|
329 f2.values[i] = (f2.values[i] - runningmean[i]) > 0 ?
|
mail@41
|
330 (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_whitening) : 0;
|
Chris@23
|
331 }
|
Chris@23
|
332 if (f2.values[i] < 0) {
|
Chris@23
|
333 cerr << "ERROR: negative value in logfreq spectrum" << endl;
|
Chris@23
|
334 }
|
Chris@23
|
335 }
|
Chris@35
|
336 fsOut[m_outputTunedSpec].push_back(f2);
|
Chris@23
|
337 count++;
|
Chris@23
|
338 }
|
Chris@23
|
339 cerr << "done." << endl;
|
matthiasm@1
|
340
|
Chris@23
|
341 /** Semitone spectrum and chromagrams
|
Chris@23
|
342 Semitone-spaced log-frequency spectrum derived from the tuned log-freq spectrum above. the spectrum
|
Chris@23
|
343 is inferred using a non-negative least squares algorithm.
|
Chris@23
|
344 Three different kinds of chromagram are calculated, "treble", "bass", and "both" (which means
|
Chris@23
|
345 bass and treble stacked onto each other).
|
Chris@23
|
346 **/
|
matthiasm@42
|
347 if (m_useNNLS == 0) {
|
Chris@23
|
348 cerr << "[NNLS Chroma Plugin] Mapping to semitone spectrum and chroma ... ";
|
Chris@23
|
349 } else {
|
Chris@23
|
350 cerr << "[NNLS Chroma Plugin] Performing NNLS and mapping to chroma ... ";
|
Chris@23
|
351 }
|
matthiasm@13
|
352
|
matthiasm@1
|
353
|
Chris@23
|
354 vector<float> oldchroma = vector<float>(12,0);
|
Chris@23
|
355 vector<float> oldbasschroma = vector<float>(12,0);
|
Chris@23
|
356 count = 0;
|
matthiasm@9
|
357
|
Chris@38
|
358 for (FeatureList::iterator it = fsOut[m_outputTunedSpec].begin(); it != fsOut[m_outputTunedSpec].end(); ++it) {
|
Chris@23
|
359 Feature f2 = *it; // logfreq spectrum
|
Chris@23
|
360 Feature f3; // semitone spectrum
|
Chris@23
|
361 Feature f4; // treble chromagram
|
Chris@23
|
362 Feature f5; // bass chromagram
|
Chris@23
|
363 Feature f6; // treble and bass chromagram
|
matthiasm@85
|
364 Feature consonance;
|
matthiasm@105
|
365 Feature monophonicness;
|
matthiasm@85
|
366
|
Chris@23
|
367 f3.hasTimestamp = true;
|
Chris@23
|
368 f3.timestamp = f2.timestamp;
|
matthiasm@1
|
369
|
Chris@23
|
370 f4.hasTimestamp = true;
|
Chris@23
|
371 f4.timestamp = f2.timestamp;
|
matthiasm@1
|
372
|
Chris@23
|
373 f5.hasTimestamp = true;
|
Chris@23
|
374 f5.timestamp = f2.timestamp;
|
matthiasm@1
|
375
|
Chris@23
|
376 f6.hasTimestamp = true;
|
Chris@23
|
377 f6.timestamp = f2.timestamp;
|
matthiasm@1
|
378
|
matthiasm@85
|
379 consonance.hasTimestamp = true;
|
matthiasm@85
|
380 consonance.timestamp = f2.timestamp;
|
matthiasm@105
|
381
|
matthiasm@105
|
382 monophonicness.hasTimestamp = true;
|
matthiasm@105
|
383 monophonicness.timestamp = f2.timestamp;
|
matthiasm@85
|
384
|
mail@77
|
385 float b[nNote];
|
matthiasm@1
|
386
|
Chris@23
|
387 bool some_b_greater_zero = false;
|
Chris@23
|
388 float sumb = 0;
|
mail@77
|
389 for (int i = 0; i < nNote; i++) {
|
mail@77
|
390 // b[i] = m_dict[(nNote * count + i) % (nNote * 84)];
|
Chris@23
|
391 b[i] = f2.values[i];
|
Chris@23
|
392 sumb += b[i];
|
Chris@23
|
393 if (b[i] > 0) {
|
Chris@23
|
394 some_b_greater_zero = true;
|
Chris@23
|
395 }
|
Chris@23
|
396 }
|
matthiasm@1
|
397
|
Chris@23
|
398 // here's where the non-negative least squares algorithm calculates the note activation x
|
matthiasm@1
|
399
|
Chris@23
|
400 vector<float> chroma = vector<float>(12, 0);
|
Chris@23
|
401 vector<float> basschroma = vector<float>(12, 0);
|
Chris@23
|
402 float currval;
|
Chris@23
|
403 unsigned iSemitone = 0;
|
matthiasm@1
|
404
|
Chris@23
|
405 if (some_b_greater_zero) {
|
matthiasm@42
|
406 if (m_useNNLS == 0) {
|
mail@80
|
407 for (unsigned iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
Chris@23
|
408 currval = 0;
|
mail@80
|
409 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
mail@80
|
410 currval += b[iNote + iBPS] * (1-abs(iBPS*1.0/(nBPS/2+1)));
|
mail@80
|
411 }
|
Chris@23
|
412 f3.values.push_back(currval);
|
Chris@23
|
413 chroma[iSemitone % 12] += currval * treblewindow[iSemitone];
|
Chris@23
|
414 basschroma[iSemitone % 12] += currval * basswindow[iSemitone];
|
Chris@23
|
415 iSemitone++;
|
Chris@23
|
416 }
|
matthiasm@1
|
417
|
Chris@23
|
418 } else {
|
Chris@35
|
419 float x[84+1000];
|
Chris@23
|
420 for (int i = 1; i < 1084; ++i) x[i] = 1.0;
|
Chris@23
|
421 vector<int> signifIndex;
|
Chris@23
|
422 int index=0;
|
Chris@23
|
423 sumb /= 84.0;
|
mail@80
|
424 for (unsigned iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
Chris@23
|
425 float currval = 0;
|
mail@80
|
426 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
mail@80
|
427 currval += b[iNote + iBPS];
|
mail@80
|
428 }
|
Chris@23
|
429 if (currval > 0) signifIndex.push_back(index);
|
Chris@23
|
430 f3.values.push_back(0); // fill the values, change later
|
Chris@23
|
431 index++;
|
Chris@23
|
432 }
|
Chris@35
|
433 float rnorm;
|
Chris@35
|
434 float w[84+1000];
|
Chris@35
|
435 float zz[84+1000];
|
Chris@23
|
436 int indx[84+1000];
|
Chris@23
|
437 int mode;
|
mail@77
|
438 int dictsize = nNote*signifIndex.size();
|
Chris@23
|
439 // cerr << "dictsize is " << dictsize << "and values size" << f3.values.size()<< endl;
|
Chris@35
|
440 float *curr_dict = new float[dictsize];
|
Chris@91
|
441 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
Chris@91
|
442 for (int iBin = 0; iBin < nNote; iBin++) {
|
mail@77
|
443 curr_dict[iNote * nNote + iBin] = 1.0 * m_dict[signifIndex[iNote] * nNote + iBin];
|
Chris@23
|
444 }
|
Chris@23
|
445 }
|
Chris@35
|
446 nnls(curr_dict, nNote, nNote, signifIndex.size(), b, x, &rnorm, w, zz, indx, &mode);
|
Chris@23
|
447 delete [] curr_dict;
|
Chris@91
|
448 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
Chris@23
|
449 f3.values[signifIndex[iNote]] = x[iNote];
|
Chris@23
|
450 // cerr << mode << endl;
|
Chris@23
|
451 chroma[signifIndex[iNote] % 12] += x[iNote] * treblewindow[signifIndex[iNote]];
|
Chris@23
|
452 basschroma[signifIndex[iNote] % 12] += x[iNote] * basswindow[signifIndex[iNote]];
|
Chris@23
|
453 }
|
Chris@23
|
454 }
|
matthiasm@79
|
455 } else {
|
matthiasm@79
|
456 for (int i = 0; i < 84; ++i) f3.values.push_back(0);
|
Chris@23
|
457 }
|
matthiasm@85
|
458
|
matthiasm@85
|
459 float notesum = 0;
|
matthiasm@85
|
460
|
matthiasm@85
|
461 consonance.values.push_back(0);
|
matthiasm@104
|
462
|
matthiasm@105
|
463 float note_max = 0;
|
matthiasm@105
|
464 float note_runnerup = 0;
|
matthiasm@105
|
465 // float note_sum = 0;
|
matthiasm@105
|
466 for (int iSemitone = 0; iSemitone < 84; iSemitone++) {
|
matthiasm@105
|
467 float currvalue = f3.values[iSemitone] * treblewindow[iSemitone];
|
matthiasm@105
|
468 if (currvalue > note_max) {
|
matthiasm@105
|
469 note_runnerup = note_max;
|
matthiasm@105
|
470 note_max = currvalue;
|
matthiasm@105
|
471 } else if (currvalue > note_runnerup) {
|
matthiasm@105
|
472 note_runnerup = currvalue;
|
matthiasm@105
|
473 }
|
matthiasm@105
|
474 // note_sum += note[iPitchClass];
|
matthiasm@105
|
475 }
|
matthiasm@105
|
476 // float note_monophonicness = 12*note_max/(12*note_max+note_sum);
|
matthiasm@105
|
477 cerr << note_max << endl;
|
matthiasm@105
|
478 cerr << note_runnerup << endl << endl;
|
matthiasm@105
|
479 float note_monophonicness = 0.5;
|
matthiasm@105
|
480 if (note_max > 0) {
|
matthiasm@105
|
481 note_monophonicness = (note_max / (note_max+note_runnerup) - 0.5) * 2;
|
matthiasm@105
|
482 }
|
matthiasm@105
|
483 monophonicness.values.push_back(note_monophonicness);
|
matthiasm@105
|
484
|
matthiasm@104
|
485 for (int iSemitone = 0; iSemitone < 84; ++iSemitone) {
|
matthiasm@104
|
486 float tempconsonance = 0;
|
matthiasm@104
|
487 int sumlength = 1;
|
matthiasm@85
|
488 for (int jSemitone = 1; jSemitone < 24; ++jSemitone) {
|
matthiasm@104
|
489 if (iSemitone+jSemitone > 84-1) break;
|
matthiasm@104
|
490 sumlength++;
|
mail@100
|
491 tempconsonance += f3.values[iSemitone+jSemitone] * (consonancepattern[jSemitone]) * treblewindow[iSemitone+jSemitone];
|
matthiasm@85
|
492 }
|
matthiasm@104
|
493 notesum += f3.values[iSemitone] * f3.values[iSemitone] * treblewindow[iSemitone] * treblewindow[iSemitone] * sumlength;
|
matthiasm@104
|
494 consonance.values[0] += (f3.values[iSemitone] * tempconsonance * treblewindow[iSemitone]) * sumlength;
|
matthiasm@85
|
495 }
|
matthiasm@104
|
496 // cerr << consonance.values[0] << " " << f3.timestamp << " "<< notesum << endl;
|
matthiasm@86
|
497 if (notesum > 0) consonance.values[0] /= notesum;
|
matthiasm@104
|
498
|
matthiasm@85
|
499
|
Chris@23
|
500 f4.values = chroma;
|
Chris@23
|
501 f5.values = basschroma;
|
Chris@23
|
502 chroma.insert(chroma.begin(), basschroma.begin(), basschroma.end()); // just stack the both chromas
|
Chris@23
|
503 f6.values = chroma;
|
matthiasm@1
|
504
|
Chris@23
|
505 if (m_doNormalizeChroma > 0) {
|
Chris@23
|
506 vector<float> chromanorm = vector<float>(3,0);
|
Chris@23
|
507 switch (int(m_doNormalizeChroma)) {
|
Chris@23
|
508 case 0: // should never end up here
|
Chris@23
|
509 break;
|
Chris@23
|
510 case 1:
|
Chris@23
|
511 chromanorm[0] = *max_element(f4.values.begin(), f4.values.end());
|
Chris@23
|
512 chromanorm[1] = *max_element(f5.values.begin(), f5.values.end());
|
Chris@23
|
513 chromanorm[2] = max(chromanorm[0], chromanorm[1]);
|
Chris@23
|
514 break;
|
Chris@23
|
515 case 2:
|
Chris@23
|
516 for (vector<float>::iterator it = f4.values.begin(); it != f4.values.end(); ++it) {
|
Chris@23
|
517 chromanorm[0] += *it;
|
Chris@23
|
518 }
|
Chris@23
|
519 for (vector<float>::iterator it = f5.values.begin(); it != f5.values.end(); ++it) {
|
Chris@23
|
520 chromanorm[1] += *it;
|
Chris@23
|
521 }
|
Chris@23
|
522 for (vector<float>::iterator it = f6.values.begin(); it != f6.values.end(); ++it) {
|
Chris@23
|
523 chromanorm[2] += *it;
|
Chris@23
|
524 }
|
Chris@23
|
525 break;
|
Chris@23
|
526 case 3:
|
Chris@23
|
527 for (vector<float>::iterator it = f4.values.begin(); it != f4.values.end(); ++it) {
|
Chris@23
|
528 chromanorm[0] += pow(*it,2);
|
Chris@23
|
529 }
|
Chris@23
|
530 chromanorm[0] = sqrt(chromanorm[0]);
|
Chris@23
|
531 for (vector<float>::iterator it = f5.values.begin(); it != f5.values.end(); ++it) {
|
Chris@23
|
532 chromanorm[1] += pow(*it,2);
|
Chris@23
|
533 }
|
Chris@23
|
534 chromanorm[1] = sqrt(chromanorm[1]);
|
Chris@23
|
535 for (vector<float>::iterator it = f6.values.begin(); it != f6.values.end(); ++it) {
|
Chris@23
|
536 chromanorm[2] += pow(*it,2);
|
Chris@23
|
537 }
|
Chris@23
|
538 chromanorm[2] = sqrt(chromanorm[2]);
|
Chris@23
|
539 break;
|
Chris@23
|
540 }
|
Chris@23
|
541 if (chromanorm[0] > 0) {
|
Chris@91
|
542 for (size_t i = 0; i < f4.values.size(); i++) {
|
Chris@23
|
543 f4.values[i] /= chromanorm[0];
|
Chris@23
|
544 }
|
Chris@23
|
545 }
|
Chris@23
|
546 if (chromanorm[1] > 0) {
|
Chris@91
|
547 for (size_t i = 0; i < f5.values.size(); i++) {
|
Chris@23
|
548 f5.values[i] /= chromanorm[1];
|
Chris@23
|
549 }
|
Chris@23
|
550 }
|
Chris@23
|
551 if (chromanorm[2] > 0) {
|
Chris@91
|
552 for (size_t i = 0; i < f6.values.size(); i++) {
|
Chris@23
|
553 f6.values[i] /= chromanorm[2];
|
Chris@23
|
554 }
|
Chris@23
|
555 }
|
Chris@23
|
556 }
|
matthiasm@13
|
557
|
Chris@35
|
558 fsOut[m_outputSemiSpec].push_back(f3);
|
Chris@35
|
559 fsOut[m_outputChroma].push_back(f4);
|
Chris@35
|
560 fsOut[m_outputBassChroma].push_back(f5);
|
Chris@35
|
561 fsOut[m_outputBothChroma].push_back(f6);
|
matthiasm@85
|
562 fsOut[m_outputConsonance].push_back(consonance);
|
matthiasm@105
|
563 fsOut[m_outputMonophonicness].push_back(monophonicness);
|
Chris@23
|
564 count++;
|
Chris@23
|
565 }
|
Chris@23
|
566 cerr << "done." << endl;
|
matthiasm@10
|
567
|
Chris@23
|
568 return fsOut;
|
matthiasm@0
|
569
|
matthiasm@0
|
570 }
|
matthiasm@0
|
571
|