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