annotate data/model/AlignmentModel.cpp @ 338:f14e2f7b24f7

* More space and time efficient AlignmentModel
author Chris Cannam
date Thu, 22 Nov 2007 11:09:26 +0000
parents 1afaf98dbf11
children ba30f4a3e3be
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@297 16 #include "AlignmentModel.h"
Chris@297 17
Chris@297 18 #include "SparseTimeValueModel.h"
Chris@297 19
Chris@297 20 AlignmentModel::AlignmentModel(Model *reference,
Chris@297 21 Model *aligned,
Chris@297 22 Model *inputModel,
Chris@297 23 SparseTimeValueModel *path) :
Chris@297 24 m_reference(reference),
Chris@297 25 m_aligned(aligned),
Chris@297 26 m_inputModel(inputModel),
Chris@338 27 m_rawPath(path),
Chris@338 28 m_path(0),
Chris@297 29 m_reversePath(0),
Chris@323 30 m_pathBegun(false),
Chris@297 31 m_pathComplete(false)
Chris@297 32 {
Chris@338 33 connect(m_rawPath, SIGNAL(modelChanged()),
Chris@297 34 this, SLOT(pathChanged()));
Chris@297 35
Chris@338 36 connect(m_rawPath, SIGNAL(modelChanged(size_t, size_t)),
Chris@297 37 this, SLOT(pathChanged(size_t, size_t)));
Chris@297 38
Chris@338 39 connect(m_rawPath, SIGNAL(completionChanged()),
Chris@297 40 this, SLOT(pathCompletionChanged()));
Chris@297 41
Chris@338 42 constructPath();
Chris@297 43 constructReversePath();
Chris@297 44 }
Chris@297 45
Chris@297 46 AlignmentModel::~AlignmentModel()
Chris@297 47 {
Chris@297 48 delete m_inputModel;
Chris@338 49 delete m_rawPath;
Chris@297 50 delete m_path;
Chris@297 51 delete m_reversePath;
Chris@297 52 }
Chris@297 53
Chris@297 54 bool
Chris@297 55 AlignmentModel::isOK() const
Chris@297 56 {
Chris@338 57 if (m_rawPath) return m_rawPath->isOK();
Chris@338 58 else return true;
Chris@297 59 }
Chris@297 60
Chris@297 61 size_t
Chris@297 62 AlignmentModel::getStartFrame() const
Chris@297 63 {
Chris@297 64 size_t a = m_reference->getStartFrame();
Chris@297 65 size_t b = m_aligned->getStartFrame();
Chris@297 66 return std::min(a, b);
Chris@297 67 }
Chris@297 68
Chris@297 69 size_t
Chris@297 70 AlignmentModel::getEndFrame() const
Chris@297 71 {
Chris@297 72 size_t a = m_reference->getEndFrame();
Chris@297 73 size_t b = m_aligned->getEndFrame();
Chris@297 74 return std::max(a, b);
Chris@297 75 }
Chris@297 76
Chris@297 77 size_t
Chris@297 78 AlignmentModel::getSampleRate() const
Chris@297 79 {
Chris@297 80 return m_reference->getSampleRate();
Chris@297 81 }
Chris@297 82
Chris@297 83 Model *
Chris@297 84 AlignmentModel::clone() const
Chris@297 85 {
Chris@297 86 return new AlignmentModel
Chris@297 87 (m_reference, m_aligned,
Chris@297 88 m_inputModel ? m_inputModel->clone() : 0,
Chris@338 89 m_rawPath ? static_cast<SparseTimeValueModel *>(m_rawPath->clone()) : 0);
Chris@297 90 }
Chris@297 91
Chris@297 92 bool
Chris@297 93 AlignmentModel::isReady(int *completion) const
Chris@297 94 {
Chris@323 95 if (!m_pathBegun) {
Chris@338 96 if (completion) *completion = 0;
Chris@323 97 return false;
Chris@323 98 }
Chris@338 99 if (m_pathComplete || !m_rawPath) {
Chris@338 100 if (completion) *completion = 100;
Chris@338 101 return true;
Chris@338 102 }
Chris@338 103 return m_rawPath->isReady(completion);
Chris@297 104 }
Chris@297 105
Chris@297 106 const ZoomConstraint *
Chris@297 107 AlignmentModel::getZoomConstraint() const
Chris@297 108 {
Chris@338 109 return 0;
Chris@297 110 }
Chris@297 111
Chris@297 112 const Model *
Chris@297 113 AlignmentModel::getReferenceModel() const
Chris@297 114 {
Chris@297 115 return m_reference;
Chris@297 116 }
Chris@297 117
Chris@297 118 const Model *
Chris@297 119 AlignmentModel::getAlignedModel() const
Chris@297 120 {
Chris@297 121 return m_aligned;
Chris@297 122 }
Chris@297 123
Chris@297 124 size_t
Chris@297 125 AlignmentModel::toReference(size_t frame) const
Chris@297 126 {
Chris@297 127 // std::cerr << "AlignmentModel::toReference(" << frame << ")" << std::endl;
Chris@297 128 if (!m_reversePath) constructReversePath();
Chris@297 129 return align(m_reversePath, frame);
Chris@297 130 }
Chris@297 131
Chris@297 132 size_t
Chris@297 133 AlignmentModel::fromReference(size_t frame) const
Chris@297 134 {
Chris@297 135 // std::cerr << "AlignmentModel::fromReference(" << frame << ")" << std::endl;
Chris@338 136 if (!m_path) constructPath();
Chris@297 137 return align(m_path, frame);
Chris@297 138 }
Chris@297 139
Chris@297 140 void
Chris@297 141 AlignmentModel::pathChanged()
Chris@297 142 {
Chris@297 143 }
Chris@297 144
Chris@297 145 void
Chris@297 146 AlignmentModel::pathChanged(size_t, size_t)
Chris@297 147 {
Chris@297 148 if (!m_pathComplete) return;
Chris@338 149 constructPath();
Chris@297 150 constructReversePath();
Chris@297 151 }
Chris@297 152
Chris@297 153 void
Chris@297 154 AlignmentModel::pathCompletionChanged()
Chris@297 155 {
Chris@323 156 m_pathBegun = true;
Chris@323 157
Chris@297 158 if (!m_pathComplete) {
Chris@338 159
Chris@297 160 int completion = 0;
Chris@338 161 m_rawPath->isReady(&completion);
Chris@338 162
Chris@333 163 // std::cerr << "AlignmentModel::pathCompletionChanged: completion = "
Chris@333 164 // << completion << std::endl;
Chris@338 165
Chris@323 166 m_pathComplete = (completion == 100);
Chris@338 167
Chris@297 168 if (m_pathComplete) {
Chris@338 169
Chris@338 170 constructPath();
Chris@297 171 constructReversePath();
Chris@338 172
Chris@338 173 delete m_rawPath;
Chris@338 174 m_rawPath = 0;
Chris@338 175
Chris@297 176 delete m_inputModel;
Chris@297 177 m_inputModel = 0;
Chris@297 178 }
Chris@297 179 }
Chris@323 180
Chris@297 181 emit completionChanged();
Chris@297 182 }
Chris@297 183
Chris@297 184 void
Chris@338 185 AlignmentModel::constructPath() const
Chris@297 186 {
Chris@338 187 if (!m_path) {
Chris@338 188 if (!m_rawPath) {
Chris@338 189 std::cerr << "ERROR: AlignmentModel::constructPath: "
Chris@338 190 << "No raw path available" << std::endl;
Chris@338 191 return;
Chris@338 192 }
Chris@338 193 m_path = new PathModel
Chris@338 194 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
Chris@338 195 } else {
Chris@338 196 if (!m_rawPath) return;
Chris@297 197 }
Chris@297 198
Chris@338 199 m_path->clear();
Chris@297 200
Chris@338 201 SparseTimeValueModel::PointList points = m_rawPath->getPoints();
Chris@297 202
Chris@297 203 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@297 204 i != points.end(); ++i) {
Chris@297 205 long frame = i->frame;
Chris@297 206 float value = i->value;
Chris@297 207 long rframe = lrintf(value * m_aligned->getSampleRate());
Chris@338 208 m_path->addPoint(PathPoint(frame, rframe));
Chris@297 209 }
Chris@297 210
Chris@338 211 std::cerr << "AlignmentModel::constructPath: " << m_path->getPointCount() << " points, at least " << (2 * m_path->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << std::endl;
Chris@338 212 }
Chris@338 213
Chris@338 214 void
Chris@338 215 AlignmentModel::constructReversePath() const
Chris@338 216 {
Chris@338 217 if (!m_reversePath) {
Chris@338 218 if (!m_rawPath) {
Chris@338 219 std::cerr << "ERROR: AlignmentModel::constructReversePath: "
Chris@338 220 << "No raw path available" << std::endl;
Chris@338 221 return;
Chris@338 222 }
Chris@338 223 m_reversePath = new PathModel
Chris@338 224 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
Chris@338 225 } else {
Chris@338 226 if (!m_rawPath) return;
Chris@338 227 }
Chris@338 228
Chris@338 229 m_reversePath->clear();
Chris@338 230
Chris@338 231 SparseTimeValueModel::PointList points = m_rawPath->getPoints();
Chris@338 232
Chris@338 233 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@338 234 i != points.end(); ++i) {
Chris@338 235 long frame = i->frame;
Chris@338 236 float value = i->value;
Chris@338 237 long rframe = lrintf(value * m_aligned->getSampleRate());
Chris@338 238 m_reversePath->addPoint(PathPoint(rframe, frame));
Chris@338 239 }
Chris@338 240
Chris@338 241 std::cerr << "AlignmentModel::constructReversePath: " << m_reversePath->getPointCount() << " points, at least " << (2 * m_reversePath->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << std::endl;
Chris@297 242 }
Chris@297 243
Chris@297 244 size_t
Chris@338 245 AlignmentModel::align(PathModel *path, size_t frame) const
Chris@297 246 {
Chris@338 247 // The path consists of a series of points, each with frame equal
Chris@338 248 // to the frame on the source model and mapframe equal to the
Chris@338 249 // frame on the target model. Both should be monotonically
Chris@338 250 // increasing.
Chris@297 251
Chris@338 252 const PathModel::PointList &points = path->getPoints();
Chris@297 253
Chris@297 254 if (points.empty()) {
Chris@297 255 // std::cerr << "AlignmentModel::align: No points" << std::endl;
Chris@297 256 return frame;
Chris@297 257 }
Chris@297 258
Chris@338 259 PathModel::Point point(frame);
Chris@338 260 PathModel::PointList::const_iterator i = points.lower_bound(point);
Chris@297 261 if (i == points.end()) --i;
Chris@338 262 while (i != points.begin() && i->frame > long(frame)) --i;
Chris@297 263
Chris@312 264 long foundFrame = i->frame;
Chris@338 265 long foundMapFrame = i->mapframe;
Chris@297 266
Chris@312 267 long followingFrame = foundFrame;
Chris@338 268 long followingMapFrame = foundMapFrame;
Chris@297 269
Chris@312 270 if (++i != points.end()) {
Chris@312 271 followingFrame = i->frame;
Chris@338 272 followingMapFrame = i->mapframe;
Chris@312 273 }
Chris@312 274
Chris@338 275 if (foundMapFrame < 0) return 0;
Chris@312 276
Chris@338 277 size_t resultFrame = foundMapFrame;
Chris@312 278
Chris@338 279 if (followingFrame != foundFrame && long(frame) > foundFrame) {
Chris@338 280 float interp =
Chris@338 281 float(frame - foundFrame) /
Chris@338 282 float(followingFrame - foundFrame);
Chris@338 283 resultFrame += lrintf((followingMapFrame - foundMapFrame) * interp);
Chris@312 284 }
Chris@312 285
Chris@338 286 std::cerr << "AlignmentModel::align: resultFrame = " << resultFrame << std::endl;
Chris@312 287
Chris@312 288 return resultFrame;
Chris@297 289 }
Chris@297 290