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