annotate src/Finder.cpp @ 75:e1a5f3095ba6 cheap_diagonals

Merge from branch refactors
author Chris Cannam
date Wed, 19 Nov 2014 12:13:28 +0000
parents 7aa1ab3db7c9 7e3c1bc0984a
children eb3392bf6150
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugin using the MATCH audio alignment
cannam@0 5 algorithm.
cannam@0 6
cannam@0 7 Centre for Digital Music, Queen Mary, University of London.
cannam@0 8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
cannam@0 9
cannam@0 10 This program is free software; you can redistribute it and/or
cannam@0 11 modify it under the terms of the GNU General Public License as
cannam@0 12 published by the Free Software Foundation; either version 2 of the
cannam@0 13 License, or (at your option) any later version. See the file
cannam@0 14 COPYING included with this distribution for more information.
cannam@0 15 */
cannam@0 16
cannam@0 17 #include "Finder.h"
cannam@0 18
Chris@30 19 #include "Path.h"
Chris@30 20
Chris@30 21 #include <algorithm>
Chris@30 22
Chris@72 23 using namespace std;
cannam@0 24
Chris@72 25 Finder::Finder(Matcher *pm)
cannam@0 26 {
Chris@72 27 m_m = pm;
Chris@72 28 m_duration1 = -1;
Chris@72 29 m_duration2 = -1;
cannam@0 30 } // constructor
cannam@0 31
cannam@0 32 Finder::~Finder()
cannam@0 33 {
cannam@0 34 }
cannam@0 35
Chris@60 36 void
Chris@60 37 Finder::setDurations(int d1, int d2)
Chris@60 38 {
Chris@72 39 m_duration1 = d1;
Chris@72 40 m_duration2 = d2;
Chris@60 41 }
Chris@60 42
Chris@45 43 Matcher::Advance
cannam@0 44 Finder::getExpandDirection(int row, int col)
cannam@0 45 {
cannam@0 46 return getExpandDirection(row, col, false);
cannam@0 47 } // getExpandDirection()
cannam@0 48
Chris@45 49 Matcher::Advance
cannam@0 50 Finder::getExpandDirection(int row, int col, bool check)
cannam@0 51 {
Chris@72 52 double min = m_m->getPathCost(row, col);
Chris@72 53
Chris@72 54 int bestRow = row;
Chris@72 55 int bestCol = col;
Chris@72 56
Chris@72 57 pair<int, int> rowRange = m_m->getRowRange(col);
Chris@72 58 if (rowRange.second > row+1) {
Chris@72 59 rowRange.second = row+1; // don't cheat by looking at future :)
Chris@72 60 }
Chris@72 61 for (int index = rowRange.first; index < rowRange.second; index++) {
Chris@72 62 double tmp = m_m->getPathCost(index, col);
cannam@0 63 if (tmp < min) {
cannam@0 64 min = tmp;
cannam@0 65 bestRow = index;
cannam@0 66 }
cannam@0 67 }
Chris@72 68
Chris@72 69 pair<int, int> colRange = m_m->getColRange(row);
Chris@72 70 if (colRange.second > col+1) {
Chris@72 71 colRange.second = col+1; // don't cheat by looking at future :)
Chris@72 72 }
Chris@72 73 for (int index = colRange.first; index < colRange.second; index++) {
Chris@72 74 double tmp = m_m->getPathCost(row, index);
cannam@0 75 if (tmp < min) {
cannam@0 76 min = tmp;
cannam@0 77 bestCol = index;
cannam@0 78 bestRow = row;
cannam@0 79 }
cannam@0 80 }
Chris@72 81
Chris@45 82 if (bestRow == row) {
Chris@45 83 if (bestCol == col) {
Chris@45 84 return Matcher::AdvanceBoth;
Chris@45 85 } else {
Chris@45 86 return Matcher::AdvanceThis;
Chris@45 87 }
Chris@45 88 } else if (bestCol == col) {
Chris@45 89 return Matcher::AdvanceOther;
Chris@45 90 } else {
Chris@46 91 return Matcher::AdvanceNone;
Chris@45 92 }
cannam@0 93
Chris@73 94 }
cannam@0 95
cannam@0 96 void
cannam@0 97 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2)
cannam@0 98 {
Chris@59 99 float diagonalWeight = sqrtf(2.f);
Chris@59 100
Chris@72 101 int prevRowStart = 0, prevRowStop = 0;
Chris@72 102
Chris@72 103 for (int r = r1; r <= r2; r++) {
Chris@72 104
Chris@72 105 pair<int, int> colRange = m_m->getColRange(r);
Chris@72 106
Chris@72 107 int rowStart = max(c1, colRange.first);
Chris@72 108 int rowStop = min(c2 + 1, colRange.second);
Chris@72 109
Chris@72 110 for (int c = rowStart; c < rowStop; c++) {
Chris@72 111
Chris@72 112 float newCost = m_m->getDistance(r, c);
Chris@72 113 Matcher::Advance dir = Matcher::AdvanceNone;
Chris@72 114
Chris@72 115 if (r > r1) { // not first row
Chris@72 116 double min = -1;
Chris@72 117 if ((c > prevRowStart) && (c <= prevRowStop)) {
Chris@72 118 // diagonal from (r-1,c-1)
Chris@75 119 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight;
Chris@72 120 dir = Matcher::AdvanceBoth;
Chris@72 121 }
Chris@72 122 if ((c >= prevRowStart) && (c < prevRowStop)) {
Chris@72 123 // vertical from (r-1,c)
Chris@72 124 double cost = m_m->getPathCost(r-1, c) + newCost;
Chris@72 125 if ((min < 0) || (cost < min)) {
Chris@72 126 min = cost;
Chris@72 127 dir = Matcher::AdvanceThis;
Chris@72 128 }
Chris@72 129 }
Chris@72 130 if (c > rowStart) {
Chris@72 131 // horizontal from (r,c-1)
Chris@72 132 double cost = m_m->getPathCost(r, c-1) + newCost;
Chris@72 133 if ((min < 0) || (cost < min)) {
Chris@72 134 min = cost;
Chris@72 135 dir = Matcher::AdvanceOther;
Chris@72 136 }
Chris@72 137 }
Chris@72 138
Chris@72 139 m_m->setPathCost(r, c, dir, min);
Chris@72 140
Chris@72 141 } else if (c > rowStart) { // first row
Chris@72 142 // horizontal from (r,c-1)
Chris@72 143 m_m->setPathCost(r, c, Matcher::AdvanceOther,
Chris@72 144 m_m->getPathCost(r, c-1) + newCost);
Chris@72 145 }
Chris@72 146 }
Chris@72 147
Chris@72 148 prevRowStart = rowStart;
Chris@72 149 prevRowStop = rowStop;
cannam@0 150 }
Chris@72 151 }
Chris@30 152
Chris@30 153 int
Chris@31 154 Finder::retrievePath(bool smooth, vector<int> &pathx, vector<int> &pathy)
Chris@30 155 {
Chris@69 156 pathx.clear();
Chris@69 157 pathy.clear();
Chris@69 158
Chris@72 159 int ex = m_m->getOtherFrameCount() - 1;
Chris@72 160 int ey = m_m->getFrameCount() - 1;
Chris@69 161
Chris@69 162 if (ex < 0 || ey < 0) {
Chris@69 163 return 0;
Chris@69 164 }
Chris@66 165
Chris@66 166 int x = ex;
Chris@66 167 int y = ey;
Chris@66 168
Chris@66 169 // cerr << "before: x = " << x << ", y = " << y << endl;
Chris@30 170
Chris@72 171 if (m_duration2 > 0 && m_duration2 < m_m->getOtherFrameCount()) {
Chris@72 172 x = m_duration2 - 1;
Chris@60 173 }
Chris@72 174 if (m_duration1 > 0 && m_duration1 < m_m->getFrameCount()) {
Chris@72 175 y = m_duration1 - 1;
Chris@60 176 }
Chris@60 177
Chris@72 178 if (!m_m->isAvailable(y, x)) {
Chris@66 179 // Path did not pass through the expected end point --
Chris@66 180 // probably means the pieces are substantially different in
Chris@66 181 // the later bits. Reset the expected end point to the end of
Chris@66 182 // both files including any trailing silence.
Chris@66 183 cerr << "NOTE: Path did not pass through expected end point, inputs are probably significantly different" << endl;
Chris@66 184 x = ex;
Chris@66 185 y = ey;
Chris@66 186 }
Chris@66 187
Chris@55 188 recalculatePathCostMatrix(0, 0, y, x);
Chris@55 189
Chris@66 190 // cerr << "start: x = " << x << ", y = " << y << endl;
Chris@66 191
Chris@72 192 while (m_m->isAvailable(y, x) && (x > 0 || y > 0)) {
Chris@30 193
Chris@33 194 // cerr << "x = " << x << ", y = " << y;
Chris@33 195
Chris@30 196 pathx.push_back(x);
Chris@30 197 pathy.push_back(y);
Chris@30 198
Chris@72 199 switch (m_m->getAdvance(y, x)) {
Chris@45 200 case Matcher::AdvanceThis:
Chris@70 201 // cerr << ", going down (dist = " << getDistance() << ")" << endl;
Chris@33 202 y--;
Chris@33 203 break;
Chris@45 204 case Matcher::AdvanceOther:
Chris@70 205 // cerr << ", going left (dist = " << getDistance() << ")" << endl;
Chris@33 206 x--;
Chris@33 207 break;
Chris@45 208 case Matcher::AdvanceBoth:
Chris@70 209 // cerr << ", going diag (dist = " << getDistance() << ")" << endl;
Chris@33 210 x--;
Chris@33 211 y--;
Chris@33 212 break;
Chris@45 213 case Matcher::AdvanceNone: // this would indicate a bug, but we wouldn't want to hang
Chris@69 214 cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl;
Chris@33 215 if (x > y) {
Chris@33 216 x--;
Chris@33 217 } else {
Chris@33 218 y--;
Chris@33 219 }
Chris@33 220 break;
Chris@30 221 }
Chris@30 222 }
Chris@30 223
Chris@72 224 if (x > 0 || y > 0) {
Chris@72 225 cerr << "WARNING: Ran out of available path at (" << y << "," << x
Chris@72 226 << ")!" << endl;
Chris@72 227 }
Chris@72 228
Chris@72 229 reverse(pathx.begin(), pathx.end());
Chris@72 230 reverse(pathy.begin(), pathy.end());
Chris@30 231
Chris@31 232 if (smooth) {
Chris@31 233 int smoothedLen = Path().smooth(pathx, pathy, pathx.size());
Chris@31 234 return smoothedLen;
Chris@31 235 } else {
Chris@31 236 return pathx.size();
Chris@31 237 }
Chris@30 238 }
Chris@30 239
Chris@30 240