annotate Agent.cpp @ 13:0d4048bfadbb

Fixes to beat insertion in Agent. We get plausible results now, but there's probably quite a lot still to do.
author Chris Cannam
date Thu, 06 Oct 2011 18:37:50 +0100
parents 59520cd6abac
children 887c629502a9
rev   line source
Chris@6 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@6 2
Chris@6 3 /*
Chris@6 4 Vamp feature extraction plugin for the BeatRoot beat tracker.
Chris@6 5
Chris@6 6 Centre for Digital Music, Queen Mary, University of London.
Chris@6 7 This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.
Chris@6 8
Chris@6 9 This program is free software; you can redistribute it and/or
Chris@6 10 modify it under the terms of the GNU General Public License as
Chris@6 11 published by the Free Software Foundation; either version 2 of the
Chris@6 12 License, or (at your option) any later version. See the file
Chris@6 13 COPYING included with this distribution for more information.
Chris@6 14 */
Chris@6 15
Chris@6 16 #include "Agent.h"
Chris@6 17 #include "BeatTracker.h"
Chris@6 18
Chris@6 19 double Agent::POST_MARGIN_FACTOR = 0.3;
Chris@6 20 double Agent::PRE_MARGIN_FACTOR = 0.15;
Chris@6 21 const double Agent::INNER_MARGIN = 0.040;
Chris@6 22 double Agent::MAX_CHANGE = 0.2;
Chris@6 23 double Agent::CONF_FACTOR = 0.5;
Chris@6 24 const double Agent::DEFAULT_CORRECTION_FACTOR = 50.0;
Chris@6 25 const double Agent::DEFAULT_EXPIRY_TIME = 10.0;
Chris@6 26
Chris@6 27 int Agent::idCounter = 0;
Chris@6 28
Chris@6 29 double Agent::innerMargin = 0.0;
Chris@6 30 double Agent::correctionFactor = 0.0;
Chris@6 31 double Agent::expiryTime = 0.0;
Chris@6 32 double Agent::decayFactor = 0.0;
Chris@6 33
Chris@8 34 bool Agent::considerAsBeat(Event e, AgentList &a) {
Chris@6 35 double err;
Chris@6 36 if (beatTime < 0) { // first event
Chris@12 37 #ifdef DEBUG_BEATROOT
Chris@12 38 std::cerr << "Ag#" << idNumber << ": accepting first event trivially at " << e.time << std::endl;
Chris@12 39 #endif
Chris@6 40 accept(e, 0, 1);
Chris@6 41 return true;
Chris@6 42 } else { // subsequent events
Chris@9 43 EventList::iterator last = events.end();
Chris@9 44 --last;
Chris@9 45 if (e.time - last->time > expiryTime) {
Chris@12 46 #ifdef DEBUG_BEATROOT
Chris@12 47 std::cerr << "Ag#" << idNumber << ": time " << e.time
Chris@12 48 << " too late relative to " << last->time << " (expiry "
Chris@12 49 << expiryTime << "), giving up" << std::endl;
Chris@12 50 #endif
Chris@6 51 phaseScore = -1.0; // flag agent to be deleted
Chris@6 52 return false;
Chris@6 53 }
Chris@8 54 double beats = nearbyint((e.time - beatTime) / beatInterval);
Chris@8 55 err = e.time - beatTime - beats * beatInterval;
Chris@12 56 #ifdef DEBUG_BEATROOT
Chris@12 57 std::cerr << "Ag#" << idNumber << ": time " << e.time << ", err " << err << " for beats " << beats << std::endl;
Chris@12 58 #endif
Chris@6 59 if ((beats > 0) && (-preMargin <= err) && (err <= postMargin)) {
Chris@12 60 if (fabs(err) > innerMargin) { // Create new agent that skips this
Chris@12 61 #ifdef DEBUG_BEATROOT
Chris@12 62 std::cerr << "Ag#" << idNumber << ": creating another new agent" << std::endl;
Chris@12 63 #endif
Chris@12 64 a.add(clone()); // event (avoids large phase jump)
Chris@12 65 }
Chris@6 66 accept(e, err, (int)beats);
Chris@6 67 return true;
Chris@6 68 }
Chris@6 69 }
Chris@6 70 return false;
Chris@6 71 } // considerAsBeat()
Chris@6 72
Chris@6 73
Chris@9 74 void Agent::fillBeats(double start) {
Chris@6 75 double prevBeat = 0, nextBeat, currentInterval, beats;
Chris@9 76 EventList::iterator ei = events.begin();
Chris@9 77 if (ei != events.end()) {
Chris@13 78 EventList::iterator ni = ei;
Chris@13 79 prevBeat = (++ni)->time;
Chris@6 80 }
Chris@13 81 for ( ; ei != events.end(); ) {
Chris@13 82 EventList::iterator ni = ei;
Chris@13 83 nextBeat = (++ni)->time;
Chris@6 84 beats = nearbyint((nextBeat - prevBeat) / beatInterval - 0.01); //prefer slow
Chris@6 85 currentInterval = (nextBeat - prevBeat) / beats;
Chris@6 86 for ( ; (nextBeat > start) && (beats > 1.5); beats--) {
Chris@6 87 prevBeat += currentInterval;
Chris@13 88 events.insert(ni, BeatTracker::newBeat(prevBeat, 0));
Chris@6 89 }
Chris@6 90 prevBeat = nextBeat;
Chris@13 91 ei = ni;
Chris@6 92 }
Chris@6 93 } // fillBeats()