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 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@420
|
8
|
Chris@420
|
9 This program is free software; you can redistribute it and/or
|
Chris@420
|
10 modify it under the terms of the GNU General Public License as
|
Chris@420
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@420
|
12 License, or (at your option) any later version. See the file
|
Chris@420
|
13 COPYING included with this distribution for more information.
|
Chris@420
|
14 */
|
Chris@420
|
15
|
Chris@420
|
16 #include "Align.h"
|
Chris@420
|
17
|
Chris@420
|
18 #include "data/model/WaveFileModel.h"
|
Chris@420
|
19 #include "data/model/AggregateWaveModel.h"
|
Chris@420
|
20 #include "data/model/RangeSummarisableTimeValueModel.h"
|
Chris@420
|
21 #include "data/model/SparseTimeValueModel.h"
|
Chris@420
|
22 #include "data/model/AlignmentModel.h"
|
Chris@420
|
23
|
Chris@420
|
24 #include "data/fileio/CSVFileReader.h"
|
Chris@420
|
25
|
Chris@420
|
26 #include "transform/TransformFactory.h"
|
Chris@420
|
27 #include "transform/ModelTransformerFactory.h"
|
Chris@420
|
28 #include "transform/FeatureExtractionModelTransformer.h"
|
Chris@420
|
29
|
Chris@420
|
30 #include <QProcess>
|
Chris@420
|
31
|
Chris@420
|
32 bool
|
Chris@420
|
33 Align::alignModelViaTransform(Model *ref, Model *other)
|
Chris@420
|
34 {
|
Chris@420
|
35 RangeSummarisableTimeValueModel *reference = qobject_cast
|
Chris@420
|
36 <RangeSummarisableTimeValueModel *>(ref);
|
Chris@420
|
37
|
Chris@420
|
38 RangeSummarisableTimeValueModel *rm = qobject_cast
|
Chris@420
|
39 <RangeSummarisableTimeValueModel *>(other);
|
Chris@420
|
40
|
Chris@420
|
41 if (!reference || !rm) return false; // but this should have been tested already
|
Chris@420
|
42
|
Chris@420
|
43 // This involves creating three new models:
|
Chris@420
|
44
|
Chris@420
|
45 // 1. an AggregateWaveModel to provide the mixdowns of the main
|
Chris@420
|
46 // model and the new model in its two channels, as input to the
|
Chris@420
|
47 // MATCH plugin
|
Chris@420
|
48
|
Chris@420
|
49 // 2. a SparseTimeValueModel, which is the model automatically
|
Chris@420
|
50 // created by FeatureExtractionPluginTransformer when running the
|
Chris@420
|
51 // MATCH plugin (thus containing the alignment path)
|
Chris@420
|
52
|
Chris@420
|
53 // 3. an AlignmentModel, which stores the path model and carries
|
Chris@420
|
54 // out alignment lookups on it.
|
Chris@420
|
55
|
Chris@420
|
56 // The first two of these are provided as arguments to the
|
Chris@420
|
57 // constructor for the third, which takes responsibility for
|
Chris@420
|
58 // deleting them. The AlignmentModel, meanwhile, is passed to the
|
Chris@420
|
59 // new model we are aligning, which also takes responsibility for
|
Chris@420
|
60 // it. We should not have to delete any of these new models here.
|
Chris@420
|
61
|
Chris@420
|
62 AggregateWaveModel::ChannelSpecList components;
|
Chris@420
|
63
|
Chris@420
|
64 components.push_back(AggregateWaveModel::ModelChannelSpec
|
Chris@420
|
65 (reference, -1));
|
Chris@420
|
66
|
Chris@420
|
67 components.push_back(AggregateWaveModel::ModelChannelSpec
|
Chris@420
|
68 (rm, -1));
|
Chris@420
|
69
|
Chris@420
|
70 Model *aggregateModel = new AggregateWaveModel(components);
|
Chris@420
|
71 ModelTransformer::Input aggregate(aggregateModel);
|
Chris@420
|
72
|
Chris@420
|
73 TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure
|
Chris@420
|
74
|
Chris@420
|
75 TransformFactory *tf = TransformFactory::getInstance();
|
Chris@420
|
76
|
Chris@420
|
77 Transform transform = tf->getDefaultTransformFor
|
Chris@420
|
78 (id, aggregateModel->getSampleRate());
|
Chris@420
|
79
|
Chris@420
|
80 transform.setStepSize(transform.getBlockSize()/2);
|
Chris@420
|
81 transform.setParameter("serialise", 1);
|
Chris@420
|
82 transform.setParameter("smooth", 0);
|
Chris@420
|
83
|
Chris@420
|
84 SVDEBUG << "Align::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
|
Chris@420
|
85
|
Chris@420
|
86 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
|
Chris@420
|
87
|
Chris@420
|
88 QString message;
|
Chris@420
|
89 Model *transformOutput = mtf->transform(transform, aggregate, message);
|
Chris@420
|
90
|
Chris@420
|
91 if (!transformOutput) {
|
Chris@420
|
92 transform.setStepSize(0);
|
Chris@420
|
93 transformOutput = mtf->transform(transform, aggregate, message);
|
Chris@420
|
94 }
|
Chris@420
|
95
|
Chris@420
|
96 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
|
Chris@420
|
97 (transformOutput);
|
Chris@420
|
98
|
Chris@420
|
99 if (!path) {
|
Chris@420
|
100 cerr << "Align::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl;
|
Chris@420
|
101 delete transformOutput;
|
Chris@420
|
102 delete aggregateModel;
|
Chris@420
|
103 m_error = message;
|
Chris@420
|
104 return false;
|
Chris@420
|
105 }
|
Chris@420
|
106
|
Chris@420
|
107 path->setCompletion(0);
|
Chris@420
|
108
|
Chris@420
|
109 AlignmentModel *alignmentModel = new AlignmentModel
|
Chris@420
|
110 (reference, other, aggregateModel, path);
|
Chris@420
|
111
|
Chris@420
|
112 rm->setAlignment(alignmentModel);
|
Chris@420
|
113
|
Chris@420
|
114 return true;
|
Chris@420
|
115 }
|
Chris@420
|
116
|
Chris@420
|
117 bool
|
Chris@420
|
118 Align::alignModelViaProgram(Model *ref, Model *other)
|
Chris@420
|
119 {
|
Chris@420
|
120 WaveFileModel *reference = qobject_cast<WaveFileModel *>(ref);
|
Chris@420
|
121 WaveFileModel *rm = qobject_cast<WaveFileModel *>(other);
|
Chris@420
|
122
|
Chris@420
|
123 if (!rm) return false; // but this should have been tested already
|
Chris@420
|
124
|
Chris@420
|
125 // Run an external program, passing to it paths to the main
|
Chris@420
|
126 // model's audio file and the new model's audio file. It returns
|
Chris@420
|
127 // the path in CSV form through stdout.
|
Chris@420
|
128
|
Chris@420
|
129 QString refPath = reference->getLocalFilename();
|
Chris@420
|
130 QString otherPath = rm->getLocalFilename();
|
Chris@420
|
131
|
Chris@420
|
132 if (refPath == "" || otherPath == "") {
|
Chris@420
|
133 m_error = "Failed to find local filepath for wave-file model";
|
Chris@420
|
134 return false;
|
Chris@420
|
135 }
|
Chris@420
|
136
|
Chris@420
|
137 QProcess process;
|
Chris@420
|
138 QString program = "/home/cannam/code/tido-audio/aligner/vect-align.sh";
|
Chris@420
|
139 QStringList args;
|
Chris@420
|
140 args << refPath << otherPath;
|
Chris@420
|
141 process.start(program, args);
|
Chris@420
|
142
|
Chris@420
|
143 process.waitForFinished(60000); //!!! nb timeout, but we can do better than blocking anyway
|
Chris@420
|
144
|
Chris@420
|
145 if (process.exitStatus() == 0) {
|
Chris@420
|
146
|
Chris@420
|
147 CSVFormat format;
|
Chris@420
|
148 format.setModelType(CSVFormat::TwoDimensionalModel);
|
Chris@420
|
149 format.setTimingType(CSVFormat::ExplicitTiming);
|
Chris@420
|
150 format.setTimeUnits(CSVFormat::TimeSeconds);
|
Chris@420
|
151 format.setColumnCount(2);
|
Chris@420
|
152 format.setColumnPurpose(0, CSVFormat::ColumnStartTime);
|
Chris@420
|
153 format.setColumnPurpose(1, CSVFormat::ColumnValue);
|
Chris@420
|
154 format.setAllowQuoting(false);
|
Chris@420
|
155 format.setSeparator(',');
|
Chris@420
|
156
|
Chris@420
|
157 CSVFileReader reader(&process, format, reference->getSampleRate());
|
Chris@420
|
158 if (!reader.isOK()) {
|
Chris@420
|
159 m_error = QString("Failed to parse output of program: %1")
|
Chris@420
|
160 .arg(reader.getError());
|
Chris@420
|
161 return false;
|
Chris@420
|
162 }
|
Chris@420
|
163
|
Chris@420
|
164 Model *csvOutput = reader.load();
|
Chris@420
|
165
|
Chris@420
|
166 SparseTimeValueModel *path = qobject_cast<SparseTimeValueModel *>(csvOutput);
|
Chris@420
|
167 if (!path) {
|
Chris@420
|
168 m_error = QString("Output of program did not produce sparse time-value model");
|
Chris@420
|
169 return false;
|
Chris@420
|
170 }
|
Chris@420
|
171
|
Chris@420
|
172 if (path->getPoints().empty()) {
|
Chris@420
|
173 m_error = QString("Output of alignment program contained no mappings");
|
Chris@420
|
174 return false;
|
Chris@420
|
175 }
|
Chris@420
|
176
|
Chris@420
|
177 AlignmentModel *alignmentModel = new AlignmentModel
|
Chris@420
|
178 (reference, other, 0, path);
|
Chris@420
|
179
|
Chris@420
|
180 rm->setAlignment(alignmentModel);
|
Chris@420
|
181
|
Chris@420
|
182 } else {
|
Chris@420
|
183 m_error = "Aligner process returned non-zero exit status";
|
Chris@420
|
184 return false;
|
Chris@420
|
185 }
|
Chris@420
|
186
|
Chris@420
|
187 cerr << "Align: success" << endl;
|
Chris@420
|
188
|
Chris@420
|
189 return true;
|
Chris@420
|
190 }
|
Chris@420
|
191
|