annotate data/model/AlignmentModel.cpp @ 1694:a9d0b5a2c242 single-point

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