annotate src/Path.cpp @ 200:f6be68852c1d

Fix off-by-one in durations
author Chris Cannam
date Fri, 27 Feb 2015 10:05:04 +0000
parents 487261a22b18
children 175c8f044e7c
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 "Path.h"
cannam@0 18
cannam@0 19 int
cannam@0 20 Path::smooth(std::vector<int> &x, std::vector<int> &y, int length)
cannam@0 21 {
Chris@188 22 if (length <= 0)
cannam@0 23 return 0;
Chris@188 24 while (m_val.size() < static_cast<std::vector<int>::size_type>(length)) {
Chris@188 25 m_val.push_back(0);
Chris@188 26 m_len.push_back(0);
cannam@0 27 }
cannam@0 28 int p = 0;
Chris@188 29 m_val[0] = m_len[0] = 0;
cannam@0 30 for (int i = 1; i < length; i++) { // H = 1; V = 2; D = 3
cannam@0 31 int current = x[i] - x[i-1] + 2 * (y[i] - y[i-1]);
Chris@188 32 if (current == m_val[p]) {
Chris@188 33 m_len[p]++;
Chris@188 34 } else if ((current == 3) || (m_val[p] == 0)) {
Chris@188 35 m_val[++p] = current;
Chris@188 36 m_len[p] = 1;
Chris@188 37 } else if (m_val[p] + current == 3) { // 1 + 2
Chris@188 38 if (--m_len[p] == 0)
cannam@0 39 p--;
Chris@188 40 if (m_val[p] == 3)
Chris@188 41 m_len[p]++;
cannam@0 42 else {
Chris@188 43 m_val[++p] = 3;
Chris@188 44 m_len[p] = 1;
cannam@0 45 }
Chris@188 46 } else { // m_val[p] == 3 && current != 3
Chris@188 47 if ((m_val[p-1] == current) ||
Chris@188 48 (m_val[p-1] == 0) ||
Chris@188 49 (m_len[p] > MAX_RUN_LENGTH)) {
Chris@188 50 m_val[++p] = current;
Chris@188 51 m_len[p] = 1;
cannam@0 52 } else {
Chris@188 53 if (--m_len[p-1] == 0) {
Chris@188 54 m_val[p-1] = m_val[p];
Chris@188 55 m_len[p-1] = m_len[p];
cannam@0 56 p--;
Chris@188 57 if (m_val[p-1] == 3) {
Chris@188 58 m_len[p-1] += m_len[p];
cannam@0 59 p--;
cannam@0 60 }
cannam@0 61 }
Chris@188 62 m_len[p]++;
cannam@0 63 }
cannam@0 64 }
cannam@0 65 }
cannam@0 66 int i = 1;
cannam@0 67 for (int pp = 1; pp <= p; pp++) {
Chris@188 68 int dx = m_val[pp] & 1;
Chris@188 69 int dy = m_val[pp] >> 1;
Chris@188 70 for (int j = m_len[pp]; j > 0; j--, i++) {
cannam@0 71 x[i] = x[i-1] + dx;
cannam@0 72 y[i] = y[i-1] + dy;
cannam@0 73 }
cannam@0 74 }
cannam@0 75 return i;
cannam@0 76 }