comparison align/TransformDTWAligner.cpp @ 773:699b5b130ea2 pitch-align

Fixes to aligner destruction sequence when re-aligning during alignment; hide debug output behind a flag
author Chris Cannam
date Mon, 01 Jun 2020 17:13:09 +0100
parents 1d6cca5a5621
children 8fa98f89eda8
comparison
equal deleted inserted replaced
772:8280f7a363d1 773:699b5b130ea2
143 SVCERR << "Align::alignModel: ERROR: Failed to create reference output model (no plugin?)" << endl; 143 SVCERR << "Align::alignModel: ERROR: Failed to create reference output model (no plugin?)" << endl;
144 emit failed(m_toAlign, message); 144 emit failed(m_toAlign, message);
145 return; 145 return;
146 } 146 }
147 147
148 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
148 SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id " 149 SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id "
149 << m_transform.getIdentifier() 150 << m_transform.getIdentifier()
150 << " is running on reference model" << endl; 151 << " is running on reference model" << endl;
152 #endif
151 153
152 message = ""; 154 message = "";
153 155
154 m_toAlignOutputModel = mtf->transform(m_transform, m_toAlign, message); 156 m_toAlignOutputModel = mtf->transform(m_transform, m_toAlign, message);
155 auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel); 157 auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel);
157 SVCERR << "Align::alignModel: ERROR: Failed to create toAlign output model (no plugin?)" << endl; 159 SVCERR << "Align::alignModel: ERROR: Failed to create toAlign output model (no plugin?)" << endl;
158 emit failed(m_toAlign, message); 160 emit failed(m_toAlign, message);
159 return; 161 return;
160 } 162 }
161 163
164 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
162 SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id " 165 SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id "
163 << m_transform.getIdentifier() 166 << m_transform.getIdentifier()
164 << " is running on toAlign model" << endl; 167 << " is running on toAlign model" << endl;
168 #endif
165 169
166 connect(referenceOutputModel.get(), SIGNAL(completionChanged(ModelId)), 170 connect(referenceOutputModel.get(), SIGNAL(completionChanged(ModelId)),
167 this, SLOT(completionChanged(ModelId))); 171 this, SLOT(completionChanged(ModelId)));
168 connect(toAlignOutputModel.get(), SIGNAL(completionChanged(ModelId)), 172 connect(toAlignOutputModel.get(), SIGNAL(completionChanged(ModelId)),
169 this, SLOT(completionChanged(ModelId))); 173 this, SLOT(completionChanged(ModelId)));
193 TransformDTWAligner::completionChanged(ModelId id) 197 TransformDTWAligner::completionChanged(ModelId id)
194 { 198 {
195 if (!m_incomplete) { 199 if (!m_incomplete) {
196 return; 200 return;
197 } 201 }
198 /* 202 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
199 SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: " 203 SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: "
200 << "model " << id << endl; 204 << "model " << id << endl;
201 */ 205 #endif
206
202 auto referenceOutputModel = ModelById::get(m_referenceOutputModel); 207 auto referenceOutputModel = ModelById::get(m_referenceOutputModel);
203 auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel); 208 auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel);
204 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel); 209 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
205 210
206 if (!referenceOutputModel || !toAlignOutputModel || !alignmentModel) { 211 if (!referenceOutputModel || !toAlignOutputModel || !alignmentModel) {
223 } else { 228 } else {
224 emit failed(m_toAlign, tr("Alignment of transform outputs failed")); 229 emit failed(m_toAlign, tr("Alignment of transform outputs failed"));
225 } 230 }
226 231
227 } else { 232 } else {
228 /* 233 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
229 SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: " 234 SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: "
230 << "not ready yet: reference completion " << referenceCompletion 235 << "not ready yet: reference completion " << referenceCompletion
231 << ", toAlign completion " << toAlignCompletion << endl; 236 << ", toAlign completion " << toAlignCompletion << endl;
232 */ 237 #endif
238
233 int completion = std::min(referenceCompletion, 239 int completion = std::min(referenceCompletion,
234 toAlignCompletion); 240 toAlignCompletion);
235 completion = (completion * 94) / 100; 241 completion = (completion * 94) / 100;
236 alignmentModel->setCompletion(completion); 242 alignmentModel->setCompletion(completion);
237 } 243 }
285 sv_samplerate_t sampleRate, 291 sv_samplerate_t sampleRate,
286 sv_frame_t resolution) 292 sv_frame_t resolution)
287 { 293 {
288 Path path(sampleRate, resolution); 294 Path path(sampleRate, resolution);
289 295
296 path.add(PathPoint(0, 0));
297
290 for (int i = 0; in_range_for(alignment, i); ++i) { 298 for (int i = 0; in_range_for(alignment, i); ++i) {
291 299
292 // DTW returns "the index into s2 for each element in s1" 300 // DTW returns "the index into s2 for each element in s1"
293 sv_frame_t refFrame = refFrames[i]; 301 sv_frame_t refFrame = refFrames[i];
294 302
336 } 344 }
337 for (double v: otherValues) { 345 for (double v: otherValues) {
338 s2.push_back(m_magnitudePreprocessor(v)); 346 s2.push_back(m_magnitudePreprocessor(v));
339 } 347 }
340 348
349 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
341 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: " 350 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: "
342 << "Have " << s1.size() << " events from reference, " 351 << "Have " << s1.size() << " events from reference, "
343 << s2.size() << " from toAlign" << endl; 352 << s2.size() << " from toAlign" << endl;
353 #endif
344 354
345 MagnitudeDTW dtw; 355 MagnitudeDTW dtw;
346 vector<size_t> alignment; 356 vector<size_t> alignment;
347 357
348 { 358 {
359 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
349 SVCERR << "TransformDTWAligner[" << this 360 SVCERR << "TransformDTWAligner[" << this
350 << "]: serialising DTW to avoid over-allocation" << endl; 361 << "]: serialising DTW to avoid over-allocation" << endl;
362 #endif
351 QMutexLocker locker(&m_dtwMutex); 363 QMutexLocker locker(&m_dtwMutex);
352 alignment = dtw.alignSeries(s1, s2); 364 alignment = dtw.alignSeries(s1, s2);
353 } 365 }
354 366
367 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
355 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: " 368 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: "
356 << "DTW produced " << alignment.size() << " points:" << endl; 369 << "DTW produced " << alignment.size() << " points:" << endl;
357 for (int i = 0; in_range_for(alignment, i) && i < 100; ++i) { 370 for (int i = 0; in_range_for(alignment, i) && i < 100; ++i) {
358 SVCERR << alignment[i] << " "; 371 SVCERR << alignment[i] << " ";
359 } 372 }
360 SVCERR << endl; 373 SVCERR << endl;
374 #endif
361 375
362 alignmentModel->setPath(makePath(alignment, 376 alignmentModel->setPath(makePath(alignment,
363 refFrames, 377 refFrames,
364 otherFrames, 378 otherFrames,
365 alignmentModel->getSampleRate(), 379 alignmentModel->getSampleRate(),
407 }; 421 };
408 422
409 vector<RiseFallDTW::Value> s1 = preprocess(refValues); 423 vector<RiseFallDTW::Value> s1 = preprocess(refValues);
410 vector<RiseFallDTW::Value> s2 = preprocess(otherValues); 424 vector<RiseFallDTW::Value> s2 = preprocess(otherValues);
411 425
426 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
412 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: " 427 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: "
413 << "Have " << s1.size() << " events from reference, " 428 << "Have " << s1.size() << " events from reference, "
414 << s2.size() << " from toAlign" << endl; 429 << s2.size() << " from toAlign" << endl;
415 430
416 SVCERR << "Reference:" << endl; 431 SVCERR << "Reference:" << endl;
422 SVCERR << "toAlign:" << endl; 437 SVCERR << "toAlign:" << endl;
423 for (int i = 0; in_range_for(s2, i) && i < 100; ++i) { 438 for (int i = 0; in_range_for(s2, i) && i < 100; ++i) {
424 SVCERR << s2[i] << " "; 439 SVCERR << s2[i] << " ";
425 } 440 }
426 SVCERR << endl; 441 SVCERR << endl;
427 442 #endif
443
428 RiseFallDTW dtw; 444 RiseFallDTW dtw;
429 vector<size_t> alignment; 445 vector<size_t> alignment;
430 446
431 { 447 {
448 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
432 SVCERR << "TransformDTWAligner[" << this 449 SVCERR << "TransformDTWAligner[" << this
433 << "]: serialising DTW to avoid over-allocation" << endl; 450 << "]: serialising DTW to avoid over-allocation" << endl;
451 #endif
434 QMutexLocker locker(&m_dtwMutex); 452 QMutexLocker locker(&m_dtwMutex);
435 alignment = dtw.alignSeries(s1, s2); 453 alignment = dtw.alignSeries(s1, s2);
436 } 454 }
437 455
456 #ifdef DEBUG_TRANSFORM_DTW_ALIGNER
438 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: " 457 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: "
439 << "DTW produced " << alignment.size() << " points:" << endl; 458 << "DTW produced " << alignment.size() << " points:" << endl;
440 for (int i = 0; i < alignment.size() && i < 100; ++i) { 459 for (int i = 0; i < alignment.size() && i < 100; ++i) {
441 SVCERR << alignment[i] << " "; 460 SVCERR << alignment[i] << " ";
442 } 461 }
443 SVCERR << endl; 462 SVCERR << endl;
463 #endif
444 464
445 alignmentModel->setPath(makePath(alignment, 465 alignmentModel->setPath(makePath(alignment,
446 refFrames, 466 refFrames,
447 otherFrames, 467 otherFrames,
448 alignmentModel->getSampleRate(), 468 alignmentModel->getSampleRate(),
449 resolution)); 469 resolution));
450 470
451 alignmentModel->setCompletion(100); 471 alignmentModel->setCompletion(100);
452 472
453 SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: Done" 473 SVCERR << "TransformDTWAligner[" << this
454 << endl; 474 << "]: performAlignmentRiseFall: Done" << endl;
455 475
456 m_incomplete = false; 476 m_incomplete = false;
457 return true; 477 return true;
458 } 478 }