annotate Matcher.cpp @ 23:64c4c0cf80c9

Add alternate Matcher API calls to provide external feature data
author Chris Cannam
date Fri, 10 Oct 2014 16:27:45 +0100
parents b15106b0abcd
children 9f60d097f0b2
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 "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
cannam@0 24 bool Matcher::silent = true;
cannam@0 25
Chris@10 26 //#define DEBUG_MATCHER 1
Chris@10 27
Chris@15 28 Matcher::Matcher(Parameters parameters, Matcher *p) :
Chris@15 29 params(parameters)
cannam@0 30 {
Chris@10 31 #ifdef DEBUG_MATCHER
Chris@15 32 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ")" << endl;
Chris@10 33 #endif
cannam@0 34
cannam@0 35 otherMatcher = p; // the first matcher will need this to be set later
cannam@0 36 firstPM = (!p);
cannam@0 37 ltAverage = 0;
cannam@0 38 frameCount = 0;
cannam@0 39 runCount = 0;
Chris@23 40 freqMapSize = 0;
Chris@23 41 externalFeatureSize = 0;
Chris@23 42 featureSize = 0;
Chris@23 43 blockSize = 0;
Chris@23 44 scale = 90;
Chris@23 45
Chris@23 46 blockSize = lrint(params.blockTime / params.hopTime);
Chris@23 47 #ifdef DEBUG_MATCHER
Chris@23 48 cerr << "Matcher: blockSize = " << blockSize << endl;
Chris@23 49 #endif
Chris@23 50
Chris@23 51 distance = 0;
Chris@23 52 bestPathCost = 0;
Chris@23 53 distYSizes = 0;
Chris@23 54 distXSize = 0;
Chris@23 55
Chris@23 56 initialised = false;
Chris@23 57 }
Chris@23 58
Chris@23 59 Matcher::Matcher(Parameters parameters, Matcher *p, int featureSize) :
Chris@23 60 params(parameters),
Chris@23 61 externalFeatureSize(featureSize)
Chris@23 62 {
Chris@23 63 #ifdef DEBUG_MATCHER
Chris@23 64 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ", " << featureSize << ")" << endl;
Chris@23 65 #endif
Chris@23 66
Chris@23 67 otherMatcher = p; // the first matcher will need this to be set later
Chris@23 68 firstPM = (!p);
Chris@23 69 ltAverage = 0;
Chris@23 70 frameCount = 0;
Chris@23 71 runCount = 0;
Chris@23 72 freqMapSize = 0;
Chris@23 73 featureSize = 0;
cannam@0 74 blockSize = 0;
cannam@0 75 scale = 90;
cannam@0 76
Chris@15 77 blockSize = lrint(params.blockTime / params.hopTime);
Chris@15 78 #ifdef DEBUG_MATCHER
Chris@15 79 cerr << "Matcher: blockSize = " << blockSize << endl;
Chris@15 80 #endif
cannam@0 81
cannam@0 82 distance = 0;
cannam@0 83 bestPathCost = 0;
cannam@0 84 distYSizes = 0;
cannam@0 85 distXSize = 0;
cannam@0 86
cannam@0 87 initialised = false;
cannam@0 88
Chris@23 89 }
cannam@0 90
cannam@0 91 Matcher::~Matcher()
cannam@0 92 {
Chris@10 93 #ifdef DEBUG_MATCHER
Chris@15 94 cerr << "Matcher(" << this << ")::~Matcher()" << endl;
Chris@10 95 #endif
cannam@0 96
cannam@0 97 if (initialised) {
cannam@0 98
cannam@0 99 for (int i = 0; i < distXSize; ++i) {
cannam@0 100 if (distance[i]) {
cannam@0 101 free(distance[i]);
cannam@0 102 free(bestPathCost[i]);
cannam@0 103 }
cannam@0 104 }
cannam@0 105 free(distance);
cannam@0 106 free(bestPathCost);
cannam@0 107
cannam@0 108 free(first);
cannam@0 109 free(last);
cannam@0 110
cannam@0 111 free(distYSizes);
cannam@0 112 }
cannam@0 113 }
cannam@0 114
cannam@0 115 void
cannam@0 116 Matcher::init()
cannam@0 117 {
cannam@0 118 if (initialised) return;
cannam@0 119
cannam@0 120 initialised = true;
cannam@0 121
Chris@23 122 if (externalFeatureSize == 0) {
Chris@23 123 freqMapSize = getFeatureSizeFor(params);
Chris@23 124 featureSize = freqMapSize;
Chris@23 125 makeFreqMap();
Chris@23 126 } else {
Chris@23 127 featureSize = externalFeatureSize;
Chris@23 128 }
Chris@16 129
Chris@23 130 initVector<double>(prevFrame, featureSize);
Chris@23 131 initVector<double>(newFrame, featureSize);
Chris@23 132 initMatrix<double>(frames, blockSize, featureSize);
Chris@13 133 initVector<double>(totalEnergies, blockSize);
cannam@0 134
Chris@15 135 int distSize = (params.maxRunCount + 1) * blockSize;
cannam@0 136
cannam@0 137 distXSize = blockSize * 2;
cannam@0 138
cannam@0 139 distance = (unsigned char **)malloc(distXSize * sizeof(unsigned char *));
cannam@0 140 bestPathCost = (int **)malloc(distXSize * sizeof(int *));
cannam@0 141 distYSizes = (int *)malloc(distXSize * sizeof(int));
cannam@0 142
cannam@0 143 for (int i = 0; i < blockSize; ++i) {
cannam@0 144 distance[i] = (unsigned char *)malloc(distSize * sizeof(unsigned char));
cannam@0 145 bestPathCost[i] = (int *)malloc(distSize * sizeof(int));
cannam@0 146 distYSizes[i] = distSize;
cannam@0 147 }
cannam@0 148 for (int i = blockSize; i < distXSize; ++i) {
cannam@0 149 distance[i] = 0;
cannam@0 150 }
cannam@0 151
cannam@0 152 first = (int *)malloc(distXSize * sizeof(int));
cannam@0 153 last = (int *)malloc(distXSize * sizeof(int));
cannam@0 154
cannam@0 155 frameCount = 0;
cannam@0 156 runCount = 0;
cannam@0 157 ltAverage = 0;
cannam@0 158
cannam@0 159 } // init
cannam@0 160
cannam@0 161 void
Chris@15 162 Matcher::makeFreqMap()
cannam@0 163 {
Chris@15 164 initVector<int>(freqMap, params.fftSize/2 + 1);
Chris@23 165
Chris@15 166 if (params.useChromaFrequencyMap) {
Chris@15 167 #ifdef DEBUG_MATCHER
Chris@15 168 cerr << "makeFreqMap: calling makeChromaFrequencyMap" << endl;
Chris@15 169 #endif
Chris@15 170 makeChromaFrequencyMap();
Chris@15 171 } else {
Chris@15 172 #ifdef DEBUG_MATCHER
Chris@15 173 cerr << "makeFreqMap: calling makeStandardFrequencyMap" << endl;
Chris@15 174 #endif
Chris@15 175 makeStandardFrequencyMap();
Chris@15 176 }
cannam@0 177 } // makeFreqMap()
cannam@0 178
Chris@16 179 int
Chris@23 180 Matcher::getFeatureSizeFor(Parameters params)
Chris@16 181 {
Chris@16 182 if (params.useChromaFrequencyMap) {
Chris@16 183 return 13;
Chris@16 184 } else {
Chris@16 185 return 84;
Chris@16 186 }
Chris@16 187 }
Chris@16 188
cannam@0 189 void
Chris@15 190 Matcher::makeStandardFrequencyMap()
cannam@0 191 {
Chris@15 192 double binWidth = params.sampleRate / params.fftSize;
cannam@0 193 int crossoverBin = (int)(2 / (pow(2, 1/12.0) - 1));
cannam@7 194 int crossoverMidi = lrint(log(crossoverBin*binWidth/440.0)/
cannam@7 195 log(2.0) * 12 + 69);
cannam@0 196 // freq = 440 * Math.pow(2, (midi-69)/12.0) / binWidth;
cannam@0 197 int i = 0;
cannam@0 198 while (i <= crossoverBin) {
cannam@0 199 freqMap[i] = i;
cannam@0 200 ++i;
cannam@0 201 }
Chris@15 202 while (i <= params.fftSize/2) {
cannam@7 203 double midi = log(i*binWidth/440.0) / log(2.0) * 12 + 69;
Chris@16 204 if (midi > 127) midi = 127;
cannam@0 205 freqMap[i++] = crossoverBin + lrint(midi) - crossoverMidi;
cannam@0 206 }
Chris@16 207 assert(freqMapSize == freqMap[i-1] + 1);
cannam@0 208 if (!silent) {
cannam@0 209 cerr << "Standard map size: " << freqMapSize
cannam@0 210 << "; Crossover at: " << crossoverBin << endl;
Chris@15 211 for (i = 0; i < params.fftSize / 2; i++)
Chris@15 212 cerr << "freqMap[" << i << "] = " << freqMap[i] << endl;
cannam@0 213 }
cannam@0 214 } // makeStandardFrequencyMap()
cannam@0 215
cannam@0 216 void
Chris@15 217 Matcher::makeChromaFrequencyMap()
cannam@0 218 {
Chris@15 219 double binWidth = params.sampleRate / params.fftSize;
cannam@0 220 int crossoverBin = (int)(1 / (pow(2, 1/12.0) - 1));
cannam@0 221 // freq = 440 * Math.pow(2, (midi-69)/12.0) / binWidth;
cannam@0 222 int i = 0;
cannam@0 223 while (i <= crossoverBin)
cannam@0 224 freqMap[i++] = 0;
Chris@15 225 while (i <= params.fftSize/2) {
cannam@7 226 double midi = log(i*binWidth/440.0) / log(2.0) * 12 + 69;
cannam@0 227 freqMap[i++] = (lrint(midi)) % 12 + 1;
cannam@0 228 }
cannam@0 229 if (!silent) {
cannam@0 230 cerr << "Chroma map size: " << freqMapSize
cannam@0 231 << "; Crossover at: " << crossoverBin << endl;
Chris@15 232 for (i = 0; i < params.fftSize / 2; i++)
cannam@0 233 cerr << "freqMap[" << i << "] = " << freqMap[i] << endl;
cannam@0 234 }
cannam@0 235 } // makeChromaFrequencyMap()
cannam@0 236
Chris@14 237 vector<double>
Chris@21 238 Matcher::consumeFrame(double *reBuffer, double *imBuffer)
cannam@0 239 {
cannam@0 240 if (!initialised) init();
cannam@0 241
Chris@21 242 vector<double> processedFrame =
Chris@21 243 processFrameFromFreqData(reBuffer, imBuffer);
Chris@21 244
Chris@21 245 calcAdvance();
Chris@21 246
Chris@23 247 return processedFrame;
Chris@23 248 }
Chris@21 249
Chris@23 250 void
Chris@23 251 Matcher::consumeFeatureVector(std::vector<double> feature)
Chris@23 252 {
Chris@23 253 if (!initialised) init();
Chris@23 254 int frameIndex = frameCount % blockSize;
Chris@23 255 frames[frameIndex] = feature;
Chris@23 256 calcAdvance();
Chris@21 257 }
Chris@21 258
Chris@21 259 vector<double>
Chris@21 260 Matcher::processFrameFromFreqData(double *reBuffer, double *imBuffer)
Chris@21 261 {
cannam@0 262 for (int i = 0; i < (int)newFrame.size(); ++i) {
cannam@0 263 newFrame[i] = 0;
cannam@0 264 }
cannam@0 265 double rms = 0;
Chris@15 266 for (int i = 0; i <= params.fftSize/2; i++) {
cannam@0 267 double mag = reBuffer[i] * reBuffer[i] +
cannam@0 268 imBuffer[i] * imBuffer[i];
cannam@0 269 rms += mag;
cannam@0 270 newFrame[freqMap[i]] += mag;
cannam@0 271 }
Chris@15 272 rms = sqrt(rms / (params.fftSize/2));
cannam@0 273
cannam@0 274 int frameIndex = frameCount % blockSize;
cannam@0 275
Chris@21 276 vector<double> processedFrame(freqMapSize, 0.0);
Chris@21 277
Chris@21 278 double totalEnergy = 0;
Chris@21 279 if (params.useSpectralDifference) {
Chris@21 280 for (int i = 0; i < freqMapSize; i++) {
Chris@21 281 totalEnergy += newFrame[i];
Chris@21 282 if (newFrame[i] > prevFrame[i]) {
Chris@21 283 processedFrame[i] = newFrame[i] - prevFrame[i];
Chris@21 284 } else {
Chris@21 285 processedFrame[i] = 0;
Chris@21 286 }
Chris@21 287 }
Chris@21 288 } else {
Chris@21 289 for (int i = 0; i < freqMapSize; i++) {
Chris@21 290 processedFrame[i] = newFrame[i];
Chris@21 291 totalEnergy += processedFrame[i];
Chris@21 292 }
Chris@21 293 }
Chris@21 294 totalEnergies[frameIndex] = totalEnergy;
Chris@21 295
Chris@21 296 double decay = frameCount >= 200 ? 0.99:
Chris@21 297 (frameCount < 100? 0: (frameCount - 100) / 100.0);
Chris@21 298
Chris@21 299 if (ltAverage == 0)
Chris@21 300 ltAverage = totalEnergy;
Chris@21 301 else
Chris@21 302 ltAverage = ltAverage * decay + totalEnergy * (1.0 - decay);
Chris@21 303
Chris@21 304 if (rms <= params.silenceThreshold)
Chris@21 305 for (int i = 0; i < freqMapSize; i++)
Chris@21 306 processedFrame[i] = 0;
Chris@21 307 else if (params.frameNorm == NormaliseFrameToSum1)
Chris@21 308 for (int i = 0; i < freqMapSize; i++)
Chris@21 309 processedFrame[i] /= totalEnergy;
Chris@21 310 else if (params.frameNorm == NormaliseFrameToLTAverage)
Chris@21 311 for (int i = 0; i < freqMapSize; i++)
Chris@21 312 processedFrame[i] /= ltAverage;
Chris@21 313
Chris@21 314 vector<double> tmp = prevFrame;
Chris@21 315 prevFrame = newFrame;
Chris@21 316 newFrame = tmp;
Chris@21 317
Chris@21 318 frames[frameIndex] = processedFrame;
Chris@21 319
Chris@23 320 if ((frameCount % 100) == 0) {
Chris@23 321 if (!silent) {
Chris@23 322 cerr << "Progress:" << frameCount << " " << ltAverage << endl;
Chris@23 323 }
Chris@23 324 }
Chris@23 325
Chris@21 326 return processedFrame;
Chris@21 327 }
Chris@21 328
Chris@21 329 void
Chris@21 330 Matcher::calcAdvance()
Chris@21 331 {
Chris@21 332 int frameIndex = frameCount % blockSize;
Chris@21 333
cannam@0 334 if (frameCount >= distXSize) {
cannam@0 335 // std::cerr << "Resizing " << distXSize << " -> " << distXSize * 2 << std::endl;
cannam@0 336 distXSize *= 2;
cannam@0 337 distance = (unsigned char **)realloc(distance, distXSize * sizeof(unsigned char *));
cannam@0 338 bestPathCost = (int **)realloc(bestPathCost, distXSize * sizeof(int *));
cannam@0 339 distYSizes = (int *)realloc(distYSizes, distXSize * sizeof(int));
cannam@0 340 first = (int *)realloc(first, distXSize * sizeof(int));
cannam@0 341 last = (int *)realloc(last, distXSize * sizeof(int));
cannam@0 342
cannam@0 343 for (int i = distXSize/2; i < distXSize; ++i) {
cannam@0 344 distance[i] = 0;
cannam@0 345 }
cannam@0 346 }
cannam@0 347
cannam@0 348 if (firstPM && (frameCount >= blockSize)) {
cannam@0 349
cannam@0 350 int len = last[frameCount - blockSize] -
cannam@0 351 first[frameCount - blockSize];
cannam@0 352
cannam@0 353 // We need to copy distance[frameCount-blockSize] to
cannam@0 354 // distance[frameCount], and then truncate
cannam@0 355 // distance[frameCount-blockSize] to its first len elements.
cannam@0 356 // Same for bestPathCost.
cannam@0 357 /*
cannam@4 358 std::cerr << "Matcher(" << this << "): moving " << distYSizes[frameCount - blockSize] << " from " << frameCount - blockSize << " to "
cannam@0 359 << frameCount << ", allocating " << len << " for "
cannam@0 360 << frameCount - blockSize << std::endl;
cannam@0 361 */
cannam@0 362 distance[frameCount] = distance[frameCount - blockSize];
cannam@0 363
cannam@0 364 distance[frameCount - blockSize] = (unsigned char *)
cannam@0 365 malloc(len * sizeof(unsigned char));
cannam@0 366 for (int i = 0; i < len; ++i) {
cannam@0 367 distance[frameCount - blockSize][i] =
cannam@0 368 distance[frameCount][i];
cannam@0 369 }
cannam@0 370
cannam@0 371 bestPathCost[frameCount] = bestPathCost[frameCount - blockSize];
cannam@0 372
cannam@0 373 bestPathCost[frameCount - blockSize] = (int *)
cannam@0 374 malloc(len * sizeof(int));
cannam@0 375 for (int i = 0; i < len; ++i) {
cannam@0 376 bestPathCost[frameCount - blockSize][i] =
cannam@0 377 bestPathCost[frameCount][i];
cannam@0 378 }
cannam@0 379
cannam@0 380 distYSizes[frameCount] = distYSizes[frameCount - blockSize];
cannam@0 381 distYSizes[frameCount - blockSize] = len;
cannam@0 382 }
cannam@0 383
cannam@0 384 int stop = otherMatcher->frameCount;
cannam@0 385 int index = stop - blockSize;
cannam@0 386 if (index < 0)
cannam@0 387 index = 0;
cannam@0 388 first[frameCount] = index;
cannam@0 389 last[frameCount] = stop;
cannam@0 390
cannam@0 391 bool overflow = false;
cannam@0 392 int mn= -1;
cannam@0 393 int mx= -1;
cannam@0 394 for ( ; index < stop; index++) {
cannam@0 395 int dMN = calcDistance(frames[frameIndex],
cannam@0 396 otherMatcher->frames[index % blockSize]);
cannam@0 397 if (mx<0)
cannam@0 398 mx = mn = dMN;
cannam@0 399 else if (dMN > mx)
cannam@0 400 mx = dMN;
cannam@0 401 else if (dMN < mn)
cannam@0 402 mn = dMN;
cannam@0 403 if (dMN >= 255) {
cannam@0 404 overflow = true;
cannam@0 405 dMN = 255;
cannam@0 406 }
cannam@0 407 if ((frameCount == 0) && (index == 0)) // first element
cannam@0 408 setValue(0, 0, 0, 0, dMN);
cannam@0 409 else if (frameCount == 0) // first row
cannam@0 410 setValue(0, index, ADVANCE_OTHER,
cannam@0 411 getValue(0, index-1, true), dMN);
cannam@0 412 else if (index == 0) // first column
cannam@0 413 setValue(frameCount, index, ADVANCE_THIS,
cannam@0 414 getValue(frameCount - 1, 0, true), dMN);
cannam@0 415 else if (index == otherMatcher->frameCount - blockSize) {
cannam@0 416 // missing value(s) due to cutoff
cannam@0 417 // - no previous value in current row (resp. column)
cannam@0 418 // - no diagonal value if prev. dir. == curr. dirn
cannam@0 419 int min2 = getValue(frameCount - 1, index, true);
cannam@0 420 // if ((firstPM && (first[frameCount - 1] == index)) ||
cannam@0 421 // (!firstPM && (last[index-1] < frameCount)))
cannam@0 422 if (first[frameCount - 1] == index)
cannam@0 423 setValue(frameCount, index, ADVANCE_THIS, min2, dMN);
cannam@0 424 else {
cannam@0 425 int min1 = getValue(frameCount - 1, index - 1, true);
cannam@0 426 if (min1 + dMN <= min2)
cannam@0 427 setValue(frameCount, index, ADVANCE_BOTH, min1,dMN);
cannam@0 428 else
cannam@0 429 setValue(frameCount, index, ADVANCE_THIS, min2,dMN);
cannam@0 430 }
cannam@0 431 } else {
cannam@0 432 int min1 = getValue(frameCount, index-1, true);
cannam@0 433 int min2 = getValue(frameCount - 1, index, true);
cannam@0 434 int min3 = getValue(frameCount - 1, index-1, true);
cannam@0 435 if (min1 <= min2) {
cannam@0 436 if (min3 + dMN <= min1)
cannam@0 437 setValue(frameCount, index, ADVANCE_BOTH, min3,dMN);
cannam@0 438 else
cannam@0 439 setValue(frameCount, index, ADVANCE_OTHER,min1,dMN);
cannam@0 440 } else {
cannam@0 441 if (min3 + dMN <= min2)
cannam@0 442 setValue(frameCount, index, ADVANCE_BOTH, min3,dMN);
cannam@0 443 else
cannam@0 444 setValue(frameCount, index, ADVANCE_THIS, min2,dMN);
cannam@0 445 }
cannam@0 446 }
cannam@0 447 otherMatcher->last[index]++;
cannam@0 448 } // loop for row (resp. column)
cannam@0 449
cannam@0 450 frameCount++;
cannam@0 451 runCount++;
cannam@0 452
cannam@0 453 otherMatcher->runCount = 0;
cannam@0 454
cannam@0 455 if (overflow && !silent)
cannam@0 456 cerr << "WARNING: overflow in distance metric: "
cannam@0 457 << "frame " << frameCount << ", val = " << mx << endl;
Chris@21 458
cannam@0 459 if (!silent)
cannam@0 460 std::cerr << "Frame " << frameCount << ", d = " << (mx-mn) << std::endl;
Chris@21 461 }
cannam@0 462
cannam@0 463 int
cannam@0 464 Matcher::calcDistance(const vector<double> &f1, const vector<double> &f2)
cannam@0 465 {
cannam@0 466 double d = 0;
cannam@0 467 double sum = 0;
Chris@23 468 for (int i = 0; i < featureSize; i++) {
cannam@0 469 d += fabs(f1[i] - f2[i]);
cannam@0 470 sum += f1[i] + f2[i];
cannam@0 471 }
cannam@0 472 // System.err.print(" " + Format.d(d,3));
cannam@0 473 if (sum == 0)
cannam@0 474 return 0;
Chris@15 475 if (params.distanceNorm == NormaliseDistanceToSum)
cannam@0 476 return (int)(scale * d / sum); // 0 <= d/sum <= 2
Chris@15 477 if (params.distanceNorm != NormaliseDistanceToLogSum)
cannam@0 478 return (int)(scale * d);
Chris@13 479
Chris@13 480 // note if this were to be restored, it would have to use
Chris@13 481 // totalEnergies vector instead of f1[freqMapSize] which used to
Chris@13 482 // store the total energy:
cannam@0 483 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
Chris@13 484
cannam@0 485 double weight = (8 + log(sum)) / 10.0;
cannam@0 486 // if (weight < mins) {
cannam@0 487 // mins = weight;
cannam@0 488 // System.err.println(Format.d(mins,3) + " " + Format.d(maxs));
cannam@0 489 // }
cannam@0 490 // if (weight > maxs) {
cannam@0 491 // maxs = weight;
cannam@0 492 // System.err.println(Format.d(mins,3) + " " + Format.d(maxs));
cannam@0 493 // }
cannam@0 494 if (weight < 0)
cannam@0 495 weight = 0;
cannam@0 496 else if (weight > 1)
cannam@0 497 weight = 1;
cannam@0 498 return (int)(scale * d / sum * weight);
cannam@0 499 } // calcDistance()
cannam@0 500
cannam@0 501 int
cannam@0 502 Matcher::getValue(int i, int j, bool firstAttempt)
cannam@0 503 {
cannam@0 504 if (firstPM)
cannam@0 505 return bestPathCost[i][j - first[i]];
cannam@0 506 else
cannam@0 507 return otherMatcher->bestPathCost[j][i - otherMatcher->first[j]];
cannam@0 508 } // getValue()
cannam@0 509
cannam@0 510 void
cannam@0 511 Matcher::setValue(int i, int j, int dir, int value, int dMN)
cannam@0 512 {
cannam@0 513 if (firstPM) {
cannam@0 514 distance[i][j - first[i]] = (unsigned char)((dMN & MASK) | dir);
cannam@0 515 bestPathCost[i][j - first[i]] =
cannam@0 516 (value + (dir==ADVANCE_BOTH? dMN*2: dMN));
cannam@0 517 } else {
cannam@0 518 if (dir == ADVANCE_THIS)
cannam@0 519 dir = ADVANCE_OTHER;
cannam@0 520 else if (dir == ADVANCE_OTHER)
cannam@0 521 dir = ADVANCE_THIS;
cannam@0 522 int idx = i - otherMatcher->first[j];
cannam@0 523 if (idx == (int)otherMatcher->distYSizes[j]) {
cannam@0 524 // This should never happen, but if we allow arbitrary
cannam@0 525 // pauses in either direction, and arbitrary lengths at
cannam@0 526 // end, it is better than a segmentation fault.
cannam@0 527 std::cerr << "Emergency resize: " << idx << " -> " << idx * 2 << std::endl;
cannam@0 528 otherMatcher->distYSizes[j] = idx * 2;
cannam@0 529 otherMatcher->bestPathCost[j] =
cannam@0 530 (int *)realloc(otherMatcher->bestPathCost[j],
cannam@0 531 idx * 2 * sizeof(int));
cannam@0 532 otherMatcher->distance[j] =
cannam@0 533 (unsigned char *)realloc(otherMatcher->distance[j],
cannam@0 534 idx * 2 * sizeof(unsigned char));
cannam@0 535 }
cannam@0 536 otherMatcher->distance[j][idx] = (unsigned char)((dMN & MASK) | dir);
cannam@0 537 otherMatcher->bestPathCost[j][idx] =
cannam@0 538 (value + (dir==ADVANCE_BOTH? dMN*2: dMN));
cannam@0 539 }
cannam@0 540 } // setValue()
cannam@0 541