comparison data/model/AlignmentModel.cpp @ 1735:d91ff235e69d by-id

Some messing with Model and AlignmentModel
author Chris Cannam
date Tue, 25 Jun 2019 15:29:34 +0100
parents 78fe29adfd16
children 5d631f6129fe
comparison
equal deleted inserted replaced
1733:c3b5564cfb78 1735:d91ff235e69d
17 17
18 #include "SparseTimeValueModel.h" 18 #include "SparseTimeValueModel.h"
19 19
20 //#define DEBUG_ALIGNMENT_MODEL 1 20 //#define DEBUG_ALIGNMENT_MODEL 1
21 21
22 AlignmentModel::AlignmentModel(Model *reference, 22 AlignmentModel::AlignmentModel(ModelId reference,
23 Model *aligned, 23 ModelId aligned,
24 SparseTimeValueModel *path) : 24 ModelId pathSource) :
25 m_reference(reference), 25 m_reference(reference),
26 m_aligned(aligned), 26 m_aligned(aligned),
27 m_rawPath(path), 27 m_pathSource(pathSource),
28 m_path(nullptr), 28 m_path(nullptr),
29 m_reversePath(nullptr), 29 m_reversePath(nullptr),
30 m_pathBegun(false), 30 m_pathBegun(false),
31 m_pathComplete(false) 31 m_pathComplete(false)
32 { 32 {
33 if (m_rawPath) { 33 auto pathSourceModel = ModelById::getAs<SparseTimeValueModel>(pathSource);
34 34
35 connect(m_rawPath, SIGNAL(modelChanged()), 35 if (pathSourceModel) {
36 this, SLOT(pathChanged())); 36
37 37 connect(pathSourceModel.get(), SIGNAL(modelChanged()),
38 connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), 38 this, SLOT(pathSourceChanged()));
39 this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t))); 39
40 connect(pathSourceModel.get(),
41 SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
42 this, SLOT(pathSourceChangedWithin(sv_frame_t, sv_frame_t)));
40 43
41 connect(m_rawPath, SIGNAL(completionChanged()), 44 connect(pathSourceModel.get(), SIGNAL(completionChanged()),
42 this, SLOT(pathCompletionChanged())); 45 this, SLOT(pathSourceCompletionChanged()));
43 46
44 constructPath(); 47 constructPath();
45 constructReversePath(); 48 constructReversePath();
46 } 49
47 50 if (pathSourceModel->isReady()) {
48 if (m_rawPath && m_rawPath->isReady()) { 51 pathSourceCompletionChanged();
49 pathCompletionChanged(); 52 }
50 } 53 }
51 54
52 if (m_reference == m_aligned) { 55 if (m_reference == m_aligned) {
53 // Trivial alignment, e.g. of main model to itself, which we 56 // Trivial alignment, e.g. of main model to itself, which we
54 // record so that we can distinguish the reference model for 57 // record so that we can distinguish the reference model for
61 { 64 {
62 #ifdef DEBUG_ALIGNMENT_MODEL 65 #ifdef DEBUG_ALIGNMENT_MODEL
63 SVCERR << "AlignmentModel(" << this << ")::~AlignmentModel()" << endl; 66 SVCERR << "AlignmentModel(" << this << ")::~AlignmentModel()" << endl;
64 #endif 67 #endif
65 68
66 if (m_rawPath) m_rawPath->aboutToDelete(); 69 //!!! if (m_pathSource) m_pathSource->aboutToDelete();
67 delete m_rawPath; 70 // delete m_pathSource;
68 71
69 if (m_path) m_path->aboutToDelete(); 72 // if (m_path) m_path->aboutToDelete();
70 delete m_path; 73 // delete m_path;
71 74
72 if (m_reversePath) m_reversePath->aboutToDelete(); 75 // if (m_reversePath) m_reversePath->aboutToDelete();
73 delete m_reversePath; 76 // delete m_reversePath;
74 } 77 }
75 78
76 bool 79 bool
77 AlignmentModel::isOK() const 80 AlignmentModel::isOK() const
78 { 81 {
79 if (m_error != "") return false; 82 if (m_error != "") return false;
80 if (m_rawPath) return m_rawPath->isOK(); 83 if (m_pathSource) return m_pathSource->isOK();
81 return true; 84 return true;
82 } 85 }
83 86
84 sv_frame_t 87 sv_frame_t
85 AlignmentModel::getStartFrame() const 88 AlignmentModel::getStartFrame() const
104 } 107 }
105 108
106 bool 109 bool
107 AlignmentModel::isReady(int *completion) const 110 AlignmentModel::isReady(int *completion) const
108 { 111 {
109 if (!m_pathBegun && m_rawPath) { 112 if (!m_pathBegun && m_pathSource) {
110 if (completion) *completion = 0; 113 if (completion) *completion = 0;
111 #ifdef DEBUG_ALIGNMENT_MODEL 114 #ifdef DEBUG_ALIGNMENT_MODEL
112 SVCERR << "AlignmentModel::isReady: path not begun" << endl; 115 SVCERR << "AlignmentModel::isReady: path not begun" << endl;
113 #endif 116 #endif
114 return false; 117 return false;
118 #ifdef DEBUG_ALIGNMENT_MODEL 121 #ifdef DEBUG_ALIGNMENT_MODEL
119 SVCERR << "AlignmentModel::isReady: path complete" << endl; 122 SVCERR << "AlignmentModel::isReady: path complete" << endl;
120 #endif 123 #endif
121 return true; 124 return true;
122 } 125 }
123 if (!m_rawPath) { 126 if (!m_pathSource) {
124 // lack of raw path could mean path is complete (in which case 127 // lack of raw path could mean path is complete (in which case
125 // m_pathComplete true above) or else no alignment has been 128 // m_pathComplete true above) or else no alignment has been
126 // set at all yet (this case) 129 // set at all yet (this case)
127 if (completion) *completion = 0; 130 if (completion) *completion = 0;
128 #ifdef DEBUG_ALIGNMENT_MODEL 131 #ifdef DEBUG_ALIGNMENT_MODEL
129 SVCERR << "AlignmentModel::isReady: no raw path" << endl; 132 SVCERR << "AlignmentModel::isReady: no raw path" << endl;
130 #endif 133 #endif
131 return false; 134 return false;
132 } 135 }
133 return m_rawPath->isReady(completion); 136 return m_pathSource->isReady(completion);
134 } 137 }
135 138
136 const ZoomConstraint * 139 const ZoomConstraint *
137 AlignmentModel::getZoomConstraint() const 140 AlignmentModel::getZoomConstraint() const
138 { 141 {
156 { 159 {
157 #ifdef DEBUG_ALIGNMENT_MODEL 160 #ifdef DEBUG_ALIGNMENT_MODEL
158 cerr << "AlignmentModel::toReference(" << frame << ")" << endl; 161 cerr << "AlignmentModel::toReference(" << frame << ")" << endl;
159 #endif 162 #endif
160 if (!m_path) { 163 if (!m_path) {
161 if (!m_rawPath) return frame; 164 if (!m_pathSource) return frame;
162 constructPath(); 165 constructPath();
163 } 166 }
164 return align(m_path, frame); 167 return align(m_path, frame);
165 } 168 }
166 169
169 { 172 {
170 #ifdef DEBUG_ALIGNMENT_MODEL 173 #ifdef DEBUG_ALIGNMENT_MODEL
171 cerr << "AlignmentModel::fromReference(" << frame << ")" << endl; 174 cerr << "AlignmentModel::fromReference(" << frame << ")" << endl;
172 #endif 175 #endif
173 if (!m_reversePath) { 176 if (!m_reversePath) {
174 if (!m_rawPath) return frame; 177 if (!m_pathSource) return frame;
175 constructReversePath(); 178 constructReversePath();
176 } 179 }
177 return align(m_reversePath, frame); 180 return align(m_reversePath, frame);
178 } 181 }
179 182
180 void 183 void
181 AlignmentModel::pathChanged() 184 AlignmentModel::pathSourceChanged()
182 { 185 {
183 if (m_pathComplete) { 186 if (m_pathComplete) {
184 cerr << "AlignmentModel: deleting raw path model" << endl; 187 cerr << "AlignmentModel: deleting raw path model" << endl;
185 if (m_rawPath) m_rawPath->aboutToDelete(); 188 if (m_pathSource) m_pathSource->aboutToDelete();
186 delete m_rawPath; 189 delete m_pathSource;
187 m_rawPath = nullptr; 190 m_pathSource = nullptr;
188 } 191 }
189 } 192 }
190 193
191 void 194 void
192 AlignmentModel::pathChangedWithin(sv_frame_t, sv_frame_t) 195 AlignmentModel::pathSourceChangedWithin(sv_frame_t, sv_frame_t)
193 { 196 {
194 if (!m_pathComplete) return; 197 if (!m_pathComplete) return;
195 constructPath(); 198 constructPath();
196 constructReversePath(); 199 constructReversePath();
197 } 200 }
198 201
199 void 202 void
200 AlignmentModel::pathCompletionChanged() 203 AlignmentModel::pathSourceCompletionChanged()
201 { 204 {
202 if (!m_rawPath) return; 205 if (!m_pathSource) return;
203 m_pathBegun = true; 206 m_pathBegun = true;
204 207
205 if (!m_pathComplete) { 208 if (!m_pathComplete) {
206 209
207 int completion = 0; 210 int completion = 0;
208 m_rawPath->isReady(&completion); 211 m_pathSource->isReady(&completion);
209 212
210 #ifdef DEBUG_ALIGNMENT_MODEL 213 #ifdef DEBUG_ALIGNMENT_MODEL
211 SVCERR << "AlignmentModel::pathCompletionChanged: completion = " 214 SVCERR << "AlignmentModel::pathCompletionChanged: completion = "
212 << completion << endl; 215 << completion << endl;
213 #endif 216 #endif
230 233
231 void 234 void
232 AlignmentModel::constructPath() const 235 AlignmentModel::constructPath() const
233 { 236 {
234 if (!m_path) { 237 if (!m_path) {
235 if (!m_rawPath) { 238 if (!m_pathSource) {
236 cerr << "ERROR: AlignmentModel::constructPath: " 239 cerr << "ERROR: AlignmentModel::constructPath: "
237 << "No raw path available" << endl; 240 << "No raw path available" << endl;
238 return; 241 return;
239 } 242 }
240 m_path = new PathModel 243 m_path = new PathModel
241 (m_rawPath->getSampleRate(), m_rawPath->getResolution(), false); 244 (m_pathSource->getSampleRate(), m_pathSource->getResolution(), false);
242 } else { 245 } else {
243 if (!m_rawPath) return; 246 if (!m_pathSource) return;
244 } 247 }
245 248
246 m_path->clear(); 249 m_path->clear();
247 250
248 EventVector points = m_rawPath->getAllEvents(); 251 EventVector points = m_pathSource->getAllEvents();
249 252
250 for (const auto &p: points) { 253 for (const auto &p: points) {
251 sv_frame_t frame = p.getFrame(); 254 sv_frame_t frame = p.getFrame();
252 double value = p.getValue(); 255 double value = p.getValue();
253 sv_frame_t rframe = lrint(value * m_aligned->getSampleRate()); 256 sv_frame_t rframe = lrint(value * m_aligned->getSampleRate());
366 } 369 }
367 370
368 void 371 void
369 AlignmentModel::setPathFrom(SparseTimeValueModel *rawpath) 372 AlignmentModel::setPathFrom(SparseTimeValueModel *rawpath)
370 { 373 {
371 if (m_rawPath) m_rawPath->aboutToDelete(); 374 if (m_pathSource) m_pathSource->aboutToDelete();
372 delete m_rawPath; 375 delete m_pathSource;
373 376
374 m_rawPath = rawpath; 377 m_pathSource = rawpath;
375 378
376 if (!m_rawPath) { 379 if (!m_pathSource) {
377 return; 380 return;
378 } 381 }
379 382
380 connect(m_rawPath, SIGNAL(modelChanged()), 383 connect(m_pathSource, SIGNAL(modelChanged()),
381 this, SLOT(pathChanged())); 384 this, SLOT(pathChanged()));
382 385
383 connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), 386 connect(m_pathSource, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
384 this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t))); 387 this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t)));
385 388
386 connect(m_rawPath, SIGNAL(completionChanged()), 389 connect(m_pathSource, SIGNAL(completionChanged()),
387 this, SLOT(pathCompletionChanged())); 390 this, SLOT(pathCompletionChanged()));
388 391
389 constructPath(); 392 constructPath();
390 constructReversePath(); 393 constructReversePath();
391 394
392 if (m_rawPath->isReady()) { 395 if (m_pathSource->isReady()) {
393 pathCompletionChanged(); 396 pathCompletionChanged();
394 } 397 }
395 } 398 }
396 399
397 void 400 void