comparison TempogramPlugin.cpp @ 20:de7213b35755

* Removed warnings of comparisons with ints and size_t
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Fri, 15 Aug 2014 15:17:28 +0100
parents e90a4797e579
children 12b952286959
comparison
equal deleted inserted replaced
19:e90a4797e579 20:de7213b35755
381 list.push_back(d3); 381 list.push_back(d3);
382 382
383 return list; 383 return list;
384 } 384 }
385 385
386 void TempogramPlugin::checkParameterValues(){
387
388 if (m_tempogramFftLength < m_tempogramWindowLength){
389 m_tempogramFftLength = m_tempogramWindowLength;
390 }
391 if (m_tempogramMinBPM > m_tempogramMaxBPM){
392 m_tempogramMinBPM = 30;
393 m_tempogramMaxBPM = 480;
394 }
395
396 float tempogramInputSampleRate = (float)m_inputSampleRate/m_inputStepSize;
397 m_tempogramMinBin = (unsigned int)(max(floor(((m_tempogramMinBPM/60)/tempogramInputSampleRate)*m_tempogramFftLength), (float)0.0));
398 m_tempogramMaxBin = (unsigned int)(min(ceil(((m_tempogramMaxBPM/60)/tempogramInputSampleRate)*m_tempogramFftLength), (float)m_tempogramFftLength/2));
399
400 if (m_tempogramMinBPM > m_cyclicTempogramMinBPM) m_cyclicTempogramMinBPM = m_tempogramMinBPM;
401 float cyclicTempogramMaxBPM = 480;
402 if (m_tempogramMaxBPM < cyclicTempogramMaxBPM) cyclicTempogramMaxBPM = m_tempogramMaxBPM;
403
404 m_cyclicTempogramNumberOfOctaves = floor(log2(cyclicTempogramMaxBPM/m_cyclicTempogramMinBPM));
405 int numberOfBinsInFirstOctave = bpmToBin(m_cyclicTempogramMinBPM);
406 if (m_cyclicTempogramOctaveDivider > numberOfBinsInFirstOctave) m_cyclicTempogramOctaveDivider = numberOfBinsInFirstOctave;
407
408 }
409
386 bool 410 bool
387 TempogramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize) 411 TempogramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
388 { 412 {
389 if (channels < getMinChannelCount() || 413 if (channels < getMinChannelCount() ||
390 channels > getMaxChannelCount()) return false; 414 channels > getMaxChannelCount()) return false;
391 415
392 // Real initialisation work goes here! 416 // Real initialisation work goes here!
393 m_inputBlockSize = blockSize; 417 m_inputBlockSize = blockSize;
394 m_inputStepSize = stepSize; 418 m_inputStepSize = stepSize;
395 419
396 m_spectrogram = SpectrogramTransposed(m_inputBlockSize/2 + 1); 420 m_spectrogram = SpectrogramTransposed(m_inputBlockSize/2.0f + 1);
397 421 checkParameterValues();
398 if (m_tempogramFftLength < m_tempogramWindowLength){
399 m_tempogramFftLength = m_tempogramWindowLength;
400 }
401 if (m_tempogramMinBPM > m_tempogramMaxBPM){
402 m_tempogramMinBPM = 30;
403 m_tempogramMaxBPM = 480;
404 }
405
406 float tempogramInputSampleRate = (float)m_inputSampleRate/m_inputStepSize;
407 m_tempogramMinBin = (unsigned int)(max(floor(((m_tempogramMinBPM/60)/tempogramInputSampleRate)*m_tempogramFftLength), (float)0.0));
408 m_tempogramMaxBin = (unsigned int)(min(ceil(((m_tempogramMaxBPM/60)/tempogramInputSampleRate)*m_tempogramFftLength), (float)m_tempogramFftLength/2));
409
410 if (m_tempogramMinBPM > m_cyclicTempogramMinBPM) m_cyclicTempogramMinBPM = m_tempogramMinBPM;
411 float cyclicTempogramMaxBPM = 480;
412 if (m_tempogramMaxBPM < cyclicTempogramMaxBPM) cyclicTempogramMaxBPM = m_tempogramMaxBPM;
413
414 m_cyclicTempogramNumberOfOctaves = floor(log2(cyclicTempogramMaxBPM/m_cyclicTempogramMinBPM));
415 int numberOfBinsInFirstOctave = bpmToBin(m_cyclicTempogramMinBPM);
416 if (m_cyclicTempogramOctaveDivider > numberOfBinsInFirstOctave) m_cyclicTempogramOctaveDivider = numberOfBinsInFirstOctave;
417
418 //cout << m_cyclicTempogramOctaveDivider << endl; 422 //cout << m_cyclicTempogramOctaveDivider << endl;
419 423
420 return true; 424 return true;
421 } 425 }
422 426
423 void 427 void
424 TempogramPlugin::reset() 428 TempogramPlugin::reset()
425 { 429 {
426 // Clear buffers, reset stored values, etc 430 // Clear buffers, reset stored values, etc
427 m_spectrogram.clear(); 431 m_spectrogram.clear();
428 m_spectrogram = SpectrogramTransposed(m_inputBlockSize/2 + 1); 432 m_spectrogram = SpectrogramTransposed(m_inputBlockSize/2.0f + 1);
433 checkParameterValues();
429 } 434 }
430 435
431 TempogramPlugin::FeatureSet 436 TempogramPlugin::FeatureSet
432 TempogramPlugin::process(const float *const *inputBuffers, Vamp::RealTime timestamp) 437 TempogramPlugin::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
433 { 438 {
437 Feature feature; 442 Feature feature;
438 443
439 const float *in = inputBuffers[0]; 444 const float *in = inputBuffers[0];
440 445
441 //calculate magnitude of FrequencyDomain input 446 //calculate magnitude of FrequencyDomain input
442 for (unsigned int i = 0; i < n; i++){ 447 for (int i = 0; i < (int)n; i++){
443 float magnitude = sqrt(in[2*i] * in[2*i] + in[2*i + 1] * in[2*i + 1]); 448 float magnitude = sqrt(in[2*i] * in[2*i] + in[2*i + 1] * in[2*i + 1]);
444 magnitude = magnitude > m_noveltyCurveMinDB ? magnitude : m_noveltyCurveMinDB; 449 magnitude = magnitude > m_noveltyCurveMinDB ? magnitude : m_noveltyCurveMinDB;
445 m_spectrogram[i].push_back(magnitude); 450 m_spectrogram[i].push_back(magnitude);
446 } 451 }
447 452
455 for (int i = 0; i < (int)ceil(m_cyclicTempogramNumberOfOctaves*m_cyclicTempogramOctaveDivider); i++){ 460 for (int i = 0; i < (int)ceil(m_cyclicTempogramNumberOfOctaves*m_cyclicTempogramOctaveDivider); i++){
456 float bpm = m_cyclicTempogramMinBPM*pow(2.0f, (float)i/m_cyclicTempogramOctaveDivider); 461 float bpm = m_cyclicTempogramMinBPM*pow(2.0f, (float)i/m_cyclicTempogramOctaveDivider);
457 int bin = bpmToBin(bpm); 462 int bin = bpmToBin(bpm);
458 463
459 logBins.push_back(bin); 464 logBins.push_back(bin);
460 cerr << bin << endl; 465 //cerr << bin << endl;
461 } 466 }
462 467
463 cerr << logBins.size() << endl; 468 //cerr << logBins.size() << endl;
464 469
465 return logBins; 470 return logBins;
466 } 471 }
467 472
468 int TempogramPlugin::bpmToBin(const float &bpm) const 473 int TempogramPlugin::bpmToBin(const float &bpm) const
470 float w = (float)bpm/60; 475 float w = (float)bpm/60;
471 float sampleRate = m_inputSampleRate/m_inputStepSize; 476 float sampleRate = m_inputSampleRate/m_inputStepSize;
472 int bin = floor((float)m_tempogramFftLength*w/sampleRate + 0.5); 477 int bin = floor((float)m_tempogramFftLength*w/sampleRate + 0.5);
473 478
474 if(bin < 0) bin = 0; 479 if(bin < 0) bin = 0;
475 else if(bin > m_tempogramFftLength/2) bin = m_tempogramFftLength; 480 else if(bin > m_tempogramFftLength/2.0f) bin = m_tempogramFftLength;
476 481
477 return bin; 482 return bin;
478 } 483 }
479 484
480 TempogramPlugin::FeatureSet 485 TempogramPlugin::FeatureSet
481 TempogramPlugin::getRemainingFeatures() 486 TempogramPlugin::getRemainingFeatures()
482 { 487 {
483 488
484 float * hannWindow = new float[m_tempogramWindowLength]; 489 float * hannWindow = new float[m_tempogramWindowLength];
485 for (unsigned int i = 0; i < m_tempogramWindowLength; i++){ 490 for (int i = 0; i < (int)m_tempogramWindowLength; i++){
486 hannWindow[i] = 0.0; 491 hannWindow[i] = 0.0;
487 } 492 }
488 493
489 FeatureSet featureSet; 494 FeatureSet featureSet;
490 495
491 //initialise novelty curve processor 496 //initialise novelty curve processor
492 size_t numberOfBlocks = m_spectrogram[0].size(); 497 size_t numberOfBlocks = m_spectrogram[0].size();
498 //cerr << numberOfBlocks << endl;
493 NoveltyCurveProcessor nc(m_inputSampleRate, m_inputBlockSize, numberOfBlocks, m_noveltyCurveCompressionConstant); 499 NoveltyCurveProcessor nc(m_inputSampleRate, m_inputBlockSize, numberOfBlocks, m_noveltyCurveCompressionConstant);
494 vector<float> noveltyCurve = nc.spectrogramToNoveltyCurve(m_spectrogram); //calculate novelty curve from magnitude data 500 vector<float> noveltyCurve = nc.spectrogramToNoveltyCurve(m_spectrogram); //calculate novelty curve from magnitude data
501 //if(noveltyCurve.size() > 50) for (int i = 0; i < 50; i++) cerr << noveltyCurve[i] << endl;
495 502
496 //push novelty curve data to featureset 1 and set timestamps 503 //push novelty curve data to featureset 1 and set timestamps
497 for (unsigned int i = 0; i < numberOfBlocks; i++){ 504 for (int i = 0; i < (int)numberOfBlocks; i++){
498 Feature noveltyCurveFeature; 505 Feature noveltyCurveFeature;
499 noveltyCurveFeature.values.push_back(noveltyCurve[i]); 506 noveltyCurveFeature.values.push_back(noveltyCurve[i]);
500 noveltyCurveFeature.hasTimestamp = false; 507 noveltyCurveFeature.hasTimestamp = false;
501 featureSet[1].push_back(noveltyCurveFeature); 508 featureSet[1].push_back(noveltyCurveFeature);
502 } 509 }
526 } 533 }
527 534
528 //Calculate cyclic tempogram 535 //Calculate cyclic tempogram
529 vector<unsigned int> logBins = calculateTempogramNearestNeighbourLogBins(); 536 vector<unsigned int> logBins = calculateTempogramNearestNeighbourLogBins();
530 537
531 assert(logBins.back() <= m_tempogramFftLength/2); 538 assert(logBins.back() <= m_tempogramFftLength/2.0f);
532 assert(logBins.size() == m_cyclicTempogramOctaveDivider*m_cyclicTempogramNumberOfOctaves); 539 assert((int)logBins.size() == m_cyclicTempogramOctaveDivider*m_cyclicTempogramNumberOfOctaves);
533 for (int block = 0; block < tempogramLength; block++){ 540 for (int block = 0; block < tempogramLength; block++){
534 Feature cyclicTempogramFeature; 541 Feature cyclicTempogramFeature;
535 542
536 for (int i = 0; i < m_cyclicTempogramOctaveDivider; i++){ 543 for (int i = 0; i < (int)m_cyclicTempogramOctaveDivider; i++){
537 float sum = 0; 544 float sum = 0;
538 for (int j = 0; j < m_cyclicTempogramNumberOfOctaves; j++){ 545 for (int j = 0; j < (int)m_cyclicTempogramNumberOfOctaves; j++){
539 sum += tempogram[block][logBins[i+j*m_cyclicTempogramOctaveDivider]]; 546 sum += tempogram[block][logBins[i+j*m_cyclicTempogramOctaveDivider]];
540 } 547 }
541 cyclicTempogramFeature.values.push_back(sum/m_cyclicTempogramNumberOfOctaves); 548 cyclicTempogramFeature.values.push_back(sum/m_cyclicTempogramNumberOfOctaves);
542 } 549 }
543 550