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@92
|
22 #include <iomanip>
|
Chris@30
|
23
|
Chris@72
|
24 using namespace std;
|
cannam@0
|
25
|
Chris@140
|
26 //#define DEBUG_FINDER 1
|
Chris@140
|
27 //#define PERFORM_ERROR_CHECKS 1
|
Chris@140
|
28
|
Chris@72
|
29 Finder::Finder(Matcher *pm)
|
cannam@0
|
30 {
|
Chris@72
|
31 m_m = pm;
|
Chris@72
|
32 m_duration1 = -1;
|
Chris@72
|
33 m_duration2 = -1;
|
cannam@0
|
34 } // constructor
|
cannam@0
|
35
|
cannam@0
|
36 Finder::~Finder()
|
cannam@0
|
37 {
|
cannam@0
|
38 }
|
cannam@0
|
39
|
Chris@60
|
40 void
|
Chris@154
|
41 Finder::setMatcher(Matcher *pm)
|
Chris@154
|
42 {
|
Chris@155
|
43 cerr << "Finder::setMatcher: finder " << this << ", matcher " << pm << endl;
|
Chris@154
|
44 m_m = pm;
|
Chris@154
|
45 }
|
Chris@154
|
46
|
Chris@154
|
47 void
|
Chris@60
|
48 Finder::setDurations(int d1, int d2)
|
Chris@60
|
49 {
|
Chris@140
|
50 #ifdef DEBUG_FINDER
|
Chris@140
|
51 cerr << "*** setDurations: " << d1 << ", " << d2 << endl;
|
Chris@140
|
52 #endif
|
Chris@72
|
53 m_duration1 = d1;
|
Chris@72
|
54 m_duration2 = d2;
|
Chris@60
|
55 }
|
Chris@60
|
56
|
Chris@154
|
57 bool
|
Chris@191
|
58 Finder::getBestRowCost(int row, int &bestCol, normpathcost_t &min)
|
Chris@154
|
59 {
|
Chris@172
|
60 if (!m_m->isRowAvailable(row)) return false;
|
Chris@205
|
61 pair<int, int> colRange = m_m->getColRangeForRow(row);
|
Chris@172
|
62 if (colRange.first >= colRange.second) return false;
|
Chris@154
|
63 for (int index = colRange.first; index < colRange.second; index++) {
|
Chris@191
|
64 normpathcost_t tmp = m_m->getNormalisedPathCost(row, index);
|
Chris@154
|
65 if (index == colRange.first || tmp < min) {
|
Chris@154
|
66 min = tmp;
|
Chris@154
|
67 bestCol = index;
|
Chris@154
|
68 }
|
Chris@154
|
69 }
|
Chris@154
|
70 return true;
|
Chris@154
|
71 }
|
Chris@154
|
72
|
Chris@154
|
73 bool
|
Chris@191
|
74 Finder::getBestColCost(int col, int &bestRow, normpathcost_t &min)
|
Chris@154
|
75 {
|
Chris@154
|
76 if (!m_m->isColAvailable(col)) return false;
|
Chris@205
|
77 pair<int, int> rowRange = m_m->getRowRangeForCol(col);
|
Chris@154
|
78 if (rowRange.first >= rowRange.second) return false;
|
Chris@191
|
79 bestRow = rowRange.first;
|
Chris@154
|
80 for (int index = rowRange.first; index < rowRange.second; index++) {
|
Chris@191
|
81 normpathcost_t tmp = m_m->getNormalisedPathCost(index, col);
|
Chris@191
|
82 if (index == rowRange.first) {
|
Chris@191
|
83 min = tmp;
|
Chris@191
|
84 } else if (tmp < min) {
|
Chris@154
|
85 min = tmp;
|
Chris@154
|
86 bestRow = index;
|
Chris@154
|
87 }
|
Chris@154
|
88 }
|
Chris@154
|
89 return true;
|
Chris@154
|
90 }
|
Chris@154
|
91
|
Chris@147
|
92 void
|
Chris@147
|
93 Finder::getBestEdgeCost(int row, int col,
|
Chris@147
|
94 int &bestRow, int &bestCol,
|
Chris@191
|
95 normpathcost_t &min)
|
cannam@0
|
96 {
|
Chris@191
|
97 min = m_m->getNormalisedPathCost(row, col);
|
Chris@72
|
98
|
Chris@147
|
99 bestRow = row;
|
Chris@147
|
100 bestCol = col;
|
Chris@72
|
101
|
Chris@205
|
102 pair<int, int> rowRange = m_m->getRowRangeForCol(col);
|
Chris@72
|
103 if (rowRange.second > row+1) {
|
Chris@72
|
104 rowRange.second = row+1; // don't cheat by looking at future :)
|
Chris@72
|
105 }
|
Chris@72
|
106 for (int index = rowRange.first; index < rowRange.second; index++) {
|
Chris@191
|
107 normpathcost_t tmp = m_m->getNormalisedPathCost(index, col);
|
cannam@0
|
108 if (tmp < min) {
|
cannam@0
|
109 min = tmp;
|
cannam@0
|
110 bestRow = index;
|
cannam@0
|
111 }
|
cannam@0
|
112 }
|
Chris@72
|
113
|
Chris@205
|
114 pair<int, int> colRange = m_m->getColRangeForRow(row);
|
Chris@72
|
115 if (colRange.second > col+1) {
|
Chris@72
|
116 colRange.second = col+1; // don't cheat by looking at future :)
|
Chris@72
|
117 }
|
Chris@72
|
118 for (int index = colRange.first; index < colRange.second; index++) {
|
Chris@191
|
119 normpathcost_t tmp = m_m->getNormalisedPathCost(row, index);
|
cannam@0
|
120 if (tmp < min) {
|
cannam@0
|
121 min = tmp;
|
cannam@0
|
122 bestCol = index;
|
cannam@0
|
123 bestRow = row;
|
cannam@0
|
124 }
|
cannam@0
|
125 }
|
Chris@147
|
126 }
|
Chris@72
|
127
|
Chris@181
|
128 advance_t
|
Chris@171
|
129 Finder::getExpandDirection()
|
Chris@171
|
130 {
|
Chris@171
|
131 return getExpandDirection(m_m->getFrameCount() - 1,
|
Chris@171
|
132 m_m->getOtherFrameCount() - 1);
|
Chris@171
|
133 }
|
Chris@171
|
134
|
Chris@181
|
135 advance_t
|
Chris@147
|
136 Finder::getExpandDirection(int row, int col)
|
Chris@147
|
137 {
|
Chris@147
|
138 // To determine which direction to expand the search area in, we
|
Chris@147
|
139 // look at the path costs along the leading edges of the search
|
Chris@147
|
140 // area (the final row and column within the area). We find the
|
Chris@147
|
141 // lowest path cost within the final row, and the lowest within
|
Chris@147
|
142 // the final column, and we compare them. If the row is cheaper
|
Chris@147
|
143 // then we expand by adding another row next to it; if the column
|
Chris@147
|
144 // is cheaper then we expand by adding another column next to
|
Chris@147
|
145 // it. (The overall lowest path cost across the row and column
|
Chris@147
|
146 // represents the best alignment we have within the entire search
|
Chris@147
|
147 // area given the data available and the assumption that the piece
|
Chris@147
|
148 // is not ending yet.)
|
Chris@147
|
149
|
Chris@147
|
150 int bestRow = row;
|
Chris@147
|
151 int bestCol = col;
|
Chris@191
|
152 normpathcost_t bestCost = -1;
|
Chris@147
|
153
|
Chris@155
|
154 // cerr << "Finder " << this << "::getExpandDirection: ";
|
Chris@155
|
155
|
Chris@147
|
156 getBestEdgeCost(row, col, bestRow, bestCol, bestCost);
|
Chris@147
|
157
|
Chris@147
|
158 // cerr << "at [" << row << "," << col << "] (cost " << m_m->getPathCost(row, col) << ") blocksize = " << m_m->getBlockSize() << " best is [" << bestRow << "," << bestCol << "] (cost " << bestCost << ")" << endl;
|
Chris@135
|
159
|
Chris@45
|
160 if (bestRow == row) {
|
Chris@45
|
161 if (bestCol == col) {
|
Chris@181
|
162 return AdvanceBoth;
|
Chris@45
|
163 } else {
|
Chris@181
|
164 return AdvanceThis;
|
Chris@45
|
165 }
|
Chris@45
|
166 } else if (bestCol == col) {
|
Chris@181
|
167 return AdvanceOther;
|
Chris@45
|
168 } else {
|
Chris@181
|
169 return AdvanceNone;
|
Chris@45
|
170 }
|
Chris@73
|
171 }
|
cannam@0
|
172
|
cannam@0
|
173 void
|
cannam@0
|
174 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2)
|
cannam@0
|
175 {
|
Chris@72
|
176 int prevRowStart = 0, prevRowStop = 0;
|
Chris@72
|
177
|
Chris@72
|
178 for (int r = r1; r <= r2; r++) {
|
Chris@72
|
179
|
Chris@205
|
180 pair<int, int> colRange = m_m->getColRangeForRow(r);
|
Chris@72
|
181
|
Chris@72
|
182 int rowStart = max(c1, colRange.first);
|
Chris@72
|
183 int rowStop = min(c2 + 1, colRange.second);
|
Chris@72
|
184
|
Chris@72
|
185 for (int c = rowStart; c < rowStop; c++) {
|
Chris@72
|
186
|
Chris@181
|
187 advance_t dir = AdvanceNone;
|
Chris@188
|
188 pathcost_t straightIncrement = m_m->getDistance(r, c);
|
Chris@188
|
189 pathcost_t diagIncrement = pathcost_t(straightIncrement *
|
Chris@188
|
190 m_m->getDiagonalWeight());
|
Chris@72
|
191
|
Chris@72
|
192 if (r > r1) { // not first row
|
Chris@182
|
193 pathcost_t min = -1;
|
Chris@72
|
194 if ((c > prevRowStart) && (c <= prevRowStop)) {
|
Chris@72
|
195 // diagonal from (r-1,c-1)
|
Chris@188
|
196 min = m_m->getPathCost(r-1, c-1) + diagIncrement;
|
Chris@181
|
197 dir = AdvanceBoth;
|
Chris@72
|
198 }
|
Chris@72
|
199 if ((c >= prevRowStart) && (c < prevRowStop)) {
|
Chris@72
|
200 // vertical from (r-1,c)
|
Chris@188
|
201 pathcost_t cost = m_m->getPathCost(r-1, c) + straightIncrement;
|
Chris@72
|
202 if ((min < 0) || (cost < min)) {
|
Chris@72
|
203 min = cost;
|
Chris@181
|
204 dir = AdvanceThis;
|
Chris@72
|
205 }
|
Chris@72
|
206 }
|
Chris@72
|
207 if (c > rowStart) {
|
Chris@72
|
208 // horizontal from (r,c-1)
|
Chris@188
|
209 pathcost_t cost = m_m->getPathCost(r, c-1) + straightIncrement;
|
Chris@72
|
210 if ((min < 0) || (cost < min)) {
|
Chris@72
|
211 min = cost;
|
Chris@181
|
212 dir = AdvanceOther;
|
Chris@72
|
213 }
|
Chris@72
|
214 }
|
Chris@72
|
215
|
Chris@72
|
216 m_m->setPathCost(r, c, dir, min);
|
Chris@72
|
217
|
Chris@72
|
218 } else if (c > rowStart) { // first row
|
Chris@72
|
219 // horizontal from (r,c-1)
|
Chris@181
|
220 m_m->setPathCost(r, c, AdvanceOther,
|
Chris@188
|
221 m_m->getPathCost(r, c-1) + straightIncrement);
|
Chris@72
|
222 }
|
Chris@72
|
223 }
|
Chris@72
|
224
|
Chris@72
|
225 prevRowStart = rowStart;
|
Chris@72
|
226 prevRowStop = rowStop;
|
cannam@0
|
227 }
|
Chris@72
|
228 }
|
Chris@30
|
229
|
Chris@82
|
230 #ifdef PERFORM_ERROR_CHECKS
|
Chris@81
|
231 Finder::ErrorPosition
|
Chris@81
|
232 Finder::checkPathCostMatrix()
|
Chris@81
|
233 {
|
Chris@81
|
234 ErrorPosition err;
|
Chris@81
|
235
|
Chris@81
|
236 int r1 = 0;
|
Chris@81
|
237 int c1 = 0;
|
Chris@81
|
238 int r2 = m_m->getFrameCount() - 1;
|
Chris@81
|
239 int c2 = m_m->getOtherFrameCount() - 1;
|
Chris@81
|
240
|
Chris@81
|
241 if (r2 < r1 || c2 < c1) {
|
Chris@81
|
242 return err;
|
Chris@81
|
243 }
|
Chris@81
|
244
|
Chris@81
|
245 int prevRowStart = 0, prevRowStop = 0;
|
Chris@81
|
246
|
Chris@81
|
247 for (int r = r1; r <= r2; r++) {
|
Chris@81
|
248
|
Chris@205
|
249 pair<int, int> colRange = m_m->getColRangeForRow(r);
|
Chris@81
|
250
|
Chris@81
|
251 int rowStart = max(c1, colRange.first);
|
Chris@81
|
252 int rowStop = min(c2 + 1, colRange.second);
|
Chris@81
|
253
|
Chris@81
|
254 for (int c = rowStart; c < rowStop; c++) {
|
Chris@81
|
255
|
Chris@188
|
256 advance_t dir = AdvanceNone;
|
Chris@212
|
257 pathcost_t updateTo = INVALID_PATHCOST;
|
Chris@188
|
258 distance_t distance = m_m->getDistance(r, c);
|
Chris@188
|
259 pathcost_t straightIncrement = distance;
|
Chris@188
|
260 pathcost_t diagIncrement = pathcost_t(distance * m_m->getDiagonalWeight());
|
Chris@188
|
261 err.distance = distance;
|
Chris@81
|
262
|
Chris@95
|
263 if (r > r1) { // not first row
|
Chris@182
|
264 pathcost_t min = -1;
|
Chris@81
|
265 if ((c > prevRowStart) && (c <= prevRowStop)) {
|
Chris@81
|
266 // diagonal from (r-1,c-1)
|
Chris@188
|
267 min = m_m->getPathCost(r-1, c-1) + diagIncrement;
|
Chris@81
|
268 err.prevCost = m_m->getPathCost(r-1, c-1);
|
Chris@181
|
269 dir = AdvanceBoth;
|
Chris@81
|
270 }
|
Chris@81
|
271 if ((c >= prevRowStart) && (c < prevRowStop)) {
|
Chris@81
|
272 // vertical from (r-1,c)
|
Chris@188
|
273 pathcost_t cost = m_m->getPathCost(r-1, c) + straightIncrement;
|
Chris@81
|
274 if ((min < 0) || (cost < min)) {
|
Chris@81
|
275 min = cost;
|
Chris@81
|
276 err.prevCost = m_m->getPathCost(r-1, c);
|
Chris@181
|
277 dir = AdvanceThis;
|
Chris@81
|
278 }
|
Chris@81
|
279 }
|
Chris@81
|
280 if (c > rowStart) {
|
Chris@81
|
281 // horizontal from (r,c-1)
|
Chris@188
|
282 pathcost_t cost = m_m->getPathCost(r, c-1) + straightIncrement;
|
Chris@81
|
283 if ((min < 0) || (cost < min)) {
|
Chris@81
|
284 min = cost;
|
Chris@81
|
285 err.prevCost = m_m->getPathCost(r, c-1);
|
Chris@181
|
286 dir = AdvanceOther;
|
Chris@81
|
287 }
|
Chris@81
|
288 }
|
Chris@81
|
289
|
Chris@81
|
290 updateTo = min;
|
Chris@81
|
291
|
Chris@82
|
292 } else { // first row
|
Chris@82
|
293
|
Chris@82
|
294 if (c > rowStart) {
|
Chris@82
|
295 // horizontal from (r,c-1)
|
Chris@188
|
296 updateTo = m_m->getPathCost(r, c-1) + straightIncrement;
|
Chris@83
|
297 err.prevCost = m_m->getPathCost(r, c-1);
|
Chris@181
|
298 dir = AdvanceOther;
|
Chris@82
|
299 }
|
Chris@81
|
300 }
|
Chris@81
|
301
|
Chris@181
|
302 if (dir != AdvanceNone) {
|
Chris@86
|
303 if (m_m->getAdvance(r, c) != dir) {
|
Chris@86
|
304 err.type = ErrorPosition::WrongAdvance;
|
Chris@86
|
305 err.r = r;
|
Chris@86
|
306 err.c = c;
|
Chris@86
|
307 err.costWas = m_m->getPathCost(r, c);
|
Chris@86
|
308 err.costShouldBe = updateTo;
|
Chris@86
|
309 err.advanceWas = m_m->getAdvance(r, c);
|
Chris@86
|
310 err.advanceShouldBe = dir;
|
Chris@86
|
311 return err;
|
Chris@86
|
312 }
|
Chris@84
|
313 if (m_m->getPathCost(r, c) != updateTo) {
|
Chris@84
|
314 err.type = ErrorPosition::WrongCost;
|
Chris@84
|
315 err.r = r;
|
Chris@84
|
316 err.c = c;
|
Chris@84
|
317 err.costWas = m_m->getPathCost(r, c);
|
Chris@84
|
318 err.costShouldBe = updateTo;
|
Chris@84
|
319 err.advanceWas = m_m->getAdvance(r, c);
|
Chris@84
|
320 err.advanceShouldBe = dir;
|
Chris@82
|
321 return err;
|
Chris@82
|
322 }
|
Chris@82
|
323 } else {
|
Chris@82
|
324 // AdvanceNone should occur only at r = r1, c = c1
|
Chris@82
|
325 if (r != r1 || c != c1) {
|
Chris@82
|
326 err.type = ErrorPosition::NoAdvance;
|
Chris@82
|
327 err.r = r;
|
Chris@82
|
328 err.c = c;
|
Chris@82
|
329 err.costWas = m_m->getPathCost(r, c);
|
Chris@82
|
330 err.costShouldBe = updateTo;
|
Chris@84
|
331 err.advanceWas = m_m->getAdvance(r, c);
|
Chris@84
|
332 err.advanceShouldBe = dir;
|
Chris@82
|
333 return err;
|
Chris@82
|
334 }
|
Chris@81
|
335 }
|
Chris@81
|
336 }
|
Chris@81
|
337
|
Chris@81
|
338 prevRowStart = rowStart;
|
Chris@81
|
339 prevRowStop = rowStop;
|
Chris@81
|
340 }
|
Chris@81
|
341
|
Chris@81
|
342 return err;
|
Chris@82
|
343 }
|
Chris@81
|
344
|
Chris@92
|
345 void
|
Chris@92
|
346 Finder::checkAndReport()
|
Chris@30
|
347 {
|
Chris@92
|
348 cerr << "Finder: Checking path-cost matrix..." << endl;
|
Chris@82
|
349 ErrorPosition err = checkPathCostMatrix();
|
Chris@92
|
350 if (err.type == ErrorPosition::NoError) {
|
Chris@92
|
351 cerr << "No errors found" << endl;
|
Chris@92
|
352 } else {
|
Chris@82
|
353 cerr << "\nWARNING: Checking path-cost matrix returned mismatch:" << endl;
|
Chris@92
|
354 cerr << "Type: " << err.type << ": ";
|
Chris@92
|
355 switch (err.type) {
|
Chris@92
|
356 case ErrorPosition::NoError: break;
|
Chris@92
|
357 case ErrorPosition::WrongCost: cerr << "WrongCost"; break;
|
Chris@92
|
358 case ErrorPosition::WrongAdvance: cerr << "WrongAdvance"; break;
|
Chris@92
|
359 case ErrorPosition::NoAdvance: cerr << "NoAdvance"; break;
|
Chris@92
|
360 }
|
Chris@92
|
361 cerr << endl;
|
Chris@84
|
362 cerr << "At row " << err.r << ", column " << err.c
|
Chris@84
|
363 << "\nShould be advancing "
|
Chris@84
|
364 << Matcher::advanceToString(err.advanceShouldBe)
|
Chris@84
|
365 << ", advance in matrix is "
|
Chris@84
|
366 << Matcher::advanceToString(err.advanceWas)
|
Chris@83
|
367 << "\nPrev cost " << err.prevCost
|
Chris@191
|
368 << " plus distance " << distance_print_t(err.distance)
|
Chris@191
|
369 << " [perhaps diagonalised] gives "
|
Chris@84
|
370 << err.costShouldBe << ", matrix contains " << err.costWas
|
Chris@83
|
371 << endl;
|
Chris@83
|
372 cerr << "Note: diagonal weight = " << m_m->getDiagonalWeight() << endl;
|
Chris@83
|
373 cerr << endl;
|
Chris@92
|
374
|
Chris@95
|
375 int w(4);
|
Chris@95
|
376 int ww(15);
|
Chris@92
|
377
|
Chris@92
|
378 cerr << "Distance matrix leading up to this point:" << endl;
|
Chris@95
|
379 cerr << setprecision(12) << setw(w) << "";
|
Chris@92
|
380 for (int i = -4; i <= 0; ++i) {
|
Chris@95
|
381 cerr << setw(ww) << i;
|
Chris@92
|
382 }
|
Chris@92
|
383 cerr << endl;
|
Chris@92
|
384 for (int j = -4; j <= 0; ++j) {
|
Chris@92
|
385 cerr << setw(w) << j;
|
Chris@92
|
386 for (int i = -4; i <= 0; ++i) {
|
Chris@191
|
387 cerr << setw(ww)
|
Chris@191
|
388 << distance_print_t(m_m->getDistance(err.r + j, err.c + i));
|
Chris@92
|
389 }
|
Chris@92
|
390 cerr << endl;
|
Chris@92
|
391 }
|
Chris@92
|
392 cerr << endl;
|
Chris@92
|
393
|
Chris@92
|
394 cerr << "Cost matrix leading up to this point:" << endl;
|
Chris@92
|
395 cerr << setw(w) << "";
|
Chris@92
|
396 for (int i = -4; i <= 0; ++i) {
|
Chris@95
|
397 cerr << setw(ww) << i;
|
Chris@92
|
398 }
|
Chris@92
|
399 cerr << endl;
|
Chris@92
|
400 for (int j = -4; j <= 0; ++j) {
|
Chris@92
|
401 cerr << setw(w) << j;
|
Chris@92
|
402 for (int i = -4; i <= 0; ++i) {
|
Chris@95
|
403 cerr << setw(ww) << m_m->getPathCost(err.r + j, err.c + i);
|
Chris@92
|
404 }
|
Chris@92
|
405 cerr << endl;
|
Chris@92
|
406 }
|
Chris@92
|
407 cerr << endl;
|
Chris@82
|
408 }
|
Chris@92
|
409 }
|
Chris@92
|
410 #endif
|
Chris@92
|
411
|
Chris@182
|
412 pathcost_t
|
Chris@163
|
413 Finder::getOverallCost()
|
Chris@163
|
414 {
|
Chris@163
|
415 int ex = m_m->getOtherFrameCount() - 1;
|
Chris@163
|
416 int ey = m_m->getFrameCount() - 1;
|
Chris@163
|
417
|
Chris@163
|
418 if (ex < 0 || ey < 0) {
|
Chris@163
|
419 return 0;
|
Chris@163
|
420 }
|
Chris@163
|
421
|
Chris@165
|
422 return m_m->getPathCost(ey, ex);
|
Chris@163
|
423 }
|
Chris@163
|
424
|
Chris@92
|
425 int
|
Chris@92
|
426 Finder::retrievePath(bool smooth, vector<int> &pathx, vector<int> &pathy)
|
Chris@92
|
427 {
|
Chris@92
|
428 pathx.clear();
|
Chris@92
|
429 pathy.clear();
|
Chris@92
|
430
|
Chris@92
|
431 #ifdef PERFORM_ERROR_CHECKS
|
Chris@92
|
432 checkAndReport();
|
Chris@82
|
433 #endif
|
Chris@82
|
434
|
Chris@72
|
435 int ex = m_m->getOtherFrameCount() - 1;
|
Chris@72
|
436 int ey = m_m->getFrameCount() - 1;
|
Chris@69
|
437
|
Chris@69
|
438 if (ex < 0 || ey < 0) {
|
Chris@69
|
439 return 0;
|
Chris@69
|
440 }
|
Chris@66
|
441
|
Chris@66
|
442 int x = ex;
|
Chris@66
|
443 int y = ey;
|
Chris@30
|
444
|
Chris@140
|
445 #ifdef DEBUG_FINDER
|
Chris@140
|
446 cerr << "*** retrievePath: smooth = " << smooth << endl;
|
Chris@140
|
447 cerr << "*** retrievePath: before: x = " << x << ", y = " << y << endl;
|
Chris@140
|
448 #endif
|
Chris@140
|
449
|
Chris@72
|
450 if (m_duration2 > 0 && m_duration2 < m_m->getOtherFrameCount()) {
|
Chris@72
|
451 x = m_duration2 - 1;
|
Chris@60
|
452 }
|
Chris@72
|
453 if (m_duration1 > 0 && m_duration1 < m_m->getFrameCount()) {
|
Chris@72
|
454 y = m_duration1 - 1;
|
Chris@60
|
455 }
|
Chris@60
|
456
|
Chris@72
|
457 if (!m_m->isAvailable(y, x)) {
|
Chris@66
|
458 // Path did not pass through the expected end point --
|
Chris@66
|
459 // probably means the pieces are substantially different in
|
Chris@66
|
460 // the later bits. Reset the expected end point to the end of
|
Chris@66
|
461 // both files including any trailing silence.
|
Chris@66
|
462 cerr << "NOTE: Path did not pass through expected end point, inputs are probably significantly different" << endl;
|
Chris@66
|
463 x = ex;
|
Chris@66
|
464 y = ey;
|
Chris@66
|
465 }
|
Chris@66
|
466
|
Chris@55
|
467 recalculatePathCostMatrix(0, 0, y, x);
|
Chris@55
|
468
|
Chris@140
|
469 #ifdef DEBUG_FINDER
|
Chris@140
|
470 cerr << "*** retrievePath: start: x = " << x << ", y = " << y << endl;
|
Chris@140
|
471 #endif
|
Chris@66
|
472
|
Chris@72
|
473 while (m_m->isAvailable(y, x) && (x > 0 || y > 0)) {
|
Chris@30
|
474
|
Chris@33
|
475 // cerr << "x = " << x << ", y = " << y;
|
Chris@33
|
476
|
Chris@30
|
477 pathx.push_back(x);
|
Chris@30
|
478 pathy.push_back(y);
|
Chris@30
|
479
|
Chris@72
|
480 switch (m_m->getAdvance(y, x)) {
|
Chris@181
|
481 case AdvanceThis:
|
Chris@70
|
482 // cerr << ", going down (dist = " << getDistance() << ")" << endl;
|
Chris@33
|
483 y--;
|
Chris@33
|
484 break;
|
Chris@181
|
485 case AdvanceOther:
|
Chris@70
|
486 // cerr << ", going left (dist = " << getDistance() << ")" << endl;
|
Chris@33
|
487 x--;
|
Chris@33
|
488 break;
|
Chris@181
|
489 case AdvanceBoth:
|
Chris@70
|
490 // cerr << ", going diag (dist = " << getDistance() << ")" << endl;
|
Chris@33
|
491 x--;
|
Chris@33
|
492 y--;
|
Chris@33
|
493 break;
|
Chris@181
|
494 case AdvanceNone: // this would indicate a bug, but we wouldn't want to hang
|
Chris@69
|
495 cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl;
|
Chris@33
|
496 if (x > y) {
|
Chris@33
|
497 x--;
|
Chris@33
|
498 } else {
|
Chris@33
|
499 y--;
|
Chris@33
|
500 }
|
Chris@33
|
501 break;
|
Chris@30
|
502 }
|
Chris@30
|
503 }
|
Chris@30
|
504
|
Chris@72
|
505 if (x > 0 || y > 0) {
|
Chris@72
|
506 cerr << "WARNING: Ran out of available path at (" << y << "," << x
|
Chris@72
|
507 << ")!" << endl;
|
Chris@72
|
508 }
|
Chris@72
|
509
|
Chris@72
|
510 reverse(pathx.begin(), pathx.end());
|
Chris@72
|
511 reverse(pathy.begin(), pathy.end());
|
Chris@30
|
512
|
Chris@31
|
513 if (smooth) {
|
Chris@180
|
514 int smoothedLen = Path().smooth
|
Chris@180
|
515 (pathx, pathy, static_cast<int>(pathx.size()));
|
Chris@31
|
516 return smoothedLen;
|
Chris@31
|
517 } else {
|
Chris@180
|
518 return static_cast<int>(pathx.size());
|
Chris@31
|
519 }
|
Chris@30
|
520 }
|
Chris@30
|
521
|