annotate src/Matcher.cpp @ 38:8cce4e13ede3 refactors

Make use of FeatureExtractor in Matcher
author Chris Cannam
date Thu, 13 Nov 2014 12:50:54 +0000
parents 16870e8770ae
children 15a7fdc02c58
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@38 28 Matcher::Matcher(Parameters parameters,
Chris@38 29 FeatureExtractor::Parameters feParams,
Chris@38 30 Matcher *p) :
Chris@26 31 params(parameters),
Chris@38 32 featureExtractor(feParams),
Chris@26 33 metric(parameters.distanceNorm)
cannam@0 34 {
Chris@10 35 #ifdef DEBUG_MATCHER
Chris@15 36 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ")" << endl;
Chris@10 37 #endif
cannam@0 38
cannam@0 39 otherMatcher = p; // the first matcher will need this to be set later
cannam@0 40 firstPM = (!p);
cannam@0 41 frameCount = 0;
cannam@0 42 runCount = 0;
Chris@38 43 featureSize = featureExtractor.getFeatureSize();
Chris@23 44 blockSize = 0;
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 initialised = false;
Chris@23 52 }
Chris@23 53
Chris@38 54 Matcher::Matcher(Parameters parameters, Matcher *p, int featureSize_) :
Chris@23 55 params(parameters),
Chris@38 56 featureSize(featureSize_),
Chris@38 57 featureExtractor(FeatureExtractor::Parameters(params.sampleRate, params.fftSize)), // unused default config
Chris@26 58 metric(parameters.distanceNorm)
Chris@23 59 {
Chris@23 60 #ifdef DEBUG_MATCHER
Chris@23 61 cerr << "Matcher::Matcher(" << params.sampleRate << ", " << p << ", " << featureSize << ")" << endl;
Chris@23 62 #endif
Chris@23 63
Chris@23 64 otherMatcher = p; // the first matcher will need this to be set later
Chris@23 65 firstPM = (!p);
Chris@23 66 frameCount = 0;
Chris@23 67 runCount = 0;
cannam@0 68 blockSize = 0;
cannam@0 69
Chris@15 70 blockSize = lrint(params.blockTime / params.hopTime);
Chris@15 71 #ifdef DEBUG_MATCHER
Chris@15 72 cerr << "Matcher: blockSize = " << blockSize << endl;
Chris@15 73 #endif
cannam@0 74
cannam@0 75 initialised = false;
Chris@23 76 }
cannam@0 77
cannam@0 78 Matcher::~Matcher()
cannam@0 79 {
Chris@10 80 #ifdef DEBUG_MATCHER
Chris@15 81 cerr << "Matcher(" << this << ")::~Matcher()" << endl;
Chris@10 82 #endif
cannam@0 83 }
cannam@0 84
cannam@0 85 void
cannam@0 86 Matcher::init()
cannam@0 87 {
cannam@0 88 if (initialised) return;
cannam@0 89
Chris@38 90 frames = vector<vector<double> >
Chris@38 91 (blockSize, vector<double>(featureSize, 0));
cannam@0 92
cannam@0 93 distXSize = blockSize * 2;
Chris@38 94 expand();
cannam@0 95
cannam@0 96 frameCount = 0;
cannam@0 97 runCount = 0;
Chris@38 98
Chris@38 99 initialised = true;
Chris@16 100 }
Chris@16 101
cannam@0 102 void
Chris@38 103 Matcher::expand()
cannam@0 104 {
Chris@38 105 int distSize = (params.maxRunCount + 1) * blockSize;
cannam@0 106
Chris@38 107 bestPathCost.resize(distXSize, vector<int>(distSize, 0));
Chris@38 108 distance.resize(distXSize, vector<unsigned char>(distSize, 0));
Chris@38 109 distYSizes.resize(blockSize, distSize);
Chris@38 110 first.resize(distXSize, 0);
Chris@38 111 last.resize(distXSize, 0);
Chris@38 112 }
cannam@0 113
Chris@14 114 vector<double>
Chris@21 115 Matcher::consumeFrame(double *reBuffer, double *imBuffer)
cannam@0 116 {
cannam@0 117 if (!initialised) init();
cannam@0 118
Chris@38 119 vector<double> real(reBuffer, reBuffer + params.fftSize/2 + 1);
Chris@38 120 vector<double> imag(imBuffer, imBuffer + params.fftSize/2 + 1);
Chris@38 121 vector<double> feature = featureExtractor.process(real, imag);
Chris@38 122 int frameIndex = frameCount % blockSize;
Chris@38 123 frames[frameIndex] = feature;
Chris@21 124 calcAdvance();
Chris@21 125
Chris@38 126 return feature;
Chris@23 127 }
Chris@21 128
Chris@23 129 void
Chris@23 130 Matcher::consumeFeatureVector(std::vector<double> feature)
Chris@23 131 {
Chris@23 132 if (!initialised) init();
Chris@23 133 int frameIndex = frameCount % blockSize;
Chris@23 134 frames[frameIndex] = feature;
Chris@23 135 calcAdvance();
Chris@21 136 }
Chris@21 137
Chris@21 138 void
Chris@21 139 Matcher::calcAdvance()
Chris@21 140 {
Chris@21 141 int frameIndex = frameCount % blockSize;
Chris@21 142
cannam@0 143 if (frameCount >= distXSize) {
Chris@38 144 expand();
cannam@0 145 }
cannam@0 146
cannam@0 147 if (firstPM && (frameCount >= blockSize)) {
cannam@0 148
cannam@0 149 int len = last[frameCount - blockSize] -
cannam@0 150 first[frameCount - blockSize];
cannam@0 151
cannam@0 152 // We need to copy distance[frameCount-blockSize] to
cannam@0 153 // distance[frameCount], and then truncate
cannam@0 154 // distance[frameCount-blockSize] to its first len elements.
cannam@0 155 // Same for bestPathCost.
cannam@0 156 /*
cannam@4 157 std::cerr << "Matcher(" << this << "): moving " << distYSizes[frameCount - blockSize] << " from " << frameCount - blockSize << " to "
cannam@0 158 << frameCount << ", allocating " << len << " for "
cannam@0 159 << frameCount - blockSize << std::endl;
cannam@0 160 */
cannam@0 161 distance[frameCount] = distance[frameCount - blockSize];
Chris@38 162 distance[frameCount - blockSize].resize(len, 0);
cannam@0 163 for (int i = 0; i < len; ++i) {
cannam@0 164 distance[frameCount - blockSize][i] =
cannam@0 165 distance[frameCount][i];
cannam@0 166 }
cannam@0 167
cannam@0 168 bestPathCost[frameCount] = bestPathCost[frameCount - blockSize];
Chris@38 169 bestPathCost[frameCount - blockSize].resize(len, 0);
cannam@0 170 for (int i = 0; i < len; ++i) {
cannam@0 171 bestPathCost[frameCount - blockSize][i] =
cannam@0 172 bestPathCost[frameCount][i];
cannam@0 173 }
cannam@0 174
cannam@0 175 distYSizes[frameCount] = distYSizes[frameCount - blockSize];
cannam@0 176 distYSizes[frameCount - blockSize] = len;
cannam@0 177 }
cannam@0 178
cannam@0 179 int stop = otherMatcher->frameCount;
cannam@0 180 int index = stop - blockSize;
cannam@0 181 if (index < 0)
cannam@0 182 index = 0;
cannam@0 183 first[frameCount] = index;
cannam@0 184 last[frameCount] = stop;
cannam@0 185
cannam@0 186 bool overflow = false;
cannam@0 187 int mn= -1;
cannam@0 188 int mx= -1;
cannam@0 189 for ( ; index < stop; index++) {
Chris@26 190
Chris@26 191 int dMN = metric.calcDistanceScaled
Chris@26 192 (frames[frameIndex],
Chris@26 193 otherMatcher->frames[index % blockSize],
Chris@29 194 params.distanceScale);
Chris@26 195
cannam@0 196 if (mx<0)
cannam@0 197 mx = mn = dMN;
cannam@0 198 else if (dMN > mx)
cannam@0 199 mx = dMN;
cannam@0 200 else if (dMN < mn)
cannam@0 201 mn = dMN;
cannam@0 202 if (dMN >= 255) {
cannam@0 203 overflow = true;
cannam@0 204 dMN = 255;
cannam@0 205 }
Chris@26 206
cannam@0 207 if ((frameCount == 0) && (index == 0)) // first element
cannam@0 208 setValue(0, 0, 0, 0, dMN);
cannam@0 209 else if (frameCount == 0) // first row
cannam@0 210 setValue(0, index, ADVANCE_OTHER,
cannam@0 211 getValue(0, index-1, true), dMN);
cannam@0 212 else if (index == 0) // first column
cannam@0 213 setValue(frameCount, index, ADVANCE_THIS,
cannam@0 214 getValue(frameCount - 1, 0, true), dMN);
cannam@0 215 else if (index == otherMatcher->frameCount - blockSize) {
cannam@0 216 // missing value(s) due to cutoff
cannam@0 217 // - no previous value in current row (resp. column)
cannam@0 218 // - no diagonal value if prev. dir. == curr. dirn
cannam@0 219 int min2 = getValue(frameCount - 1, index, true);
cannam@0 220 // if ((firstPM && (first[frameCount - 1] == index)) ||
cannam@0 221 // (!firstPM && (last[index-1] < frameCount)))
cannam@0 222 if (first[frameCount - 1] == index)
cannam@0 223 setValue(frameCount, index, ADVANCE_THIS, min2, dMN);
cannam@0 224 else {
cannam@0 225 int min1 = getValue(frameCount - 1, index - 1, true);
cannam@0 226 if (min1 + dMN <= min2)
cannam@0 227 setValue(frameCount, index, ADVANCE_BOTH, min1,dMN);
cannam@0 228 else
cannam@0 229 setValue(frameCount, index, ADVANCE_THIS, min2,dMN);
cannam@0 230 }
cannam@0 231 } else {
cannam@0 232 int min1 = getValue(frameCount, index-1, true);
cannam@0 233 int min2 = getValue(frameCount - 1, index, true);
cannam@0 234 int min3 = getValue(frameCount - 1, index-1, true);
cannam@0 235 if (min1 <= min2) {
cannam@0 236 if (min3 + dMN <= min1)
cannam@0 237 setValue(frameCount, index, ADVANCE_BOTH, min3,dMN);
cannam@0 238 else
cannam@0 239 setValue(frameCount, index, ADVANCE_OTHER,min1,dMN);
cannam@0 240 } else {
cannam@0 241 if (min3 + dMN <= min2)
cannam@0 242 setValue(frameCount, index, ADVANCE_BOTH, min3,dMN);
cannam@0 243 else
cannam@0 244 setValue(frameCount, index, ADVANCE_THIS, min2,dMN);
cannam@0 245 }
cannam@0 246 }
cannam@0 247 otherMatcher->last[index]++;
cannam@0 248 } // loop for row (resp. column)
cannam@0 249
cannam@0 250 frameCount++;
cannam@0 251 runCount++;
cannam@0 252
cannam@0 253 otherMatcher->runCount = 0;
cannam@0 254
cannam@0 255 if (overflow && !silent)
cannam@0 256 cerr << "WARNING: overflow in distance metric: "
cannam@0 257 << "frame " << frameCount << ", val = " << mx << endl;
Chris@21 258
cannam@0 259 if (!silent)
cannam@0 260 std::cerr << "Frame " << frameCount << ", d = " << (mx-mn) << std::endl;
Chris@21 261 }
cannam@0 262
cannam@0 263 int
cannam@0 264 Matcher::getValue(int i, int j, bool firstAttempt)
cannam@0 265 {
cannam@0 266 if (firstPM)
cannam@0 267 return bestPathCost[i][j - first[i]];
cannam@0 268 else
cannam@0 269 return otherMatcher->bestPathCost[j][i - otherMatcher->first[j]];
cannam@0 270 } // getValue()
cannam@0 271
cannam@0 272 void
cannam@0 273 Matcher::setValue(int i, int j, int dir, int value, int dMN)
cannam@0 274 {
cannam@0 275 if (firstPM) {
cannam@0 276 distance[i][j - first[i]] = (unsigned char)((dMN & MASK) | dir);
cannam@0 277 bestPathCost[i][j - first[i]] =
cannam@0 278 (value + (dir==ADVANCE_BOTH? dMN*2: dMN));
cannam@0 279 } else {
cannam@0 280 if (dir == ADVANCE_THIS)
cannam@0 281 dir = ADVANCE_OTHER;
cannam@0 282 else if (dir == ADVANCE_OTHER)
cannam@0 283 dir = ADVANCE_THIS;
cannam@0 284 int idx = i - otherMatcher->first[j];
cannam@0 285 if (idx == (int)otherMatcher->distYSizes[j]) {
cannam@0 286 // This should never happen, but if we allow arbitrary
cannam@0 287 // pauses in either direction, and arbitrary lengths at
cannam@0 288 // end, it is better than a segmentation fault.
cannam@0 289 std::cerr << "Emergency resize: " << idx << " -> " << idx * 2 << std::endl;
cannam@0 290 otherMatcher->distYSizes[j] = idx * 2;
Chris@38 291 otherMatcher->bestPathCost[j].resize(idx * 2, 0);
Chris@38 292 otherMatcher->distance[j].resize(idx * 2, 0);
cannam@0 293 }
cannam@0 294 otherMatcher->distance[j][idx] = (unsigned char)((dMN & MASK) | dir);
cannam@0 295 otherMatcher->bestPathCost[j][idx] =
cannam@0 296 (value + (dir==ADVANCE_BOTH? dMN*2: dMN));
cannam@0 297 }
cannam@0 298 } // setValue()
cannam@0 299