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