comparison CepstrumPitchTracker.cpp @ 28:7927e7afbe07

Simplify hypothesis implementation
author Chris Cannam
date Fri, 13 Jul 2012 21:24:22 +0100
parents e358f133e670
children afcd1f4e603c
comparison
equal deleted inserted replaced
27:e358f133e670 28:7927e7afbe07
36 using Vamp::RealTime; 36 using Vamp::RealTime;
37 37
38 CepstrumPitchTracker::Hypothesis::Hypothesis() 38 CepstrumPitchTracker::Hypothesis::Hypothesis()
39 { 39 {
40 m_state = New; 40 m_state = New;
41 m_age = 0;
42 } 41 }
43 42
44 CepstrumPitchTracker::Hypothesis::~Hypothesis() 43 CepstrumPitchTracker::Hypothesis::~Hypothesis()
45 { 44 {
46 } 45 }
66 if (cents < -80 || cents > 80) return false; 65 if (cents < -80 || cents > 80) return false;
67 66
68 return true; 67 return true;
69 } 68 }
70 69
70 bool
71 CepstrumPitchTracker::Hypothesis::isOutOfDateFor(Estimate s)
72 {
73 if (m_pending.empty()) return false;
74
75 return ((s.time - m_pending[m_pending.size()-1].time) >
76 RealTime::fromMilliseconds(40));
77 }
78
71 bool 79 bool
72 CepstrumPitchTracker::Hypothesis::isSatisfied() 80 CepstrumPitchTracker::Hypothesis::isSatisfied()
73 { 81 {
74 if (m_pending.empty()) return false; 82 if (m_pending.empty()) return false;
75 83
86 std::cerr << "meanConfidence = " << meanConfidence << ", lengthRequired = " << lengthRequired << ", length = " << m_pending.size() << std::endl; 94 std::cerr << "meanConfidence = " << meanConfidence << ", lengthRequired = " << lengthRequired << ", length = " << m_pending.size() << std::endl;
87 95
88 return (m_pending.size() > lengthRequired); 96 return (m_pending.size() > lengthRequired);
89 } 97 }
90 98
91 void
92 CepstrumPitchTracker::Hypothesis::advanceTime()
93 {
94 ++m_age;
95 }
96
97 bool 99 bool
98 CepstrumPitchTracker::Hypothesis::test(Estimate s) 100 CepstrumPitchTracker::Hypothesis::accept(Estimate s)
99 { 101 {
100 bool accept = false; 102 bool accept = false;
101 103
102 switch (m_state) { 104 switch (m_state) {
103 105
105 m_state = Provisional; 107 m_state = Provisional;
106 accept = true; 108 accept = true;
107 break; 109 break;
108 110
109 case Provisional: 111 case Provisional:
110 if (m_age > 3) { 112 if (isOutOfDateFor(s)) {
111 m_state = Rejected; 113 m_state = Rejected;
112 } else if (isWithinTolerance(s)) { 114 } else if (isWithinTolerance(s)) {
113 accept = true; 115 accept = true;
114 } 116 }
115 break; 117 break;
116 118
117 case Satisfied: 119 case Satisfied:
118 if (m_age > 3) { 120 if (isOutOfDateFor(s)) {
119 m_state = Expired; 121 m_state = Expired;
120 } else if (isWithinTolerance(s)) { 122 } else if (isWithinTolerance(s)) {
121 accept = true; 123 accept = true;
122 } 124 }
123 break; 125 break;
129 break; 131 break;
130 } 132 }
131 133
132 if (accept) { 134 if (accept) {
133 m_pending.push_back(s); 135 m_pending.push_back(s);
134 m_age = 0;
135 if (m_state == Provisional && isSatisfied()) { 136 if (m_state == Provisional && isSatisfied()) {
136 m_state = Satisfied; 137 m_state = Satisfied;
137 } 138 }
138 } 139 }
139 140
140 return accept && (m_state == Satisfied); 141 return accept;
141 } 142 }
142 143
143 CepstrumPitchTracker::Hypothesis::State 144 CepstrumPitchTracker::Hypothesis::State
144 CepstrumPitchTracker::Hypothesis::getState() 145 CepstrumPitchTracker::Hypothesis::getState()
145 { 146 {
580 Hypothesis::Estimate e; 581 Hypothesis::Estimate e;
581 e.freq = peakfreq; 582 e.freq = peakfreq;
582 e.time = timestamp; 583 e.time = timestamp;
583 e.confidence = confidence; 584 e.confidence = confidence;
584 585
585 m_accepted.advanceTime(); 586 // m_good.advanceTime();
586
587 for (int i = 0; i < m_possible.size(); ++i) { 587 for (int i = 0; i < m_possible.size(); ++i) {
588 m_possible[i].advanceTime(); 588 // m_possible[i].advanceTime();
589 } 589 }
590 590
591 if (!m_accepted.test(e)) { 591 if (!m_good.accept(e)) {
592 592
593 int candidate = -1; 593 int candidate = -1;
594 bool accepted = false; 594 bool accepted = false;
595 595
596 for (int i = 0; i < m_possible.size(); ++i) { 596 for (int i = 0; i < m_possible.size(); ++i) {
597 if (m_possible[i].test(e)) { 597 if (m_possible[i].accept(e)) {
598 accepted = true;
599 if (m_possible[i].getState() == Hypothesis::Satisfied) { 598 if (m_possible[i].getState() == Hypothesis::Satisfied) {
599 accepted = true;
600 candidate = i; 600 candidate = i;
601 } 601 }
602 break; 602 break;
603 } 603 }
604 } 604 }
605 605
606 if (!accepted) { 606 if (!accepted) {
607 Hypothesis h; 607 Hypothesis h;
608 h.test(e); //!!! must succeed as h is new, so perhaps there should be a ctor for this 608 h.accept(e); //!!! must succeed as h is new, so perhaps there should be a ctor for this
609 m_possible.push_back(h); 609 m_possible.push_back(h);
610 } 610 }
611 611
612 if (m_accepted.getState() == Hypothesis::Expired) { 612 if (m_good.getState() == Hypothesis::Expired) {
613 m_accepted.addFeatures(fs); 613 m_good.addFeatures(fs);
614 } 614 }
615 615
616 if (m_accepted.getState() == Hypothesis::Expired || 616 if (m_good.getState() == Hypothesis::Expired ||
617 m_accepted.getState() == Hypothesis::Rejected) { 617 m_good.getState() == Hypothesis::Rejected) {
618 if (candidate >= 0) { 618 if (candidate >= 0) {
619 m_accepted = m_possible[candidate]; 619 m_good = m_possible[candidate];
620 } else { 620 } else {
621 m_accepted = Hypothesis(); 621 m_good = Hypothesis();
622 } 622 }
623 } 623 }
624 624
625 // reap rejected/expired hypotheses from possible list 625 // reap rejected/expired hypotheses from possible list
626 Hypotheses toReap = m_possible; 626 Hypotheses toReap = m_possible;
632 m_possible.push_back(h); 632 m_possible.push_back(h);
633 } 633 }
634 } 634 }
635 } 635 }
636 636
637 std::cerr << "accepted length = " << m_accepted.getPendingLength() 637 std::cerr << "accepted length = " << m_good.getPendingLength()
638 << ", state = " << m_accepted.getState() 638 << ", state = " << m_good.getState()
639 << ", hypothesis count = " << m_possible.size() << std::endl; 639 << ", hypothesis count = " << m_possible.size() << std::endl;
640 640
641 delete[] data; 641 delete[] data;
642 return fs; 642 return fs;
643 } 643 }
644 644
645 CepstrumPitchTracker::FeatureSet 645 CepstrumPitchTracker::FeatureSet
646 CepstrumPitchTracker::getRemainingFeatures() 646 CepstrumPitchTracker::getRemainingFeatures()
647 { 647 {
648 FeatureSet fs; 648 FeatureSet fs;
649 if (m_accepted.getState() == Hypothesis::Satisfied) { 649 if (m_good.getState() == Hypothesis::Satisfied) {
650 m_accepted.addFeatures(fs); 650 m_good.addFeatures(fs);
651 } 651 }
652 return fs; 652 return fs;
653 } 653 }