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: This file copyright 2006 Chris Cannam and QMUL. 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@420: Chris@420: #include "data/model/WaveFileModel.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@420: Chris@420: bool Chris@420: Align::alignModelViaTransform(Model *ref, Model *other) Chris@420: { 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@420: // This involves creating three 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@420: // 2. a SparseTimeValueModel, which is the model automatically Chris@420: // created by FeatureExtractionPluginTransformer when running the Chris@420: // MATCH plugin (thus 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@420: // The first two of these are provided as arguments to the Chris@420: // constructor for the third, which takes responsibility for Chris@420: // deleting them. The AlignmentModel, meanwhile, is passed to the Chris@420: // new model we are aligning, which also takes responsibility for Chris@420: // it. We should not have to delete any of these new models here. 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@420: Model *aggregateModel = new AggregateWaveModel(components); Chris@420: ModelTransformer::Input aggregate(aggregateModel); Chris@420: Chris@420: TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure 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@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@420: Model *transformOutput = mtf->transform(transform, aggregate, message); Chris@420: Chris@420: if (!transformOutput) { Chris@420: transform.setStepSize(0); Chris@420: transformOutput = mtf->transform(transform, aggregate, message); Chris@420: } Chris@420: Chris@420: SparseTimeValueModel *path = dynamic_cast Chris@420: (transformOutput); Chris@420: Chris@420: if (!path) { Chris@420: cerr << "Align::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl; Chris@420: delete transformOutput; Chris@420: delete aggregateModel; Chris@420: m_error = message; Chris@420: return false; Chris@420: } Chris@420: Chris@420: path->setCompletion(0); Chris@420: Chris@420: AlignmentModel *alignmentModel = new AlignmentModel Chris@420: (reference, other, aggregateModel, path); Chris@420: Chris@420: rm->setAlignment(alignmentModel); Chris@420: Chris@420: return true; Chris@420: } Chris@420: Chris@420: bool Chris@420: Align::alignModelViaProgram(Model *ref, Model *other) Chris@420: { Chris@420: WaveFileModel *reference = qobject_cast(ref); Chris@420: WaveFileModel *rm = qobject_cast(other); Chris@420: Chris@420: if (!rm) return false; // but this should have been tested already Chris@420: 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@420: QString refPath = reference->getLocalFilename(); Chris@420: QString otherPath = rm->getLocalFilename(); Chris@420: Chris@420: if (refPath == "" || otherPath == "") { Chris@420: m_error = "Failed to find local filepath for wave-file model"; Chris@420: return false; Chris@420: } Chris@420: Chris@420: QProcess process; Chris@420: QString program = "/home/cannam/code/tido-audio/aligner/vect-align.sh"; Chris@420: QStringList args; Chris@420: args << refPath << otherPath; Chris@420: process.start(program, args); Chris@420: Chris@420: process.waitForFinished(60000); //!!! nb timeout, but we can do better than blocking anyway Chris@420: Chris@420: if (process.exitStatus() == 0) { Chris@420: Chris@420: CSVFormat format; Chris@420: format.setModelType(CSVFormat::TwoDimensionalModel); Chris@420: format.setTimingType(CSVFormat::ExplicitTiming); Chris@420: format.setTimeUnits(CSVFormat::TimeSeconds); Chris@420: format.setColumnCount(2); Chris@420: format.setColumnPurpose(0, CSVFormat::ColumnStartTime); Chris@420: format.setColumnPurpose(1, CSVFormat::ColumnValue); Chris@420: format.setAllowQuoting(false); Chris@420: format.setSeparator(','); Chris@420: Chris@420: CSVFileReader reader(&process, format, reference->getSampleRate()); Chris@420: if (!reader.isOK()) { Chris@420: m_error = QString("Failed to parse output of program: %1") Chris@420: .arg(reader.getError()); Chris@420: return false; Chris@420: } Chris@420: Chris@420: Model *csvOutput = reader.load(); Chris@420: Chris@420: SparseTimeValueModel *path = qobject_cast(csvOutput); Chris@420: if (!path) { Chris@420: m_error = QString("Output of program did not produce sparse time-value model"); Chris@420: return false; Chris@420: } Chris@420: Chris@420: if (path->getPoints().empty()) { Chris@420: m_error = QString("Output of alignment program contained no mappings"); Chris@420: return false; Chris@420: } Chris@420: Chris@420: AlignmentModel *alignmentModel = new AlignmentModel Chris@420: (reference, other, 0, path); Chris@420: Chris@420: rm->setAlignment(alignmentModel); Chris@420: Chris@420: } else { Chris@420: m_error = "Aligner process returned non-zero exit status"; Chris@420: return false; Chris@420: } Chris@420: Chris@420: cerr << "Align: success" << endl; Chris@420: Chris@420: return true; Chris@420: } Chris@420: