File: AgentList.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-16
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 214,188 kB
  • sloc: cpp: 291,198; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 244; perl: 82; pascal: 79
file content (147 lines) | stat: -rw-r--r-- 5,385 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
  Vamp feature extraction plugin for the BeatRoot beat tracker.

  Centre for Digital Music, Queen Mary, University of London.
  This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.  See the file
  COPYING included with this distribution for more information.
*/

#include "AgentList.h"
#include "Agent.h"

#include <algorithm>
#include <cmath>


bool AgentList::useAverageSalience = false;
const double AgentList::DEFAULT_BI = 0.02;
const double AgentList::DEFAULT_BT = 0.04;


void AgentList::add(Agent *newAgent, bool sort)
      {
      push_back(newAgent);
      if (sort)
            this->sort();
      }

void AgentList::sort()
      {
      struct {
            bool operator()(const Agent *a, const Agent *b)
                  {
                  if (a->beatInterval == b->beatInterval)
                        return a->idNumber < b->idNumber;         // ensure stable ordering
                  return a->beatInterval < b->beatInterval;
                  }
            } agentComparator;

      std::sort(list.begin(), list.end(), agentComparator);
      }

void AgentList::remove(const AgentList::iterator &itr)
      {
      list.erase(itr);
      }

void AgentList::removeDuplicates()
      {
      sort();
      for (iterator itr = begin(); itr != end(); ++itr) {
            if ((*itr)->phaseScore < 0.0)       // already flagged for deletion
                  continue;
            iterator itr2 = itr;
            for (++itr2; itr2 != end(); ++itr2) {
                  if ((*itr2)->beatInterval - (*itr)->beatInterval > DEFAULT_BI)
                        break;
                  if (std::fabs((*itr)->beatTime - (*itr2)->beatTime) > DEFAULT_BT)
                        continue;
                  if ((*itr)->phaseScore < (*itr2)->phaseScore) {
                        (*itr)->phaseScore = -1.0;	// flag for deletion
                        break;
                        }
                  else {
                        (*itr2)->phaseScore = -1.0;	// flag for deletion
                        }
                  }
            }
      int removed = 0;
      for (iterator itr = begin(); itr != end(); ) {
            if ((*itr)->phaseScore < 0.0) {
                  ++removed;
                  delete *itr;
                  list.erase(itr);
                  }
            else {
                  ++itr;
                  }
            }
      }


void AgentList::beatTrack(const EventList &el,
                          const AgentParameters &params,
                          double stop)
      {
      EventList::const_iterator ei = el.begin();
      bool phaseGiven = !empty() && ((*begin())->beatTime >= 0); // if given for one, assume given for others
      while (ei != el.end()) {
            Event ev = *ei;
            ++ei;
            if ((stop > 0) && (ev.time > stop))
                  break;
            bool created = phaseGiven;
            double prevBeatInterval = -1.0;
                        // cc: Duplicate our list of agents, and scan through the
                        // copy.  This means we can safely add agents to our own
                        // list while scanning without disrupting our scan.  Each
                        // agent needs to be re-added to our own list explicitly
                        // (since it is modified by e.g. considerAsBeat)
            Container currentAgents = list;
            list.clear();
            for (Container::iterator ai = currentAgents.begin();
                        ai != currentAgents.end(); ++ai) {
                  Agent *currentAgent = *ai;
                  if (currentAgent->beatInterval != prevBeatInterval) {
                        if ((prevBeatInterval >= 0) && !created && (ev.time < 5.0)) {
                                          // Create new agent with different phase
                              Agent *newAgent = new Agent(params, prevBeatInterval);
                                          // This may add another agent to our list as well
                              newAgent->considerAsBeat(ev, *this);
                              add(newAgent);
                              }
                        prevBeatInterval = currentAgent->beatInterval;
                        created = phaseGiven;
                        }
                  if (currentAgent->considerAsBeat(ev, *this))
                        created = true;
                  add(currentAgent);
                  }           // loop for each agent
            removeDuplicates();
            }           // loop for each event
      }

Agent *AgentList::bestAgent()
      {
      double best = -1.0;
      Agent *bestAg = 0;
      for (iterator itr = begin(); itr != end(); ++itr) {
            if ((*itr)->events.empty())
                  continue;
            double conf = (*itr)->phaseScore
                        / (useAverageSalience ? (double)(*itr)->beatCount : 1.0);
            if (conf > best) {
                  bestAg = *itr;
                  best = conf;
                  }
            }
      return bestAg;
      }