annotate data/model/AlignmentModel.cpp @ 1719:6af7bd6aaf79 3.3-stable

Branch
author Chris Cannam
date Tue, 21 May 2019 17:14:23 +0100
parents 901f37d32060
children d08b560102a1
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@409 20 //#define DEBUG_ALIGNMENT_MODEL 1
Chris@376 21
Chris@297 22 AlignmentModel::AlignmentModel(Model *reference,
Chris@297 23 Model *aligned,
Chris@1429 24 SparseTimeValueModel *path) :
Chris@297 25 m_reference(reference),
Chris@297 26 m_aligned(aligned),
Chris@338 27 m_rawPath(path),
Chris@1582 28 m_path(nullptr),
Chris@1582 29 m_reversePath(nullptr),
Chris@323 30 m_pathBegun(false),
Chris@297 31 m_pathComplete(false)
Chris@297 32 {
Chris@371 33 if (m_rawPath) {
Chris@297 34
Chris@371 35 connect(m_rawPath, SIGNAL(modelChanged()),
Chris@371 36 this, SLOT(pathChanged()));
Chris@297 37
Chris@1038 38 connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1038 39 this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t)));
Chris@371 40
Chris@371 41 connect(m_rawPath, SIGNAL(completionChanged()),
Chris@371 42 this, SLOT(pathCompletionChanged()));
Chris@407 43
Chris@407 44 constructPath();
Chris@407 45 constructReversePath();
Chris@371 46 }
Chris@297 47
Chris@407 48 if (m_rawPath && m_rawPath->isReady()) {
Chris@407 49 pathCompletionChanged();
Chris@407 50 }
Chris@297 51 }
Chris@297 52
Chris@297 53 AlignmentModel::~AlignmentModel()
Chris@297 54 {
Chris@407 55 if (m_rawPath) m_rawPath->aboutToDelete();
Chris@338 56 delete m_rawPath;
Chris@407 57
Chris@407 58 if (m_path) m_path->aboutToDelete();
Chris@297 59 delete m_path;
Chris@407 60
Chris@407 61 if (m_reversePath) m_reversePath->aboutToDelete();
Chris@297 62 delete m_reversePath;
Chris@297 63 }
Chris@297 64
Chris@297 65 bool
Chris@297 66 AlignmentModel::isOK() const
Chris@297 67 {
Chris@338 68 if (m_rawPath) return m_rawPath->isOK();
Chris@338 69 else return true;
Chris@297 70 }
Chris@297 71
Chris@1038 72 sv_frame_t
Chris@297 73 AlignmentModel::getStartFrame() const
Chris@297 74 {
Chris@1038 75 sv_frame_t a = m_reference->getStartFrame();
Chris@1038 76 sv_frame_t b = m_aligned->getStartFrame();
Chris@297 77 return std::min(a, b);
Chris@297 78 }
Chris@297 79
Chris@1038 80 sv_frame_t
Chris@297 81 AlignmentModel::getEndFrame() const
Chris@297 82 {
Chris@1038 83 sv_frame_t a = m_reference->getEndFrame();
Chris@1038 84 sv_frame_t b = m_aligned->getEndFrame();
Chris@297 85 return std::max(a, b);
Chris@297 86 }
Chris@297 87
Chris@1040 88 sv_samplerate_t
Chris@297 89 AlignmentModel::getSampleRate() const
Chris@297 90 {
Chris@297 91 return m_reference->getSampleRate();
Chris@297 92 }
Chris@297 93
Chris@297 94 bool
Chris@297 95 AlignmentModel::isReady(int *completion) const
Chris@297 96 {
Chris@411 97 if (!m_pathBegun && m_rawPath) {
Chris@338 98 if (completion) *completion = 0;
Chris@1561 99 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1561 100 SVDEBUG << "AlignmentModel::isReady: path not begun" << endl;
Chris@1561 101 #endif
Chris@323 102 return false;
Chris@323 103 }
Chris@1016 104 if (m_pathComplete) {
Chris@338 105 if (completion) *completion = 100;
Chris@1561 106 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1561 107 SVDEBUG << "AlignmentModel::isReady: path complete" << endl;
Chris@1561 108 #endif
Chris@338 109 return true;
Chris@338 110 }
Chris@1016 111 if (!m_rawPath) {
Chris@1016 112 // lack of raw path could mean path is complete (in which case
Chris@1016 113 // m_pathComplete true above) or else no alignment has been
Chris@1016 114 // set at all yet (this case)
Chris@1016 115 if (completion) *completion = 0;
Chris@1561 116 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1561 117 SVDEBUG << "AlignmentModel::isReady: no raw path" << endl;
Chris@1561 118 #endif
Chris@1016 119 return false;
Chris@1016 120 }
Chris@338 121 return m_rawPath->isReady(completion);
Chris@297 122 }
Chris@297 123
Chris@297 124 const ZoomConstraint *
Chris@297 125 AlignmentModel::getZoomConstraint() const
Chris@297 126 {
Chris@1582 127 return nullptr;
Chris@297 128 }
Chris@297 129
Chris@297 130 const Model *
Chris@297 131 AlignmentModel::getReferenceModel() const
Chris@297 132 {
Chris@297 133 return m_reference;
Chris@297 134 }
Chris@297 135
Chris@297 136 const Model *
Chris@297 137 AlignmentModel::getAlignedModel() const
Chris@297 138 {
Chris@297 139 return m_aligned;
Chris@297 140 }
Chris@297 141
Chris@1038 142 sv_frame_t
Chris@1038 143 AlignmentModel::toReference(sv_frame_t frame) const
Chris@297 144 {
Chris@376 145 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 146 cerr << "AlignmentModel::toReference(" << frame << ")" << endl;
Chris@376 147 #endif
Chris@371 148 if (!m_path) {
Chris@371 149 if (!m_rawPath) return frame;
Chris@371 150 constructPath();
Chris@371 151 }
Chris@339 152 return align(m_path, frame);
Chris@297 153 }
Chris@297 154
Chris@1038 155 sv_frame_t
Chris@1038 156 AlignmentModel::fromReference(sv_frame_t frame) const
Chris@297 157 {
Chris@376 158 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 159 cerr << "AlignmentModel::fromReference(" << frame << ")" << endl;
Chris@376 160 #endif
Chris@371 161 if (!m_reversePath) {
Chris@371 162 if (!m_rawPath) return frame;
Chris@371 163 constructReversePath();
Chris@371 164 }
Chris@339 165 return align(m_reversePath, frame);
Chris@297 166 }
Chris@297 167
Chris@297 168 void
Chris@297 169 AlignmentModel::pathChanged()
Chris@297 170 {
Chris@339 171 if (m_pathComplete) {
Chris@843 172 cerr << "AlignmentModel: deleting raw path model" << endl;
Chris@407 173 if (m_rawPath) m_rawPath->aboutToDelete();
Chris@339 174 delete m_rawPath;
Chris@1582 175 m_rawPath = nullptr;
Chris@339 176 }
Chris@297 177 }
Chris@297 178
Chris@297 179 void
Chris@1038 180 AlignmentModel::pathChangedWithin(sv_frame_t, sv_frame_t)
Chris@297 181 {
Chris@297 182 if (!m_pathComplete) return;
Chris@338 183 constructPath();
Chris@297 184 constructReversePath();
Chris@297 185 }
Chris@297 186
Chris@297 187 void
Chris@297 188 AlignmentModel::pathCompletionChanged()
Chris@297 189 {
Chris@339 190 if (!m_rawPath) return;
Chris@323 191 m_pathBegun = true;
Chris@323 192
Chris@297 193 if (!m_pathComplete) {
Chris@338 194
Chris@297 195 int completion = 0;
Chris@338 196 m_rawPath->isReady(&completion);
Chris@338 197
Chris@376 198 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 199 cerr << "AlignmentModel::pathCompletionChanged: completion = "
Chris@687 200 << completion << endl;
Chris@376 201 #endif
Chris@338 202
Chris@323 203 m_pathComplete = (completion == 100);
Chris@338 204
Chris@297 205 if (m_pathComplete) {
Chris@338 206
Chris@338 207 constructPath();
Chris@297 208 constructReversePath();
Chris@407 209
Chris@297 210 }
Chris@297 211 }
Chris@323 212
Chris@297 213 emit completionChanged();
Chris@297 214 }
Chris@297 215
Chris@297 216 void
Chris@338 217 AlignmentModel::constructPath() const
Chris@297 218 {
Chris@338 219 if (!m_path) {
Chris@338 220 if (!m_rawPath) {
Chris@843 221 cerr << "ERROR: AlignmentModel::constructPath: "
Chris@843 222 << "No raw path available" << endl;
Chris@338 223 return;
Chris@338 224 }
Chris@338 225 m_path = new PathModel
Chris@338 226 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
Chris@338 227 } else {
Chris@338 228 if (!m_rawPath) return;
Chris@297 229 }
Chris@297 230
Chris@338 231 m_path->clear();
Chris@297 232
Chris@338 233 SparseTimeValueModel::PointList points = m_rawPath->getPoints();
Chris@297 234
Chris@297 235 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@297 236 i != points.end(); ++i) {
Chris@1038 237 sv_frame_t frame = i->frame;
Chris@1038 238 double value = i->value;
Chris@1038 239 sv_frame_t rframe = lrint(value * m_aligned->getSampleRate());
Chris@338 240 m_path->addPoint(PathPoint(frame, rframe));
Chris@297 241 }
Chris@297 242
Chris@376 243 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 244 cerr << "AlignmentModel::constructPath: " << m_path->getPointCount() << " points, at least " << (2 * m_path->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
Chris@376 245 #endif
Chris@338 246 }
Chris@338 247
Chris@338 248 void
Chris@338 249 AlignmentModel::constructReversePath() const
Chris@338 250 {
Chris@338 251 if (!m_reversePath) {
Chris@407 252 if (!m_path) {
Chris@843 253 cerr << "ERROR: AlignmentModel::constructReversePath: "
Chris@843 254 << "No forward path available" << endl;
Chris@407 255 return;
Chris@407 256 }
Chris@407 257 m_reversePath = new PathModel
Chris@407 258 (m_path->getSampleRate(), m_path->getResolution(), false);
Chris@338 259 } else {
Chris@407 260 if (!m_path) return;
Chris@338 261 }
Chris@338 262
Chris@338 263 m_reversePath->clear();
Chris@407 264
Chris@407 265 PathModel::PointList points = m_path->getPoints();
Chris@407 266
Chris@407 267 for (PathModel::PointList::const_iterator i = points.begin();
Chris@407 268 i != points.end(); ++i) {
Chris@1038 269 sv_frame_t frame = i->frame;
Chris@1038 270 sv_frame_t rframe = i->mapframe;
Chris@407 271 m_reversePath->addPoint(PathPoint(rframe, frame));
Chris@407 272 }
Chris@338 273
Chris@376 274 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 275 cerr << "AlignmentModel::constructReversePath: " << m_reversePath->getPointCount() << " points, at least " << (2 * m_reversePath->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
Chris@376 276 #endif
Chris@297 277 }
Chris@297 278
Chris@1038 279 sv_frame_t
Chris@1038 280 AlignmentModel::align(PathModel *path, sv_frame_t frame) const
Chris@297 281 {
Chris@339 282 if (!path) return frame;
Chris@339 283
Chris@338 284 // The path consists of a series of points, each with frame equal
Chris@338 285 // to the frame on the source model and mapframe equal to the
Chris@338 286 // frame on the target model. Both should be monotonically
Chris@338 287 // increasing.
Chris@297 288
Chris@338 289 const PathModel::PointList &points = path->getPoints();
Chris@297 290
Chris@297 291 if (points.empty()) {
Chris@379 292 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 293 cerr << "AlignmentModel::align: No points" << endl;
Chris@379 294 #endif
Chris@297 295 return frame;
Chris@297 296 }
Chris@297 297
Chris@376 298 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 299 cerr << "AlignmentModel::align: frame " << frame << " requested" << endl;
Chris@376 300 #endif
Chris@376 301
Chris@338 302 PathModel::Point point(frame);
Chris@338 303 PathModel::PointList::const_iterator i = points.lower_bound(point);
Chris@376 304 if (i == points.end()) {
Chris@376 305 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@843 306 cerr << "Note: i == points.end()" << endl;
Chris@376 307 #endif
Chris@376 308 --i;
Chris@376 309 }
Chris@1038 310 while (i != points.begin() && i->frame > frame) --i;
Chris@297 311
Chris@1038 312 sv_frame_t foundFrame = i->frame;
Chris@1038 313 sv_frame_t foundMapFrame = i->mapframe;
Chris@297 314
Chris@1038 315 sv_frame_t followingFrame = foundFrame;
Chris@1038 316 sv_frame_t followingMapFrame = foundMapFrame;
Chris@297 317
Chris@312 318 if (++i != points.end()) {
Chris@376 319 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@843 320 cerr << "another point available" << endl;
Chris@376 321 #endif
Chris@312 322 followingFrame = i->frame;
Chris@338 323 followingMapFrame = i->mapframe;
Chris@376 324 } else {
Chris@376 325 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@843 326 cerr << "no other point available" << endl;
Chris@376 327 #endif
Chris@376 328 }
Chris@312 329
Chris@1075 330 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 331 cerr << "foundFrame = " << foundFrame << ", foundMapFrame = " << foundMapFrame
Chris@1075 332 << ", followingFrame = " << followingFrame << ", followingMapFrame = "
Chris@1075 333 << followingMapFrame << endl;
Chris@1075 334 #endif
Chris@1075 335
Chris@338 336 if (foundMapFrame < 0) return 0;
Chris@312 337
Chris@1038 338 sv_frame_t resultFrame = foundMapFrame;
Chris@312 339
Chris@1038 340 if (followingFrame != foundFrame && frame > foundFrame) {
Chris@1038 341 double interp =
Chris@1038 342 double(frame - foundFrame) /
Chris@1038 343 double(followingFrame - foundFrame);
Chris@1038 344 resultFrame += lrint(double(followingMapFrame - foundMapFrame) * interp);
Chris@312 345 }
Chris@312 346
Chris@376 347 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 348 cerr << "AlignmentModel::align: resultFrame = " << resultFrame << endl;
Chris@376 349 #endif
Chris@312 350
Chris@312 351 return resultFrame;
Chris@297 352 }
Chris@407 353
Chris@407 354 void
Chris@1016 355 AlignmentModel::setPathFrom(SparseTimeValueModel *rawpath)
Chris@1016 356 {
Chris@1016 357 if (m_rawPath) m_rawPath->aboutToDelete();
Chris@1016 358 delete m_rawPath;
Chris@1016 359
Chris@1016 360 m_rawPath = rawpath;
Chris@1016 361
Chris@1016 362 connect(m_rawPath, SIGNAL(modelChanged()),
Chris@1016 363 this, SLOT(pathChanged()));
Chris@1016 364
Chris@1046 365 connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1046 366 this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t)));
Chris@1016 367
Chris@1016 368 connect(m_rawPath, SIGNAL(completionChanged()),
Chris@1016 369 this, SLOT(pathCompletionChanged()));
Chris@1016 370
Chris@1016 371 constructPath();
Chris@1016 372 constructReversePath();
Chris@1016 373
Chris@1016 374 if (m_rawPath->isReady()) {
Chris@1016 375 pathCompletionChanged();
Chris@1016 376 }
Chris@1016 377 }
Chris@1016 378
Chris@1016 379 void
Chris@407 380 AlignmentModel::setPath(PathModel *path)
Chris@407 381 {
Chris@407 382 if (m_path) m_path->aboutToDelete();
Chris@407 383 delete m_path;
Chris@407 384 m_path = path;
Chris@1560 385 m_pathComplete = true;
Chris@662 386 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 387 cerr << "AlignmentModel::setPath: path = " << m_path << endl;
Chris@662 388 #endif
Chris@407 389 constructReversePath();
Chris@662 390 #ifdef DEBUG_ALIGNMENT_MODEL
Chris@1075 391 cerr << "AlignmentModel::setPath: after construction path = "
Chris@687 392 << m_path << ", rpath = " << m_reversePath << endl;
Chris@662 393 #endif
Chris@407 394 }
Chris@297 395
Chris@407 396 void
Chris@407 397 AlignmentModel::toXml(QTextStream &stream,
Chris@407 398 QString indent,
Chris@407 399 QString extraAttributes) const
Chris@407 400 {
Chris@407 401 if (!m_path) {
Chris@690 402 SVDEBUG << "AlignmentModel::toXml: no path" << endl;
Chris@407 403 return;
Chris@407 404 }
Chris@407 405
Chris@407 406 m_path->toXml(stream, indent, "");
Chris@407 407
Chris@407 408 Model::toXml(stream, indent,
Chris@407 409 QString("type=\"alignment\" reference=\"%1\" aligned=\"%2\" path=\"%3\" %4")
Chris@407 410 .arg(getObjectExportId(m_reference))
Chris@407 411 .arg(getObjectExportId(m_aligned))
Chris@407 412 .arg(getObjectExportId(m_path))
Chris@407 413 .arg(extraAttributes));
Chris@407 414 }
Chris@407 415
Chris@407 416