annotate data/model/AlignmentModel.cpp @ 1737:5d631f6129fe by-id

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