Chris@297
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@297
|
2
|
Chris@297
|
3 /*
|
Chris@297
|
4 Sonic Visualiser
|
Chris@297
|
5 An audio file viewer and annotation editor.
|
Chris@297
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@297
|
7 This file copyright 2007 QMUL.
|
Chris@297
|
8
|
Chris@297
|
9 This program is free software; you can redistribute it and/or
|
Chris@297
|
10 modify it under the terms of the GNU General Public License as
|
Chris@297
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@297
|
12 License, or (at your option) any later version. See the file
|
Chris@297
|
13 COPYING included with this distribution for more information.
|
Chris@297
|
14 */
|
Chris@297
|
15
|
Chris@297
|
16 #include "AlignmentModel.h"
|
Chris@297
|
17
|
Chris@297
|
18 #include "SparseTimeValueModel.h"
|
Chris@297
|
19
|
Chris@409
|
20 //#define DEBUG_ALIGNMENT_MODEL 1
|
Chris@376
|
21
|
Chris@1735
|
22 AlignmentModel::AlignmentModel(ModelId reference,
|
Chris@1735
|
23 ModelId aligned,
|
Chris@1735
|
24 ModelId pathSource) :
|
Chris@297
|
25 m_reference(reference),
|
Chris@297
|
26 m_aligned(aligned),
|
Chris@1735
|
27 m_pathSource(pathSource),
|
Chris@1582
|
28 m_path(nullptr),
|
Chris@1582
|
29 m_reversePath(nullptr),
|
Chris@323
|
30 m_pathBegun(false),
|
Chris@297
|
31 m_pathComplete(false)
|
Chris@297
|
32 {
|
Chris@1737
|
33 setPathFrom(pathSource);
|
Chris@1696
|
34
|
Chris@1696
|
35 if (m_reference == m_aligned) {
|
Chris@1696
|
36 // Trivial alignment, e.g. of main model to itself, which we
|
Chris@1696
|
37 // record so that we can distinguish the reference model for
|
Chris@1696
|
38 // alignments from an unaligned model. No path required
|
Chris@1696
|
39 m_pathComplete = true;
|
Chris@1696
|
40 }
|
Chris@297
|
41 }
|
Chris@297
|
42
|
Chris@297
|
43 AlignmentModel::~AlignmentModel()
|
Chris@297
|
44 {
|
Chris@1691
|
45 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1691
|
46 SVCERR << "AlignmentModel(" << this << ")::~AlignmentModel()" << endl;
|
Chris@1691
|
47 #endif
|
Chris@297
|
48 }
|
Chris@297
|
49
|
Chris@297
|
50 bool
|
Chris@297
|
51 AlignmentModel::isOK() const
|
Chris@297
|
52 {
|
Chris@1705
|
53 if (m_error != "") return false;
|
Chris@1737
|
54 if (m_pathSource.isNone()) return true;
|
Chris@1737
|
55 auto pathSourceModel =
|
Chris@1737
|
56 ModelById::getAs<SparseTimeValueModel>(m_pathSource);
|
Chris@1737
|
57 if (pathSourceModel) {
|
Chris@1737
|
58 return pathSourceModel->isOK();
|
Chris@1737
|
59 }
|
Chris@1705
|
60 return true;
|
Chris@297
|
61 }
|
Chris@297
|
62
|
Chris@1038
|
63 sv_frame_t
|
Chris@297
|
64 AlignmentModel::getStartFrame() const
|
Chris@297
|
65 {
|
Chris@1737
|
66 auto reference = ModelById::get(m_reference);
|
Chris@1737
|
67 auto aligned = ModelById::get(m_aligned);
|
Chris@1737
|
68
|
Chris@1737
|
69 if (reference && aligned) {
|
Chris@1737
|
70 sv_frame_t a = reference->getStartFrame();
|
Chris@1737
|
71 sv_frame_t b = aligned->getStartFrame();
|
Chris@1737
|
72 return std::min(a, b);
|
Chris@1737
|
73 } else {
|
Chris@1737
|
74 return 0;
|
Chris@1737
|
75 }
|
Chris@297
|
76 }
|
Chris@297
|
77
|
Chris@1038
|
78 sv_frame_t
|
Chris@1725
|
79 AlignmentModel::getTrueEndFrame() const
|
Chris@297
|
80 {
|
Chris@1737
|
81 auto reference = ModelById::get(m_reference);
|
Chris@1737
|
82 auto aligned = ModelById::get(m_aligned);
|
Chris@1737
|
83
|
Chris@1737
|
84 if (reference && aligned) {
|
Chris@1737
|
85 sv_frame_t a = reference->getEndFrame();
|
Chris@1737
|
86 sv_frame_t b = aligned->getEndFrame();
|
Chris@1737
|
87 return std::max(a, b);
|
Chris@1737
|
88 } else {
|
Chris@1737
|
89 return 0;
|
Chris@1737
|
90 }
|
Chris@297
|
91 }
|
Chris@297
|
92
|
Chris@1040
|
93 sv_samplerate_t
|
Chris@297
|
94 AlignmentModel::getSampleRate() const
|
Chris@297
|
95 {
|
Chris@1737
|
96 auto reference = ModelById::get(m_reference);
|
Chris@1737
|
97 if (reference) {
|
Chris@1737
|
98 return reference->getSampleRate();
|
Chris@1737
|
99 } else {
|
Chris@1737
|
100 return 0;
|
Chris@1737
|
101 }
|
Chris@297
|
102 }
|
Chris@297
|
103
|
Chris@297
|
104 bool
|
Chris@297
|
105 AlignmentModel::isReady(int *completion) const
|
Chris@297
|
106 {
|
Chris@1737
|
107 if (!m_pathBegun && !m_pathSource.isNone()) {
|
Chris@338
|
108 if (completion) *completion = 0;
|
Chris@1561
|
109 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1696
|
110 SVCERR << "AlignmentModel::isReady: path not begun" << endl;
|
Chris@1561
|
111 #endif
|
Chris@323
|
112 return false;
|
Chris@323
|
113 }
|
Chris@1016
|
114 if (m_pathComplete) {
|
Chris@338
|
115 if (completion) *completion = 100;
|
Chris@1561
|
116 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1696
|
117 SVCERR << "AlignmentModel::isReady: path complete" << endl;
|
Chris@1561
|
118 #endif
|
Chris@338
|
119 return true;
|
Chris@338
|
120 }
|
Chris@1737
|
121 if (m_pathSource.isNone()) {
|
Chris@1016
|
122 // lack of raw path could mean path is complete (in which case
|
Chris@1737
|
123 // m_pathComplete true above) or else no path source has been
|
Chris@1016
|
124 // set at all yet (this case)
|
Chris@1016
|
125 if (completion) *completion = 0;
|
Chris@1561
|
126 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1696
|
127 SVCERR << "AlignmentModel::isReady: no raw path" << endl;
|
Chris@1561
|
128 #endif
|
Chris@1016
|
129 return false;
|
Chris@1016
|
130 }
|
Chris@1737
|
131 auto pathSourceModel =
|
Chris@1737
|
132 ModelById::getAs<SparseTimeValueModel>(m_pathSource);
|
Chris@1737
|
133 if (pathSourceModel) {
|
Chris@1737
|
134 return pathSourceModel->isReady(completion);
|
Chris@1737
|
135 } else {
|
Chris@1737
|
136 return true; // there is no meaningful answer here
|
Chris@1737
|
137 }
|
Chris@297
|
138 }
|
Chris@297
|
139
|
Chris@297
|
140 const ZoomConstraint *
|
Chris@297
|
141 AlignmentModel::getZoomConstraint() const
|
Chris@297
|
142 {
|
Chris@1582
|
143 return nullptr;
|
Chris@297
|
144 }
|
Chris@297
|
145
|
Chris@1737
|
146 ModelId
|
Chris@297
|
147 AlignmentModel::getReferenceModel() const
|
Chris@297
|
148 {
|
Chris@297
|
149 return m_reference;
|
Chris@297
|
150 }
|
Chris@297
|
151
|
Chris@1737
|
152 ModelId
|
Chris@297
|
153 AlignmentModel::getAlignedModel() const
|
Chris@297
|
154 {
|
Chris@297
|
155 return m_aligned;
|
Chris@297
|
156 }
|
Chris@297
|
157
|
Chris@1038
|
158 sv_frame_t
|
Chris@1038
|
159 AlignmentModel::toReference(sv_frame_t frame) const
|
Chris@297
|
160 {
|
Chris@376
|
161 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
162 cerr << "AlignmentModel::toReference(" << frame << ")" << endl;
|
Chris@376
|
163 #endif
|
Chris@371
|
164 if (!m_path) {
|
Chris@1738
|
165 if (m_pathSource.isNone()) {
|
Chris@1738
|
166 return frame;
|
Chris@1738
|
167 }
|
Chris@371
|
168 constructPath();
|
Chris@371
|
169 }
|
Chris@1738
|
170 if (!m_path) {
|
Chris@1738
|
171 return frame;
|
Chris@1738
|
172 }
|
Chris@1738
|
173
|
Chris@1738
|
174 return performAlignment(*m_path, frame);
|
Chris@297
|
175 }
|
Chris@297
|
176
|
Chris@1038
|
177 sv_frame_t
|
Chris@1038
|
178 AlignmentModel::fromReference(sv_frame_t frame) const
|
Chris@297
|
179 {
|
Chris@376
|
180 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
181 cerr << "AlignmentModel::fromReference(" << frame << ")" << endl;
|
Chris@376
|
182 #endif
|
Chris@371
|
183 if (!m_reversePath) {
|
Chris@1738
|
184 if (m_pathSource.isNone()) {
|
Chris@1738
|
185 return frame;
|
Chris@1738
|
186 }
|
Chris@371
|
187 constructReversePath();
|
Chris@371
|
188 }
|
Chris@1738
|
189 if (!m_reversePath) {
|
Chris@1738
|
190 return frame;
|
Chris@1738
|
191 }
|
Chris@297
|
192
|
Chris@1738
|
193 return performAlignment(*m_reversePath, frame);
|
Chris@297
|
194 }
|
Chris@297
|
195
|
Chris@297
|
196 void
|
Chris@1752
|
197 AlignmentModel::pathSourceChangedWithin(ModelId, sv_frame_t, sv_frame_t)
|
Chris@297
|
198 {
|
Chris@297
|
199 if (!m_pathComplete) return;
|
Chris@338
|
200 constructPath();
|
Chris@297
|
201 constructReversePath();
|
Chris@297
|
202 }
|
Chris@297
|
203
|
Chris@297
|
204 void
|
Chris@1752
|
205 AlignmentModel::pathSourceCompletionChanged(ModelId)
|
Chris@297
|
206 {
|
Chris@1737
|
207 auto pathSourceModel =
|
Chris@1737
|
208 ModelById::getAs<SparseTimeValueModel>(m_pathSource);
|
Chris@1737
|
209 if (!pathSourceModel) return;
|
Chris@1737
|
210
|
Chris@323
|
211 m_pathBegun = true;
|
Chris@323
|
212
|
Chris@297
|
213 if (!m_pathComplete) {
|
Chris@338
|
214
|
Chris@297
|
215 int completion = 0;
|
Chris@1737
|
216 pathSourceModel->isReady(&completion);
|
Chris@338
|
217
|
Chris@376
|
218 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1666
|
219 SVCERR << "AlignmentModel::pathCompletionChanged: completion = "
|
Chris@1666
|
220 << completion << endl;
|
Chris@376
|
221 #endif
|
Chris@338
|
222
|
Chris@323
|
223 m_pathComplete = (completion == 100);
|
Chris@338
|
224
|
Chris@297
|
225 if (m_pathComplete) {
|
Chris@338
|
226
|
Chris@338
|
227 constructPath();
|
Chris@297
|
228 constructReversePath();
|
Chris@1666
|
229
|
Chris@1691
|
230 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1691
|
231 SVCERR << "AlignmentModel: path complete" << endl;
|
Chris@1691
|
232 #endif
|
Chris@297
|
233 }
|
Chris@297
|
234 }
|
Chris@323
|
235
|
Chris@1752
|
236 emit completionChanged(getId());
|
Chris@297
|
237 }
|
Chris@297
|
238
|
Chris@297
|
239 void
|
Chris@338
|
240 AlignmentModel::constructPath() const
|
Chris@297
|
241 {
|
Chris@1737
|
242 auto alignedModel = ModelById::get(m_aligned);
|
Chris@1737
|
243 if (!alignedModel) return;
|
Chris@1737
|
244
|
Chris@1737
|
245 auto pathSourceModel =
|
Chris@1737
|
246 ModelById::getAs<SparseTimeValueModel>(m_pathSource);
|
Chris@338
|
247 if (!m_path) {
|
Chris@1760
|
248 if (!pathSourceModel) {
|
Chris@843
|
249 cerr << "ERROR: AlignmentModel::constructPath: "
|
Chris@1760
|
250 << "No raw path available (id is " << m_pathSource
|
Chris@1760
|
251 << ")" << endl;
|
Chris@338
|
252 return;
|
Chris@338
|
253 }
|
Chris@1738
|
254 m_path.reset(new Path
|
Chris@1737
|
255 (pathSourceModel->getSampleRate(),
|
Chris@1738
|
256 pathSourceModel->getResolution()));
|
Chris@338
|
257 } else {
|
Chris@1737
|
258 if (!pathSourceModel) return;
|
Chris@297
|
259 }
|
Chris@297
|
260
|
Chris@338
|
261 m_path->clear();
|
Chris@297
|
262
|
Chris@1737
|
263 EventVector points = pathSourceModel->getAllEvents();
|
Chris@1651
|
264
|
Chris@1651
|
265 for (const auto &p: points) {
|
Chris@1651
|
266 sv_frame_t frame = p.getFrame();
|
Chris@1651
|
267 double value = p.getValue();
|
Chris@1737
|
268 sv_frame_t rframe = lrint(value * alignedModel->getSampleRate());
|
Chris@1662
|
269 m_path->add(PathPoint(frame, rframe));
|
Chris@297
|
270 }
|
Chris@297
|
271
|
Chris@376
|
272 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
273 cerr << "AlignmentModel::constructPath: " << m_path->getPointCount() << " points, at least " << (2 * m_path->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
|
Chris@376
|
274 #endif
|
Chris@338
|
275 }
|
Chris@338
|
276
|
Chris@338
|
277 void
|
Chris@338
|
278 AlignmentModel::constructReversePath() const
|
Chris@338
|
279 {
|
Chris@338
|
280 if (!m_reversePath) {
|
Chris@407
|
281 if (!m_path) {
|
Chris@843
|
282 cerr << "ERROR: AlignmentModel::constructReversePath: "
|
Chris@843
|
283 << "No forward path available" << endl;
|
Chris@407
|
284 return;
|
Chris@407
|
285 }
|
Chris@1738
|
286 m_reversePath.reset(new Path
|
Chris@1737
|
287 (m_path->getSampleRate(),
|
Chris@1738
|
288 m_path->getResolution()));
|
Chris@338
|
289 } else {
|
Chris@407
|
290 if (!m_path) return;
|
Chris@338
|
291 }
|
Chris@338
|
292
|
Chris@338
|
293 m_reversePath->clear();
|
Chris@407
|
294
|
Chris@1738
|
295 Path::Points points = m_path->getPoints();
|
Chris@407
|
296
|
Chris@1738
|
297 for (auto p: points) {
|
Chris@1738
|
298 sv_frame_t frame = p.frame;
|
Chris@1738
|
299 sv_frame_t rframe = p.mapframe;
|
Chris@1662
|
300 m_reversePath->add(PathPoint(rframe, frame));
|
Chris@407
|
301 }
|
Chris@338
|
302
|
Chris@376
|
303 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
304 cerr << "AlignmentModel::constructReversePath: " << m_reversePath->getPointCount() << " points, at least " << (2 * m_reversePath->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
|
Chris@376
|
305 #endif
|
Chris@297
|
306 }
|
Chris@297
|
307
|
Chris@1038
|
308 sv_frame_t
|
Chris@1738
|
309 AlignmentModel::performAlignment(const Path &path, sv_frame_t frame) const
|
Chris@297
|
310 {
|
Chris@338
|
311 // The path consists of a series of points, each with frame equal
|
Chris@338
|
312 // to the frame on the source model and mapframe equal to the
|
Chris@338
|
313 // frame on the target model. Both should be monotonically
|
Chris@338
|
314 // increasing.
|
Chris@297
|
315
|
Chris@1738
|
316 const Path::Points &points = path.getPoints();
|
Chris@297
|
317
|
Chris@297
|
318 if (points.empty()) {
|
Chris@379
|
319 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
320 cerr << "AlignmentModel::align: No points" << endl;
|
Chris@379
|
321 #endif
|
Chris@297
|
322 return frame;
|
Chris@297
|
323 }
|
Chris@297
|
324
|
Chris@376
|
325 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
326 cerr << "AlignmentModel::align: frame " << frame << " requested" << endl;
|
Chris@376
|
327 #endif
|
Chris@376
|
328
|
Chris@1662
|
329 PathPoint point(frame);
|
Chris@1738
|
330 Path::Points::const_iterator i = points.lower_bound(point);
|
Chris@376
|
331 if (i == points.end()) {
|
Chris@376
|
332 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@843
|
333 cerr << "Note: i == points.end()" << endl;
|
Chris@376
|
334 #endif
|
Chris@376
|
335 --i;
|
Chris@376
|
336 }
|
Chris@1738
|
337 while (i != points.begin() && i->frame > frame) {
|
Chris@1738
|
338 --i;
|
Chris@1738
|
339 }
|
Chris@297
|
340
|
Chris@1038
|
341 sv_frame_t foundFrame = i->frame;
|
Chris@1038
|
342 sv_frame_t foundMapFrame = i->mapframe;
|
Chris@297
|
343
|
Chris@1038
|
344 sv_frame_t followingFrame = foundFrame;
|
Chris@1038
|
345 sv_frame_t followingMapFrame = foundMapFrame;
|
Chris@297
|
346
|
Chris@312
|
347 if (++i != points.end()) {
|
Chris@376
|
348 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@843
|
349 cerr << "another point available" << endl;
|
Chris@376
|
350 #endif
|
Chris@312
|
351 followingFrame = i->frame;
|
Chris@338
|
352 followingMapFrame = i->mapframe;
|
Chris@376
|
353 } else {
|
Chris@376
|
354 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@843
|
355 cerr << "no other point available" << endl;
|
Chris@376
|
356 #endif
|
Chris@376
|
357 }
|
Chris@312
|
358
|
Chris@1075
|
359 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
360 cerr << "foundFrame = " << foundFrame << ", foundMapFrame = " << foundMapFrame
|
Chris@1075
|
361 << ", followingFrame = " << followingFrame << ", followingMapFrame = "
|
Chris@1075
|
362 << followingMapFrame << endl;
|
Chris@1075
|
363 #endif
|
Chris@1075
|
364
|
Chris@1738
|
365 if (foundMapFrame < 0) {
|
Chris@1738
|
366 return 0;
|
Chris@1738
|
367 }
|
Chris@312
|
368
|
Chris@1038
|
369 sv_frame_t resultFrame = foundMapFrame;
|
Chris@312
|
370
|
Chris@1038
|
371 if (followingFrame != foundFrame && frame > foundFrame) {
|
Chris@1038
|
372 double interp =
|
Chris@1038
|
373 double(frame - foundFrame) /
|
Chris@1038
|
374 double(followingFrame - foundFrame);
|
Chris@1038
|
375 resultFrame += lrint(double(followingMapFrame - foundMapFrame) * interp);
|
Chris@312
|
376 }
|
Chris@312
|
377
|
Chris@376
|
378 #ifdef DEBUG_ALIGNMENT_MODEL
|
Chris@1075
|
379 cerr << "AlignmentModel::align: resultFrame = " << resultFrame << endl;
|
Chris@376
|
380 #endif
|
Chris@312
|
381
|
Chris@312
|
382 return resultFrame;
|
Chris@297
|
383 }
|
Chris@407
|
384
|
Chris@407
|
385 void
|
Chris@1737
|
386 AlignmentModel::setPathFrom(ModelId pathSource)
|
Chris@1016
|
387 {
|
Chris@1737
|
388 m_pathSource = pathSource;
|
Chris@1737
|
389
|
Chris@1737
|
390 auto pathSourceModel =
|
Chris@1737
|
391 ModelById::getAs<SparseTimeValueModel>(m_pathSource);
|
Chris@1737
|
392
|
Chris@1737
|
393 if (pathSourceModel) {
|
Chris@1016
|
394
|
Chris@1737
|
395 connect(pathSourceModel.get(),
|
Chris@1752
|
396 SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
|
Chris@1752
|
397 this, SLOT(pathSourceChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
|
Chris@1737
|
398
|
Chris@1752
|
399 connect(pathSourceModel.get(), SIGNAL(completionChanged(ModelId)),
|
Chris@1752
|
400 this, SLOT(pathSourceCompletionChanged(ModelId)));
|
Chris@1737
|
401
|
Chris@1737
|
402 constructPath();
|
Chris@1737
|
403 constructReversePath();
|
Chris@1737
|
404
|
Chris@1737
|
405 if (pathSourceModel->isReady()) {
|
Chris@1752
|
406 pathSourceCompletionChanged(m_pathSource);
|
Chris@1737
|
407 }
|
Chris@1712
|
408 }
|
Chris@1016
|
409 }
|
Chris@1016
|
410
|
Chris@1016
|
411 void
|
Chris@1738
|
412 AlignmentModel::setPath(const Path &path)
|
Chris@407
|
413 {
|
Chris@1738
|
414 m_path.reset(new Path(path));
|
Chris@1560
|
415 m_pathComplete = true;
|
Chris@407
|
416 constructReversePath();
|
Chris@407
|
417 }
|
Chris@297
|
418
|
Chris@407
|
419 void
|
Chris@407
|
420 AlignmentModel::toXml(QTextStream &stream,
|
Chris@407
|
421 QString indent,
|
Chris@407
|
422 QString extraAttributes) const
|
Chris@407
|
423 {
|
Chris@407
|
424 if (!m_path) {
|
Chris@690
|
425 SVDEBUG << "AlignmentModel::toXml: no path" << endl;
|
Chris@407
|
426 return;
|
Chris@407
|
427 }
|
Chris@407
|
428
|
Chris@407
|
429 m_path->toXml(stream, indent, "");
|
Chris@407
|
430
|
Chris@407
|
431 Model::toXml(stream, indent,
|
Chris@407
|
432 QString("type=\"alignment\" reference=\"%1\" aligned=\"%2\" path=\"%3\" %4")
|
Chris@1738
|
433 .arg(ModelById::getExportId(m_reference))
|
Chris@1738
|
434 .arg(ModelById::getExportId(m_aligned))
|
Chris@1677
|
435 .arg(m_path->getExportId())
|
Chris@407
|
436 .arg(extraAttributes));
|
Chris@407
|
437 }
|