Chris@420: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@420: Chris@420: /* Chris@420: Sonic Visualiser Chris@420: An audio file viewer and annotation editor. Chris@420: Centre for Digital Music, Queen Mary, University of London. Chris@420: Chris@420: This program is free software; you can redistribute it and/or Chris@420: modify it under the terms of the GNU General Public License as Chris@420: published by the Free Software Foundation; either version 2 of the Chris@420: License, or (at your option) any later version. See the file Chris@420: COPYING included with this distribution for more information. Chris@420: */ Chris@420: Chris@420: #include "Align.h" Chris@665: #include "Document.h" Chris@420: Chris@420: #include "data/model/WaveFileModel.h" Chris@515: #include "data/model/ReadOnlyWaveFileModel.h" Chris@420: #include "data/model/AggregateWaveModel.h" Chris@420: #include "data/model/RangeSummarisableTimeValueModel.h" Chris@420: #include "data/model/SparseTimeValueModel.h" Chris@420: #include "data/model/AlignmentModel.h" Chris@420: Chris@420: #include "data/fileio/CSVFileReader.h" Chris@420: Chris@420: #include "transform/TransformFactory.h" Chris@420: #include "transform/ModelTransformerFactory.h" Chris@420: #include "transform/FeatureExtractionModelTransformer.h" Chris@420: Chris@420: #include Chris@422: #include Chris@430: #include Chris@422: Chris@422: bool Chris@670: Align::alignModel(Document *doc, Model *ref, Model *other, QString &error) Chris@422: { Chris@422: QSettings settings; Chris@422: settings.beginGroup("Preferences"); Chris@422: bool useProgram = settings.value("use-external-alignment", false).toBool(); Chris@422: QString program = settings.value("external-alignment-program", "").toString(); Chris@422: settings.endGroup(); Chris@422: Chris@422: if (useProgram && (program != "")) { Chris@670: return alignModelViaProgram(doc, ref, other, program, error); Chris@422: } else { Chris@670: return alignModelViaTransform(doc, ref, other, error); Chris@422: } Chris@422: } Chris@420: Chris@428: QString Chris@428: Align::getAlignmentTransformName() Chris@428: { Chris@428: QSettings settings; Chris@428: settings.beginGroup("Alignment"); Chris@428: TransformId id = Chris@428: settings.value("transform-id", Chris@428: "vamp:match-vamp-plugin:match:path").toString(); Chris@428: settings.endGroup(); Chris@428: return id; Chris@428: } Chris@428: Chris@670: QString Chris@670: Align::getTuningDifferenceTransformName() Chris@670: { Chris@670: QSettings settings; Chris@670: settings.beginGroup("Alignment"); Chris@670: bool performPitchCompensation = Chris@670: settings.value("align-pitch-aware", false).toBool(); Chris@670: QString id = ""; Chris@670: //!!! if (performPitchCompensation) { Chris@670: id = settings.value Chris@670: ("tuning-difference-transform-id", Chris@670: "vamp:tuning-difference:tuning-difference:tuningfreq") Chris@670: .toString(); Chris@670: // } Chris@670: settings.endGroup(); Chris@670: return id; Chris@670: } Chris@670: Chris@428: bool Chris@428: Align::canAlign() Chris@428: { Chris@670: TransformFactory *factory = TransformFactory::getInstance(); Chris@428: TransformId id = getAlignmentTransformName(); Chris@670: TransformId tdId = getTuningDifferenceTransformName(); Chris@670: return factory->haveTransform(id) && Chris@670: (tdId == "" || factory->haveTransform(tdId)); Chris@428: } Chris@428: Chris@420: bool Chris@670: Align::alignModelViaTransform(Document *doc, Model *ref, Model *other, Chris@670: QString &error) Chris@420: { Chris@670: QMutexLocker locker (&m_mutex); Chris@670: Chris@420: RangeSummarisableTimeValueModel *reference = qobject_cast Chris@420: (ref); Chris@420: Chris@420: RangeSummarisableTimeValueModel *rm = qobject_cast Chris@420: (other); Chris@420: Chris@420: if (!reference || !rm) return false; // but this should have been tested already Chris@420: Chris@670: // This involves creating either three or four new models: Chris@420: Chris@420: // 1. an AggregateWaveModel to provide the mixdowns of the main Chris@420: // model and the new model in its two channels, as input to the Chris@420: // MATCH plugin Chris@420: Chris@670: // 2a. a SparseTimeValueModel which will be automatically created Chris@670: // by FeatureExtractionModelTransformer when running the Chris@670: // TuningDifference plugin to receive the relative tuning of the Chris@670: // second model (if pitch-aware alignment is enabled in the Chris@670: // preferences) Chris@670: Chris@670: // 2b. a SparseTimeValueModel which will be automatically created Chris@670: // by FeatureExtractionPluginTransformer when running the MATCH Chris@670: // plugin to perform alignment (so containing the alignment path) Chris@420: Chris@420: // 3. an AlignmentModel, which stores the path model and carries Chris@420: // out alignment lookups on it. Chris@420: Chris@670: // The AggregateWaveModel [1] is registered with the document, Chris@670: // which deletes it when it is invalidated (when one of its Chris@670: // components is deleted). The SparseTimeValueModel [2a] is reused Chris@670: // by us when starting the alignment process proper, and is then Chris@670: // deleted by us. The SparseTimeValueModel [2b] is passed to the Chris@670: // AlignmentModel, which takes ownership of it. The AlignmentModel Chris@670: // is attached to the new model we are aligning, which also takes Chris@670: // ownership of it. The only one of these models that we need to Chris@670: // delete here is the SparseTimeValueModel [2a]. Chris@420: Chris@420: AggregateWaveModel::ChannelSpecList components; Chris@420: Chris@420: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@420: (reference, -1)); Chris@420: Chris@420: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@420: (rm, -1)); Chris@420: Chris@665: AggregateWaveModel *aggregateModel = new AggregateWaveModel(components); Chris@665: doc->addAggregateModel(aggregateModel); Chris@670: Chris@670: AlignmentModel *alignmentModel = Chris@670: new AlignmentModel(reference, other, nullptr); Chris@670: Chris@670: connect(alignmentModel, SIGNAL(completionChanged()), Chris@670: this, SLOT(alignmentCompletionChanged())); Chris@670: Chris@670: TransformId tdId = getTuningDifferenceTransformName(); Chris@670: Chris@670: if (tdId == "") { Chris@670: Chris@670: if (beginTransformDrivenAlignment(aggregateModel, alignmentModel)) { Chris@670: rm->setAlignment(alignmentModel); Chris@670: } else { Chris@670: error = alignmentModel->getError(); Chris@670: delete alignmentModel; Chris@670: return false; Chris@670: } Chris@670: Chris@670: } else { Chris@670: Chris@670: // Have a tuning-difference transform id, so run it Chris@670: // asynchronously first Chris@670: Chris@670: TransformFactory *tf = TransformFactory::getInstance(); Chris@670: Chris@670: Transform transform = tf->getDefaultTransformFor Chris@670: (tdId, aggregateModel->getSampleRate()); Chris@670: Chris@670: SVDEBUG << "Align::alignModel: Tuning difference transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl; Chris@670: Chris@670: ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance(); Chris@670: Chris@670: QString message; Chris@670: Model *transformOutput = mtf->transform(transform, aggregateModel, message); Chris@670: Chris@670: SparseTimeValueModel *tdout = dynamic_cast Chris@670: (transformOutput); Chris@670: Chris@670: if (!tdout) { Chris@670: SVCERR << "Align::alignModel: ERROR: Failed to create tuning-difference output model (no Tuning Difference plugin?)" << endl; Chris@670: delete tdout; Chris@670: error = message; Chris@670: return false; Chris@670: } Chris@670: Chris@670: rm->setAlignment(alignmentModel); Chris@665: Chris@670: connect(tdout, SIGNAL(completionChanged()), Chris@670: this, SLOT(tuningDifferenceCompletionChanged())); Chris@420: Chris@670: m_pendingTuningDiffs[tdout] = Chris@670: std::pair Chris@670: (aggregateModel, alignmentModel); Chris@670: } Chris@670: Chris@670: return true; Chris@670: } Chris@670: Chris@670: bool Chris@670: Align::beginTransformDrivenAlignment(AggregateWaveModel *aggregateModel, Chris@670: AlignmentModel *alignmentModel, Chris@670: float tuningFrequency) Chris@670: { Chris@428: TransformId id = getAlignmentTransformName(); Chris@420: Chris@420: TransformFactory *tf = TransformFactory::getInstance(); Chris@420: Chris@420: Transform transform = tf->getDefaultTransformFor Chris@420: (id, aggregateModel->getSampleRate()); Chris@420: Chris@420: transform.setStepSize(transform.getBlockSize()/2); Chris@420: transform.setParameter("serialise", 1); Chris@420: transform.setParameter("smooth", 0); Chris@420: Chris@670: if (tuningFrequency != 0.f) { Chris@670: transform.setParameter("freq2", tuningFrequency); Chris@670: } Chris@670: Chris@420: SVDEBUG << "Align::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl; Chris@420: Chris@420: ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance(); Chris@420: Chris@420: QString message; Chris@670: Model *transformOutput = mtf->transform Chris@670: (transform, aggregateModel, message); Chris@420: Chris@420: if (!transformOutput) { Chris@420: transform.setStepSize(0); Chris@670: transformOutput = mtf->transform Chris@670: (transform, aggregateModel, message); Chris@420: } Chris@420: Chris@420: SparseTimeValueModel *path = dynamic_cast Chris@420: (transformOutput); Chris@420: Chris@670: //!!! callers will need to be updated to get error from Chris@670: //!!! alignment model after initial call Chris@670: Chris@420: if (!path) { Chris@649: SVCERR << "Align::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl; Chris@420: delete transformOutput; Chris@670: alignmentModel->setError(message); Chris@420: return false; Chris@420: } Chris@420: Chris@420: path->setCompletion(0); Chris@670: alignmentModel->setPathFrom(path); Chris@420: Chris@428: connect(alignmentModel, SIGNAL(completionChanged()), Chris@428: this, SLOT(alignmentCompletionChanged())); Chris@420: Chris@420: return true; Chris@420: } Chris@420: Chris@428: void Chris@670: Align::tuningDifferenceCompletionChanged() Chris@670: { Chris@670: QMutexLocker locker (&m_mutex); Chris@670: Chris@670: SparseTimeValueModel *td = qobject_cast(sender()); Chris@670: if (!td) return; Chris@670: if (!td->isReady()) return; Chris@670: Chris@670: disconnect(td, SIGNAL(completionChanged()), Chris@670: this, SLOT(alignmentCompletionChanged())); Chris@670: Chris@670: if (m_pendingTuningDiffs.find(td) == m_pendingTuningDiffs.end()) { Chris@670: SVCERR << "ERROR: Align::tuningDifferenceCompletionChanged: Model " Chris@670: << td << " not found in pending tuning diff map!" << endl; Chris@670: return; Chris@670: } Chris@670: Chris@670: std::pair models = Chris@670: m_pendingTuningDiffs[td]; Chris@670: Chris@670: float tuningFrequency = 440.f; Chris@670: Chris@670: if (!td->isEmpty()) { Chris@670: tuningFrequency = td->getAllEvents()[0].getValue(); Chris@670: SVCERR << "Align::tuningDifferenceCompletionChanged: Reported tuning frequency = " << tuningFrequency << endl; Chris@670: } else { Chris@670: SVCERR << "Align::tuningDifferenceCompletionChanged: No tuning frequency reported" << endl; Chris@670: } Chris@670: Chris@670: m_pendingTuningDiffs.erase(td); Chris@670: Chris@670: beginTransformDrivenAlignment Chris@670: (models.first, models.second, tuningFrequency); Chris@670: } Chris@670: Chris@670: void Chris@428: Align::alignmentCompletionChanged() Chris@428: { Chris@670: QMutexLocker locker (&m_mutex); Chris@670: Chris@428: AlignmentModel *am = qobject_cast(sender()); Chris@428: if (!am) return; Chris@428: if (am->isReady()) { Chris@428: disconnect(am, SIGNAL(completionChanged()), Chris@428: this, SLOT(alignmentCompletionChanged())); Chris@428: emit alignmentComplete(am); Chris@428: } Chris@428: } Chris@428: Chris@420: bool Chris@670: Align::alignModelViaProgram(Document *, Model *ref, Model *other, Chris@670: QString program, QString &error) Chris@420: { Chris@670: QMutexLocker locker (&m_mutex); Chris@670: Chris@420: WaveFileModel *reference = qobject_cast(ref); Chris@420: WaveFileModel *rm = qobject_cast(other); Chris@420: Chris@515: if (!reference || !rm) { Chris@515: return false; // but this should have been tested already Chris@515: } Chris@420: Chris@636: while (!reference->isReady(nullptr) || !rm->isReady(nullptr)) { Chris@430: qApp->processEvents(); Chris@430: } Chris@430: Chris@420: // Run an external program, passing to it paths to the main Chris@420: // model's audio file and the new model's audio file. It returns Chris@420: // the path in CSV form through stdout. Chris@420: Chris@515: ReadOnlyWaveFileModel *roref = qobject_cast(reference); Chris@515: ReadOnlyWaveFileModel *rorm = qobject_cast(rm); Chris@515: if (!roref || !rorm) { Chris@649: SVCERR << "ERROR: Align::alignModelViaProgram: Can't align non-read-only models via program (no local filename available)" << endl; Chris@515: return false; Chris@515: } Chris@515: Chris@515: QString refPath = roref->getLocalFilename(); Chris@515: QString otherPath = rorm->getLocalFilename(); Chris@420: Chris@420: if (refPath == "" || otherPath == "") { Chris@670: error = "Failed to find local filepath for wave-file model"; Chris@595: return false; Chris@420: } Chris@420: Chris@665: AlignmentModel *alignmentModel = Chris@665: new AlignmentModel(reference, other, nullptr); Chris@423: rm->setAlignment(alignmentModel); Chris@423: Chris@423: QProcess *process = new QProcess; Chris@420: QStringList args; Chris@420: args << refPath << otherPath; Chris@423: Chris@423: connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), Chris@423: this, SLOT(alignmentProgramFinished(int, QProcess::ExitStatus))); Chris@420: Chris@670: m_pendingProcesses[process] = alignmentModel; Chris@423: process->start(program, args); Chris@420: Chris@423: bool success = process->waitForStarted(); Chris@423: Chris@423: if (!success) { Chris@649: SVCERR << "ERROR: Align::alignModelViaProgram: Program did not start" Chris@649: << endl; Chris@670: error = "Alignment program could not be started"; Chris@670: m_pendingProcesses.erase(process); Chris@636: rm->setAlignment(nullptr); // deletes alignmentModel as well Chris@423: delete process; Chris@423: } Chris@423: Chris@423: return success; Chris@423: } Chris@423: Chris@423: void Chris@423: Align::alignmentProgramFinished(int exitCode, QProcess::ExitStatus status) Chris@423: { Chris@670: QMutexLocker locker (&m_mutex); Chris@670: Chris@649: SVCERR << "Align::alignmentProgramFinished" << endl; Chris@423: Chris@423: QProcess *process = qobject_cast(sender()); Chris@423: Chris@670: if (m_pendingProcesses.find(process) == m_pendingProcesses.end()) { Chris@649: SVCERR << "ERROR: Align::alignmentProgramFinished: Process " << process Chris@649: << " not found in process model map!" << endl; Chris@423: return; Chris@423: } Chris@423: Chris@670: AlignmentModel *alignmentModel = m_pendingProcesses[process]; Chris@423: Chris@423: if (exitCode == 0 && status == 0) { Chris@420: Chris@595: CSVFormat format; Chris@595: format.setModelType(CSVFormat::TwoDimensionalModel); Chris@595: format.setTimingType(CSVFormat::ExplicitTiming); Chris@595: format.setTimeUnits(CSVFormat::TimeSeconds); Chris@595: format.setColumnCount(2); Chris@425: // The output format has time in the reference file first, and Chris@425: // time in the "other" file in the second column. This is a Chris@425: // more natural approach for a command-line alignment tool, Chris@425: // but it's the opposite of what we expect for native Chris@425: // alignment paths, which map from "other" file to Chris@425: // reference. These column purpose settings reflect that. Chris@595: format.setColumnPurpose(1, CSVFormat::ColumnStartTime); Chris@595: format.setColumnPurpose(0, CSVFormat::ColumnValue); Chris@595: format.setAllowQuoting(false); Chris@595: format.setSeparator(','); Chris@420: Chris@595: CSVFileReader reader(process, format, alignmentModel->getSampleRate()); Chris@595: if (!reader.isOK()) { Chris@649: SVCERR << "ERROR: Align::alignmentProgramFinished: Failed to parse output" Chris@649: << endl; Chris@670: alignmentModel->setError Chris@670: (QString("Failed to parse output of program: %1") Chris@670: .arg(reader.getError())); Chris@423: goto done; Chris@595: } Chris@420: Chris@595: Model *csvOutput = reader.load(); Chris@420: Chris@595: SparseTimeValueModel *path = qobject_cast(csvOutput); Chris@595: if (!path) { Chris@649: SVCERR << "ERROR: Align::alignmentProgramFinished: Output did not convert to sparse time-value model" Chris@649: << endl; Chris@670: alignmentModel->setError Chris@670: ("Output of program did not produce sparse time-value model"); Chris@423: goto done; Chris@595: } Chris@420: Chris@649: if (path->isEmpty()) { Chris@649: SVCERR << "ERROR: Align::alignmentProgramFinished: Output contained no mappings" Chris@649: << endl; Chris@670: alignmentModel->setError Chris@670: ("Output of alignment program contained no mappings"); Chris@423: goto done; Chris@595: } Chris@420: Chris@649: SVCERR << "Align::alignmentProgramFinished: Setting alignment path (" Chris@650: << path->getEventCount() << " point(s))" << endl; Chris@650: Chris@423: alignmentModel->setPathFrom(path); Chris@420: Chris@428: emit alignmentComplete(alignmentModel); Chris@428: Chris@420: } else { Chris@649: SVCERR << "ERROR: Align::alignmentProgramFinished: Aligner program " Chris@649: << "failed: exit code " << exitCode << ", status " << status Chris@649: << endl; Chris@670: alignmentModel->setError Chris@670: ("Aligner process returned non-zero exit status"); Chris@420: } Chris@420: Chris@423: done: Chris@670: m_pendingProcesses.erase(process); Chris@423: delete process; Chris@420: } Chris@420: