annotate framework/Align.cpp @ 671:b6cafe05017d tuning-difference

Make a completion figure available to alignment, + a couple of other fixes
author Chris Cannam
date Thu, 16 May 2019 15:55:46 +0100
parents 0960e27c3232
children ae7584dbd668
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@420 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@420 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@670 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@420 120
Chris@420 121 // 3. an AlignmentModel, which stores the path model and carries
Chris@420 122 // out alignment lookups on it.
Chris@420 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@420 133
Chris@420 134 AggregateWaveModel::ChannelSpecList components;
Chris@420 135
Chris@420 136 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@420 137 (reference, -1));
Chris@420 138
Chris@420 139 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@420 140 (rm, -1));
Chris@420 141
Chris@665 142 AggregateWaveModel *aggregateModel = new AggregateWaveModel(components);
Chris@665 143 doc->addAggregateModel(aggregateModel);
Chris@670 144
Chris@670 145 AlignmentModel *alignmentModel =
Chris@670 146 new AlignmentModel(reference, other, nullptr);
Chris@670 147
Chris@670 148 TransformId tdId = getTuningDifferenceTransformName();
Chris@670 149
Chris@670 150 if (tdId == "") {
Chris@670 151
Chris@670 152 if (beginTransformDrivenAlignment(aggregateModel, alignmentModel)) {
Chris@670 153 rm->setAlignment(alignmentModel);
Chris@670 154 } else {
Chris@670 155 error = alignmentModel->getError();
Chris@670 156 delete alignmentModel;
Chris@670 157 return false;
Chris@670 158 }
Chris@670 159
Chris@670 160 } else {
Chris@670 161
Chris@670 162 // Have a tuning-difference transform id, so run it
Chris@670 163 // asynchronously first
Chris@670 164
Chris@670 165 TransformFactory *tf = TransformFactory::getInstance();
Chris@670 166
Chris@670 167 Transform transform = tf->getDefaultTransformFor
Chris@670 168 (tdId, aggregateModel->getSampleRate());
Chris@670 169
Chris@671 170 transform.setParameter("maxduration", 50);
Chris@671 171 transform.setParameter("maxrange", 5);
Chris@671 172
Chris@670 173 SVDEBUG << "Align::alignModel: Tuning difference transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@670 174
Chris@670 175 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@670 176
Chris@670 177 QString message;
Chris@670 178 Model *transformOutput = mtf->transform(transform, aggregateModel, message);
Chris@670 179
Chris@670 180 SparseTimeValueModel *tdout = dynamic_cast<SparseTimeValueModel *>
Chris@670 181 (transformOutput);
Chris@670 182
Chris@670 183 if (!tdout) {
Chris@670 184 SVCERR << "Align::alignModel: ERROR: Failed to create tuning-difference output model (no Tuning Difference plugin?)" << endl;
Chris@670 185 delete tdout;
Chris@670 186 error = message;
Chris@670 187 return false;
Chris@670 188 }
Chris@670 189
Chris@670 190 rm->setAlignment(alignmentModel);
Chris@665 191
Chris@670 192 connect(tdout, SIGNAL(completionChanged()),
Chris@670 193 this, SLOT(tuningDifferenceCompletionChanged()));
Chris@420 194
Chris@671 195 TuningDiffRec rec;
Chris@671 196 rec.input = aggregateModel;
Chris@671 197 rec.alignment = alignmentModel;
Chris@671 198
Chris@671 199 // This model exists only so that the AlignmentModel can get a
Chris@671 200 // completion value from somewhere while the tuning difference
Chris@671 201 // calculation is going on
Chris@671 202 rec.preparatory = new SparseTimeValueModel
Chris@671 203 (aggregateModel->getSampleRate(), 1);;
Chris@671 204 rec.preparatory->setCompletion(0);
Chris@671 205 alignmentModel->setPathFrom(rec.preparatory);
Chris@671 206
Chris@671 207 m_pendingTuningDiffs[tdout] = rec;
Chris@670 208 }
Chris@670 209
Chris@670 210 return true;
Chris@670 211 }
Chris@670 212
Chris@671 213 void
Chris@671 214 Align::tuningDifferenceCompletionChanged()
Chris@671 215 {
Chris@671 216 QMutexLocker locker (&m_mutex);
Chris@671 217
Chris@671 218 SparseTimeValueModel *td = qobject_cast<SparseTimeValueModel *>(sender());
Chris@671 219 if (!td) return;
Chris@671 220
Chris@671 221 if (m_pendingTuningDiffs.find(td) == m_pendingTuningDiffs.end()) {
Chris@671 222 SVCERR << "ERROR: Align::tuningDifferenceCompletionChanged: Model "
Chris@671 223 << td << " not found in pending tuning diff map!" << endl;
Chris@671 224 return;
Chris@671 225 }
Chris@671 226
Chris@671 227 TuningDiffRec rec = m_pendingTuningDiffs[td];
Chris@671 228
Chris@671 229 int completion = 0;
Chris@671 230 bool done = td->isReady(&completion);
Chris@671 231
Chris@671 232 SVCERR << "Align::tuningDifferenceCompletionChanged: done = " << done << ", completion = " << completion << endl;
Chris@671 233
Chris@671 234 if (!done) {
Chris@671 235 // This will be the completion the alignment model reports,
Chris@671 236 // before the alignment actually begins. It goes up from 0 to
Chris@671 237 // 99 (not 100!) and then back to 0 again when we start
Chris@671 238 // calculating the actual path in the following phase
Chris@671 239 int clamped = (completion == 100 ? 99 : completion);
Chris@671 240 SVCERR << "Align::tuningDifferenceCompletionChanged: setting rec.preparatory completion to " << clamped << endl;
Chris@671 241 rec.preparatory->setCompletion(clamped);
Chris@671 242 return;
Chris@671 243 }
Chris@671 244
Chris@671 245 float tuningFrequency = 440.f;
Chris@671 246
Chris@671 247 if (!td->isEmpty()) {
Chris@671 248 tuningFrequency = td->getAllEvents()[0].getValue();
Chris@671 249 SVCERR << "Align::tuningDifferenceCompletionChanged: Reported tuning frequency = " << tuningFrequency << endl;
Chris@671 250 } else {
Chris@671 251 SVCERR << "Align::tuningDifferenceCompletionChanged: No tuning frequency reported" << endl;
Chris@671 252 }
Chris@671 253
Chris@671 254 m_pendingTuningDiffs.erase(td);
Chris@671 255 td->aboutToDelete();
Chris@671 256 delete td;
Chris@671 257
Chris@671 258 rec.alignment->setPathFrom(nullptr);
Chris@671 259
Chris@671 260 beginTransformDrivenAlignment
Chris@671 261 (rec.input, rec.alignment, tuningFrequency);
Chris@671 262 }
Chris@671 263
Chris@670 264 bool
Chris@670 265 Align::beginTransformDrivenAlignment(AggregateWaveModel *aggregateModel,
Chris@670 266 AlignmentModel *alignmentModel,
Chris@670 267 float tuningFrequency)
Chris@670 268 {
Chris@428 269 TransformId id = getAlignmentTransformName();
Chris@420 270
Chris@420 271 TransformFactory *tf = TransformFactory::getInstance();
Chris@420 272
Chris@420 273 Transform transform = tf->getDefaultTransformFor
Chris@420 274 (id, aggregateModel->getSampleRate());
Chris@420 275
Chris@420 276 transform.setStepSize(transform.getBlockSize()/2);
Chris@420 277 transform.setParameter("serialise", 1);
Chris@420 278 transform.setParameter("smooth", 0);
Chris@420 279
Chris@670 280 if (tuningFrequency != 0.f) {
Chris@670 281 transform.setParameter("freq2", tuningFrequency);
Chris@670 282 }
Chris@670 283
Chris@420 284 SVDEBUG << "Align::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@420 285
Chris@420 286 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@420 287
Chris@420 288 QString message;
Chris@670 289 Model *transformOutput = mtf->transform
Chris@670 290 (transform, aggregateModel, message);
Chris@420 291
Chris@420 292 if (!transformOutput) {
Chris@420 293 transform.setStepSize(0);
Chris@670 294 transformOutput = mtf->transform
Chris@670 295 (transform, aggregateModel, message);
Chris@420 296 }
Chris@420 297
Chris@420 298 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
Chris@420 299 (transformOutput);
Chris@420 300
Chris@670 301 //!!! callers will need to be updated to get error from
Chris@670 302 //!!! alignment model after initial call
Chris@670 303
Chris@420 304 if (!path) {
Chris@649 305 SVCERR << "Align::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl;
Chris@420 306 delete transformOutput;
Chris@670 307 alignmentModel->setError(message);
Chris@420 308 return false;
Chris@420 309 }
Chris@420 310
Chris@420 311 path->setCompletion(0);
Chris@670 312 alignmentModel->setPathFrom(path);
Chris@420 313
Chris@428 314 connect(alignmentModel, SIGNAL(completionChanged()),
Chris@428 315 this, SLOT(alignmentCompletionChanged()));
Chris@420 316
Chris@420 317 return true;
Chris@420 318 }
Chris@420 319
Chris@428 320 void
Chris@428 321 Align::alignmentCompletionChanged()
Chris@428 322 {
Chris@670 323 QMutexLocker locker (&m_mutex);
Chris@670 324
Chris@428 325 AlignmentModel *am = qobject_cast<AlignmentModel *>(sender());
Chris@428 326 if (!am) return;
Chris@428 327 if (am->isReady()) {
Chris@428 328 disconnect(am, SIGNAL(completionChanged()),
Chris@428 329 this, SLOT(alignmentCompletionChanged()));
Chris@428 330 emit alignmentComplete(am);
Chris@428 331 }
Chris@428 332 }
Chris@428 333
Chris@420 334 bool
Chris@670 335 Align::alignModelViaProgram(Document *, Model *ref, Model *other,
Chris@670 336 QString program, QString &error)
Chris@420 337 {
Chris@670 338 QMutexLocker locker (&m_mutex);
Chris@670 339
Chris@420 340 WaveFileModel *reference = qobject_cast<WaveFileModel *>(ref);
Chris@420 341 WaveFileModel *rm = qobject_cast<WaveFileModel *>(other);
Chris@420 342
Chris@515 343 if (!reference || !rm) {
Chris@515 344 return false; // but this should have been tested already
Chris@515 345 }
Chris@420 346
Chris@636 347 while (!reference->isReady(nullptr) || !rm->isReady(nullptr)) {
Chris@430 348 qApp->processEvents();
Chris@430 349 }
Chris@430 350
Chris@420 351 // Run an external program, passing to it paths to the main
Chris@420 352 // model's audio file and the new model's audio file. It returns
Chris@420 353 // the path in CSV form through stdout.
Chris@420 354
Chris@515 355 ReadOnlyWaveFileModel *roref = qobject_cast<ReadOnlyWaveFileModel *>(reference);
Chris@515 356 ReadOnlyWaveFileModel *rorm = qobject_cast<ReadOnlyWaveFileModel *>(rm);
Chris@515 357 if (!roref || !rorm) {
Chris@649 358 SVCERR << "ERROR: Align::alignModelViaProgram: Can't align non-read-only models via program (no local filename available)" << endl;
Chris@515 359 return false;
Chris@515 360 }
Chris@515 361
Chris@515 362 QString refPath = roref->getLocalFilename();
Chris@515 363 QString otherPath = rorm->getLocalFilename();
Chris@420 364
Chris@420 365 if (refPath == "" || otherPath == "") {
Chris@670 366 error = "Failed to find local filepath for wave-file model";
Chris@595 367 return false;
Chris@420 368 }
Chris@420 369
Chris@665 370 AlignmentModel *alignmentModel =
Chris@665 371 new AlignmentModel(reference, other, nullptr);
Chris@423 372 rm->setAlignment(alignmentModel);
Chris@423 373
Chris@423 374 QProcess *process = new QProcess;
Chris@420 375 QStringList args;
Chris@420 376 args << refPath << otherPath;
Chris@423 377
Chris@423 378 connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@423 379 this, SLOT(alignmentProgramFinished(int, QProcess::ExitStatus)));
Chris@420 380
Chris@670 381 m_pendingProcesses[process] = alignmentModel;
Chris@423 382 process->start(program, args);
Chris@420 383
Chris@423 384 bool success = process->waitForStarted();
Chris@423 385
Chris@423 386 if (!success) {
Chris@649 387 SVCERR << "ERROR: Align::alignModelViaProgram: Program did not start"
Chris@649 388 << endl;
Chris@670 389 error = "Alignment program could not be started";
Chris@670 390 m_pendingProcesses.erase(process);
Chris@636 391 rm->setAlignment(nullptr); // deletes alignmentModel as well
Chris@423 392 delete process;
Chris@423 393 }
Chris@423 394
Chris@423 395 return success;
Chris@423 396 }
Chris@423 397
Chris@423 398 void
Chris@423 399 Align::alignmentProgramFinished(int exitCode, QProcess::ExitStatus status)
Chris@423 400 {
Chris@670 401 QMutexLocker locker (&m_mutex);
Chris@670 402
Chris@649 403 SVCERR << "Align::alignmentProgramFinished" << endl;
Chris@423 404
Chris@423 405 QProcess *process = qobject_cast<QProcess *>(sender());
Chris@423 406
Chris@670 407 if (m_pendingProcesses.find(process) == m_pendingProcesses.end()) {
Chris@649 408 SVCERR << "ERROR: Align::alignmentProgramFinished: Process " << process
Chris@649 409 << " not found in process model map!" << endl;
Chris@423 410 return;
Chris@423 411 }
Chris@423 412
Chris@670 413 AlignmentModel *alignmentModel = m_pendingProcesses[process];
Chris@423 414
Chris@423 415 if (exitCode == 0 && status == 0) {
Chris@420 416
Chris@595 417 CSVFormat format;
Chris@595 418 format.setModelType(CSVFormat::TwoDimensionalModel);
Chris@595 419 format.setTimingType(CSVFormat::ExplicitTiming);
Chris@595 420 format.setTimeUnits(CSVFormat::TimeSeconds);
Chris@595 421 format.setColumnCount(2);
Chris@425 422 // The output format has time in the reference file first, and
Chris@425 423 // time in the "other" file in the second column. This is a
Chris@425 424 // more natural approach for a command-line alignment tool,
Chris@425 425 // but it's the opposite of what we expect for native
Chris@425 426 // alignment paths, which map from "other" file to
Chris@425 427 // reference. These column purpose settings reflect that.
Chris@595 428 format.setColumnPurpose(1, CSVFormat::ColumnStartTime);
Chris@595 429 format.setColumnPurpose(0, CSVFormat::ColumnValue);
Chris@595 430 format.setAllowQuoting(false);
Chris@595 431 format.setSeparator(',');
Chris@420 432
Chris@595 433 CSVFileReader reader(process, format, alignmentModel->getSampleRate());
Chris@595 434 if (!reader.isOK()) {
Chris@649 435 SVCERR << "ERROR: Align::alignmentProgramFinished: Failed to parse output"
Chris@649 436 << endl;
Chris@670 437 alignmentModel->setError
Chris@670 438 (QString("Failed to parse output of program: %1")
Chris@670 439 .arg(reader.getError()));
Chris@423 440 goto done;
Chris@595 441 }
Chris@420 442
Chris@595 443 Model *csvOutput = reader.load();
Chris@420 444
Chris@595 445 SparseTimeValueModel *path = qobject_cast<SparseTimeValueModel *>(csvOutput);
Chris@595 446 if (!path) {
Chris@649 447 SVCERR << "ERROR: Align::alignmentProgramFinished: Output did not convert to sparse time-value model"
Chris@649 448 << endl;
Chris@670 449 alignmentModel->setError
Chris@670 450 ("Output of program did not produce sparse time-value model");
Chris@423 451 goto done;
Chris@595 452 }
Chris@420 453
Chris@649 454 if (path->isEmpty()) {
Chris@649 455 SVCERR << "ERROR: Align::alignmentProgramFinished: Output contained no mappings"
Chris@649 456 << endl;
Chris@670 457 alignmentModel->setError
Chris@670 458 ("Output of alignment program contained no mappings");
Chris@423 459 goto done;
Chris@595 460 }
Chris@420 461
Chris@649 462 SVCERR << "Align::alignmentProgramFinished: Setting alignment path ("
Chris@650 463 << path->getEventCount() << " point(s))" << endl;
Chris@650 464
Chris@423 465 alignmentModel->setPathFrom(path);
Chris@420 466
Chris@428 467 emit alignmentComplete(alignmentModel);
Chris@428 468
Chris@420 469 } else {
Chris@649 470 SVCERR << "ERROR: Align::alignmentProgramFinished: Aligner program "
Chris@649 471 << "failed: exit code " << exitCode << ", status " << status
Chris@649 472 << endl;
Chris@670 473 alignmentModel->setError
Chris@670 474 ("Aligner process returned non-zero exit status");
Chris@420 475 }
Chris@420 476
Chris@423 477 done:
Chris@670 478 m_pendingProcesses.erase(process);
Chris@423 479 delete process;
Chris@420 480 }
Chris@420 481