1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "TransformDTWAligner.h"
#include "DTW.h"
#include "data/model/SparseTimeValueModel.h"
#include "data/model/NoteModel.h"
#include "data/model/RangeSummarisableTimeValueModel.h"
#include "data/model/AlignmentModel.h"
#include "data/model/AggregateWaveModel.h"
#include "framework/Document.h"
#include "transform/ModelTransformerFactory.h"
#include "transform/FeatureExtractionModelTransformer.h"
#include <QSettings>
#include <QMutex>
#include <QMutexLocker>
namespace sv {
using std::vector;
static
TransformDTWAligner::MagnitudePreprocessor identityMagnitudePreprocessor =
[](double x) {
return x;
};
static
TransformDTWAligner::RiseFallPreprocessor identityRiseFallPreprocessor =
[](double prev, double curr) {
if (prev == curr) {
return RiseFallDTW::Value({ RiseFallDTW::Direction::None, 0.0 });
} else if (curr > prev) {
return RiseFallDTW::Value({ RiseFallDTW::Direction::Up, curr - prev });
} else {
return RiseFallDTW::Value({ RiseFallDTW::Direction::Down, prev - curr });
}
};
QMutex
TransformDTWAligner::m_dtwMutex;
TransformDTWAligner::TransformDTWAligner(Document *doc,
ModelId reference,
ModelId toAlign,
bool subsequence,
Transform transform,
DTWType dtwType) :
m_document(doc),
m_reference(reference),
m_toAlign(toAlign),
m_transform(transform),
m_dtwType(dtwType),
m_subsequence(subsequence),
m_incomplete(true),
m_magnitudePreprocessor(identityMagnitudePreprocessor),
m_riseFallPreprocessor(identityRiseFallPreprocessor)
{
}
TransformDTWAligner::TransformDTWAligner(Document *doc,
ModelId reference,
ModelId toAlign,
bool subsequence,
Transform transform,
MagnitudePreprocessor outputPreprocessor) :
m_document(doc),
m_reference(reference),
m_toAlign(toAlign),
m_transform(transform),
m_dtwType(Magnitude),
m_subsequence(subsequence),
m_incomplete(true),
m_magnitudePreprocessor(outputPreprocessor),
m_riseFallPreprocessor(identityRiseFallPreprocessor)
{
}
TransformDTWAligner::TransformDTWAligner(Document *doc,
ModelId reference,
ModelId toAlign,
bool subsequence,
Transform transform,
RiseFallPreprocessor outputPreprocessor) :
m_document(doc),
m_reference(reference),
m_toAlign(toAlign),
m_transform(transform),
m_dtwType(RiseFall),
m_subsequence(subsequence),
m_incomplete(true),
m_magnitudePreprocessor(identityMagnitudePreprocessor),
m_riseFallPreprocessor(outputPreprocessor)
{
}
TransformDTWAligner::~TransformDTWAligner()
{
if (m_incomplete) {
if (auto toAlign = ModelById::get(m_toAlign)) {
toAlign->setAlignment({});
}
}
ModelById::release(m_referenceOutputModel);
ModelById::release(m_toAlignOutputModel);
}
bool
TransformDTWAligner::isAvailable()
{
//!!! needs to be isAvailable(QString transformId)?
return true;
}
void
TransformDTWAligner::begin()
{
auto reference =
ModelById::getAs<RangeSummarisableTimeValueModel>(m_reference);
auto toAlign =
ModelById::getAs<RangeSummarisableTimeValueModel>(m_toAlign);
if (!reference || !toAlign) return;
SVCERR << "TransformDTWAligner[" << this << "]: begin(): aligning "
<< m_toAlign << " against reference " << m_reference << endl;
ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
QString message;
m_referenceOutputModel = mtf->transform(m_transform, m_reference, message);
auto referenceOutputModel = ModelById::get(m_referenceOutputModel);
if (!referenceOutputModel) {
SVCERR << "Align::alignModel: ERROR: Failed to create reference output model (no plugin?)" << endl;
emit failed(m_toAlign, message);
return;
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id "
<< m_transform.getIdentifier()
<< " is running on reference model" << endl;
#endif
message = "";
m_toAlignOutputModel = mtf->transform(m_transform, m_toAlign, message);
auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel);
if (!toAlignOutputModel) {
SVCERR << "Align::alignModel: ERROR: Failed to create toAlign output model (no plugin?)" << endl;
emit failed(m_toAlign, message);
return;
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: begin(): transform id "
<< m_transform.getIdentifier()
<< " is running on toAlign model" << endl;
#endif
connect(referenceOutputModel.get(), SIGNAL(completionChanged(ModelId)),
this, SLOT(completionChanged(ModelId)));
connect(toAlignOutputModel.get(), SIGNAL(completionChanged(ModelId)),
this, SLOT(completionChanged(ModelId)));
auto alignmentModel = std::make_shared<AlignmentModel>
(m_reference, m_toAlign, ModelId());
m_alignmentModel = ModelById::add(alignmentModel);
toAlign->setAlignment(m_alignmentModel);
m_document->addNonDerivedModel(m_alignmentModel);
// we wouldn't normally expect these to be true here, but...
int completion = 0;
if (referenceOutputModel->isReady(&completion) &&
toAlignOutputModel->isReady(&completion)) {
SVCERR << "TransformDTWAligner[" << this << "]: begin(): output models "
<< "are ready already! calling performAlignment" << endl;
if (performAlignment()) {
emit complete(m_alignmentModel);
} else {
emit failed(m_toAlign, tr("Failed to calculate alignment using DTW"));
}
}
}
void
TransformDTWAligner::completionChanged(ModelId
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
id
#endif
)
{
if (!m_incomplete) {
return;
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: "
<< "model " << id << endl;
#endif
auto referenceOutputModel = ModelById::get(m_referenceOutputModel);
auto toAlignOutputModel = ModelById::get(m_toAlignOutputModel);
auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
if (!referenceOutputModel || !toAlignOutputModel || !alignmentModel) {
return;
}
int referenceCompletion = 0, toAlignCompletion = 0;
bool referenceReady = referenceOutputModel->isReady(&referenceCompletion);
bool toAlignReady = toAlignOutputModel->isReady(&toAlignCompletion);
if (referenceReady && toAlignReady) {
SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: "
<< "both models ready, calling performAlignment" << endl;
alignmentModel->setCompletion(95);
if (performAlignment()) {
emit complete(m_alignmentModel);
} else {
emit failed(m_toAlign, tr("Alignment of transform outputs failed"));
}
} else {
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: completionChanged: "
<< "not ready yet: reference completion " << referenceCompletion
<< ", toAlign completion " << toAlignCompletion << endl;
#endif
int completion = std::min(referenceCompletion,
toAlignCompletion);
completion = (completion * 94) / 100;
alignmentModel->setCompletion(completion);
}
}
bool
TransformDTWAligner::performAlignment()
{
if (m_dtwType == Magnitude) {
return performAlignmentMagnitude();
} else {
return performAlignmentRiseFall();
}
}
bool
TransformDTWAligner::getValuesFrom(ModelId modelId,
vector<sv_frame_t> &frames,
vector<double> &values,
sv_frame_t &resolution)
{
EventVector events;
if (auto model = ModelById::getAs<SparseTimeValueModel>(modelId)) {
resolution = model->getResolution();
events = model->getAllEvents();
} else if (auto model = ModelById::getAs<NoteModel>(modelId)) {
resolution = model->getResolution();
events = model->getAllEvents();
} else {
SVCERR << "TransformDTWAligner::getValuesFrom: Type of model "
<< modelId << " is not supported" << endl;
return false;
}
frames.clear();
values.clear();
for (auto e: events) {
frames.push_back(e.getFrame());
values.push_back(e.getValue());
}
return true;
}
Path
TransformDTWAligner::makePath(const vector<size_t> &alignment,
const vector<sv_frame_t> &refFrames,
const vector<sv_frame_t> &otherFrames,
sv_samplerate_t sampleRate,
sv_frame_t resolution)
{
Path path(sampleRate, int(resolution));
path.add(PathPoint(0, 0));
for (int i = 0; in_range_for(alignment, i); ++i) {
// DTW returns "the index into s1 for each element in s2"
sv_frame_t alignedFrame = otherFrames[i];
if (!in_range_for(refFrames, alignment[i])) {
SVCERR << "TransformDTWAligner::makePath: Internal error: "
<< "DTW maps index " << i << " in other frame vector "
<< "(size " << otherFrames.size() << ") onto index "
<< alignment[i] << " in ref frame vector "
<< "(only size " << refFrames.size() << ")" << endl;
continue;
}
sv_frame_t refFrame = refFrames[alignment[i]];
path.add(PathPoint(alignedFrame, refFrame));
}
return path;
}
bool
TransformDTWAligner::performAlignmentMagnitude()
{
auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
if (!alignmentModel) {
return false;
}
vector<sv_frame_t> refFrames, otherFrames;
vector<double> refValues, otherValues;
sv_frame_t resolution = 0;
if (!getValuesFrom(m_referenceOutputModel,
refFrames, refValues, resolution)) {
return false;
}
if (!getValuesFrom(m_toAlignOutputModel,
otherFrames, otherValues, resolution)) {
return false;
}
vector<double> s1, s2;
for (double v: refValues) {
s1.push_back(m_magnitudePreprocessor(v));
}
for (double v: otherValues) {
s2.push_back(m_magnitudePreprocessor(v));
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: "
<< "Have " << s1.size() << " events from reference, "
<< s2.size() << " from toAlign" << endl;
#endif
MagnitudeDTW dtw;
vector<size_t> alignment;
{
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this
<< "]: serialising DTW to avoid over-allocation" << endl;
#endif
QMutexLocker locker(&m_dtwMutex);
if (m_subsequence) {
alignment = dtw.alignSubsequence(s1, s2);
} else {
alignment = dtw.alignSequences(s1, s2);
}
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentMagnitude: "
<< "DTW produced " << alignment.size() << " points:" << endl;
for (int i = 0; in_range_for(alignment, i) && i < 100; ++i) {
SVCERR << alignment[i] << " ";
}
SVCERR << endl;
#endif
alignmentModel->setPath(makePath(alignment,
refFrames,
otherFrames,
alignmentModel->getSampleRate(),
resolution));
alignmentModel->setCompletion(100);
SVCERR << "TransformDTWAligner[" << this
<< "]: performAlignmentMagnitude: Done" << endl;
m_incomplete = false;
return true;
}
bool
TransformDTWAligner::performAlignmentRiseFall()
{
auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
if (!alignmentModel) {
return false;
}
vector<sv_frame_t> refFrames, otherFrames;
vector<double> refValues, otherValues;
sv_frame_t resolution = 0;
if (!getValuesFrom(m_referenceOutputModel,
refFrames, refValues, resolution)) {
return false;
}
if (!getValuesFrom(m_toAlignOutputModel,
otherFrames, otherValues, resolution)) {
return false;
}
auto preprocess =
[this](const std::vector<double> &vv) {
vector<RiseFallDTW::Value> s;
double prev = 0.0;
for (auto curr: vv) {
s.push_back(m_riseFallPreprocessor(prev, curr));
prev = curr;
}
return s;
};
vector<RiseFallDTW::Value> s1 = preprocess(refValues);
vector<RiseFallDTW::Value> s2 = preprocess(otherValues);
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: "
<< "Have " << s1.size() << " events from reference, "
<< s2.size() << " from toAlign" << endl;
SVCERR << "Reference:" << endl;
for (int i = 0; in_range_for(s1, i) && i < 100; ++i) {
SVCERR << s1[i] << " ";
}
SVCERR << endl;
SVCERR << "toAlign:" << endl;
for (int i = 0; in_range_for(s2, i) && i < 100; ++i) {
SVCERR << s2[i] << " ";
}
SVCERR << endl;
#endif
RiseFallDTW dtw;
vector<size_t> alignment;
{
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this
<< "]: serialising DTW to avoid over-allocation" << endl;
#endif
QMutexLocker locker(&m_dtwMutex);
if (m_subsequence) {
alignment = dtw.alignSubsequence(s1, s2);
} else {
alignment = dtw.alignSequences(s1, s2);
}
}
#ifdef DEBUG_TRANSFORM_DTW_ALIGNER
SVCERR << "TransformDTWAligner[" << this << "]: performAlignmentRiseFall: "
<< "DTW produced " << alignment.size() << " points:" << endl;
for (int i = 0; i < alignment.size() && i < 100; ++i) {
SVCERR << alignment[i] << " ";
}
SVCERR << endl;
#endif
alignmentModel->setPath(makePath(alignment,
refFrames,
otherFrames,
alignmentModel->getSampleRate(),
resolution));
alignmentModel->setCompletion(100);
SVCERR << "TransformDTWAligner[" << this
<< "]: performAlignmentRiseFall: Done" << endl;
m_incomplete = false;
return true;
}
} // end namespace sv
|