changeset 54:751b43d119cf

Fix feeder (and its tests!) so they now pass
author Chris Cannam
date Thu, 27 Sep 2012 15:20:47 +0100
parents 19c8c6ca4406
children b32290646213
files AgentFeeder.cpp AgentFeeder.h Makefile.osx test/TestAgentFeeder.cpp
diffstat 4 files changed, 133 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/AgentFeeder.cpp	Wed Sep 26 17:14:03 2012 +0100
+++ b/AgentFeeder.cpp	Thu Sep 27 15:20:47 2012 +0100
@@ -26,71 +26,83 @@
 
 void AgentFeeder::feed(NoteHypothesis::Estimate e)
 {
-    if (!m_current.accept(e)) {
+    if (m_haveCurrent) {
+        if (m_current.accept(e)) {
+            return;
+        }
+        if (m_current.getState() == NoteHypothesis::Expired) {
+            m_accepted.push_back(m_current);
+            m_haveCurrent = false;
+        }
+    }
 
-	if (m_current.getState() == NoteHypothesis::Expired) {
-	    m_accepted.push_back(m_current);
-	}
+    bool swallowed = false;
 
-	bool swallowed = false;
+    Hypotheses newCandidates;
 
-	Hypotheses newCandidates;
-
-	for (typename Hypotheses::iterator i = m_candidates.begin();
-	     i != m_candidates.end(); ++i) {
-
-	    NoteHypothesis h = *i;
+    for (Hypotheses::iterator i = m_candidates.begin();
+         i != m_candidates.end(); ++i) {
+        
+        NoteHypothesis h = *i;
+        
+        if (swallowed) {
+            
+            // don't offer: each observation can only belong to one
+            // satisfied hypothesis
+            newCandidates.push_back(h);
+            
+        } else {
+            
+            if (h.accept(e)) {
                 
-	    if (swallowed) {
-
-		// don't offer: each observation can only belong to one
-		// satisfied hypothesis
-		newCandidates.push_back(h);
-
-	    } else {
-
-		if (h.accept(e)) {
-
-		    if (h.getState() == NoteHypothesis::Satisfied) {
-
-			swallowed = true;
-        
-			if (m_current.getState() == NoteHypothesis::Expired ||
-			    m_current.getState() == NoteHypothesis::Rejected) {
-			    m_current = h;
-			} else {
-			    newCandidates.push_back(h);
-			}
-                            
-		    } else {
-			newCandidates.push_back(h);
-		    }
-		}
-	    }
-	}
-            
-	if (!swallowed) {
-	    NoteHypothesis h;
-	    h.accept(e); // must succeed, as h is new
-	    newCandidates.push_back(h);
-	}
-
-	// reap rejected/expired hypotheses from candidates list,
-	// and assign back to m_candidates
-
-	m_candidates.clear();
-
-	for (typename Hypotheses::const_iterator i = newCandidates.begin();
-	     i != newCandidates.end(); ++i) {
-	    NoteHypothesis h = *i;
-	    if (h.getState() != NoteHypothesis::Rejected && 
-		h.getState() != NoteHypothesis::Expired) {
-		m_candidates.push_back(h);
-	    }
-	}
-    }  
+                if (h.getState() == NoteHypothesis::Satisfied) {
+                    
+                    swallowed = true;
+                    
+                    if (!m_haveCurrent ||
+                        m_current.getState() == NoteHypothesis::Expired ||
+                        m_current.getState() == NoteHypothesis::Rejected) {
+                        m_current = h;
+                        m_haveCurrent = true;
+                    } else {
+                        newCandidates.push_back(h);
+                    }
+                    
+                } else {
+                    newCandidates.push_back(h);
+                }
+            }
+        }
+    }
+    
+    if (!swallowed) {
+        NoteHypothesis h;
+        if (h.accept(e)) {
+            newCandidates.push_back(h);
+        }
+    }
+    
+    m_candidates = reap(newCandidates);
 }
 
