annotate src/Matcher.h @ 146:214b72d55796 noise

Update unit tests
author Chris Cannam
date Fri, 16 Jan 2015 16:48:55 +0000
parents 6914a6a01ffc
children 4159f6b71942
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 #ifndef _MATCHER_H_
cannam@0 18 #define _MATCHER_H_
cannam@0 19
cannam@0 20 #include <vector>
cannam@0 21 #include <iostream>
cannam@0 22 #include <sstream>
cannam@0 23 #include <cmath>
cannam@0 24
Chris@26 25 #include "DistanceMetric.h"
cannam@0 26
cannam@0 27 using std::vector;
cannam@0 28 using std::string;
cannam@0 29 using std::cerr;
cannam@0 30 using std::endl;
cannam@0 31
Chris@74 32 /** Represents an audio feature stream that can be matched to another
Chris@74 33 * audio stream of the same piece of music. The matching algorithm
Chris@74 34 * uses dynamic time warping.
cannam@0 35 */
cannam@0 36 class Matcher
cannam@0 37 {
Chris@15 38 public:
Chris@45 39 enum Advance {
Chris@45 40 AdvanceNone,
Chris@45 41 AdvanceBoth,
Chris@45 42 AdvanceThis,
Chris@45 43 AdvanceOther
Chris@45 44 };
Chris@83 45 static string advanceToString(Advance a) {
Chris@83 46 switch (a) {
Chris@83 47 case AdvanceNone: return "AdvanceNone";
Chris@83 48 case AdvanceBoth: return "AdvanceBoth";
Chris@83 49 case AdvanceThis: return "AdvanceThis";
Chris@83 50 case AdvanceOther: return "AdvanceOther";
Chris@83 51 }
Chris@83 52 return "(unknown)";
Chris@83 53 }
Chris@45 54
Chris@15 55 struct Parameters {
Chris@15 56
Chris@113 57 Parameters(double hopTime_) :
Chris@15 58 hopTime(hopTime_),
Chris@15 59 blockTime(10.0),
Chris@83 60 maxRunCount(3),
Chris@83 61 diagonalWeight(2.0)
Chris@15 62 {}
Chris@15 63
Chris@15 64 /** Spacing of audio frames (determines the amount of overlap or
Chris@15 65 * skip between frames). This value is expressed in
Chris@83 66 * seconds.
Chris@83 67 */
Chris@15 68 double hopTime;
Chris@38 69
Chris@15 70 /** The width of the search band (error margin) around the current
Chris@15 71 * match position, measured in seconds. Strictly speaking the
Chris@15 72 * width is measured backwards from the current point, since the
Chris@15 73 * algorithm has to work causally.
Chris@15 74 */
Chris@15 75 double blockTime;
Chris@15 76
Chris@15 77 /** Maximum number of frames sequentially processed by this
Chris@15 78 * matcher, without a frame of the other matcher being
Chris@15 79 * processed.
Chris@15 80 */
Chris@15 81 int maxRunCount;
Chris@83 82
Chris@83 83 /** Weight applied to cost of diagonal step relative to
Chris@83 84 * horizontal or vertical step. The default of 2.0 means that
Chris@83 85 * a diagonal is not favoured over horizontal+vertical
Chris@83 86 * combined, which is good when maintaining gross tracking of
Chris@83 87 * performances that may have wildly differing speeds but
Chris@83 88 * which also leads to quite jaggy paths. A more typical
Chris@83 89 * normal DTW approach for performances with similar speeds
Chris@83 90 * might use 1.0 or something close to it.
Chris@83 91 */
Chris@83 92 float diagonalWeight;
Chris@15 93 };
Chris@15 94
cannam@0 95 /** Constructor for Matcher.
Chris@74 96 *
Chris@74 97 * A Matcher expects to be provided with feature vectors
Chris@74 98 * calculated by some external code (for example, a
Chris@74 99 * FeatureExtractor). Call consumeFeatureVector to provide each
Chris@74 100 * feature frame.
Chris@23 101 *
Chris@23 102 * @param p The Matcher representing the performance with which
Chris@23 103 * this one is going to be matched. Some information is shared
Chris@23 104 * between the two matchers (currently one possesses the distance
Chris@23 105 * matrix and optimal path matrix).
Chris@23 106 */
Chris@143 107 Matcher(Parameters params, DistanceMetric::Parameters dparams, Matcher *p);
Chris@23 108
Chris@78 109 /** Destructor for Matcher.
Chris@78 110 */
cannam@0 111 ~Matcher();
cannam@0 112
cannam@0 113 /** Adds a link to the Matcher object representing the performance
cannam@0 114 * which is going to be matched to this one.
cannam@0 115 *
cannam@0 116 * @param p the Matcher representing the other performance
cannam@0 117 */
cannam@0 118 void setOtherMatcher(Matcher *p) {
Chris@43 119 m_otherMatcher = p;
Chris@74 120 }
cannam@0 121
Chris@78 122 int getBlockSize() {
Chris@78 123 return m_blockSize;
Chris@78 124 }
Chris@78 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@83 138 float 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@74 152 void consumeFeatureVector(std::vector<double> 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@72 161
Chris@72 162 /** Tests whether a location is available in the minimum cost matrix.
Chris@72 163 *
Chris@72 164 * @param i the frame number of this Matcher
Chris@72 165 * @param j the frame number of the other Matcher
Chris@72 166 * @return true if the location is in range and contains a valid cost
Chris@72 167 */
Chris@72 168 bool isAvailable(int i, int j);
Chris@72 169
Chris@72 170 /** Returns the valid range of frames in the other Matcher for the
Chris@72 171 * given frame in this Matcher's minimum cost matrix.
Chris@72 172 *
Chris@72 173 * @param i the frame number of this Matcher
Chris@72 174 * @return the first, last pair of frame numbers for the other
Chris@72 175 * Matcher. Note that the last frame is exclusive (last valid
Chris@72 176 * frame + 1).
Chris@72 177 */
Chris@72 178 std::pair<int, int> getColRange(int i);
Chris@72 179
Chris@72 180 /** Returns the valid range of frames in this Matcher for the
Chris@72 181 * given frame in the other Matcher's minimum cost matrix.
Chris@72 182 *
Chris@72 183 * @param i the frame number of the other Matcher
Chris@72 184 * @return the first, last pair of frame numbers for this
Chris@72 185 * Matcher. Note that the last frame is exclusive (last valid
Chris@72 186 * frame + 1).
Chris@72 187 */
Chris@72 188 std::pair<int, int> getRowRange(int i);
Chris@72 189
Chris@72 190 /** Retrieves a value from the distance matrix.
Chris@72 191 *
Chris@72 192 * @param i the frame number of this Matcher
Chris@72 193 * @param j the frame number of the other Matcher
Chris@72 194 * @return the distance metric at this location
Chris@72 195 */
Chris@72 196 float getDistance(int i, int j);
Chris@72 197
Chris@72 198 /** Sets a value to the distance matrix.
Chris@72 199 *
Chris@72 200 * @param i the frame number of this Matcher
Chris@72 201 * @param j the frame number of the other Matcher
Chris@72 202 * @param value the distance metric to set for this location
Chris@72 203 */
Chris@96 204 void setDistance(int i, int j, float distance);
Chris@72 205
Chris@72 206 /** Retrieves a value from the minimum cost matrix.
Chris@72 207 *
Chris@72 208 * @param i the frame number of this Matcher
Chris@72 209 * @param j the frame number of the other Matcher
Chris@72 210 * @return the cost of the minimum cost path to this location
Chris@72 211 */
Chris@72 212 double getPathCost(int i, int j);
Chris@72 213
Chris@72 214 /** Sets a value and an advance direction to the minimum cost matrix.
Chris@72 215 *
Chris@72 216 * @param i the frame number of this Matcher
Chris@72 217 * @param j the frame number of the other Matcher
Chris@72 218 * @param dir the direction from which this position is reached with
Chris@72 219 * minimum cost
Chris@72 220 * @param value the cost of the minimum cost path to set for this location
Chris@72 221 */
Chris@72 222 void setPathCost(int i, int j, Advance dir, double value);
Chris@135 223
Chris@135 224 /** Retrieves a value from the minimum cost matrix, normalised for
Chris@135 225 * path length.
Chris@135 226 *
Chris@135 227 * @param i the frame number of this Matcher
Chris@135 228 * @param j the frame number of the other Matcher
Chris@135 229 * @return the cost of the minimum cost path to this location,
Chris@135 230 * normalised by the Manhattan distance from 0,0 to i,j
Chris@135 231 */
Chris@135 232 double getNormalisedPathCost(int i, int j);
Chris@72 233
Chris@72 234 /** Retrieves an advance direction from the matrix.
Chris@72 235 *
Chris@72 236 * @param i the frame number of this Matcher
Chris@72 237 * @param j the frame number of the other Matcher
Chris@72 238 * @return the direction from which this position is reached with
Chris@72 239 * minimum cost
Chris@72 240 */
Chris@72 241 Advance getAdvance(int i, int j);
Chris@72 242
cannam@0 243 protected:
Chris@38 244 /** Create internal structures and reset. */
cannam@0 245 void init();
cannam@0 246
Chris@38 247 /** The distXSize value has changed: resize internal buffers. */
Chris@41 248 void size();
cannam@0 249
Chris@71 250 /** Updates an entry in the distance matrix and the optimal path matrix.
cannam@0 251 *
cannam@0 252 * @param i the frame number of this Matcher
cannam@0 253 * @param j the frame number of the other Matcher
cannam@0 254 * @param dir the direction from which this position is reached with
cannam@0 255 * minimum cost
cannam@0 256 * @param value the cost of the minimum path except the current step
cannam@0 257 * @param dMN the distance cost between the two frames
cannam@0 258 */
Chris@71 259 void updateValue(int i, int j, Advance dir, double value, float dMN);
cannam@0 260
Chris@21 261 void calcAdvance();
Chris@21 262
Chris@42 263 /** Points to the other performance with which this one is being
Chris@42 264 * compared. The data for the distance metric and the dynamic
Chris@42 265 * time warping is shared between the two matchers. In the
Chris@42 266 * original version, only one of the two performance matchers
Chris@42 267 * contained the distance metric. (See <code>first</code>)
Chris@42 268 */
Chris@43 269 Matcher *m_otherMatcher;
Chris@42 270
Chris@42 271 /** Indicates which performance is considered primary (the
Chris@42 272 * score). This is the performance shown on the vertical axis,
Chris@42 273 * and referred to as "this" in the codes for the direction of
Chris@42 274 * DTW steps. */
Chris@43 275 bool m_firstPM;
Chris@42 276
Chris@42 277 /** Configuration parameters */
Chris@43 278 Parameters m_params;
Chris@42 279
Chris@42 280 /** Width of the search band in FFT frames (see <code>blockTime</code>) */
Chris@43 281 int m_blockSize;
Chris@42 282
Chris@42 283 /** The number of frames of audio data which have been read. */
Chris@43 284 int m_frameCount;
Chris@42 285
Chris@42 286 /** The number of frames sequentially processed by this matcher,
Chris@42 287 * without a frame of the other matcher being processed.
Chris@42 288 */
Chris@43 289 int m_runCount;
Chris@42 290
Chris@50 291 /** A block of previously seen feature frames is stored in this
Chris@50 292 * structure for calculation of the distance matrix as the new
Chris@50 293 * frames are received. One can think of the structure of the
Chris@50 294 * array as a circular buffer of vectors. */
Chris@43 295 vector<vector<double> > m_frames;
Chris@42 296
Chris@42 297 /** The best path cost matrix. */
Chris@53 298 vector<vector<double> > m_bestPathCost;
Chris@42 299
Chris@42 300 /** The distance matrix. */
Chris@45 301 vector<vector<float> > m_distance;
Chris@42 302
Chris@45 303 /** The advance direction matrix. */
Chris@45 304 vector<vector<Advance> > m_advance;
Chris@45 305
Chris@45 306 /** The bounds of each row of data in the distance, path cost, and
Chris@45 307 * advance direction matrices.*/
Chris@43 308 vector<int> m_first;
Chris@43 309 vector<int> m_last;
Chris@42 310
Chris@45 311 /** Width of distance, path cost, and advance direction matrices
Chris@45 312 * and first and last vectors */
Chris@43 313 int m_distXSize;
Chris@42 314
Chris@43 315 bool m_initialised;
Chris@42 316
Chris@43 317 DistanceMetric m_metric;
Chris@78 318 };
cannam@0 319
cannam@0 320 #endif