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