+AgentFeeder::Hypotheses
+AgentFeeder::reap(Hypotheses candidates)
+{
+    // reap rejected/expired hypotheses from list of candidates
+
+    Hypotheses survived;
+    for (Hypotheses::const_iterator i = candidates.begin();
+         i != candidates.end(); ++i) {
+        NoteHypothesis h = *i;
+        if (h.getState() != NoteHypothesis::Rejected && 
+            h.getState() != NoteHypothesis::Expired) {
+            survived.push_back(h);
+        }
+    }
+
+    return survived;
+}
+    
 void
 AgentFeeder::finish()
 {
--- a/AgentFeeder.h	Wed Sep 26 17:14:03 2012 +0100
+++ b/AgentFeeder.h	Thu Sep 27 15:20:47 2012 +0100
@@ -49,7 +49,7 @@
 class AgentFeeder
 {
 public:
-    AgentFeeder() { }
+    AgentFeeder() : m_haveCurrent(false) { }
 
     void feed(NoteHypothesis::Estimate);
     void finish();
@@ -60,9 +60,12 @@
         return m_accepted;
     }
 
+    Hypotheses reap(Hypotheses);
+
 private:
     Hypotheses m_candidates;
     NoteHypothesis m_current;
+    bool m_haveCurrent;
     Hypotheses m_accepted;
 };
 
--- a/Makefile.osx	Wed Sep 26 17:14:03 2012 +0100
+++ b/Makefile.osx	Thu Sep 27 15:20:47 2012 +0100
@@ -1,9 +1,9 @@
 
-CFLAGS := -I../inst/include -Wall -g -fPIC 
+CFLAGS := -I../inst/include -I/usr/local/boost -Wall -g -fPIC 
 CXXFLAGS := $(CFLAGS)
 
-LDFLAGS := -dynamiclib -L../inst/lib -lvamp-sdk
-
+LDFLAGS := -L../inst/lib -lvamp-sdk -L/usr/local/boost/stage/lib 
+PLUGIN_LDFLAGS := -dynamiclib
 PLUGIN_EXT := .dylib
 
 include Makefile.inc
--- a/test/TestAgentFeeder.cpp	Wed Sep 26 17:14:03 2012 +0100
+++ b/test/TestAgentFeeder.cpp	Thu Sep 27 15:20:47 2012 +0100
@@ -31,6 +31,8 @@
 
 static Vamp::RealTime ms(int n) { return Vamp::RealTime::fromMilliseconds(n); }
 
+static const int low = 500, high = 700;
+
 typedef NoteHypothesis::Estimate Est;
 
 BOOST_AUTO_TEST_SUITE(TestAgentFeeder)
