Mercurial > hg > beatroot-vamp
comparison AgentList.cpp @ 15:887c629502a9
refactor: pull method implementations into .cpp files
author | Chris Cannam |
---|---|
date | Wed, 12 Oct 2011 10:55:52 +0100 |
parents | f04f87b5e643 |
children | 33d0b18b2509 |
comparison
equal
deleted
inserted
replaced
14:f1252b6a7cf5 | 15:887c629502a9 |
---|---|
17 | 17 |
18 bool AgentList::useAverageSalience = false; | 18 bool AgentList::useAverageSalience = false; |
19 const double AgentList::DEFAULT_BI = 0.02; | 19 const double AgentList::DEFAULT_BI = 0.02; |
20 const double AgentList::DEFAULT_BT = 0.04; | 20 const double AgentList::DEFAULT_BT = 0.04; |
21 | 21 |
22 void AgentList::removeDuplicates() | |
23 { | |
24 sort(); | |
25 for (iterator itr = begin(); itr != end(); ++itr) { | |
26 if (itr->phaseScore < 0.0) // already flagged for deletion | |
27 continue; | |
28 iterator itr2 = itr; | |
29 for (++itr2; itr2 != end(); ++itr2) { | |
30 if (itr2->beatInterval - itr->beatInterval > DEFAULT_BI) | |
31 break; | |
32 if (fabs(itr->beatTime - itr2->beatTime) > DEFAULT_BT) | |
33 continue; | |
34 if (itr->phaseScore < itr2->phaseScore) { | |
35 itr->phaseScore = -1.0; // flag for deletion | |
36 if (itr2->topScoreTime < itr->topScoreTime) | |
37 itr2->topScoreTime = itr->topScoreTime; | |
38 break; | |
39 } else { | |
40 itr2->phaseScore = -1.0; // flag for deletion | |
41 if (itr->topScoreTime < itr2->topScoreTime) | |
42 itr->topScoreTime = itr2->topScoreTime; | |
43 } | |
44 } | |
45 } | |
46 int removed = 0; | |
47 for (iterator itr = begin(); itr != end(); ) { | |
48 if (itr->phaseScore < 0.0) { | |
49 ++removed; | |
50 list.erase(itr); | |
51 } else { | |
52 ++itr; | |
53 } | |
54 } | |
55 #ifdef DEBUG_BEATROOT | |
56 if (removed > 0) { | |
57 std::cerr << "removeDuplicates: removed " << removed << ", have " | |
58 << list.size() << " agent(s) remaining" << std::endl; | |
59 } | |
60 int n = 0; | |
61 for (Container::iterator i = list.begin(); i != list.end(); ++i) { | |
62 std::cerr << "agent " << n++ << ": time " << i->beatTime << std::endl; | |
63 } | |
64 #endif | |
65 } // removeDuplicates() | |
66 | |
67 | |
68 void AgentList::beatTrack(EventList el, double stop) | |
69 { | |
70 EventList::iterator ei = el.begin(); | |
71 bool phaseGiven = !empty() && (begin()->beatTime >= 0); // if given for one, assume given for others | |
72 while (ei != el.end()) { | |
73 Event ev = *ei; | |
74 ++ei; | |
75 if ((stop > 0) && (ev.time > stop)) | |
76 break; | |
77 bool created = phaseGiven; | |
78 double prevBeatInterval = -1.0; | |
79 // cc: Duplicate our list of agents, and scan through the | |
80 // copy. This means we can safely add agents to our own | |
81 // list while scanning without disrupting our scan. Each | |
82 // agent needs to be re-added to our own list explicitly | |
83 // (since it is modified by e.g. considerAsBeat) | |
84 Container currentAgents = list; | |
85 list.clear(); | |
86 for (Container::iterator ai = currentAgents.begin(); | |
87 ai != currentAgents.end(); ++ai) { | |
88 Agent currentAgent = *ai; | |
89 if (currentAgent.beatInterval != prevBeatInterval) { | |
90 if ((prevBeatInterval>=0) && !created && (ev.time<5.0)) { | |
91 #ifdef DEBUG_BEATROOT | |
92 std::cerr << "Creating a new agent" << std::endl; | |
93 #endif | |
94 // Create new agent with different phase | |
95 Agent newAgent(prevBeatInterval); | |
96 // This may add another agent to our list as well | |
97 newAgent.considerAsBeat(ev, *this); | |
98 add(newAgent); | |
99 } | |
100 prevBeatInterval = currentAgent.beatInterval; | |
101 created = phaseGiven; | |
102 } | |
103 if (currentAgent.considerAsBeat(ev, *this)) | |
104 created = true; | |
105 add(currentAgent); | |
106 } // loop for each agent | |
107 removeDuplicates(); | |
108 } // loop for each event | |
109 } // beatTrack() | |
110 | |
111 Agent *AgentList::bestAgent() | |
112 { | |
113 double best = -1.0; | |
114 Agent *bestAg = 0; | |
115 for (iterator itr = begin(); itr != end(); ++itr) { | |
116 if (itr->events.empty()) continue; | |
117 double startTime = itr->events.begin()->time; | |
118 double conf = (itr->phaseScore + itr->tempoScore) / | |
119 (useAverageSalience? (double)itr->beatCount: 1.0); | |
120 if (conf > best) { | |
121 bestAg = &(*itr); | |
122 best = conf; | |
123 } | |
124 } | |
125 #ifdef DEBUG_BEATROOT | |
126 if (bestAg) { | |
127 std::cerr << "Best agent: Ag#" << bestAg->idNumber << std::endl; | |
128 std::cerr << " Av-salience = " << best << std::endl; | |
129 } else { | |
130 std::cerr << "No surviving agent - beat tracking failed" << std::endl; | |
131 } | |
132 #endif | |
133 return bestAg; | |
134 } // bestAgent() | |
135 |