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@120
|
70
|
matthiasm@120
|
71 const char* notenames[24] = {
|
matthiasm@120
|
72 "A (bass)","Bb (bass)","B (bass)","C (bass)","C# (bass)","D (bass)","Eb (bass)","E (bass)","F (bass)","F# (bass)","G (bass)","Ab (bass)",
|
matthiasm@120
|
73 "A","Bb","B","C","C#","D","Eb","E","F","F#","G","Ab"};
|
matthiasm@120
|
74
|
matthiasm@120
|
75
|
matthiasm@0
|
76 vector<string> chromanames;
|
matthiasm@0
|
77 vector<string> bothchromanames;
|
matthiasm@0
|
78 for (int iNote = 0; iNote < 24; iNote++) {
|
matthiasm@0
|
79 bothchromanames.push_back(notenames[iNote]);
|
matthiasm@0
|
80 if (iNote < 12) {
|
matthiasm@43
|
81 chromanames.push_back(notenames[iNote+12]);
|
matthiasm@0
|
82 }
|
matthiasm@0
|
83 }
|
matthiasm@0
|
84
|
Chris@35
|
85 int index = 0;
|
matthiasm@0
|
86
|
Chris@164
|
87 float featureRate =
|
Chris@164
|
88 (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize;
|
Chris@164
|
89
|
mail@117
|
90 OutputDescriptor logfreqspecOutput;
|
mail@117
|
91 logfreqspecOutput.identifier = "logfreqspec";
|
mail@117
|
92 logfreqspecOutput.name = "Log-Frequency Spectrum";
|
mail@117
|
93 logfreqspecOutput.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping.";
|
mail@117
|
94 logfreqspecOutput.unit = "";
|
mail@117
|
95 logfreqspecOutput.hasFixedBinCount = true;
|
mail@117
|
96 logfreqspecOutput.binCount = nNote;
|
mail@117
|
97 logfreqspecOutput.hasKnownExtents = false;
|
mail@117
|
98 logfreqspecOutput.isQuantized = false;
|
mail@117
|
99 logfreqspecOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@117
|
100 logfreqspecOutput.hasDuration = false;
|
Chris@164
|
101 logfreqspecOutput.sampleRate = featureRate;
|
mail@117
|
102 list.push_back(logfreqspecOutput);
|
mail@117
|
103 m_outputLogfreqspec = index++;
|
matthiasm@0
|
104
|
mail@117
|
105 OutputDescriptor tunedlogfreqspecOutput;
|
mail@117
|
106 tunedlogfreqspecOutput.identifier = "tunedlogfreqspec";
|
mail@117
|
107 tunedlogfreqspecOutput.name = "Tuned Log-Frequency Spectrum";
|
mail@117
|
108 tunedlogfreqspecOutput.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping, then its tuned using the estimated tuning frequency.";
|
mail@117
|
109 tunedlogfreqspecOutput.unit = "";
|
mail@117
|
110 tunedlogfreqspecOutput.hasFixedBinCount = true;
|
mail@117
|
111 tunedlogfreqspecOutput.binCount = nNote;
|
mail@117
|
112 tunedlogfreqspecOutput.hasKnownExtents = false;
|
mail@117
|
113 tunedlogfreqspecOutput.isQuantized = false;
|
mail@117
|
114 tunedlogfreqspecOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@117
|
115 tunedlogfreqspecOutput.hasDuration = false;
|
Chris@164
|
116 tunedlogfreqspecOutput.sampleRate = featureRate;
|
mail@117
|
117 list.push_back(tunedlogfreqspecOutput);
|
mail@117
|
118 m_outputTunedlogfreqspec = index++;
|
matthiasm@0
|
119
|
mail@117
|
120 OutputDescriptor semitonespectrumOutput;
|
mail@117
|
121 semitonespectrumOutput.identifier = "semitonespectrum";
|
mail@117
|
122 semitonespectrumOutput.name = "Semitone Spectrum";
|
mail@117
|
123 semitonespectrumOutput.description = "A semitone-spaced log-frequency spectrum derived from the third-of-a-semitone-spaced tuned log-frequency spectrum.";
|
mail@117
|
124 semitonespectrumOutput.unit = "";
|
mail@117
|
125 semitonespectrumOutput.hasFixedBinCount = true;
|
mail@117
|
126 semitonespectrumOutput.binCount = 84;
|
mail@117
|
127 semitonespectrumOutput.hasKnownExtents = false;
|
mail@117
|
128 semitonespectrumOutput.isQuantized = false;
|
mail@117
|
129 semitonespectrumOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@117
|
130 semitonespectrumOutput.hasDuration = false;
|
Chris@164
|
131 semitonespectrumOutput.sampleRate = featureRate;
|
mail@117
|
132 list.push_back(semitonespectrumOutput);
|
mail@117
|
133 m_outputSemitonespectrum = index++;
|
matthiasm@0
|
134
|
mail@117
|
135 OutputDescriptor chromaOutput;
|
mail@117
|
136 chromaOutput.identifier = "chroma";
|
mail@117
|
137 chromaOutput.name = "Chromagram";
|
mail@117
|
138 chromaOutput.description = "Tuning-adjusted chromagram from NNLS approximate transcription, with an emphasis on the medium note range.";
|
mail@117
|
139 chromaOutput.unit = "";
|
mail@117
|
140 chromaOutput.hasFixedBinCount = true;
|
mail@117
|
141 chromaOutput.binCount = 12;
|
mail@117
|
142 chromaOutput.binNames = chromanames;
|
mail@117
|
143 chromaOutput.hasKnownExtents = false;
|
mail@117
|
144 chromaOutput.isQuantized = false;
|
mail@117
|
145 chromaOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@117
|
146 chromaOutput.hasDuration = false;
|
Chris@164
|
147 chromaOutput.sampleRate = featureRate;
|
mail@117
|
148 list.push_back(chromaOutput);
|
Chris@35
|
149 m_outputChroma = index++;
|
matthiasm@0
|
150
|
mail@116
|
151 OutputDescriptor basschromaOutput;
|
mail@116
|
152 basschromaOutput.identifier = "basschroma";
|
mail@116
|
153 basschromaOutput.name = "Bass Chromagram";
|
mail@116
|
154 basschromaOutput.description = "Tuning-adjusted bass chromagram from NNLS approximate transcription, with an emphasis on the bass note range.";
|
mail@116
|
155 basschromaOutput.unit = "";
|
mail@116
|
156 basschromaOutput.hasFixedBinCount = true;
|
mail@116
|
157 basschromaOutput.binCount = 12;
|
mail@116
|
158 basschromaOutput.binNames = chromanames;
|
mail@116
|
159 basschromaOutput.hasKnownExtents = false;
|
mail@116
|
160 basschromaOutput.isQuantized = false;
|
mail@116
|
161 basschromaOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@116
|
162 basschromaOutput.hasDuration = false;
|
Chris@164
|
163 basschromaOutput.sampleRate = featureRate;
|
mail@116
|
164 list.push_back(basschromaOutput);
|
mail@117
|
165 m_outputBasschroma = index++;
|
matthiasm@0
|
166
|
mail@116
|
167 OutputDescriptor bothchromaOutput;
|
mail@116
|
168 bothchromaOutput.identifier = "bothchroma";
|
mail@116
|
169 bothchromaOutput.name = "Chromagram and Bass Chromagram";
|
mail@116
|
170 bothchromaOutput.description = "Tuning-adjusted chromagram and bass chromagram (stacked on top of each other) from NNLS approximate transcription.";
|
mail@116
|
171 bothchromaOutput.unit = "";
|
mail@116
|
172 bothchromaOutput.hasFixedBinCount = true;
|
mail@116
|
173 bothchromaOutput.binCount = 24;
|
mail@116
|
174 bothchromaOutput.binNames = bothchromanames;
|
mail@116
|
175 bothchromaOutput.hasKnownExtents = false;
|
mail@116
|
176 bothchromaOutput.isQuantized = false;
|
mail@116
|
177 bothchromaOutput.sampleType = OutputDescriptor::FixedSampleRate;
|
mail@116
|
178 bothchromaOutput.hasDuration = false;
|
Chris@164
|
179 bothchromaOutput.sampleRate = featureRate;
|
mail@116
|
180 list.push_back(bothchromaOutput);
|
matthiasm@129
|
181 m_outputBothchroma = index++;
|
matthiasm@0
|
182 return list;
|
matthiasm@0
|
183 }
|
matthiasm@0
|
184
|
matthiasm@0
|
185
|
matthiasm@0
|
186 bool
|
matthiasm@0
|
187 NNLSChroma::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
matthiasm@0
|
188 {
|
Chris@23
|
189 if (debug_on) {
|
Chris@23
|
190 cerr << "--> initialise";
|
Chris@23
|
191 }
|
matthiasm@1
|
192
|
Chris@35
|
193 if (!NNLSBase::initialise(channels, stepSize, blockSize)) {
|
Chris@35
|
194 return false;
|
Chris@35
|
195 }
|
matthiasm@1
|
196
|
matthiasm@0
|
197 return true;
|
matthiasm@0
|
198 }
|
matthiasm@0
|
199
|
matthiasm@0
|
200 void
|
matthiasm@0
|
201 NNLSChroma::reset()
|
matthiasm@0
|
202 {
|
Chris@23
|
203 if (debug_on) cerr << "--> reset";
|
Chris@35
|
204 NNLSBase::reset();
|
matthiasm@0
|
205 }
|
matthiasm@0
|
206
|
matthiasm@0
|
207 NNLSChroma::FeatureSet
|
matthiasm@0
|
208 NNLSChroma::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
|
matthiasm@0
|
209 {
|
Chris@23
|
210 if (debug_on) cerr << "--> process" << endl;
|
Chris@35
|
211
|
Chris@35
|
212 NNLSBase::baseProcess(inputBuffers, timestamp);
|
matthiasm@0
|
213
|
Chris@23
|
214 FeatureSet fs;
|
mail@117
|
215 fs[m_outputLogfreqspec].push_back(m_logSpectrum[m_logSpectrum.size()-1]);
|
Chris@23
|
216 return fs;
|
matthiasm@0
|
217 }
|
matthiasm@0
|
218
|
matthiasm@0
|
219 NNLSChroma::FeatureSet
|
matthiasm@0
|
220 NNLSChroma::getRemainingFeatures()
|
matthiasm@0
|
221 {
|
mail@119
|
222
|
mail@119
|
223 if (debug_on) cerr << "--> getRemainingFeatures" << endl;
|
Chris@23
|
224 FeatureSet fsOut;
|
Chris@35
|
225 if (m_logSpectrum.size() == 0) return fsOut;
|
mail@119
|
226
|
Chris@23
|
227 /** Calculate Tuning
|
Chris@23
|
228 calculate tuning from (using the angle of the complex number defined by the
|
Chris@23
|
229 cumulative mean real and imag values)
|
Chris@23
|
230 **/
|
mail@80
|
231 float meanTuningImag = 0;
|
mail@80
|
232 float meanTuningReal = 0;
|
mail@80
|
233 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
mail@80
|
234 meanTuningReal += m_meanTunings[iBPS] * cosvalues[iBPS];
|
mail@80
|
235 meanTuningImag += m_meanTunings[iBPS] * sinvalues[iBPS];
|
mail@80
|
236 }
|
Chris@23
|
237 float cumulativetuning = 440 * pow(2,atan2(meanTuningImag, meanTuningReal)/(24*M_PI));
|
Chris@23
|
238 float normalisedtuning = atan2(meanTuningImag, meanTuningReal)/(2*M_PI);
|
Chris@23
|
239 int intShift = floor(normalisedtuning * 3);
|
mail@80
|
240 float floatShift = normalisedtuning * 3 - intShift; // floatShift is a really bad name for this
|
matthiasm@1
|
241
|
Chris@23
|
242 char buffer0 [50];
|
matthiasm@1
|
243
|
Chris@23
|
244 sprintf(buffer0, "estimated tuning: %0.1f Hz", cumulativetuning);
|
mail@119
|
245
|
Chris@23
|
246 /** Tune Log-Frequency Spectrogram
|
Chris@23
|
247 calculate a tuned log-frequency spectrogram (f2): use the tuning estimated above (kinda f0) to
|
Chris@23
|
248 perform linear interpolation on the existing log-frequency spectrogram (kinda f1).
|
Chris@23
|
249 **/
|
Chris@163
|
250 if (debug_on) cerr << endl << "[NNLS Chroma Plugin] Tuning Log-Frequency Spectrogram ... ";
|
matthiasm@13
|
251
|
Chris@23
|
252 float tempValue = 0;
|
matthiasm@120
|
253
|
Chris@23
|
254 int count = 0;
|
mail@77
|
255
|
matthiasm@1
|
256
|
Chris@35
|
257 for (FeatureList::iterator i = m_logSpectrum.begin(); i != m_logSpectrum.end(); ++i) {
|
Chris@23
|
258 Feature f1 = *i;
|
Chris@23
|
259 Feature f2; // tuned log-frequency spectrum
|
Chris@23
|
260 f2.hasTimestamp = true;
|
Chris@23
|
261 f2.timestamp = f1.timestamp;
|
Chris@23
|
262 f2.values.push_back(0.0); f2.values.push_back(0.0); // set lower edge to zero
|
matthiasm@1
|
263
|
matthiasm@85
|
264
|
Chris@23
|
265 if (m_tuneLocal) {
|
Chris@23
|
266 intShift = floor(m_localTuning[count] * 3);
|
mail@80
|
267 floatShift = m_localTuning[count] * 3 - intShift; // floatShift is a really bad name for this
|
Chris@23
|
268 }
|
matthiasm@1
|
269
|
mail@80
|
270 // cerr << intShift << " " << floatShift << endl;
|
matthiasm@1
|
271
|
matthiasm@122
|
272 for (int k = 2; k < (int)f1.values.size() - 3; ++k) { // interpolate all inner bins
|
mail@80
|
273 tempValue = f1.values[k + intShift] * (1-floatShift) + f1.values[k+intShift+1] * floatShift;
|
Chris@23
|
274 f2.values.push_back(tempValue);
|
Chris@23
|
275 }
|
matthiasm@1
|
276
|
Chris@23
|
277 f2.values.push_back(0.0); f2.values.push_back(0.0); f2.values.push_back(0.0); // upper edge
|
mail@77
|
278
|
Chris@23
|
279 vector<float> runningmean = SpecialConvolution(f2.values,hw);
|
Chris@23
|
280 vector<float> runningstd;
|
mail@77
|
281 for (int i = 0; i < nNote; i++) { // first step: squared values into vector (variance)
|
Chris@23
|
282 runningstd.push_back((f2.values[i] - runningmean[i]) * (f2.values[i] - runningmean[i]));
|
Chris@23
|
283 }
|
Chris@23
|
284 runningstd = SpecialConvolution(runningstd,hw); // second step convolve
|
mail@77
|
285 for (int i = 0; i < nNote; i++) {
|
Chris@23
|
286 runningstd[i] = sqrt(runningstd[i]); // square root to finally have running std
|
Chris@23
|
287 if (runningstd[i] > 0) {
|
Chris@23
|
288 // f2.values[i] = (f2.values[i] / runningmean[i]) > thresh ?
|
mail@41
|
289 // (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_whitening) : 0;
|
Chris@23
|
290 f2.values[i] = (f2.values[i] - runningmean[i]) > 0 ?
|
mail@41
|
291 (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_whitening) : 0;
|
Chris@23
|
292 }
|
Chris@23
|
293 if (f2.values[i] < 0) {
|
Chris@23
|
294 cerr << "ERROR: negative value in logfreq spectrum" << endl;
|
Chris@23
|
295 }
|
Chris@23
|
296 }
|
mail@117
|
297 fsOut[m_outputTunedlogfreqspec].push_back(f2);
|
Chris@23
|
298 count++;
|
Chris@23
|
299 }
|
Chris@163
|
300 if (debug_on) cerr << "done." << endl;
|
matthiasm@1
|
301
|
Chris@23
|
302 /** Semitone spectrum and chromagrams
|
Chris@23
|
303 Semitone-spaced log-frequency spectrum derived from the tuned log-freq spectrum above. the spectrum
|
Chris@23
|
304 is inferred using a non-negative least squares algorithm.
|
Chris@23
|
305 Three different kinds of chromagram are calculated, "treble", "bass", and "both" (which means
|
Chris@23
|
306 bass and treble stacked onto each other).
|
Chris@23
|
307 **/
|
matthiasm@42
|
308 if (m_useNNLS == 0) {
|
Chris@163
|
309 if (debug_on) cerr << "[NNLS Chroma Plugin] Mapping to semitone spectrum and chroma ... ";
|
Chris@23
|
310 } else {
|
Chris@163
|
311 if (debug_on) cerr << "[NNLS Chroma Plugin] Performing NNLS and mapping to chroma ... ";
|
Chris@23
|
312 }
|
matthiasm@13
|
313
|
matthiasm@1
|
314
|
Chris@23
|
315 vector<float> oldchroma = vector<float>(12,0);
|
Chris@23
|
316 vector<float> oldbasschroma = vector<float>(12,0);
|
Chris@23
|
317 count = 0;
|
matthiasm@9
|
318
|
mail@117
|
319 for (FeatureList::iterator it = fsOut[m_outputTunedlogfreqspec].begin(); it != fsOut[m_outputTunedlogfreqspec].end(); ++it) {
|
Chris@23
|
320 Feature f2 = *it; // logfreq spectrum
|
Chris@23
|
321 Feature f3; // semitone spectrum
|
Chris@23
|
322 Feature f4; // treble chromagram
|
Chris@23
|
323 Feature f5; // bass chromagram
|
Chris@23
|
324 Feature f6; // treble and bass chromagram
|
matthiasm@85
|
325
|
Chris@23
|
326 f3.hasTimestamp = true;
|
Chris@23
|
327 f3.timestamp = f2.timestamp;
|
matthiasm@1
|
328
|
Chris@23
|
329 f4.hasTimestamp = true;
|
Chris@23
|
330 f4.timestamp = f2.timestamp;
|
matthiasm@1
|
331
|
Chris@23
|
332 f5.hasTimestamp = true;
|
Chris@23
|
333 f5.timestamp = f2.timestamp;
|
matthiasm@1
|
334
|
Chris@23
|
335 f6.hasTimestamp = true;
|
Chris@23
|
336 f6.timestamp = f2.timestamp;
|
matthiasm@1
|
337
|
mail@77
|
338 float b[nNote];
|
matthiasm@1
|
339
|
Chris@23
|
340 bool some_b_greater_zero = false;
|
Chris@23
|
341 float sumb = 0;
|
mail@77
|
342 for (int i = 0; i < nNote; i++) {
|
mail@77
|
343 // b[i] = m_dict[(nNote * count + i) % (nNote * 84)];
|
Chris@23
|
344 b[i] = f2.values[i];
|
Chris@23
|
345 sumb += b[i];
|
Chris@23
|
346 if (b[i] > 0) {
|
Chris@23
|
347 some_b_greater_zero = true;
|
Chris@23
|
348 }
|
Chris@23
|
349 }
|
matthiasm@1
|
350
|
Chris@23
|
351 // here's where the non-negative least squares algorithm calculates the note activation x
|
matthiasm@1
|
352
|
Chris@23
|
353 vector<float> chroma = vector<float>(12, 0);
|
Chris@23
|
354 vector<float> basschroma = vector<float>(12, 0);
|
Chris@23
|
355 float currval;
|
matthiasm@122
|
356 int iSemitone = 0;
|
matthiasm@1
|
357
|
Chris@23
|
358 if (some_b_greater_zero) {
|
matthiasm@42
|
359 if (m_useNNLS == 0) {
|
matthiasm@122
|
360 for (int iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
Chris@23
|
361 currval = 0;
|
mail@80
|
362 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
mail@80
|
363 currval += b[iNote + iBPS] * (1-abs(iBPS*1.0/(nBPS/2+1)));
|
mail@80
|
364 }
|
Chris@23
|
365 f3.values.push_back(currval);
|
Chris@23
|
366 chroma[iSemitone % 12] += currval * treblewindow[iSemitone];
|
Chris@23
|
367 basschroma[iSemitone % 12] += currval * basswindow[iSemitone];
|
Chris@23
|
368 iSemitone++;
|
Chris@23
|
369 }
|
matthiasm@1
|
370
|
Chris@23
|
371 } else {
|
Chris@35
|
372 float x[84+1000];
|
Chris@23
|
373 for (int i = 1; i < 1084; ++i) x[i] = 1.0;
|
Chris@23
|
374 vector<int> signifIndex;
|
Chris@23
|
375 int index=0;
|
Chris@23
|
376 sumb /= 84.0;
|
matthiasm@122
|
377 for (int iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
Chris@23
|
378 float currval = 0;
|
mail@80
|
379 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
mail@80
|
380 currval += b[iNote + iBPS];
|
mail@80
|
381 }
|
Chris@23
|
382 if (currval > 0) signifIndex.push_back(index);
|
Chris@23
|
383 f3.values.push_back(0); // fill the values, change later
|
Chris@23
|
384 index++;
|
Chris@23
|
385 }
|
Chris@35
|
386 float rnorm;
|
Chris@35
|
387 float w[84+1000];
|
Chris@35
|
388 float zz[84+1000];
|
Chris@23
|
389 int indx[84+1000];
|
Chris@23
|
390 int mode;
|
mail@77
|
391 int dictsize = nNote*signifIndex.size();
|
Chris@23
|
392 // cerr << "dictsize is " << dictsize << "and values size" << f3.values.size()<< endl;
|
Chris@35
|
393 float *curr_dict = new float[dictsize];
|
Chris@91
|
394 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
Chris@91
|
395 for (int iBin = 0; iBin < nNote; iBin++) {
|
mail@77
|
396 curr_dict[iNote * nNote + iBin] = 1.0 * m_dict[signifIndex[iNote] * nNote + iBin];
|
Chris@23
|
397 }
|
Chris@23
|
398 }
|
Chris@35
|
399 nnls(curr_dict, nNote, nNote, signifIndex.size(), b, x, &rnorm, w, zz, indx, &mode);
|
Chris@23
|
400 delete [] curr_dict;
|
Chris@91
|
401 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
Chris@23
|
402 f3.values[signifIndex[iNote]] = x[iNote];
|
Chris@23
|
403 // cerr << mode << endl;
|
Chris@23
|
404 chroma[signifIndex[iNote] % 12] += x[iNote] * treblewindow[signifIndex[iNote]];
|
Chris@23
|
405 basschroma[signifIndex[iNote] % 12] += x[iNote] * basswindow[signifIndex[iNote]];
|
Chris@23
|
406 }
|
Chris@23
|
407 }
|
matthiasm@79
|
408 } else {
|
matthiasm@79
|
409 for (int i = 0; i < 84; ++i) f3.values.push_back(0);
|
Chris@23
|
410 }
|
matthiasm@85
|
411
|
matthiasm@129
|
412
|
Chris@23
|
413 f4.values = chroma;
|
Chris@23
|
414 f5.values = basschroma;
|
Chris@23
|
415 chroma.insert(chroma.begin(), basschroma.begin(), basschroma.end()); // just stack the both chromas
|
Chris@23
|
416 f6.values = chroma;
|
matthiasm@1
|
417
|
Chris@23
|
418 if (m_doNormalizeChroma > 0) {
|
Chris@23
|
419 vector<float> chromanorm = vector<float>(3,0);
|
Chris@23
|
420 switch (int(m_doNormalizeChroma)) {
|
Chris@23
|
421 case 0: // should never end up here
|
Chris@23
|
422 break;
|
Chris@23
|
423 case 1:
|
Chris@23
|
424 chromanorm[0] = *max_element(f4.values.begin(), f4.values.end());
|
Chris@23
|
425 chromanorm[1] = *max_element(f5.values.begin(), f5.values.end());
|
Chris@23
|
426 chromanorm[2] = max(chromanorm[0], chromanorm[1]);
|
Chris@23
|
427 break;
|
Chris@23
|
428 case 2:
|
Chris@23
|
429 for (vector<float>::iterator it = f4.values.begin(); it != f4.values.end(); ++it) {
|
Chris@23
|
430 chromanorm[0] += *it;
|
Chris@23
|
431 }
|
Chris@23
|
432 for (vector<float>::iterator it = f5.values.begin(); it != f5.values.end(); ++it) {
|
Chris@23
|
433 chromanorm[1] += *it;
|
Chris@23
|
434 }
|
Chris@23
|
435 for (vector<float>::iterator it = f6.values.begin(); it != f6.values.end(); ++it) {
|
Chris@23
|
436 chromanorm[2] += *it;
|
Chris@23
|
437 }
|
Chris@23
|
438 break;
|
Chris@23
|
439 case 3:
|
Chris@23
|
440 for (vector<float>::iterator it = f4.values.begin(); it != f4.values.end(); ++it) {
|
Chris@23
|
441 chromanorm[0] += pow(*it,2);
|
Chris@23
|
442 }
|
Chris@23
|
443 chromanorm[0] = sqrt(chromanorm[0]);
|
Chris@23
|
444 for (vector<float>::iterator it = f5.values.begin(); it != f5.values.end(); ++it) {
|
Chris@23
|
445 chromanorm[1] += pow(*it,2);
|
Chris@23
|
446 }
|
Chris@23
|
447 chromanorm[1] = sqrt(chromanorm[1]);
|
Chris@23
|
448 for (vector<float>::iterator it = f6.values.begin(); it != f6.values.end(); ++it) {
|
Chris@23
|
449 chromanorm[2] += pow(*it,2);
|
Chris@23
|
450 }
|
Chris@23
|
451 chromanorm[2] = sqrt(chromanorm[2]);
|
Chris@23
|
452 break;
|
Chris@23
|
453 }
|
Chris@23
|
454 if (chromanorm[0] > 0) {
|
matthiasm@122
|
455 for (int i = 0; i < (int)f4.values.size(); i++) {
|
Chris@23
|
456 f4.values[i] /= chromanorm[0];
|
Chris@23
|
457 }
|
Chris@23
|
458 }
|
Chris@23
|
459 if (chromanorm[1] > 0) {
|
matthiasm@122
|
460 for (int i = 0; i < (int)f5.values.size(); i++) {
|
Chris@23
|
461 f5.values[i] /= chromanorm[1];
|
Chris@23
|
462 }
|
Chris@23
|
463 }
|
Chris@23
|
464 if (chromanorm[2] > 0) {
|
matthiasm@122
|
465 for (int i = 0; i < (int)f6.values.size(); i++) {
|
Chris@23
|
466 f6.values[i] /= chromanorm[2];
|
Chris@23
|
467 }
|
Chris@23
|
468 }
|
Chris@23
|
469 }
|
matthiasm@13
|
470
|
mail@117
|
471 fsOut[m_outputSemitonespectrum].push_back(f3);
|
Chris@35
|
472 fsOut[m_outputChroma].push_back(f4);
|
mail@117
|
473 fsOut[m_outputBasschroma].push_back(f5);
|
mail@117
|
474 fsOut[m_outputBothchroma].push_back(f6);
|
Chris@23
|
475 count++;
|
Chris@23
|
476 }
|
Chris@163
|
477 if (debug_on) cerr << "done." << endl;
|
matthiasm@10
|
478
|
Chris@23
|
479 return fsOut;
|
matthiasm@0
|
480
|
matthiasm@0
|
481 }
|
matthiasm@0
|
482
|