annotate framework/Align.cpp @ 672:ae7584dbd668 tuning-difference

Provide facility to re-align models
author Chris Cannam
date Fri, 17 May 2019 09:45:12 +0100
parents b6cafe05017d
children b375fdbb74bc
rev   line source
Chris@420 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@420 2
Chris@420 3 /*
Chris@420 4 Sonic Visualiser
Chris@420 5 An audio file viewer and annotation editor.
Chris@420 6 Centre for Digital Music, Queen Mary, University of London.
Chris@420 7
Chris@420 8 This program is free software; you can redistribute it and/or
Chris@420 9 modify it under the terms of the GNU General Public License as
Chris@420 10 published by the Free Software Foundation; either version 2 of the
Chris@420 11 License, or (at your option) any later version. See the file
Chris@420 12 COPYING included with this distribution for more information.
Chris@420 13 */
Chris@420 14
Chris@420 15 #include "Align.h"
Chris@665 16 #include "Document.h"
Chris@420 17
Chris@420 18 #include "data/model/WaveFileModel.h"
Chris@515 19 #include "data/model/ReadOnlyWaveFileModel.h"
Chris@420 20 #include "data/model/AggregateWaveModel.h"
Chris@420 21 #include "data/model/RangeSummarisableTimeValueModel.h"
Chris@420 22 #include "data/model/SparseTimeValueModel.h"
Chris@420 23 #include "data/model/AlignmentModel.h"
Chris@420 24
Chris@420 25 #include "data/fileio/CSVFileReader.h"
Chris@420 26
Chris@420 27 #include "transform/TransformFactory.h"
Chris@420 28 #include "transform/ModelTransformerFactory.h"
Chris@420 29 #include "transform/FeatureExtractionModelTransformer.h"
Chris@420 30
Chris@420 31 #include <QProcess>
Chris@422 32 #include <QSettings>
Chris@430 33 #include <QApplication>
Chris@422 34
Chris@422 35 bool
Chris@670 36 Align::alignModel(Document *doc, Model *ref, Model *other, QString &error)
Chris@422 37 {
Chris@422 38 QSettings settings;
Chris@422 39 settings.beginGroup("Preferences");
Chris@422 40 bool useProgram = settings.value("use-external-alignment", false).toBool();
Chris@422 41 QString program = settings.value("external-alignment-program", "").toString();
Chris@422 42 settings.endGroup();
Chris@422 43
Chris@422 44 if (useProgram && (program != "")) {
Chris@670 45 return alignModelViaProgram(doc, ref, other, program, error);
Chris@422 46 } else {
Chris@670 47 return alignModelViaTransform(doc, ref, other, error);
Chris@422 48 }
Chris@422 49 }
Chris@420 50
Chris@428 51 QString
Chris@428 52 Align::getAlignmentTransformName()
Chris@428 53 {
Chris@428 54 QSettings settings;
Chris@428 55 settings.beginGroup("Alignment");
Chris@428 56 TransformId id =
Chris@428 57 settings.value("transform-id",
Chris@428 58 "vamp:match-vamp-plugin:match:path").toString();
Chris@428 59 settings.endGroup();
Chris@428 60 return id;
Chris@428 61 }
Chris@428 62
Chris@670 63 QString
Chris@670 64 Align::getTuningDifferenceTransformName()
Chris@670 65 {
Chris@670 66 QSettings settings;
Chris@670 67 settings.beginGroup("Alignment");
Chris@670 68 bool performPitchCompensation =
Chris@670 69 settings.value("align-pitch-aware", false).toBool();
Chris@670 70 QString id = "";
Chris@671 71 if (performPitchCompensation) {
Chris@670 72 id = settings.value
Chris@670 73 ("tuning-difference-transform-id",
Chris@670 74 "vamp:tuning-difference:tuning-difference:tuningfreq")
Chris@670 75 .toString();
Chris@671 76 }
Chris@670 77 settings.endGroup();
Chris@670 78 return id;
Chris@670 79 }
Chris@670 80
Chris@428 81 bool
Chris@428 82 Align::canAlign()
Chris@428 83 {
Chris@670 84 TransformFactory *factory = TransformFactory::getInstance();
Chris@428 85 TransformId id = getAlignmentTransformName();
Chris@670 86 TransformId tdId = getTuningDifferenceTransformName();
Chris@670 87 return factory->haveTransform(id) &&
Chris@670 88 (tdId == "" || factory->haveTransform(tdId));
Chris@428 89 }
Chris@428 90
Chris@420 91 bool
Chris@670 92 Align::alignModelViaTransform(Document *doc, Model *ref, Model *other,
Chris@670 93 QString &error)
Chris@420 94 {
Chris@670 95 QMutexLocker locker (&m_mutex);
Chris@670 96
Chris@420 97 RangeSummarisableTimeValueModel *reference = qobject_cast
Chris@420 98 <RangeSummarisableTimeValueModel *>(ref);
Chris@420 99
Chris@420 100 RangeSummarisableTimeValueModel *rm = qobject_cast
Chris@420 101 <RangeSummarisableTimeValueModel *>(other);
Chris@420 102
Chris@420 103 if (!reference || !rm) return false; // but this should have been tested already
Chris@420 104
Chris@670 105 // This involves creating either three or four new models:
Chris@672 106 //
Chris@420 107 // 1. an AggregateWaveModel to provide the mixdowns of the main
Chris@420 108 // model and the new model in its two channels, as input to the
Chris@420 109 // MATCH plugin
Chris@672 110 //
Chris@670 111 // 2a. a SparseTimeValueModel which will be automatically created
Chris@670 112 // by FeatureExtractionModelTransformer when running the
Chris@670 113 // TuningDifference plugin to receive the relative tuning of the
Chris@670 114 // second model (if pitch-aware alignment is enabled in the
Chris@670 115 // preferences)
Chris@672 116 //
Chris@670 117 // 2b. a SparseTimeValueModel which will be automatically created
Chris@670 118 // by FeatureExtractionPluginTransformer when running the MATCH
Chris@670 119 // plugin to perform alignment (so containing the alignment path)
Chris@672 120 //
Chris@420 121 // 3. an AlignmentModel, which stores the path model and carries
Chris@420 122 // out alignment lookups on it.
Chris@672 123 //
Chris@670 124 // The AggregateWaveModel [1] is registered with the document,
Chris@670 125 // which deletes it when it is invalidated (when one of its
Chris@670 126 // components is deleted). The SparseTimeValueModel [2a] is reused
Chris@670 127 // by us when starting the alignment process proper, and is then
Chris@670 128 // deleted by us. The SparseTimeValueModel [2b] is passed to the
Chris@670 129 // AlignmentModel, which takes ownership of it. The AlignmentModel
Chris@670 130 // is attached to the new model we are aligning, which also takes
Chris@670 131 // ownership of it. The only one of these models that we need to
Chris@670 132 // delete here is the SparseTimeValueModel [2a].
Chris@672 133 //
Chris@672 134 // (We also create a sneaky additional SparseTimeValueModel
Chris@672 135 // temporarily so we can attach completion information to it -
Chris@672 136 // this is quite unnecessary from the perspective of simply
Chris@672 137 // producing the results.)
Chris@420 138
Chris@420 139 AggregateWaveModel::ChannelSpecList components;
Chris@420 140
Chris@420 141 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@420 142 (reference, -1));
Chris@420 143
Chris@420 144 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@420 145 (rm, -1));
Chris@420 146
Chris@665 147 AggregateWaveModel *aggregateModel = new AggregateWaveModel(components);
Chris@665 148 doc->addAggregateModel(aggregateModel);
Chris@670 149
Chris@670 150 AlignmentModel *alignmentModel =
Chris@670 151 new AlignmentModel(reference, other, nullptr);
Chris@670 152
Chris@670 153 TransformId tdId = getTuningDifferenceTransformName();
Chris@670 154
Chris@670 155 if (tdId == "") {
Chris@670 156
Chris@670 157 if (beginTransformDrivenAlignment(aggregateModel, alignmentModel)) {
Chris@670 158 rm->setAlignment(alignmentModel);
Chris@670 159 } else {
Chris@670 160 error = alignmentModel->getError();
Chris@670 161 delete alignmentModel;
Chris@670 162 return false;
Chris@670 163 }
Chris@670 164
Chris@670 165 } else {
Chris@670 166
Chris@670 167 // Have a tuning-difference transform id, so run it
Chris@670 168 // asynchronously first
Chris@670 169
Chris@670 170 TransformFactory *tf = TransformFactory::getInstance();
Chris@670 171
Chris@670 172 Transform transform = tf->getDefaultTransformFor
Chris@670 173 (tdId, aggregateModel->getSampleRate());
Chris@670 174
Chris@671 175 transform.setParameter("maxduration", 50);
Chris@671 176 transform.setParameter("maxrange", 5);
Chris@671 177
Chris@670 178 SVDEBUG << "Align::alignModel: Tuning difference transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@670 179
Chris@670 180 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@670 181
Chris@670 182 QString message;
Chris@670 183 Model *transformOutput = mtf->transform(transform, aggregateModel, message);
Chris@670 184
Chris@670 185 SparseTimeValueModel *tdout = dynamic_cast<SparseTimeValueModel *>
Chris@670 186 (transformOutput);
Chris@670 187
Chris@670 188 if (!tdout) {
Chris@670 189 SVCERR << "Align::alignModel: ERROR: Failed to create tuning-difference output model (no Tuning Difference plugin?)" << endl;
Chris@670 190 delete tdout;
Chris@670 191 error = message;
Chris@670 192 return false;
Chris@670 193 }
Chris@670 194
Chris@670 195 rm->setAlignment(alignmentModel);
Chris@665 196
Chris@670 197 connect(tdout, SIGNAL(completionChanged()),
Chris@670 198 this, SLOT(tuningDifferenceCompletionChanged()));
Chris@420 199
Chris@671 200 TuningDiffRec rec;
Chris@671 201 rec.input = aggregateModel;
Chris@671 202 rec.alignment = alignmentModel;
Chris@671 203
Chris@671 204 // This model exists only so that the AlignmentModel can get a
Chris@671 205 // completion value from somewhere while the tuning difference
Chris@671 206 // calculation is going on
Chris@671 207 rec.preparatory = new SparseTimeValueModel
Chris@671 208 (aggregateModel->getSampleRate(), 1);;
Chris@671 209 rec.preparatory->setCompletion(0);
Chris@671 210 alignmentModel->setPathFrom(rec.preparatory);
Chris@671 211
Chris@671 212 m_pendingTuningDiffs[tdout] = rec;
Chris@670 213 }
Chris@670 214
Chris@670 215 return true;
Chris@670 216 }
Chris@670 217
Chris@671 218 void
Chris@671 219 Align::tuningDifferenceCompletionChanged()
Chris@671 220 {
Chris@671 221 QMutexLocker locker (&m_mutex);
Chris@671 222
Chris@671 223 SparseTimeValueModel *td = qobject_cast<SparseTimeValueModel *>(sender());
Chris@671 224 if (!td) return;
Chris@671 225
Chris@671 226 if (m_pendingTuningDiffs.find(td) == m_pendingTuningDiffs.end()) {
Chris@671 227 SVCERR << "ERROR: Align::tuningDifferenceCompletionChanged: Model "
Chris@671 228 << td << " not found in pending tuning diff map!" << endl;
Chris@671 229 return;
Chris@671 230 }
Chris@671 231
Chris@671 232 TuningDiffRec rec = m_pendingTuningDiffs[td];
Chris@671 233
Chris@671 234 int completion = 0;
Chris@671 235 bool done = td->isReady(&completion);
Chris@671 236
Chris@671 237 SVCERR << "Align::tuningDifferenceCompletionChanged: done = " << done << ", completion = " << completion << endl;
Chris@671 238
Chris@671 239 if (!done) {
Chris@671 240 // This will be the completion the alignment model reports,
Chris@671 241 // before the alignment actually begins. It goes up from 0 to
Chris@671 242 // 99 (not 100!) and then back to 0 again when we start
Chris@671 243 // calculating the actual path in the following phase
Chris@671 244 int clamped = (completion == 100 ? 99 : completion);
Chris@671 245 SVCERR << "Align::tuningDifferenceCompletionChanged: setting rec.preparatory completion to " << clamped << endl;
Chris@671 246 rec.preparatory->setCompletion(clamped);
Chris@671 247 return;
Chris@671 248 }
Chris@671 249
Chris@671 250 float tuningFrequency = 440.f;
Chris@671 251
Chris@671 252 if (!td->isEmpty()) {
Chris@671 253 tuningFrequency = td->getAllEvents()[0].getValue();
Chris@671 254 SVCERR << "Align::tuningDifferenceCompletionChanged: Reported tuning frequency = " << tuningFrequency << endl;
Chris@671 255 } else {
Chris@671 256 SVCERR << "Align::tuningDifferenceCompletionChanged: No tuning frequency reported" << endl;
Chris@671 257 }
Chris@671 258
Chris@671 259 m_pendingTuningDiffs.erase(td);
Chris@671 260 td->aboutToDelete();
Chris@671 261 delete td;
Chris@671 262
Chris@671 263 rec.alignment->setPathFrom(nullptr);
Chris@671 264
Chris@671 265 beginTransformDrivenAlignment
Chris@671 266 (rec.input, rec.alignment, tuningFrequency);
Chris@671 267 }
Chris@671 268
Chris@670 269 bool
Chris@670 270 Align::beginTransformDrivenAlignment(AggregateWaveModel *aggregateModel,
Chris@670 271 AlignmentModel *alignmentModel,
Chris@670 272 float tuningFrequency)
Chris@670 273 {
Chris@428 274 TransformId id = getAlignmentTransformName();
Chris@420 275
Chris@420 276 TransformFactory *tf = TransformFactory::getInstance();
Chris@420 277
Chris@420 278 Transform transform = tf->getDefaultTransformFor
Chris@420 279 (id, aggregateModel->getSampleRate());
Chris@420 280
Chris@420 281 transform.setStepSize(transform.getBlockSize()/2);
Chris@420 282 transform.setParameter("serialise", 1);
Chris@420 283 transform.setParameter("smooth", 0);
Chris@420 284
Chris@670 285 if (tuningFrequency != 0.f) {
Chris@670 286 transform.setParameter("freq2", tuningFrequency);
Chris@670 287 }
Chris@670 288
Chris@420 289 SVDEBUG << "Align::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@420 290
Chris@420 291 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@420 292
Chris@420 293 QString message;
Chris@670 294 Model *transformOutput = mtf->transform
Chris@670 295 (transform, aggregateModel, message);
Chris@420 296
Chris@420 297 if (!transformOutput) {
Chris@420 298 transform.setStepSize(0);
Chris@670 299 transformOutput = mtf->transform
Chris@670 300 (transform, aggregateModel, message);
Chris@420 301 }
Chris@420 302
Chris@420 303 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
Chris@420 304 (transformOutput);
Chris@420 305
Chris@670 306 //!!! callers will need to be updated to get error from
Chris@670 307 //!!! alignment model after initial call
Chris@670 308
Chris@420 309 if (!path) {
Chris@649 310 SVCERR << "Align::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl;
Chris@420 311 delete transformOutput;
Chris@670 312 alignmentModel->setError(message);
Chris@420 313 return false;
Chris@420 314 }
Chris@420 315
Chris@420 316 path->setCompletion(0);
Chris@670 317 alignmentModel->setPathFrom(path);
Chris@420 318
Chris@428 319 connect(alignmentModel, SIGNAL(completionChanged()),
Chris@428 320 this, SLOT(alignmentCompletionChanged()));
Chris@420 321
Chris@420 322 return true;
Chris@420 323 }
Chris@420 324
Chris@428 325 void
Chris@428 326 Align::alignmentCompletionChanged()
Chris@428 327 {
Chris@670 328 QMutexLocker locker (&m_mutex);
Chris@670 329
Chris@428 330 AlignmentModel *am = qobject_cast<AlignmentModel *>(sender());
Chris@428 331 if (!am) return;
Chris@428 332 if (am->isReady()) {
Chris@428 333 disconnect(am, SIGNAL(completionChanged()),
Chris@428 334 this, SLOT(alignmentCompletionChanged()));
Chris@428 335 emit alignmentComplete(am);
Chris@428 336 }
Chris@428 337 }
Chris@428 338
Chris@420 339 bool
Chris@670 340 Align::alignModelViaProgram(Document *, Model *ref, Model *other,
Chris@670 341 QString program, QString &error)
Chris@420 342 {
Chris@670 343 QMutexLocker locker (&m_mutex);
Chris@670 344
Chris@420 345 WaveFileModel *reference = qobject_cast<WaveFileModel *>(ref);
Chris@420 346 WaveFileModel *rm = qobject_cast<WaveFileModel *>(other);
Chris@420 347
Chris@515 348 if (!reference || !rm) {
Chris@515 349 return false; // but this should have been tested already
Chris@515 350 }
Chris@420 351
Chris@636 352 while (!reference->isReady(nullptr) || !rm->isReady(nullptr)) {
Chris@430 353 qApp->processEvents();
Chris@430 354 }
Chris@430 355
Chris@420 356 // Run an external program, passing to it paths to the main
Chris@420 357 // model's audio file and the new model's audio file. It returns
Chris@420 358 // the path in CSV form through stdout.
Chris@420 359
Chris@515 360 ReadOnlyWaveFileModel *roref = qobject_cast<ReadOnlyWaveFileModel *>(reference);
Chris@515 361 ReadOnlyWaveFileModel *rorm = qobject_cast<ReadOnlyWaveFileModel *>(rm);
Chris@515 362 if (!roref || !rorm) {
Chris@649 363 SVCERR << "ERROR: Align::alignModelViaProgram: Can't align non-read-only models via program (no local filename available)" << endl;
Chris@515 364 return false;
Chris@515 365 }
Chris@515 366
Chris@515 367 QString refPath = roref->getLocalFilename();
Chris@515 368 QString otherPath = rorm->getLocalFilename();
Chris@420 369
Chris@420 370 if (refPath == "" || otherPath == "") {
Chris@670 371 error = "Failed to find local filepath for wave-file model";
Chris@595 372 return false;
Chris@420 373 }
Chris@420 374
Chris@665 375 AlignmentModel *alignmentModel =
Chris@665 376 new AlignmentModel(reference, other, nullptr);
Chris@423 377 rm->setAlignment(alignmentModel);
Chris@423 378
Chris@423 379 QProcess *process = new QProcess;
Chris@420 380 QStringList args;
Chris@420 381 args << refPath << otherPath;
Chris@423 382
Chris@423 383 connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@423 384 this, SLOT(alignmentProgramFinished(int, QProcess::ExitStatus)));
Chris@420 385
Chris@670 386 m_pendingProcesses[process] = alignmentModel;
Chris@423 387 process->start(program, args);
Chris@420 388
Chris@423 389 bool success = process->waitForStarted();
Chris@423 390
Chris@423 391 if (!success) {
Chris@649 392 SVCERR << "ERROR: Align::alignModelViaProgram: Program did not start"
Chris@649 393 << endl;
Chris@670 394 error = "Alignment program could not be started";
Chris@670 395 m_pendingProcesses.erase(process);
Chris@636 396 rm->setAlignment(nullptr); // deletes alignmentModel as well
Chris@423 397 delete process;
Chris@423 398 }
Chris@423 399
Chris@423 400 return success;
Chris@423 401 }
Chris@423 402
Chris@423 403 void
Chris@423 404 Align::alignmentProgramFinished(int exitCode, QProcess::ExitStatus status)
Chris@423 405 {
Chris@670 406 QMutexLocker locker (&m_mutex);
Chris@670 407
Chris@649 408 SVCERR << "Align::alignmentProgramFinished" << endl;
Chris@423 409
Chris@423 410 QProcess *process = qobject_cast<QProcess *>(sender());
Chris@423 411
Chris@670 412 if (m_pendingProcesses.find(process) == m_pendingProcesses.end()) {
Chris@649 413 SVCERR << "ERROR: Align::alignmentProgramFinished: Process " << process
Chris@649 414 << " not found in process model map!" << endl;
Chris@423 415 return;
Chris@423 416 }
Chris@423 417
Chris@670 418 AlignmentModel *alignmentModel = m_pendingProcesses[process];
Chris@423 419
Chris@423 420 if (exitCode == 0 && status == 0) {
Chris@420 421
Chris@595 422 CSVFormat format;
Chris@595 423 format.setModelType(CSVFormat::TwoDimensionalModel);
Chris@595 424 format.setTimingType(CSVFormat::ExplicitTiming);
Chris@595 425 format.setTimeUnits(CSVFormat::TimeSeconds);
Chris@595 426 format.setColumnCount(2);
Chris@425 427 // The output format has time in the reference file first, and
Chris@425 428 // time in the "other" file in the second column. This is a
Chris@425 429 // more natural approach for a command-line alignment tool,
Chris@425 430 // but it's the opposite of what we expect for native
Chris@425 431 // alignment paths, which map from "other" file to
Chris@425 432 // reference. These column purpose settings reflect that.
Chris@595 433 format.setColumnPurpose(1, CSVFormat::ColumnStartTime);
Chris@595 434 format.setColumnPurpose(0, CSVFormat::ColumnValue);
Chris@595 435 format.setAllowQuoting(false);
Chris@595 436 format.setSeparator(',');
Chris@420 437
Chris@595 438 CSVFileReader reader(process, format, alignmentModel->getSampleRate());
Chris@595 439 if (!reader.isOK()) {
Chris@649 440 SVCERR << "ERROR: Align::alignmentProgramFinished: Failed to parse output"
Chris@649 441 << endl;
Chris@670 442 alignmentModel->setError
Chris@670 443 (QString("Failed to parse output of program: %1")
Chris@670 444 .arg(reader.getError()));
Chris@423 445 goto done;
Chris@595 446 }
Chris@420 447
Chris@595 448 Model *csvOutput = reader.load();
Chris@420 449
Chris@595 450 SparseTimeValueModel *path = qobject_cast<SparseTimeValueModel *>(csvOutput);
Chris@595 451 if (!path) {
Chris@649 452 SVCERR << "ERROR: Align::alignmentProgramFinished: Output did not convert to sparse time-value model"
Chris@649 453 << endl;
Chris@670 454 alignmentModel->setError
Chris@670 455 ("Output of program did not produce sparse time-value model");
Chris@423 456 goto done;
Chris@595 457 }
Chris@420 458
Chris@649 459 if (path->isEmpty()) {
Chris@649 460 SVCERR << "ERROR: Align::alignmentProgramFinished: Output contained no mappings"
Chris@649 461 << endl;
Chris@670 462 alignmentModel->setError
Chris@670 463 ("Output of alignment program contained no mappings");
Chris@423 464 goto done;
Chris@595 465 }
Chris@420 466
Chris@649 467 SVCERR << "Align::alignmentProgramFinished: Setting alignment path ("
Chris@650 468 << path->getEventCount() << " point(s))" << endl;
Chris@650 469
Chris@423 470 alignmentModel->setPathFrom(path);
Chris@420 471
Chris@428 472 emit alignmentComplete(alignmentModel);
Chris@428 473
Chris@420 474 } else {
Chris@649 475 SVCERR << "ERROR: Align::alignmentProgramFinished: Aligner program "
Chris@649 476 << "failed: exit code " << exitCode << ", status " << status
Chris@649 477 << endl;
Chris@670 478 alignmentModel->setError
Chris@670 479 ("Aligner process returned non-zero exit status");
Chris@420 480 }
Chris@420 481
Chris@423 482 done:
Chris@670 483 m_pendingProcesses.erase(process);
Chris@423 484 delete process;
Chris@420 485 }
Chris@420 486