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