@@ -45,10 +47,10 @@
 
 BOOST_AUTO_TEST_CASE(feederSingle)
 {
-    Est e0(1, ms(0), 1);
-    Est e10(1, ms(10), 1);
-    Est e20(1, ms(20), 1);
-    Est e30(1, ms(30), 1);
+    Est e0(low, ms(0), 1);
+    Est e10(low, ms(10), 1);
+    Est e20(low, ms(20), 1);
+    Est e30(low, ms(30), 1);
 
     AgentFeeder f;
     f.feed(e0);
@@ -64,15 +66,15 @@
 
 BOOST_AUTO_TEST_CASE(feederPairSeparate)
 {
-    Est e0(1, ms(0), 1);
-    Est e10(1, ms(10), 1);
-    Est e20(1, ms(20), 1);
-    Est e30(1, ms(30), 1);
+    Est e0(low, ms(0), 1);
+    Est e10(low, ms(10), 1);
+    Est e20(low, ms(20), 1);
+    Est e30(low, ms(30), 1);
 
-    Est f0(3, ms(2000), 1);
-    Est f10(3, ms(2010), 1);
-    Est f20(3, ms(2020), 1);
-    Est f30(3, ms(2030), 1);
+    Est f0(high, ms(2000), 1);
+    Est f10(high, ms(2010), 1);
+    Est f20(high, ms(2020), 1);
+    Est f30(high, ms(2030), 1);
 
     AgentFeeder f;
     f.feed(e0);
@@ -98,22 +100,23 @@
 
     // (With fffffff stopping before eeee has expired.)
 
-    // This should give us one hypothesis, eeee.
+    // This should give us one hypothesis, eeee, because eeee is still
+    // the current hypothesis by the time fffffff ends.
 
-    Est e0(1, ms(0), 1);
-    Est e10(1, ms(10), 1);
+    Est e0(low, ms(0), 1);
+    Est e10(low, ms(10), 1);
 
-    Est e20(1, ms(20), 1);
-    Est f20(3, ms(20), 1);
+    Est e20(low, ms(20), 1);
+    Est f20(high, ms(20), 1);
 
-    Est e30(1, ms(30), 1);
-    Est f30(3, ms(30), 1);
+    Est e30(low, ms(30), 1);
+    Est f30(high, ms(30), 1);
 
-    Est f40(3, ms(40), 1);
-    Est f50(3, ms(50), 1);
-    Est f60(3, ms(60), 1);
-    Est f70(3, ms(70), 1);
-    Est f80(3, ms(80), 1);
+    Est f40(high, ms(40), 1);
+    Est f41(high, ms(41), 1);
+    Est f42(high, ms(42), 1);
+    Est f43(high, ms(43), 1);
+    Est f44(high, ms(44), 1);
 
     AgentFeeder f;
     f.feed(e0);
@@ -123,10 +126,10 @@
     f.feed(e30);
     f.feed(f30);
     f.feed(f40);
-    f.feed(f50);
-    f.feed(f60);
-    f.feed(f70);
-    f.feed(f80);
+    f.feed(f41);
+    f.feed(f42);
+    f.feed(f43);
+    f.feed(f44);
     f.finish();
 
     AgentFeeder::Hypotheses accepted = f.getAcceptedHypotheses();
@@ -153,20 +156,20 @@
     // hypothesis ends, the fffffff one should replace it. So,
     // both should be recognised.
 
-    Est e0(1, ms(0), 1);
-    Est e10(1, ms(500), 1);
+    Est e0(low, ms(0), 1);
+    Est e10(low, ms(10), 1);
 
-    Est e20(1, ms(1000), 1);
-    Est f20(3, ms(1000), 1);
+    Est e20(low, ms(20), 1);
+    Est f20(high, ms(20), 1);
 
-    Est e30(1, ms(1500), 1);
-    Est f30(3, ms(1500), 1);
+    Est e30(low, ms(30), 1);
+    Est f30(high, ms(30), 1);
 
-    Est f40(3, ms(2000), 1);
-    Est f50(3, ms(2500), 1);
-    Est f60(3, ms(3000), 1);
-    Est f70(3, ms(3500), 1);
-    Est f80(3, ms(4000), 1);
+    Est f40(high, ms(40), 1);
+    Est f50(high, ms(50), 1);
+    Est f60(high, ms(60), 1);
+    Est f70(high, ms(70), 1);
+    Est f80(high, ms(80), 1);
 
     AgentFeeder f;
     f.feed(e0);
@@ -192,7 +195,7 @@
     BOOST_CHECK_EQUAL(i->getAcceptedEstimates().size(), size_t(4));
     ++i;
 
-    BOOST_CHECK_EQUAL(i->getStartTime(), ms(1000)); 
+    BOOST_CHECK_EQUAL(i->getStartTime(), ms(20)); 
     BOOST_CHECK_EQUAL(i->getAcceptedEstimates().size(), size_t(7));
     ++i;
 }
@@ -208,19 +211,19 @@
     // satisfied hypothesis eeeeeeee while it is still in
     // progress.
 
-    Est e0(1, ms(0), 1);
-    Est e10(1, ms(10), 1);
-    Est e20(1, ms(20), 1);
-    Est e30(1, ms(30), 1);
-    Est e40(1, ms(40), 1);
-    Est e50(1, ms(50), 1);
-    Est e60(1, ms(60), 1);
-    Est e70(1, ms(70), 1);
+    Est e0(low, ms(0), 1);
+    Est e10(low, ms(10), 1);
+    Est e20(low, ms(20), 1);
+    Est e30(low, ms(30), 1);
+    Est e40(low, ms(40), 1);
+    Est e50(low, ms(50), 1);
+    Est e60(low, ms(60), 1);
+    Est e70(low, ms(70), 1);
 
-    Est f20(3, ms(20), 1);
-    Est f30(3, ms(30), 1);
-    Est f40(3, ms(40), 1);
-    Est f50(3, ms(50), 1);
+    Est f20(high, ms(20), 1);
+    Est f30(high, ms(30), 1);
+    Est f40(high, ms(40), 1);
+    Est f50(high, ms(50), 1);
 
     AgentFeeder f;