comparison AgentList.cpp @ 16:33d0b18b2509

Allocate Agents separately on the heap and make AgentList contain pointers only (much quicker)
author Chris Cannam
date Wed, 12 Oct 2011 17:01:57 +0100
parents 887c629502a9
children 6afcb5edd7ab
comparison
equal deleted inserted replaced
15:887c629502a9 16:33d0b18b2509
21 21
22 void AgentList::removeDuplicates() 22 void AgentList::removeDuplicates()
23 { 23 {
24 sort(); 24 sort();
25 for (iterator itr = begin(); itr != end(); ++itr) { 25 for (iterator itr = begin(); itr != end(); ++itr) {
26 if (itr->phaseScore < 0.0) // already flagged for deletion 26 if ((*itr)->phaseScore < 0.0) // already flagged for deletion
27 continue; 27 continue;
28 iterator itr2 = itr; 28 iterator itr2 = itr;
29 for (++itr2; itr2 != end(); ++itr2) { 29 for (++itr2; itr2 != end(); ++itr2) {
30 if (itr2->beatInterval - itr->beatInterval > DEFAULT_BI) 30 if ((*itr2)->beatInterval - (*itr)->beatInterval > DEFAULT_BI)
31 break; 31 break;
32 if (fabs(itr->beatTime - itr2->beatTime) > DEFAULT_BT) 32 if (fabs((*itr)->beatTime - (*itr2)->beatTime) > DEFAULT_BT)
33 continue; 33 continue;
34 if (itr->phaseScore < itr2->phaseScore) { 34 if ((*itr)->phaseScore < (*itr2)->phaseScore) {
35 itr->phaseScore = -1.0; // flag for deletion 35 (*itr)->phaseScore = -1.0; // flag for deletion
36 if (itr2->topScoreTime < itr->topScoreTime) 36 if ((*itr2)->topScoreTime < (*itr)->topScoreTime)
37 itr2->topScoreTime = itr->topScoreTime; 37 (*itr2)->topScoreTime = (*itr)->topScoreTime;
38 break; 38 break;
39 } else { 39 } else {
40 itr2->phaseScore = -1.0; // flag for deletion 40 (*itr2)->phaseScore = -1.0; // flag for deletion
41 if (itr->topScoreTime < itr2->topScoreTime) 41 if ((*itr)->topScoreTime < (*itr2)->topScoreTime)
42 itr->topScoreTime = itr2->topScoreTime; 42 (*itr)->topScoreTime = (*itr2)->topScoreTime;
43 } 43 }
44 } 44 }
45 } 45 }
46 int removed = 0; 46 int removed = 0;
47 for (iterator itr = begin(); itr != end(); ) { 47 for (iterator itr = begin(); itr != end(); ) {
48 if (itr->phaseScore < 0.0) { 48 if ((*itr)->phaseScore < 0.0) {
49 ++removed; 49 ++removed;
50 delete *itr;
50 list.erase(itr); 51 list.erase(itr);
51 } else { 52 } else {
52 ++itr; 53 ++itr;
53 } 54 }
54 } 55 }
57 std::cerr << "removeDuplicates: removed " << removed << ", have " 58 std::cerr << "removeDuplicates: removed " << removed << ", have "
58 << list.size() << " agent(s) remaining" << std::endl; 59 << list.size() << " agent(s) remaining" << std::endl;
59 } 60 }
60 int n = 0; 61 int n = 0;
61 for (Container::iterator i = list.begin(); i != list.end(); ++i) { 62 for (Container::iterator i = list.begin(); i != list.end(); ++i) {
62 std::cerr << "agent " << n++ << ": time " << i->beatTime << std::endl; 63 std::cerr << "agent " << n++ << ": time " << (*i)->beatTime << std::endl;
63 } 64 }
64 #endif 65 #endif
65 } // removeDuplicates() 66 } // removeDuplicates()
66 67
67 68
68 void AgentList::beatTrack(EventList el, double stop) 69 void AgentList::beatTrack(EventList el, double stop)
69 { 70 {
70 EventList::iterator ei = el.begin(); 71 EventList::iterator ei = el.begin();
71 bool phaseGiven = !empty() && (begin()->beatTime >= 0); // if given for one, assume given for others 72 bool phaseGiven = !empty() && ((*begin())->beatTime >= 0); // if given for one, assume given for others
72 while (ei != el.end()) { 73 while (ei != el.end()) {
73 Event ev = *ei; 74 Event ev = *ei;
74 ++ei; 75 ++ei;
75 if ((stop > 0) && (ev.time > stop)) 76 if ((stop > 0) && (ev.time > stop))
76 break; 77 break;
83 // (since it is modified by e.g. considerAsBeat) 84 // (since it is modified by e.g. considerAsBeat)
84 Container currentAgents = list; 85 Container currentAgents = list;
85 list.clear(); 86 list.clear();
86 for (Container::iterator ai = currentAgents.begin(); 87 for (Container::iterator ai = currentAgents.begin();
87 ai != currentAgents.end(); ++ai) { 88 ai != currentAgents.end(); ++ai) {
88 Agent currentAgent = *ai; 89 Agent *currentAgent = *ai;
89 if (currentAgent.beatInterval != prevBeatInterval) { 90 if (currentAgent->beatInterval != prevBeatInterval) {
90 if ((prevBeatInterval>=0) && !created && (ev.time<5.0)) { 91 if ((prevBeatInterval>=0) && !created && (ev.time<5.0)) {
91 #ifdef DEBUG_BEATROOT 92 #ifdef DEBUG_BEATROOT
92 std::cerr << "Creating a new agent" << std::endl; 93 std::cerr << "Creating a new agent" << std::endl;
93 #endif 94 #endif
94 // Create new agent with different phase 95 // Create new agent with different phase
95 Agent newAgent(prevBeatInterval); 96 Agent *newAgent = new Agent(prevBeatInterval);
96 // This may add another agent to our list as well 97 // This may add another agent to our list as well
97 newAgent.considerAsBeat(ev, *this); 98 newAgent->considerAsBeat(ev, *this);
98 add(newAgent); 99 add(newAgent);
99 } 100 }
100 prevBeatInterval = currentAgent.beatInterval; 101 prevBeatInterval = currentAgent->beatInterval;
101 created = phaseGiven; 102 created = phaseGiven;
102 } 103 }
103 if (currentAgent.considerAsBeat(ev, *this)) 104 if (currentAgent->considerAsBeat(ev, *this))
104 created = true; 105 created = true;
105 add(currentAgent); 106 add(currentAgent);
106 } // loop for each agent 107 } // loop for each agent
107 removeDuplicates(); 108 removeDuplicates();
108 } // loop for each event 109 } // loop for each event
111 Agent *AgentList::bestAgent() 112 Agent *AgentList::bestAgent()
112 { 113 {
113 double best = -1.0; 114 double best = -1.0;
114 Agent *bestAg = 0; 115 Agent *bestAg = 0;
115 for (iterator itr = begin(); itr != end(); ++itr) { 116 for (iterator itr = begin(); itr != end(); ++itr) {
116 if (itr->events.empty()) continue; 117 if ((*itr)->events.empty()) continue;
117 double startTime = itr->events.begin()->time; 118 double startTime = (*itr)->events.begin()->time;
118 double conf = (itr->phaseScore + itr->tempoScore) / 119 double conf = ((*itr)->phaseScore + (*itr)->tempoScore) /
119 (useAverageSalience? (double)itr->beatCount: 1.0); 120 (useAverageSalience? (double)(*itr)->beatCount: 1.0);
120 if (conf > best) { 121 if (conf > best) {
121 bestAg = &(*itr); 122 bestAg = *itr;
122 best = conf; 123 best = conf;
123 } 124 }
124 } 125 }
125 #ifdef DEBUG_BEATROOT 126 #ifdef DEBUG_BEATROOT
126 if (bestAg) { 127 if (bestAg) {