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@297
|
20 AlignmentModel::AlignmentModel(Model *reference,
|
Chris@297
|
21 Model *aligned,
|
Chris@297
|
22 Model *inputModel,
|
Chris@297
|
23 SparseTimeValueModel *path) :
|
Chris@297
|
24 m_reference(reference),
|
Chris@297
|
25 m_aligned(aligned),
|
Chris@297
|
26 m_inputModel(inputModel),
|
Chris@338
|
27 m_rawPath(path),
|
Chris@338
|
28 m_path(0),
|
Chris@297
|
29 m_reversePath(0),
|
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@371
|
38 connect(m_rawPath, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@371
|
39 this, SLOT(pathChanged(size_t, size_t)));
|
Chris@371
|
40
|
Chris@371
|
41 connect(m_rawPath, SIGNAL(completionChanged()),
|
Chris@371
|
42 this, SLOT(pathCompletionChanged()));
|
Chris@371
|
43 }
|
Chris@297
|
44
|
Chris@338
|
45 constructPath();
|
Chris@297
|
46 constructReversePath();
|
Chris@297
|
47 }
|
Chris@297
|
48
|
Chris@297
|
49 AlignmentModel::~AlignmentModel()
|
Chris@297
|
50 {
|
Chris@297
|
51 delete m_inputModel;
|
Chris@338
|
52 delete m_rawPath;
|
Chris@297
|
53 delete m_path;
|
Chris@297
|
54 delete m_reversePath;
|
Chris@297
|
55 }
|
Chris@297
|
56
|
Chris@297
|
57 bool
|
Chris@297
|
58 AlignmentModel::isOK() const
|
Chris@297
|
59 {
|
Chris@338
|
60 if (m_rawPath) return m_rawPath->isOK();
|
Chris@338
|
61 else return true;
|
Chris@297
|
62 }
|
Chris@297
|
63
|
Chris@297
|
64 size_t
|
Chris@297
|
65 AlignmentModel::getStartFrame() const
|
Chris@297
|
66 {
|
Chris@297
|
67 size_t a = m_reference->getStartFrame();
|
Chris@297
|
68 size_t b = m_aligned->getStartFrame();
|
Chris@297
|
69 return std::min(a, b);
|
Chris@297
|
70 }
|
Chris@297
|
71
|
Chris@297
|
72 size_t
|
Chris@297
|
73 AlignmentModel::getEndFrame() const
|
Chris@297
|
74 {
|
Chris@297
|
75 size_t a = m_reference->getEndFrame();
|
Chris@297
|
76 size_t b = m_aligned->getEndFrame();
|
Chris@297
|
77 return std::max(a, b);
|
Chris@297
|
78 }
|
Chris@297
|
79
|
Chris@297
|
80 size_t
|
Chris@297
|
81 AlignmentModel::getSampleRate() const
|
Chris@297
|
82 {
|
Chris@297
|
83 return m_reference->getSampleRate();
|
Chris@297
|
84 }
|
Chris@297
|
85
|
Chris@297
|
86 Model *
|
Chris@297
|
87 AlignmentModel::clone() const
|
Chris@297
|
88 {
|
Chris@297
|
89 return new AlignmentModel
|
Chris@297
|
90 (m_reference, m_aligned,
|
Chris@297
|
91 m_inputModel ? m_inputModel->clone() : 0,
|
Chris@338
|
92 m_rawPath ? static_cast<SparseTimeValueModel *>(m_rawPath->clone()) : 0);
|
Chris@297
|
93 }
|
Chris@297
|
94
|
Chris@297
|
95 bool
|
Chris@297
|
96 AlignmentModel::isReady(int *completion) const
|
Chris@297
|
97 {
|
Chris@323
|
98 if (!m_pathBegun) {
|
Chris@338
|
99 if (completion) *completion = 0;
|
Chris@323
|
100 return false;
|
Chris@323
|
101 }
|
Chris@338
|
102 if (m_pathComplete || !m_rawPath) {
|
Chris@338
|
103 if (completion) *completion = 100;
|
Chris@338
|
104 return true;
|
Chris@338
|
105 }
|
Chris@338
|
106 return m_rawPath->isReady(completion);
|
Chris@297
|
107 }
|
Chris@297
|
108
|
Chris@297
|
109 const ZoomConstraint *
|
Chris@297
|
110 AlignmentModel::getZoomConstraint() const
|
Chris@297
|
111 {
|
Chris@338
|
112 return 0;
|
Chris@297
|
113 }
|
Chris@297
|
114
|
Chris@297
|
115 const Model *
|
Chris@297
|
116 AlignmentModel::getReferenceModel() const
|
Chris@297
|
117 {
|
Chris@297
|
118 return m_reference;
|
Chris@297
|
119 }
|
Chris@297
|
120
|
Chris@297
|
121 const Model *
|
Chris@297
|
122 AlignmentModel::getAlignedModel() const
|
Chris@297
|
123 {
|
Chris@297
|
124 return m_aligned;
|
Chris@297
|
125 }
|
Chris@297
|
126
|
Chris@297
|
127 size_t
|
Chris@297
|
128 AlignmentModel::toReference(size_t frame) const
|
Chris@297
|
129 {
|
Chris@297
|
130 // std::cerr << "AlignmentModel::toReference(" << frame << ")" << std::endl;
|
Chris@371
|
131 if (!m_path) {
|
Chris@371
|
132 if (!m_rawPath) return frame;
|
Chris@371
|
133 constructPath();
|
Chris@371
|
134 }
|
Chris@339
|
135 return align(m_path, frame);
|
Chris@297
|
136 }
|
Chris@297
|
137
|
Chris@297
|
138 size_t
|
Chris@297
|
139 AlignmentModel::fromReference(size_t frame) const
|
Chris@297
|
140 {
|
Chris@297
|
141 // std::cerr << "AlignmentModel::fromReference(" << frame << ")" << std::endl;
|
Chris@371
|
142 if (!m_reversePath) {
|
Chris@371
|
143 if (!m_rawPath) return frame;
|
Chris@371
|
144 constructReversePath();
|
Chris@371
|
145 }
|
Chris@339
|
146 return align(m_reversePath, frame);
|
Chris@297
|
147 }
|
Chris@297
|
148
|
Chris@297
|
149 void
|
Chris@297
|
150 AlignmentModel::pathChanged()
|
Chris@297
|
151 {
|
Chris@339
|
152 if (m_pathComplete) {
|
Chris@339
|
153 std::cerr << "AlignmentModel: deleting raw path model" << std::endl;
|
Chris@339
|
154 delete m_rawPath;
|
Chris@339
|
155 m_rawPath = 0;
|
Chris@339
|
156 }
|
Chris@297
|
157 }
|
Chris@297
|
158
|
Chris@297
|
159 void
|
Chris@297
|
160 AlignmentModel::pathChanged(size_t, size_t)
|
Chris@297
|
161 {
|
Chris@297
|
162 if (!m_pathComplete) return;
|
Chris@338
|
163 constructPath();
|
Chris@297
|
164 constructReversePath();
|
Chris@297
|
165 }
|
Chris@297
|
166
|
Chris@297
|
167 void
|
Chris@297
|
168 AlignmentModel::pathCompletionChanged()
|
Chris@297
|
169 {
|
Chris@339
|
170 if (!m_rawPath) return;
|
Chris@323
|
171 m_pathBegun = true;
|
Chris@323
|
172
|
Chris@297
|
173 if (!m_pathComplete) {
|
Chris@338
|
174
|
Chris@297
|
175 int completion = 0;
|
Chris@338
|
176 m_rawPath->isReady(&completion);
|
Chris@338
|
177
|
Chris@333
|
178 // std::cerr << "AlignmentModel::pathCompletionChanged: completion = "
|
Chris@333
|
179 // << completion << std::endl;
|
Chris@338
|
180
|
Chris@323
|
181 m_pathComplete = (completion == 100);
|
Chris@338
|
182
|
Chris@297
|
183 if (m_pathComplete) {
|
Chris@338
|
184
|
Chris@338
|
185 constructPath();
|
Chris@297
|
186 constructReversePath();
|
Chris@338
|
187
|
Chris@297
|
188 delete m_inputModel;
|
Chris@297
|
189 m_inputModel = 0;
|
Chris@297
|
190 }
|
Chris@297
|
191 }
|
Chris@323
|
192
|
Chris@297
|
193 emit completionChanged();
|
Chris@297
|
194 }
|
Chris@297
|
195
|
Chris@297
|
196 void
|
Chris@338
|
197 AlignmentModel::constructPath() const
|
Chris@297
|
198 {
|
Chris@338
|
199 if (!m_path) {
|
Chris@338
|
200 if (!m_rawPath) {
|
Chris@338
|
201 std::cerr << "ERROR: AlignmentModel::constructPath: "
|
Chris@338
|
202 << "No raw path available" << std::endl;
|
Chris@338
|
203 return;
|
Chris@338
|
204 }
|
Chris@338
|
205 m_path = new PathModel
|
Chris@338
|
206 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
|
Chris@338
|
207 } else {
|
Chris@338
|
208 if (!m_rawPath) return;
|
Chris@297
|
209 }
|
Chris@297
|
210
|
Chris@338
|
211 m_path->clear();
|
Chris@297
|
212
|
Chris@338
|
213 SparseTimeValueModel::PointList points = m_rawPath->getPoints();
|
Chris@297
|
214
|
Chris@297
|
215 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@297
|
216 i != points.end(); ++i) {
|
Chris@297
|
217 long frame = i->frame;
|
Chris@297
|
218 float value = i->value;
|
Chris@297
|
219 long rframe = lrintf(value * m_aligned->getSampleRate());
|
Chris@338
|
220 m_path->addPoint(PathPoint(frame, rframe));
|
Chris@297
|
221 }
|
Chris@297
|
222
|
Chris@339
|
223 // std::cerr << "AlignmentModel::constructPath: " << m_path->getPointCount() << " points, at least " << (2 * m_path->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << std::endl;
|
Chris@338
|
224 }
|
Chris@338
|
225
|
Chris@338
|
226 void
|
Chris@338
|
227 AlignmentModel::constructReversePath() const
|
Chris@338
|
228 {
|
Chris@338
|
229 if (!m_reversePath) {
|
Chris@338
|
230 if (!m_rawPath) {
|
Chris@338
|
231 std::cerr << "ERROR: AlignmentModel::constructReversePath: "
|
Chris@338
|
232 << "No raw path available" << std::endl;
|
Chris@338
|
233 return;
|
Chris@338
|
234 }
|
Chris@338
|
235 m_reversePath = new PathModel
|
Chris@338
|
236 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
|
Chris@338
|
237 } else {
|
Chris@338
|
238 if (!m_rawPath) return;
|
Chris@338
|
239 }
|
Chris@338
|
240
|
Chris@338
|
241 m_reversePath->clear();
|
Chris@338
|
242
|
Chris@338
|
243 SparseTimeValueModel::PointList points = m_rawPath->getPoints();
|
Chris@338
|
244
|
Chris@338
|
245 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@338
|
246 i != points.end(); ++i) {
|
Chris@338
|
247 long frame = i->frame;
|
Chris@338
|
248 float value = i->value;
|
Chris@338
|
249 long rframe = lrintf(value * m_aligned->getSampleRate());
|
Chris@338
|
250 m_reversePath->addPoint(PathPoint(rframe, frame));
|
Chris@338
|
251 }
|
Chris@338
|
252
|
Chris@339
|
253 // std::cerr << "AlignmentModel::constructReversePath: " << m_reversePath->getPointCount() << " points, at least " << (2 * m_reversePath->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << std::endl;
|
Chris@297
|
254 }
|
Chris@297
|
255
|
Chris@297
|
256 size_t
|
Chris@338
|
257 AlignmentModel::align(PathModel *path, size_t frame) const
|
Chris@297
|
258 {
|
Chris@339
|
259 if (!path) return frame;
|
Chris@339
|
260
|
Chris@338
|
261 // The path consists of a series of points, each with frame equal
|
Chris@338
|
262 // to the frame on the source model and mapframe equal to the
|
Chris@338
|
263 // frame on the target model. Both should be monotonically
|
Chris@338
|
264 // increasing.
|
Chris@297
|
265
|
Chris@338
|
266 const PathModel::PointList &points = path->getPoints();
|
Chris@297
|
267
|
Chris@297
|
268 if (points.empty()) {
|
Chris@297
|
269 // std::cerr << "AlignmentModel::align: No points" << std::endl;
|
Chris@297
|
270 return frame;
|
Chris@297
|
271 }
|
Chris@297
|
272
|
Chris@338
|
273 PathModel::Point point(frame);
|
Chris@338
|
274 PathModel::PointList::const_iterator i = points.lower_bound(point);
|
Chris@297
|
275 if (i == points.end()) --i;
|
Chris@338
|
276 while (i != points.begin() && i->frame > long(frame)) --i;
|
Chris@297
|
277
|
Chris@312
|
278 long foundFrame = i->frame;
|
Chris@338
|
279 long foundMapFrame = i->mapframe;
|
Chris@297
|
280
|
Chris@312
|
281 long followingFrame = foundFrame;
|
Chris@338
|
282 long followingMapFrame = foundMapFrame;
|
Chris@297
|
283
|
Chris@312
|
284 if (++i != points.end()) {
|
Chris@312
|
285 followingFrame = i->frame;
|
Chris@338
|
286 followingMapFrame = i->mapframe;
|
Chris@312
|
287 }
|
Chris@312
|
288
|
Chris@338
|
289 if (foundMapFrame < 0) return 0;
|
Chris@312
|
290
|
Chris@338
|
291 size_t resultFrame = foundMapFrame;
|
Chris@312
|
292
|
Chris@338
|
293 if (followingFrame != foundFrame && long(frame) > foundFrame) {
|
Chris@338
|
294 float interp =
|
Chris@338
|
295 float(frame - foundFrame) /
|
Chris@338
|
296 float(followingFrame - foundFrame);
|
Chris@338
|
297 resultFrame += lrintf((followingMapFrame - foundMapFrame) * interp);
|
Chris@312
|
298 }
|
Chris@312
|
299
|
Chris@339
|
300 // std::cerr << "AlignmentModel::align: resultFrame = " << resultFrame << std::endl;
|
Chris@312
|
301
|
Chris@312
|
302 return resultFrame;
|
Chris@297
|
303 }
|
Chris@297
|
304
|