annotate data/model/AlignmentModel.cpp @ 1735:d91ff235e69d by-id

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