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 "Matcher.h"
|
cannam@0
|
18
|
cannam@0
|
19 #include <iostream>
|
cannam@0
|
20
|
cannam@4
|
21 #include <cstdlib>
|
Chris@16
|
22 #include <cassert>
|
cannam@4
|
23
|
Chris@72
|
24 using namespace std;
|
Chris@72
|
25
|
Chris@10
|
26 //#define DEBUG_MATCHER 1
|
Chris@10
|
27
|
Chris@143
|
28 Matcher::Matcher(Parameters parameters, DistanceMetric::Parameters dparams,
|
Chris@143
|
29 Matcher *p) :
|
Chris@43
|
30 m_params(parameters),
|
Chris@143
|
31 m_metric(dparams)
|
Chris@23
|
32 {
|
Chris@23
|
33 #ifdef DEBUG_MATCHER
|
Chris@143
|
34 cerr << "*** Matcher: hopTime = " << parameters.hopTime
|
Chris@140
|
35 << ", blockTime = " << parameters.blockTime
|
Chris@140
|
36 << ", maxRunCount = " << parameters.maxRunCount
|
Chris@140
|
37 << ", diagonalWeight = " << parameters.diagonalWeight << endl;
|
Chris@23
|
38 #endif
|
Chris@140
|
39
|
Chris@43
|
40 m_otherMatcher = p; // the first matcher will need this to be set later
|
Chris@43
|
41 m_firstPM = (!p);
|
Chris@43
|
42 m_frameCount = 0;
|
Chris@43
|
43 m_runCount = 0;
|
Chris@43
|
44 m_blockSize = 0;
|
Chris@202
|
45 m_distXSize = 0;
|
cannam@0
|
46
|
Chris@180
|
47 m_blockSize = int(m_params.blockTime / m_params.hopTime + 0.5);
|
Chris@15
|
48 #ifdef DEBUG_MATCHER
|
Chris@43
|
49 cerr << "Matcher: m_blockSize = " << m_blockSize << endl;
|
Chris@15
|
50 #endif
|
cannam@0
|
51
|
Chris@43
|
52 m_initialised = false;
|
Chris@23
|
53 }
|
cannam@0
|
54
|
cannam@0
|
55 Matcher::~Matcher()
|
cannam@0
|
56 {
|
Chris@10
|
57 #ifdef DEBUG_MATCHER
|
Chris@15
|
58 cerr << "Matcher(" << this << ")::~Matcher()" << endl;
|
Chris@10
|
59 #endif
|
cannam@0
|
60 }
|
cannam@0
|
61
|
cannam@0
|
62 void
|
cannam@0
|
63 Matcher::init()
|
cannam@0
|
64 {
|
Chris@43
|
65 if (m_initialised) return;
|
cannam@0
|
66
|
Chris@182
|
67 m_features = featureseq_t(m_blockSize);
|
cannam@0
|
68
|
Chris@43
|
69 m_distXSize = m_blockSize * 2;
|
Chris@45
|
70
|
Chris@41
|
71 size();
|
cannam@0
|
72
|
Chris@43
|
73 m_frameCount = 0;
|
Chris@43
|
74 m_runCount = 0;
|
Chris@38
|
75
|
Chris@43
|
76 m_initialised = true;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@87
|
79 bool
|
Chris@203
|
80 Matcher::isAvailable(int i, int j)
|
Chris@154
|
81 {
|
Chris@203
|
82 if (m_firstPM) {
|
Chris@203
|
83 if (isInRange(i, j)) {
|
Chris@203
|
84 return (m_bestPathCost[i][j - m_first[i]] != InvalidPathCost);
|
Chris@203
|
85 } else {
|
Chris@203
|
86 return false;
|
Chris@154
|
87 }
|
Chris@203
|
88 } else {
|
Chris@203
|
89 return m_otherMatcher->isAvailable(j, i);
|
Chris@154
|
90 }
|
Chris@154
|
91 }
|
Chris@154
|
92
|
Chris@154
|
93 bool
|
Chris@203
|
94 Matcher::isRowAvailable(int i)
|
Chris@154
|
95 {
|
Chris@203
|
96 if (m_firstPM) {
|
Chris@203
|
97
|
Chris@203
|
98 if (i < 0 || i >= int(m_first.size())) return false;
|
Chris@203
|
99 for (auto c: m_bestPathCost[i]) {
|
Chris@203
|
100 if (c != InvalidPathCost) return true;
|
Chris@203
|
101 }
|
Chris@203
|
102 return false;
|
Chris@203
|
103
|
Chris@203
|
104 } else {
|
Chris@203
|
105 return m_otherMatcher->isColAvailable(i);
|
Chris@203
|
106 }
|
Chris@203
|
107 }
|
Chris@203
|
108
|
Chris@203
|
109 bool
|
Chris@203
|
110 Matcher::isColAvailable(int j)
|
Chris@203
|
111 {
|
Chris@203
|
112 if (m_firstPM) {
|
Chris@203
|
113 for (int i = 0; i < int(m_first.size()); ++i) {
|
Chris@203
|
114 if (j >= m_first[i] &&
|
Chris@203
|
115 j < int(m_first[i] + m_bestPathCost[i].size())) {//!!! m_last[i]?
|
Chris@203
|
116 if (m_bestPathCost[i][j - m_first[i]] != InvalidPathCost) {
|
Chris@203
|
117 return true;
|
Chris@203
|
118 }
|
Chris@203
|
119 }
|
Chris@203
|
120 }
|
Chris@203
|
121 return false;
|
Chris@203
|
122 } else {
|
Chris@203
|
123 return m_otherMatcher->isRowAvailable(j);
|
Chris@203
|
124 }
|
Chris@154
|
125 }
|
Chris@154
|
126
|
Chris@154
|
127 bool
|
Chris@87
|
128 Matcher::isInRange(int i, int j)
|
Chris@87
|
129 {
|
Chris@87
|
130 if (m_firstPM) {
|
Chris@87
|
131 return ((i >= 0) &&
|
Chris@87
|
132 (i < int(m_first.size())) &&
|
Chris@87
|
133 (j >= m_first[i]) &&
|
Chris@87
|
134 (j < int(m_first[i] + m_bestPathCost[i].size())));
|
Chris@87
|
135 } else {
|
Chris@87
|
136 return m_otherMatcher->isInRange(j, i);
|
Chris@87
|
137 }
|
Chris@87
|
138 }
|
Chris@87
|
139
|
Chris@87
|
140 pair<int, int>
|
Chris@87
|
141 Matcher::getColRange(int i)
|
Chris@87
|
142 {
|
Chris@87
|
143 if (i < 0 || i >= int(m_first.size())) {
|
Chris@87
|
144 cerr << "ERROR: Matcher::getColRange(" << i << "): Index out of range"
|
Chris@87
|
145 << endl;
|
Chris@87
|
146 throw "Index out of range";
|
Chris@87
|
147 } else {
|
Chris@87
|
148 return pair<int, int>(m_first[i], m_last[i]);
|
Chris@87
|
149 }
|
Chris@87
|
150 }
|
Chris@87
|
151
|
Chris@87
|
152 pair<int, int>
|
Chris@87
|
153 Matcher::getRowRange(int i)
|
Chris@87
|
154 {
|
Chris@87
|
155 return m_otherMatcher->getColRange(i);
|
Chris@87
|
156 }
|
Chris@87
|
157
|
Chris@182
|
158 distance_t
|
Chris@87
|
159 Matcher::getDistance(int i, int j)
|
Chris@87
|
160 {
|
Chris@87
|
161 if (m_firstPM) {
|
Chris@87
|
162 if (!isInRange(i, j)) {
|
Chris@87
|
163 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
164 << "Location is not in range" << endl;
|
Chris@87
|
165 throw "Distance not available";
|
Chris@87
|
166 }
|
Chris@182
|
167 distance_t dist = m_distance[i][j - m_first[i]];
|
Chris@183
|
168 if (dist == InvalidDistance) {
|
Chris@87
|
169 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
170 << "Location is in range, but distance ("
|
Chris@191
|
171 << distance_print_t(dist)
|
Chris@191
|
172 << ") is invalid or has not been set" << endl;
|
Chris@87
|
173 throw "Distance not available";
|
Chris@87
|
174 }
|
Chris@87
|
175 return dist;
|
Chris@87
|
176 } else {
|
Chris@87
|
177 return m_otherMatcher->getDistance(j, i);
|
Chris@87
|
178 }
|
Chris@87
|
179 }
|
Chris@87
|
180
|
Chris@87
|
181 void
|
Chris@182
|
182 Matcher::setDistance(int i, int j, distance_t distance)
|
Chris@87
|
183 {
|
Chris@87
|
184 if (m_firstPM) {
|
Chris@87
|
185 if (!isInRange(i, j)) {
|
Chris@87
|
186 cerr << "ERROR: Matcher::setDistance(" << i << ", " << j << ", "
|
Chris@191
|
187 << distance_print_t(distance)
|
Chris@191
|
188 << "): Location is out of range" << endl;
|
Chris@87
|
189 throw "Indices out of range";
|
Chris@87
|
190 }
|
Chris@87
|
191 m_distance[i][j - m_first[i]] = distance;
|
Chris@87
|
192 } else {
|
Chris@87
|
193 m_otherMatcher->setDistance(j, i, distance);
|
Chris@87
|
194 }
|
Chris@87
|
195 }
|
Chris@87
|
196
|
Chris@191
|
197 normpathcost_t
|
Chris@135
|
198 Matcher::getNormalisedPathCost(int i, int j)
|
Chris@135
|
199 {
|
Chris@135
|
200 // normalised for path length. 1+ prevents division by zero here
|
Chris@191
|
201 return normpathcost_t(getPathCost(i, j)) / normpathcost_t(1 + i + j);
|
Chris@135
|
202 }
|
Chris@135
|
203
|
Chris@182
|
204 pathcost_t
|
Chris@87
|
205 Matcher::getPathCost(int i, int j)
|
Chris@87
|
206 {
|
Chris@87
|
207 if (m_firstPM) {
|
Chris@203
|
208 #ifdef PERFORM_ERROR_CHECKS
|
Chris@87
|
209 if (!isAvailable(i, j)) {
|
Chris@87
|
210 if (!isInRange(i, j)) {
|
Chris@87
|
211 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
212 << "Location is not in range" << endl;
|
Chris@87
|
213 } else {
|
Chris@87
|
214 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
215 << "Location is in range, but pathCost ("
|
Chris@87
|
216 << m_bestPathCost[i][j - m_first[i]]
|
Chris@87
|
217 << ") is invalid or has not been set" << endl;
|
Chris@87
|
218 }
|
Chris@87
|
219 throw "Path cost not available";
|
Chris@87
|
220 }
|
Chris@203
|
221 #endif
|
Chris@87
|
222 return m_bestPathCost[i][j - m_first[i]];
|
Chris@87
|
223 } else {
|
Chris@87
|
224 return m_otherMatcher->getPathCost(j, i);
|
Chris@87
|
225 }
|
Chris@87
|
226 }
|
Chris@87
|
227
|
Chris@87
|
228 void
|
Chris@182
|
229 Matcher::setPathCost(int i, int j, advance_t dir, pathcost_t pathCost)
|
Chris@87
|
230 {
|
Chris@87
|
231 if (m_firstPM) {
|
Chris@87
|
232 if (!isInRange(i, j)) {
|
Chris@87
|
233 cerr << "ERROR: Matcher::setPathCost(" << i << ", " << j << ", "
|
Chris@87
|
234 << dir << ", " << pathCost
|
Chris@87
|
235 << "): Location is out of range" << endl;
|
Chris@87
|
236 throw "Indices out of range";
|
Chris@87
|
237 }
|
Chris@87
|
238 m_advance[i][j - m_first[i]] = dir;
|
Chris@87
|
239 m_bestPathCost[i][j - m_first[i]] = pathCost;
|
Chris@87
|
240 } else {
|
Chris@87
|
241 if (dir == AdvanceThis) {
|
Chris@87
|
242 dir = AdvanceOther;
|
Chris@87
|
243 } else if (dir == AdvanceOther) {
|
Chris@87
|
244 dir = AdvanceThis;
|
Chris@87
|
245 }
|
Chris@87
|
246 m_otherMatcher->setPathCost(j, i, dir, pathCost);
|
Chris@87
|
247 }
|
Chris@87
|
248 }
|
Chris@87
|
249
|
cannam@0
|
250 void
|
Chris@41
|
251 Matcher::size()
|
cannam@0
|
252 {
|
Chris@43
|
253 int distSize = (m_params.maxRunCount + 1) * m_blockSize;
|
Chris@183
|
254 m_bestPathCost.resize(m_distXSize, pathcostvec_t(distSize, InvalidPathCost));
|
Chris@183
|
255 m_distance.resize(m_distXSize, distancevec_t(distSize, InvalidDistance));
|
Chris@182
|
256 m_advance.resize(m_distXSize, advancevec_t(distSize, AdvanceNone));
|
Chris@43
|
257 m_first.resize(m_distXSize, 0);
|
Chris@43
|
258 m_last.resize(m_distXSize, 0);
|
Chris@38
|
259 }
|
cannam@0
|
260
|
Chris@23
|
261 void
|
Chris@182
|
262 Matcher::consumeFeatureVector(const feature_t &feature)
|
Chris@23
|
263 {
|
Chris@43
|
264 if (!m_initialised) init();
|
Chris@43
|
265 int frameIndex = m_frameCount % m_blockSize;
|
Chris@182
|
266 m_features[frameIndex] = feature;
|
Chris@23
|
267 calcAdvance();
|
Chris@21
|
268 }
|
Chris@21
|
269
|
Chris@21
|
270 void
|
Chris@21
|
271 Matcher::calcAdvance()
|
Chris@21
|
272 {
|
Chris@43
|
273 int frameIndex = m_frameCount % m_blockSize;
|
Chris@21
|
274
|
Chris@43
|
275 if (m_frameCount >= m_distXSize) {
|
Chris@43
|
276 m_distXSize *= 2;
|
Chris@41
|
277 size();
|
cannam@0
|
278 }
|
cannam@0
|
279
|
Chris@43
|
280 if (m_firstPM && (m_frameCount >= m_blockSize)) {
|
cannam@0
|
281
|
Chris@43
|
282 int len = m_last[m_frameCount - m_blockSize] -
|
Chris@43
|
283 m_first[m_frameCount - m_blockSize];
|
cannam@0
|
284
|
Chris@43
|
285 // We need to copy distance[m_frameCount-m_blockSize] to
|
Chris@43
|
286 // distance[m_frameCount], and then truncate
|
Chris@43
|
287 // distance[m_frameCount-m_blockSize] to its first len elements.
|
cannam@0
|
288 // Same for bestPathCost.
|
cannam@0
|
289
|
Chris@182
|
290 distancevec_t dOld(m_distance[m_frameCount - m_blockSize]);
|
Chris@183
|
291 distancevec_t dNew(len, InvalidDistance);
|
cannam@0
|
292
|
Chris@182
|
293 pathcostvec_t bpcOld(m_bestPathCost[m_frameCount - m_blockSize]);
|
Chris@183
|
294 pathcostvec_t bpcNew(len, InvalidPathCost);
|
Chris@69
|
295
|
Chris@182
|
296 advancevec_t adOld(m_advance[m_frameCount - m_blockSize]);
|
Chris@182
|
297 advancevec_t adNew(len, AdvanceNone);
|
Chris@69
|
298
|
Chris@69
|
299 for (int i = 0; i < len; ++i) {
|
Chris@69
|
300 dNew[i] = dOld[i];
|
Chris@69
|
301 bpcNew[i] = bpcOld[i];
|
Chris@69
|
302 adNew[i] = adOld[i];
|
Chris@69
|
303 }
|
Chris@45
|
304
|
Chris@69
|
305 m_distance[m_frameCount] = dOld;
|
Chris@69
|
306 m_distance[m_frameCount - m_blockSize] = dNew;
|
Chris@69
|
307
|
Chris@69
|
308 m_bestPathCost[m_frameCount] = bpcOld;
|
Chris@69
|
309 m_bestPathCost[m_frameCount - m_blockSize] = bpcNew;
|
Chris@69
|
310
|
Chris@69
|
311 m_advance[m_frameCount] = adOld;
|
Chris@69
|
312 m_advance[m_frameCount - m_blockSize] = adNew;
|
cannam@0
|
313 }
|
cannam@0
|
314
|
Chris@43
|
315 int stop = m_otherMatcher->m_frameCount;
|
Chris@43
|
316 int index = stop - m_blockSize;
|
Chris@86
|
317 if (index < 0) index = 0;
|
Chris@86
|
318
|
Chris@43
|
319 m_first[m_frameCount] = index;
|
Chris@43
|
320 m_last[m_frameCount] = stop;
|
cannam@0
|
321
|
cannam@0
|
322 for ( ; index < stop; index++) {
|
Chris@26
|
323
|
Chris@188
|
324 distance_t distance = m_metric.calcDistance
|
Chris@182
|
325 (m_features[frameIndex],
|
Chris@182
|
326 m_otherMatcher->m_features[index % m_blockSize]);
|
Chris@26
|
327
|
Chris@188
|
328 pathcost_t straightIncrement(distance);
|
Chris@188
|
329 pathcost_t diagIncrement = pathcost_t(distance * m_params.diagonalWeight);
|
Chris@89
|
330
|
Chris@86
|
331 if ((m_frameCount == 0) && (index == 0)) { // first element
|
Chris@86
|
332
|
Chris@86
|
333 updateValue(0, 0, AdvanceNone,
|
Chris@86
|
334 0,
|
Chris@86
|
335 distance);
|
Chris@86
|
336
|
Chris@86
|
337 } else if (m_frameCount == 0) { // first row
|
Chris@86
|
338
|
Chris@71
|
339 updateValue(0, index, AdvanceOther,
|
Chris@86
|
340 getPathCost(0, index-1),
|
Chris@86
|
341 distance);
|
Chris@86
|
342
|
Chris@86
|
343 } else if (index == 0) { // first column
|
Chris@86
|
344
|
Chris@71
|
345 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
346 getPathCost(m_frameCount - 1, 0),
|
Chris@86
|
347 distance);
|
Chris@86
|
348
|
Chris@86
|
349 } else if (index == m_otherMatcher->m_frameCount - m_blockSize) {
|
Chris@86
|
350
|
cannam@0
|
351 // missing value(s) due to cutoff
|
cannam@0
|
352 // - no previous value in current row (resp. column)
|
cannam@0
|
353 // - no diagonal value if prev. dir. == curr. dirn
|
Chris@86
|
354
|
Chris@182
|
355 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@86
|
356
|
Chris@91
|
357 // cerr << "NOTE: missing value at i = " << m_frameCount << ", j = "
|
Chris@91
|
358 // << index << " (first = " << m_firstPM << ")" << endl;
|
Chris@86
|
359
|
Chris@43
|
360 // if ((m_firstPM && (first[m_frameCount - 1] == index)) ||
|
Chris@43
|
361 // (!m_firstPM && (m_last[index-1] < m_frameCount)))
|
Chris@86
|
362 if (m_first[m_frameCount - 1] == index) {
|
Chris@86
|
363
|
Chris@86
|
364 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
365 min2, distance);
|
Chris@86
|
366
|
Chris@86
|
367 } else {
|
Chris@86
|
368
|
Chris@182
|
369 pathcost_t min1 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@188
|
370 if (min1 + diagIncrement <= min2 + straightIncrement) {
|
Chris@86
|
371 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
372 min1, distance);
|
Chris@86
|
373 } else {
|
Chris@86
|
374 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
375 min2, distance);
|
Chris@86
|
376 }
|
cannam@0
|
377 }
|
Chris@86
|
378
|
cannam@0
|
379 } else {
|
Chris@86
|
380
|
Chris@182
|
381 pathcost_t min1 = getPathCost(m_frameCount, index - 1);
|
Chris@182
|
382 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@182
|
383 pathcost_t min3 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@87
|
384
|
Chris@188
|
385 pathcost_t cost1 = min1 + straightIncrement;
|
Chris@188
|
386 pathcost_t cost2 = min2 + straightIncrement;
|
Chris@188
|
387 pathcost_t cost3 = min3 + diagIncrement;
|
Chris@93
|
388
|
Chris@93
|
389 // Choosing is easy if there is a strict cheapest of the
|
Chris@93
|
390 // three. If two or more share the lowest cost, we choose
|
Chris@93
|
391 // in order of preference: cost3 (AdvanceBoth), cost2
|
Chris@94
|
392 // (AdvanceThis), cost1 (AdvanceOther) if we are the first
|
Chris@94
|
393 // matcher; and cost3 (AdvanceBoth), cost1 (AdvanceOther),
|
Chris@94
|
394 // cost2 (AdvanceThis) if we are the second matcher. That
|
Chris@94
|
395 // is, we always prioritise the diagonal followed by the
|
Chris@94
|
396 // first matcher.
|
Chris@94
|
397
|
Chris@94
|
398 if (( m_firstPM && (cost1 < cost2)) ||
|
Chris@94
|
399 (!m_firstPM && (cost1 <= cost2))) {
|
Chris@89
|
400 if (cost3 <= cost1) {
|
Chris@86
|
401 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
402 min3, distance);
|
Chris@86
|
403 } else {
|
Chris@86
|
404 updateValue(m_frameCount, index, AdvanceOther,
|
Chris@86
|
405 min1, distance);
|
Chris@86
|
406 }
|
cannam@0
|
407 } else {
|
Chris@89
|
408 if (cost3 <= cost2) {
|
Chris@86
|
409 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
410 min3, distance);
|
Chris@86
|
411 } else {
|
Chris@86
|
412 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
413 min2, distance);
|
Chris@86
|
414 }
|
cannam@0
|
415 }
|
cannam@0
|
416 }
|
Chris@86
|
417
|
Chris@43
|
418 m_otherMatcher->m_last[index]++;
|
cannam@0
|
419 } // loop for row (resp. column)
|
cannam@0
|
420
|
Chris@43
|
421 m_frameCount++;
|
Chris@43
|
422 m_runCount++;
|
cannam@0
|
423
|
Chris@43
|
424 m_otherMatcher->m_runCount = 0;
|
Chris@21
|
425 }
|
cannam@0
|
426
|
cannam@0
|
427 void
|
Chris@182
|
428 Matcher::updateValue(int i, int j, advance_t dir, pathcost_t value, distance_t distance)
|
cannam@0
|
429 {
|
Chris@188
|
430 pathcost_t increment = distance;
|
Chris@83
|
431 if (dir == AdvanceBoth) {
|
Chris@188
|
432 increment = pathcost_t(increment * m_params.diagonalWeight);
|
Chris@188
|
433 }
|
Chris@188
|
434
|
Chris@188
|
435 pathcost_t newValue = value + increment;
|
Chris@188
|
436 if (MaxPathCost - increment < value) {
|
Chris@188
|
437 cerr << "ERROR: Path cost overflow at i=" << i << ", j=" << j << ": "
|
Chris@188
|
438 << value << " + " << increment << " > " << MaxPathCost << endl;
|
Chris@188
|
439 newValue = MaxPathCost;
|
Chris@83
|
440 }
|
Chris@83
|
441
|
Chris@43
|
442 if (m_firstPM) {
|
Chris@45
|
443
|
Chris@96
|
444 setDistance(i, j, distance);
|
Chris@188
|
445 setPathCost(i, j, dir, newValue);
|
Chris@45
|
446
|
cannam@0
|
447 } else {
|
Chris@45
|
448
|
Chris@86
|
449 if (dir == AdvanceThis) dir = AdvanceOther;
|
Chris@86
|
450 else if (dir == AdvanceOther) dir = AdvanceThis;
|
Chris@84
|
451
|
Chris@43
|
452 int idx = i - m_otherMatcher->m_first[j];
|
Chris@45
|
453
|
Chris@188
|
454 if (idx < 0 || size_t(idx) == m_otherMatcher->m_distance[j].size()) {
|
cannam@0
|
455 // This should never happen, but if we allow arbitrary
|
cannam@0
|
456 // pauses in either direction, and arbitrary lengths at
|
cannam@0
|
457 // end, it is better than a segmentation fault.
|
Chris@72
|
458 cerr << "Emergency resize: " << idx << " -> " << idx * 2 << endl;
|
Chris@183
|
459 m_otherMatcher->m_bestPathCost[j].resize(idx * 2, InvalidPathCost);
|
Chris@183
|
460 m_otherMatcher->m_distance[j].resize(idx * 2, InvalidDistance);
|
Chris@46
|
461 m_otherMatcher->m_advance[j].resize(idx * 2, AdvanceNone);
|
cannam@0
|
462 }
|
Chris@45
|
463
|
Chris@96
|
464 m_otherMatcher->setDistance(j, i, distance);
|
Chris@188
|
465 m_otherMatcher->setPathCost(j, i, dir, newValue);
|
cannam@0
|
466 }
|
Chris@71
|
467 }
|
cannam@0
|
468
|
Chris@181
|
469 advance_t
|
Chris@72
|
470 Matcher::getAdvance(int i, int j)
|
Chris@72
|
471 {
|
Chris@72
|
472 if (m_firstPM) {
|
Chris@72
|
473 if (!isInRange(i, j)) {
|
Chris@72
|
474 cerr << "ERROR: Matcher::getAdvance(" << i << ", " << j << "): "
|
Chris@72
|
475 << "Location is not in range" << endl;
|
Chris@72
|
476 throw "Advance not available";
|
Chris@72
|
477 }
|
Chris@72
|
478 return m_advance[i][j - m_first[i]];
|
Chris@72
|
479 } else {
|
Chris@72
|
480 return m_otherMatcher->getAdvance(j, i);
|
Chris@72
|
481 }
|
Chris@72
|
482 }
|
Chris@202
|
483
|
Chris@202
|
484 static double k(size_t sz)
|
Chris@202
|
485 {
|
Chris@202
|
486 return double(sz) / 1024.0;
|
Chris@202
|
487 }
|
Chris@202
|
488
|
Chris@202
|
489 void
|
Chris@202
|
490 Matcher::printStats()
|
Chris@202
|
491 {
|
Chris@202
|
492 if (m_firstPM) cerr << endl;
|
Chris@202
|
493
|
Chris@202
|
494 cerr << "Matcher[" << this << "] (" << (m_firstPM ? "first" : "second") << "):" << endl;
|
Chris@202
|
495 cerr << "- block size " << m_blockSize << ", frame count " << m_frameCount << ", dist x size " << m_distXSize << ", initialised " << m_initialised << endl;
|
Chris@202
|
496
|
Chris@202
|
497 if (m_features.empty()) {
|
Chris@202
|
498 cerr << "- have no features yet" << endl;
|
Chris@202
|
499 } else {
|
Chris@202
|
500 cerr << "- have " << m_features.size() << " features of " << m_features[0].size() << " bins each (= "
|
Chris@202
|
501 << k(m_features.size() * m_features[0].size() * sizeof(featurebin_t)) << "K)" << endl;
|
Chris@202
|
502 }
|
Chris@202
|
503
|
Chris@202
|
504 size_t cells = 0;
|
Chris@202
|
505 for (const auto &d: m_distance) {
|
Chris@202
|
506 cells += d.size();
|
Chris@202
|
507 }
|
Chris@202
|
508 if (m_distance.empty()) {
|
Chris@202
|
509 cerr << "- have no cells in matrix" << endl;
|
Chris@202
|
510 } else {
|
Chris@202
|
511 cerr << "- have " << m_distance.size() << " cols in matrix with avg "
|
Chris@203
|
512 << double(cells) / double(m_distance.size()) << " rows, total "
|
Chris@202
|
513 << cells << " cells" << endl;
|
Chris@202
|
514 cerr << "- path costs " << k(cells * sizeof(pathcost_t))
|
Chris@202
|
515 << "K, distances " << k(cells * sizeof(distance_t))
|
Chris@202
|
516 << "K, advances " << k(cells * sizeof(advance_t)) << "K" << endl;
|
Chris@202
|
517 }
|
Chris@202
|
518
|
Chris@202
|
519 if (m_firstPM && m_otherMatcher) {
|
Chris@202
|
520 m_otherMatcher->printStats();
|
Chris@202
|
521 cerr << endl;
|
Chris@202
|
522 }
|
Chris@202
|
523 }
|
Chris@202
|
524
|