Mercurial > hg > nnls-chroma
comparison chromamethods.cpp @ 91:b56dde3417d4 matthiasm-plugin
* Fix the "comparison between signed and unsigned" warnings; remove some ifdef'd-out old code
author | Chris Cannam |
---|---|
date | Thu, 02 Dec 2010 13:05:23 +0000 |
parents | b095d83585c9 |
children | dab7e7bfeba1 |
comparison
equal
deleted
inserted
replaced
90:b095d83585c9 | 91:b56dde3417d4 |
---|---|
117 } | 117 } |
118 float fft_width = fs * 2.0 / blocksize; | 118 float fft_width = fs * 2.0 / blocksize; |
119 | 119 |
120 // linear oversampled frequency vector | 120 // linear oversampled frequency vector |
121 vector<float> oversampled_f; | 121 vector<float> oversampled_f; |
122 for (unsigned int i = 0; i < oversampling * blocksize/2; ++i) { | 122 for (int i = 0; i < oversampling * blocksize/2; ++i) { |
123 oversampled_f.push_back(i * ((fs * 1.0 / blocksize) / oversampling)); | 123 oversampled_f.push_back(i * ((fs * 1.0 / blocksize) / oversampling)); |
124 } | 124 } |
125 | 125 |
126 // pitch-spaced frequency vector | 126 // pitch-spaced frequency vector |
127 int minMIDI = 21 + minoctave * 12 - 1; // this includes one additional semitone! | 127 int minMIDI = 21 + minoctave * 12 - 1; // this includes one additional semitone! |
193 | 193 |
194 float curr_f; | 194 float curr_f; |
195 float floatbin; | 195 float floatbin; |
196 float curr_amp; | 196 float curr_amp; |
197 // now for every combination calculate the matrix element | 197 // now for every combination calculate the matrix element |
198 for (unsigned iOut = 0; iOut < 12 * (maxoctave - minoctave); ++iOut) { | 198 for (int iOut = 0; iOut < 12 * (maxoctave - minoctave); ++iOut) { |
199 // cerr << iOut << endl; | 199 // cerr << iOut << endl; |
200 for (unsigned iHarm = 1; iHarm <= 20; ++iHarm) { | 200 for (int iHarm = 1; iHarm <= 20; ++iHarm) { |
201 curr_f = 440 * pow(2,(minMIDI-69+iOut)*1.0/12) * iHarm; | 201 curr_f = 440 * pow(2,(minMIDI-69+iOut)*1.0/12) * iHarm; |
202 // if (curr_f > cq_f[nNote-1]) break; | 202 // if (curr_f > cq_f[nNote-1]) break; |
203 floatbin = ((iOut + 1) * binspersemitone + 1) + binspersemitone * 12 * log2(iHarm); | 203 floatbin = ((iOut + 1) * binspersemitone + 1) + binspersemitone * 12 * log2(iHarm); |
204 // cerr << floatbin << endl; | 204 // cerr << floatbin << endl; |
205 curr_amp = pow(s_param,float(iHarm-1)); | 205 curr_amp = pow(s_param,float(iHarm-1)); |
206 // cerr << "curramp" << curr_amp << endl; | 206 // cerr << "curramp" << curr_amp << endl; |
207 for (unsigned iNote = 0; iNote < nNote; ++iNote) { | 207 for (int iNote = 0; iNote < nNote; ++iNote) { |
208 if (abs(iNote+1.0-floatbin)<2) { | 208 if (abs(iNote+1.0-floatbin)<2) { |
209 dm[iNote + nNote * iOut] += cospuls(iNote+1.0, floatbin, binspersemitone + 0.0) * curr_amp; | 209 dm[iNote + nNote * iOut] += cospuls(iNote+1.0, floatbin, binspersemitone + 0.0) * curr_amp; |
210 // dm[iNote + nNote * iOut] += 1 * curr_amp; | 210 // dm[iNote + nNote * iOut] += 1 * curr_amp; |
211 } | 211 } |
212 } | 212 } |
340 | 340 |
341 vector<string> ppath = getPluginPath(); | 341 vector<string> ppath = getPluginPath(); |
342 | 342 |
343 bool hasExternalDictinoary = true; | 343 bool hasExternalDictinoary = true; |
344 | 344 |
345 for (int i = 0; i < ppath.size(); ++i) { | 345 for (size_t i = 0; i < ppath.size(); ++i) { |
346 chordDictFilename = ppath[i] + "/" + chordDictBase; | 346 chordDictFilename = ppath[i] + "/" + chordDictBase; |
347 cerr << "Looking for chord.dict in " << chordDictFilename << "..." ; | 347 cerr << "Looking for chord.dict in " << chordDictFilename << "..." ; |
348 fstream fin; | 348 fstream fin; |
349 fin.open(chordDictFilename.c_str(),ios::in); | 349 fin.open(chordDictFilename.c_str(),ios::in); |
350 if( fin.is_open() ) | 350 if( fin.is_open() ) |
351 { | 351 { |
352 fin.close(); | 352 fin.close(); |
353 cerr << " success." << endl; | 353 cerr << " success." << endl; |
354 break; | 354 break; |
355 } else { | 355 } else { |
356 if (i < ppath.size()-1) cerr << " (not found yet) ..." << endl; | 356 if (i+1 < ppath.size()) cerr << " (not found yet) ..." << endl; |
357 else { | 357 else { |
358 cerr << "* ERROR: failed to find chord dictionary." << endl; | 358 cerr << "* ERROR: failed to find chord dictionary." << endl; |
359 hasExternalDictinoary = false; | 359 hasExternalDictinoary = false; |
360 } | 360 } |
361 } | 361 } |
364 iostreams::stream<iostreams::file_source> chordDictFile(chordDictFilename); | 364 iostreams::stream<iostreams::file_source> chordDictFile(chordDictFilename); |
365 string line; | 365 string line; |
366 // int iElement = 0; | 366 // int iElement = 0; |
367 int nChord = 0; | 367 int nChord = 0; |
368 | 368 |
369 vector<string> tempChordNames = staticChordnames(); | 369 vector<string> tempChordNames = staticChordnames(); |
370 vector<float> tempChordDict = staticChordvalues();; | 370 vector<float> tempChordDict = staticChordvalues(); |
371 vector<string> loadedChordNames; | 371 vector<string> loadedChordNames; |
372 vector<float> loadedChordDict; | 372 vector<float> loadedChordDict; |
373 if (hasExternalDictinoary && chordDictFile.is_open()) { | 373 if (hasExternalDictinoary && chordDictFile.is_open()) { |
374 cerr << "-----------------> " << tempChordNames.size() << endl; | 374 cerr << "-----------------> " << tempChordNames.size() << endl; |
375 tempChordDict.clear(); | 375 tempChordDict.clear(); |
398 tempChordNames.push_back(chordType); | 398 tempChordNames.push_back(chordType); |
399 } | 399 } |
400 } | 400 } |
401 } | 401 } |
402 | 402 |
403 | |
404 | 403 |
405 for (int iType = 0; iType < tempChordNames.size(); ++iType) { | 404 for (int iType = 0; iType < (int)tempChordNames.size(); ++iType) { |
406 // now make all 12 chords of every type | 405 // now make all 12 chords of every type |
407 for (unsigned iSemitone = 0; iSemitone < 12; iSemitone++) { | 406 for (int iSemitone = 0; iSemitone < 12; iSemitone++) { |
408 vector<int> tempchordnotes; | 407 vector<int> tempchordnotes; |
409 // add bass slash notation | 408 // add bass slash notation |
410 string slashNotation = ""; | 409 string slashNotation = ""; |
411 for (unsigned kSemitone = 1; kSemitone < 12; kSemitone++) { | 410 for (int kSemitone = 1; kSemitone < 12; kSemitone++) { |
412 if (tempChordDict[24*iType+(kSemitone) % 12] > 0.99) { | 411 if (tempChordDict[24*iType+(kSemitone) % 12] > 0.99) { |
413 slashNotation = bassnames[iSemitone][kSemitone]; | 412 slashNotation = bassnames[iSemitone][kSemitone]; |
414 } | 413 } |
415 } | 414 } |
416 if (slashNotation=="") tempchordnotes.push_back(MIDI_basenote + (iSemitone+12) % 12); | 415 if (slashNotation=="") tempchordnotes.push_back(MIDI_basenote + (iSemitone+12) % 12); |
417 for (unsigned kSemitone = 0; kSemitone < 12; kSemitone++) { // bass pitch classes | 416 for (int kSemitone = 0; kSemitone < 12; kSemitone++) { // bass pitch classes |
418 // cerr << ((kSemitone - iSemitone + 12) % 12) << endl; | 417 // cerr << ((kSemitone - iSemitone + 12) % 12) << endl; |
419 float bassValue = 0; | 418 float bassValue = 0; |
420 if (tempChordDict[24*iType+(kSemitone - iSemitone + 12) % 12]==1) { | 419 if (tempChordDict[24*iType+(kSemitone - iSemitone + 12) % 12]==1) { |
421 bassValue = 1; | 420 bassValue = 1; |
422 tempchordnotes.push_back(MIDI_basenote + (kSemitone+12) % 12); | 421 tempchordnotes.push_back(MIDI_basenote + (kSemitone+12) % 12); |
423 } else { | 422 } else { |
424 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] == 1) bassValue = 0.5; | 423 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] == 1) bassValue = 0.5; |
425 } | 424 } |
426 loadedChordDict.push_back(bassValue); | 425 loadedChordDict.push_back(bassValue); |
427 } | 426 } |
428 for (unsigned kSemitone = 0; kSemitone < 12; kSemitone++) { // chord pitch classes | 427 for (int kSemitone = 0; kSemitone < 12; kSemitone++) { // chord pitch classes |
429 loadedChordDict.push_back(tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12]); | 428 loadedChordDict.push_back(tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12]); |
430 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] > 0) tempchordnotes.push_back(MIDI_basenote + (kSemitone+12+6) % 12 - 6 + 24); | 429 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] > 0) tempchordnotes.push_back(MIDI_basenote + (kSemitone+12+6) % 12 - 6 + 24); |
431 } | 430 } |
432 ostringstream os; | 431 ostringstream os; |
433 if (slashNotation.empty()) { | 432 if (slashNotation.empty()) { |
454 vector<int> tempchordvector; | 453 vector<int> tempchordvector; |
455 m_chordnotes->push_back(tempchordvector); | 454 m_chordnotes->push_back(tempchordvector); |
456 float exponent = 2.0; | 455 float exponent = 2.0; |
457 // float m_boostN = 1.1; | 456 // float m_boostN = 1.1; |
458 // cerr << " N BOOST : " << boostN << endl << endl; | 457 // cerr << " N BOOST : " << boostN << endl << endl; |
459 for (int iChord = 0; iChord < loadedChordDict.size()/24; iChord++) { | 458 for (int iChord = 0; iChord < (int)loadedChordDict.size()/24; iChord++) { |
460 float sum = 0; | 459 float sum = 0; |
461 float stand = 0; | 460 float stand = 0; |
462 for (int iST = 0; iST < 24; ++iST) { | 461 for (int iST = 0; iST < 24; ++iST) { |
463 sum += loadedChordDict[24 * iChord + iST]; | 462 sum += loadedChordDict[24 * iChord + iST]; |
464 } | 463 } |
465 for (int iST = 0; iST < 24; ++iST) { | 464 for (int iST = 0; iST < 24; ++iST) { |
466 // loadedChordDict[24 * iChord + iST] -= sum/24; | 465 // loadedChordDict[24 * iChord + iST] -= sum/24; |
467 stand += pow(abs(loadedChordDict[24 * iChord + iST]),exponent)/24; | 466 stand += pow(abs(loadedChordDict[24 * iChord + iST]),exponent)/24; |
468 } | 467 } |
469 if (iChord < loadedChordDict.size()/24 - 1) { | 468 if (iChord < (int)loadedChordDict.size()/24 - 1) { |
470 stand = pow(stand,(float)1.0/exponent); | 469 stand = powf(stand,1.0f/exponent); |
471 } else { | 470 } else { |
472 stand = pow(stand,(float)1.0/exponent) / boostN; | 471 stand = powf(stand,1.0f/exponent) / boostN; |
473 } | 472 } |
474 for (int iST = 0; iST < 24; ++iST) { | 473 for (int iST = 0; iST < 24; ++iST) { |
475 loadedChordDict[24 * iChord + iST] /= stand; | 474 loadedChordDict[24 * iChord + iST] /= stand; |
476 } | 475 } |
477 | 476 |
478 } | 477 } |
479 | 478 |
480 | 479 |
481 | 480 |
482 nChord = 0; | 481 nChord = 0; |
483 for (int i = 0; i < loadedChordNames.size(); i++) { | 482 for (int i = 0; i < (int)loadedChordNames.size(); i++) { |
484 nChord++; | 483 nChord++; |
485 } | 484 } |
486 chordDictFile.close(); | 485 chordDictFile.close(); |
487 | 486 |
488 | 487 |