annotate src/Matcher.h @ 232:50a9c8eb4cac

Make memory statistics available
author Chris Cannam
date Fri, 10 Jun 2016 11:50:31 +0100
parents 175c8f044e7c
children 4d0e9225c179
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.
Chris@230 8 Copyright (c) 2007-2015 Simon Dixon, Chris Cannam, and Queen Mary
Chris@230 9 University of London, Copyright (c) 2014-2015 Tido GmbH.
cannam@0 10
cannam@0 11 This program is free software; you can redistribute it and/or
cannam@0 12 modify it under the terms of the GNU General Public License as
cannam@0 13 published by the Free Software Foundation; either version 2 of the
cannam@0 14 License, or (at your option) any later version. See the file
cannam@0 15 COPYING included with this distribution for more information.
cannam@0 16 */
cannam@0 17
Chris@181 18 #ifndef MATCHER_H
Chris@181 19 #define MATCHER_H
cannam@0 20
cannam@0 21 #include <vector>
cannam@0 22 #include <iostream>
cannam@0 23 #include <sstream>
cannam@0 24 #include <cmath>
cannam@0 25
Chris@26 26 #include "DistanceMetric.h"
Chris@187 27 #include "MatchTypes.h"
cannam@0 28
cannam@0 29 using std::vector;
cannam@0 30 using std::string;
cannam@0 31 using std::cerr;
cannam@0 32 using std::endl;
cannam@0 33
Chris@74 34 /** Represents an audio feature stream that can be matched to another
Chris@74 35 * audio stream of the same piece of music. The matching algorithm
Chris@74 36 * uses dynamic time warping.
cannam@0 37 */
cannam@0 38 class Matcher
cannam@0 39 {
Chris@15 40 public:
Chris@181 41 static string advanceToString(advance_t a) {
Chris@83 42 switch (a) {
Chris@83 43 case AdvanceNone: return "AdvanceNone";
Chris@83 44 case AdvanceBoth: return "AdvanceBoth";
Chris@83 45 case AdvanceThis: return "AdvanceThis";
Chris@83 46 case AdvanceOther: return "AdvanceOther";
Chris@83 47 }
Chris@83 48 return "(unknown)";
Chris@83 49 }
Chris@45 50
Chris@15 51 struct Parameters {
Chris@15 52
Chris@113 53 Parameters(double hopTime_) :
Chris@15 54 hopTime(hopTime_),
Chris@15 55 blockTime(10.0),
Chris@83 56 maxRunCount(3),
Chris@83 57 diagonalWeight(2.0)
Chris@15 58 {}
Chris@15 59
Chris@15 60 /** Spacing of audio frames (determines the amount of overlap or
Chris@15 61 * skip between frames). This value is expressed in
Chris@83 62 * seconds.
Chris@83 63 */
Chris@15 64 double hopTime;
Chris@38 65
Chris@15 66 /** The width of the search band (error margin) around the current
Chris@15 67 * match position, measured in seconds. Strictly speaking the
Chris@15 68 * width is measured backwards from the current point, since the
Chris@15 69 * algorithm has to work causally.
Chris@15 70 */
Chris@15 71 double blockTime;
Chris@15 72
Chris@15 73 /** Maximum number of frames sequentially processed by this
Chris@15 74 * matcher, without a frame of the other matcher being
Chris@15 75 * processed.
Chris@15 76 */
Chris@15 77 int maxRunCount;
Chris@83 78
Chris@83 79 /** Weight applied to cost of diagonal step relative to
Chris@83 80 * horizontal or vertical step. The default of 2.0 means that
Chris@83 81 * a diagonal is not favoured over horizontal+vertical
Chris@83 82 * combined, which is good when maintaining gross tracking of
Chris@83 83 * performances that may have wildly differing speeds but
Chris@83 84 * which also leads to quite jaggy paths. A more typical
Chris@83 85 * normal DTW approach for performances with similar speeds
Chris@83 86 * might use 1.0 or something close to it.
Chris@83 87 */
Chris@188 88 double diagonalWeight;
Chris@15 89 };
Chris@15 90
cannam@0 91 /** Constructor for Matcher.
Chris@74 92 *
Chris@74 93 * A Matcher expects to be provided with feature vectors
Chris@74 94 * calculated by some external code (for example, a
Chris@74 95 * FeatureExtractor). Call consumeFeatureVector to provide each
Chris@74 96 * feature frame.
Chris@23 97 *
Chris@23 98 * @param p The Matcher representing the performance with which
Chris@23 99 * this one is going to be matched. Some information is shared
Chris@23 100 * between the two matchers (currently one possesses the distance
Chris@23 101 * matrix and optimal path matrix).
Chris@23 102 */
Chris@143 103 Matcher(Parameters params, DistanceMetric::Parameters dparams, Matcher *p);
Chris@23 104
Chris@78 105 /** Destructor for Matcher.
Chris@78 106 */
cannam@0 107 ~Matcher();
cannam@0 108
cannam@0 109 /** Adds a link to the Matcher object representing the performance
cannam@0 110 * which is going to be matched to this one.
cannam@0 111 *
cannam@0 112 * @param p the Matcher representing the other performance
cannam@0 113 */
cannam@0 114 void setOtherMatcher(Matcher *p) {
Chris@43 115 m_otherMatcher = p;
Chris@74 116 }
cannam@0 117
Chris@78 118 int getBlockSize() {
Chris@78 119 return m_blockSize;
Chris@78 120 }
Chris@78 121
Chris@171 122 bool isFillingInitialBlock() {
Chris@171 123 return m_frameCount < m_blockSize;
Chris@171 124 }
Chris@171 125
Chris@78 126 bool isOverrunning() {
Chris@78 127 return m_runCount >= m_params.maxRunCount;
Chris@78 128 }
Chris@78 129
cannam@0 130 int getFrameCount() {
Chris@43 131 return m_frameCount;
cannam@0 132 }
cannam@0 133
Chris@72 134 int getOtherFrameCount() {
Chris@72 135 return m_otherMatcher->getFrameCount();
Chris@72 136 }
Chris@74 137
Chris@188 138 double getDiagonalWeight() {
Chris@83 139 return m_params.diagonalWeight;
Chris@83 140 }
Chris@83 141
Chris@74 142 /** Processes a feature vector frame, presumably calculated from
Chris@74 143 * audio data by some external code such as a FeatureExtractor.
Chris@74 144 * Calculates the distance to all frames stored in the
Chris@74 145 * otherMatcher and stores in the distance matrix, before
Chris@74 146 * updating the optimal path matrix using the dynamic time
Chris@74 147 * warping algorithm.
Chris@74 148 *
Chris@104 149 * The supplied features must always be of the same size (within
Chris@104 150 * any pair of Matcher objects).
Chris@74 151 */
Chris@182 152 void consumeFeatureVector(const feature_t &feature);
Chris@72 153
Chris@72 154 /** Tests whether a location is in range in the minimum cost matrix.
Chris@72 155 *
Chris@72 156 * @param i the frame number of this Matcher
Chris@72 157 * @param j the frame number of the other Matcher
Chris@72 158 * @return true if the location is in range
Chris@72 159 */
Chris@72 160 bool isInRange(int i, int j);
Chris@203 161
Chris@203 162 /** Tests whether a location is available in the minimum cost
Chris@203 163 * matrix, that is, whether it is in range and contains a valid
Chris@203 164 * cost value. Note this and its associated isRowAvailable,
Chris@203 165 * isColAvailable checks are more expensive than isInRange and
Chris@203 166 * are really intended for error checking. (If a row is in range,
Chris@203 167 * it should always be available.)
Chris@203 168 *
Chris@203 169 * @param i the frame number of this Matcher
Chris@203 170 * @param j the frame number of the other Matcher
Chris@203 171 * @return true if the location is in range and contains a valid cost
Chris@203 172 */
Chris@203 173 bool isAvailable(int i, int j);
Chris@154 174
Chris@154 175 /** Tests whether any locations in the given row are available.
Chris@154 176 */
Chris@154 177 bool isRowAvailable(int i);
Chris@154 178
Chris@154 179 /** Tests whether any locations in the given column are available.
Chris@154 180 */
Chris@154 181 bool isColAvailable(int i);
Chris@72 182
Chris@154 183 /** Returns the valid range of columns for the given row, that is,
Chris@154 184 * the range of frames in the other Matcher for the given frame
Chris@154 185 * in this Matcher's minimum cost matrix.
Chris@72 186 *
Chris@72 187 * @param i the frame number of this Matcher
Chris@72 188 * @return the first, last pair of frame numbers for the other
Chris@72 189 * Matcher. Note that the last frame is exclusive (last valid
Chris@72 190 * frame + 1).
Chris@72 191 */
Chris@205 192 std::pair<int, int> getColRangeForRow(int i);
Chris@72 193
Chris@154 194 /** Returns the valid range of rows for the given column, that is,
Chris@154 195 * the range of frames in this Matcher for the given frame in the
Chris@154 196 * other Matcher's minimum cost matrix.
Chris@72 197 *
Chris@72 198 * @param i the frame number of the other Matcher
Chris@72 199 * @return the first, last pair of frame numbers for this
Chris@72 200 * Matcher. Note that the last frame is exclusive (last valid
Chris@72 201 * frame + 1).
Chris@72 202 */
Chris@205 203 std::pair<int, int> getRowRangeForCol(int i);
Chris@72 204
Chris@72 205 /** Retrieves a value from the distance matrix.
Chris@72 206 *
Chris@72 207 * @param i the frame number of this Matcher
Chris@72 208 * @param j the frame number of the other Matcher
Chris@72 209 * @return the distance metric at this location
Chris@72 210 */
Chris@182 211 distance_t getDistance(int i, int j);
Chris@72 212
Chris@72 213 /** Sets a value to the distance matrix.
Chris@72 214 *
Chris@72 215 * @param i the frame number of this Matcher
Chris@72 216 * @param j the frame number of the other Matcher
Chris@72 217 * @param value the distance metric to set for this location
Chris@72 218 */
Chris@182 219 void setDistance(int i, int j, distance_t distance);
Chris@72 220
Chris@72 221 /** Retrieves a value from the minimum cost matrix.
Chris@72 222 *
Chris@72 223 * @param i the frame number of this Matcher
Chris@72 224 * @param j the frame number of the other Matcher
Chris@72 225 * @return the cost of the minimum cost path to this location
Chris@72 226 */
Chris@182 227 pathcost_t getPathCost(int i, int j);
Chris@72 228
Chris@72 229 /** Sets a value and an advance direction to the minimum cost matrix.
Chris@72 230 *
Chris@72 231 * @param i the frame number of this Matcher
Chris@72 232 * @param j the frame number of the other Matcher
Chris@72 233 * @param dir the direction from which this position is reached with
Chris@72 234 * minimum cost
Chris@72 235 * @param value the cost of the minimum cost path to set for this location
Chris@72 236 */
Chris@182 237 void setPathCost(int i, int j, advance_t dir, pathcost_t value);
Chris@135 238
Chris@135 239 /** Retrieves a value from the minimum cost matrix, normalised for
Chris@135 240 * path length.
Chris@135 241 *
Chris@135 242 * @param i the frame number of this Matcher
Chris@135 243 * @param j the frame number of the other Matcher
Chris@135 244 * @return the cost of the minimum cost path to this location,
Chris@135 245 * normalised by the Manhattan distance from 0,0 to i,j
Chris@135 246 */
Chris@191 247 normpathcost_t getNormalisedPathCost(int i, int j);
Chris@72 248
Chris@72 249 /** Retrieves an advance direction from the matrix.
Chris@72 250 *
Chris@72 251 * @param i the frame number of this Matcher
Chris@72 252 * @param j the frame number of the other Matcher
Chris@72 253 * @return the direction from which this position is reached with
Chris@72 254 * minimum cost
Chris@72 255 */
Chris@181 256 advance_t getAdvance(int i, int j);
Chris@202 257
Chris@232 258 struct MemoryStats {
Chris@232 259 double features_k;
Chris@232 260 double pathcosts_k;
Chris@232 261 double distances_k;
Chris@232 262 double advances_k;
Chris@232 263 double total_k() const {
Chris@232 264 return features_k + pathcosts_k + distances_k + advances_k;
Chris@232 265 }
Chris@232 266 };
Chris@232 267
Chris@232 268 /** Obtain some stats about memory consumption.
Chris@232 269 */
Chris@232 270 MemoryStats getMemoryStats() const;
Chris@232 271
Chris@202 272 /** Print some stats about memory consumption etc to stderr.
Chris@202 273 */
Chris@202 274 void printStats();
Chris@72 275
cannam@0 276 protected:
Chris@38 277 /** Create internal structures and reset. */
cannam@0 278 void init();
cannam@0 279
Chris@38 280 /** The distXSize value has changed: resize internal buffers. */
Chris@41 281 void size();
cannam@0 282
Chris@71 283 /** Updates an entry in the distance matrix and the optimal path matrix.
cannam@0 284 *
cannam@0 285 * @param i the frame number of this Matcher
cannam@0 286 * @param j the frame number of the other Matcher
cannam@0 287 * @param dir the direction from which this position is reached with
cannam@0 288 * minimum cost
cannam@0 289 * @param value the cost of the minimum path except the current step
cannam@0 290 * @param dMN the distance cost between the two frames
cannam@0 291 */
Chris@182 292 void updateValue(int i, int j, advance_t dir, pathcost_t value, distance_t dMN);
cannam@0 293
Chris@21 294 void calcAdvance();
Chris@21 295
Chris@211 296 /**
Chris@211 297 * Add the given distance increment to the given path cost, and
Chris@211 298 * return the result clipped (if necessary) at MaxPathCost.
Chris@211 299 */
Chris@211 300 pathcost_t addToCost(pathcost_t cost, pathcost_t increment);
Chris@211 301
Chris@42 302 /** Points to the other performance with which this one is being
Chris@211 303 * compared. (See <code>m_firstPM</code>)
Chris@42 304 */
Chris@43 305 Matcher *m_otherMatcher;
Chris@42 306
Chris@42 307 /** Indicates which performance is considered primary (the
Chris@42 308 * score). This is the performance shown on the vertical axis,
Chris@42 309 * and referred to as "this" in the codes for the direction of
Chris@42 310 * DTW steps. */
Chris@43 311 bool m_firstPM;
Chris@42 312
Chris@42 313 /** Configuration parameters */
Chris@43 314 Parameters m_params;
Chris@42 315
Chris@42 316 /** Width of the search band in FFT frames (see <code>blockTime</code>) */
Chris@43 317 int m_blockSize;
Chris@42 318
Chris@42 319 /** The number of frames of audio data which have been read. */
Chris@43 320 int m_frameCount;
Chris@42 321
Chris@42 322 /** The number of frames sequentially processed by this matcher,
Chris@42 323 * without a frame of the other matcher being processed.
Chris@42 324 */
Chris@43 325 int m_runCount;
Chris@42 326
Chris@50 327 /** A block of previously seen feature frames is stored in this
Chris@50 328 * structure for calculation of the distance matrix as the new
Chris@50 329 * frames are received. One can think of the structure of the
Chris@50 330 * array as a circular buffer of vectors. */
Chris@182 331 featureseq_t m_features;
Chris@42 332
Chris@42 333 /** The best path cost matrix. */
Chris@182 334 pathcostmat_t m_bestPathCost;
Chris@42 335
Chris@42 336 /** The distance matrix. */
Chris@182 337 distancemat_t m_distance;
Chris@42 338
Chris@45 339 /** The advance direction matrix. */
Chris@182 340 advancemat_t m_advance;
Chris@45 341
Chris@45 342 /** The bounds of each row of data in the distance, path cost, and
Chris@45 343 * advance direction matrices.*/
Chris@43 344 vector<int> m_first;
Chris@43 345 vector<int> m_last;
Chris@42 346
Chris@45 347 /** Width of distance, path cost, and advance direction matrices
Chris@45 348 * and first and last vectors */
Chris@43 349 int m_distXSize;
Chris@42 350
Chris@43 351 bool m_initialised;
Chris@42 352
Chris@43 353 DistanceMetric m_metric;
Chris@78 354 };
cannam@0 355
Chris@232 356 inline Matcher::MemoryStats operator+(const Matcher::MemoryStats &a,
Chris@232 357 const Matcher::MemoryStats &b)
Chris@232 358 {
Chris@232 359 Matcher::MemoryStats m = a;
Chris@232 360 m.features_k += b.features_k;
Chris@232 361 m.pathcosts_k += b.pathcosts_k;
Chris@232 362 m.distances_k += b.distances_k;
Chris@232 363 m.advances_k += b.advances_k;
Chris@232 364 return m;
Chris@232 365 }
Chris@232 366
cannam@0 367 #endif