comparison src/Silvet.cpp @ 311:99af9557dfc1

Pull out applyEM function
author Chris Cannam
date Tue, 28 Apr 2015 09:41:40 +0100
parents 07ee4ebea57c
children 796d403dc83b
comparison
equal deleted inserted replaced
310:37d2d04d94db 311:99af9557dfc1
28 28
29 using std::vector; 29 using std::vector;
30 using std::cout; 30 using std::cout;
31 using std::cerr; 31 using std::cerr;
32 using std::endl; 32 using std::endl;
33 using std::pair;
33 using Vamp::RealTime; 34 using Vamp::RealTime;
34 35
35 static int processingSampleRate = 44100; 36 static int processingSampleRate = 44100;
36 static int processingBPO = 60; 37 static int processingBPO = 60;
37 38
539 fs[m_fcqOutputNo].push_back(f); 540 fs[m_fcqOutputNo].push_back(f);
540 } 541 }
541 542
542 int width = filtered.size(); 543 int width = filtered.size();
543 544
544 int iterations = m_hqMode ? 20 : 10; 545 Grid localPitches(width);
545
546 Grid localPitches(width, vector<double>(pack.templateNoteCount, 0.0));
547 546
548 bool wantShifts = m_hqMode && m_fineTuning; 547 bool wantShifts = m_hqMode && m_fineTuning;
549 int shiftCount = 1; 548 int shiftCount = 1;
550 if (wantShifts) { 549 if (wantShifts) {
551 shiftCount = pack.templateMaxShift * 2 + 1; 550 shiftCount = pack.templateMaxShift * 2 + 1;
552 } 551 }
553 552
554 vector<vector<int> > localBestShifts; 553 vector<vector<int> > localBestShifts;
555 if (wantShifts) { 554 if (wantShifts) {
556 localBestShifts = 555 localBestShifts = vector<vector<int> >(width);
557 vector<vector<int> >(width, vector<int>(pack.templateNoteCount, 0)); 556 }
558 } 557
559
560 double columnThreshold = 1e-5;
561
562 #pragma omp parallel for
563 for (int i = 0; i < width; ++i) { 558 for (int i = 0; i < width; ++i) {
564 559 auto out = applyEM(pack, filtered.at(i), wantShifts);
565 double sum = 0.0; 560 localPitches[i] = out.first;
566 for (int j = 0; j < pack.templateHeight; ++j) { 561 if (wantShifts) localBestShifts[i] = out.second;
567 sum += filtered.at(i).at(j);
568 }
569 if (sum < columnThreshold) continue;
570
571 EM em(&pack, m_hqMode);
572
573 em.setPitchSparsity(pack.pitchSparsity);
574 em.setSourceSparsity(pack.sourceSparsity);
575
576 for (int j = 0; j < iterations; ++j) {
577 em.iterate(filtered.at(i).data());
578 }
579
580 const float *pitchDist = em.getPitchDistribution();
581 const float *const *shiftDist = em.getShifts();
582
583 for (int j = 0; j < pack.templateNoteCount; ++j) {
584
585 localPitches[i][j] = pitchDist[j] * sum;
586
587 int bestShift = 0;
588 float bestShiftValue = 0.0;
589 if (wantShifts) {
590 for (int k = 0; k < shiftCount; ++k) {
591 float value = shiftDist[k][j];
592 if (k == 0 || value > bestShiftValue) {
593 bestShiftValue = value;
594 bestShift = k;
595 }
596 }
597 localBestShifts[i][j] = bestShift;
598 }
599 }
600 } 562 }
601 563
602 for (int i = 0; i < width; ++i) { 564 for (int i = 0; i < width; ++i) {
603 565
604 // This returns a filtered column, and pushes the 566 // This returns a filtered column, and pushes the
631 fs[m_notesOutputNo].push_back(*fi); 593 fs[m_notesOutputNo].push_back(*fi);
632 } 594 }
633 } 595 }
634 596
635 return fs; 597 return fs;
598 }
599
600 pair<vector<double>, vector<int> >
601 Silvet::applyEM(const InstrumentPack &pack,
602 const vector<double> &column,
603 bool wantShifts)
604 {
605 double columnThreshold = 1e-5;
606
607 vector<double> pitches(pack.templateNoteCount, 0.0);
608 vector<int> bestShifts;
609
610 double sum = 0.0;
611 for (int j = 0; j < pack.templateHeight; ++j) {
612 sum += column.at(j);
613 }
614 if (sum < columnThreshold) return { pitches, bestShifts };
615
616 EM em(&pack, m_hqMode);
617
618 em.setPitchSparsity(pack.pitchSparsity);
619 em.setSourceSparsity(pack.sourceSparsity);
620
621 int iterations = m_hqMode ? 20 : 10;
622
623 for (int j = 0; j < iterations; ++j) {
624 em.iterate(column.data());
625 }
626
627 const float *pitchDist = em.getPitchDistribution();
628 const float *const *shiftDist = em.getShifts();
629
630 int shiftCount = 1;
631 if (wantShifts) {
632 shiftCount = pack.templateMaxShift * 2 + 1;
633 }
634
635 for (int j = 0; j < pack.templateNoteCount; ++j) {
636
637 pitches[j] = pitchDist[j] * sum;
638
639 int bestShift = 0;
640 float bestShiftValue = 0.0;
641 if (wantShifts) {
642 for (int k = 0; k < shiftCount; ++k) {
643 float value = shiftDist[k][j];
644 if (k == 0 || value > bestShiftValue) {
645 bestShiftValue = value;
646 bestShift = k;
647 }
648 }
649 bestShifts.push_back(bestShift);
650 }
651 }
652
653 return { pitches, bestShifts };
636 } 654 }
637 655
638 Silvet::Grid 656 Silvet::Grid
639 Silvet::preProcess(const Grid &in) 657 Silvet::preProcess(const Grid &in)
640 { 658 {