annotate align/Align.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 32e66fcc4cb7
rev   line source
Chris@420 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@420 2
Chris@420 3 /*
Chris@420 4 Sonic Visualiser
Chris@420 5 An audio file viewer and annotation editor.
Chris@420 6 Centre for Digital Music, Queen Mary, University of London.
Chris@420 7
Chris@420 8 This program is free software; you can redistribute it and/or
Chris@420 9 modify it under the terms of the GNU General Public License as
Chris@420 10 published by the Free Software Foundation; either version 2 of the
Chris@420 11 License, or (at your option) any later version. See the file
Chris@420 12 COPYING included with this distribution for more information.
Chris@420 13 */
Chris@420 14
Chris@420 15 #include "Align.h"
Chris@767 16
Chris@767 17 #include "LinearAligner.h"
Chris@753 18 #include "TransformAligner.h"
Chris@767 19 #include "TransformDTWAligner.h"
Chris@753 20 #include "ExternalProgramAligner.h"
Chris@767 21
Chris@744 22 #include "framework/Document.h"
Chris@420 23
Chris@767 24 #include "transform/Transform.h"
Chris@767 25 #include "transform/TransformFactory.h"
Chris@767 26
Chris@768 27 #include "base/Pitch.h"
Chris@768 28
Chris@422 29 #include <QSettings>
Chris@761 30 #include <QTimer>
Chris@422 31
Chris@767 32 using std::make_shared;
Chris@767 33
Chris@767 34 QString
Chris@767 35 Align::getAlignmentTypeTag(AlignmentType type)
Chris@767 36 {
Chris@767 37 switch (type) {
Chris@767 38 case NoAlignment:
Chris@767 39 default:
Chris@767 40 return "no-alignment";
Chris@767 41 case LinearAlignment:
Chris@767 42 return "linear-alignment";
Chris@767 43 case TrimmedLinearAlignment:
Chris@767 44 return "trimmed-linear-alignment";
Chris@767 45 case MATCHAlignment:
Chris@767 46 return "match-alignment";
Chris@767 47 case MATCHAlignmentWithPitchCompare:
Chris@767 48 return "match-alignment-with-pitch";
Chris@771 49 case SungNoteContourAlignment:
Chris@771 50 return "sung-note-alignment";
Chris@767 51 case TransformDrivenDTWAlignment:
Chris@767 52 return "transform-driven-alignment";
Chris@767 53 case ExternalProgramAlignment:
Chris@767 54 return "external-program-alignment";
Chris@767 55 }
Chris@767 56 }
Chris@767 57
Chris@767 58 Align::AlignmentType
Chris@767 59 Align::getAlignmentTypeForTag(QString tag)
Chris@767 60 {
Chris@767 61 for (int i = 0; i <= int(LastAlignmentType); ++i) {
Chris@767 62 if (tag == getAlignmentTypeTag(AlignmentType(i))) {
Chris@767 63 return AlignmentType(i);
Chris@767 64 }
Chris@767 65 }
Chris@767 66 return NoAlignment;
Chris@767 67 }
Chris@767 68
Chris@761 69 void
Chris@753 70 Align::alignModel(Document *doc,
Chris@753 71 ModelId reference,
Chris@761 72 ModelId toAlign)
Chris@761 73 {
Chris@767 74 if (addAligner(doc, reference, toAlign)) {
Chris@767 75 m_aligners[toAlign]->begin();
Chris@767 76 }
Chris@761 77 }
Chris@761 78
Chris@761 79 void
Chris@761 80 Align::scheduleAlignment(Document *doc,
Chris@761 81 ModelId reference,
Chris@761 82 ModelId toAlign)
Chris@761 83 {
Chris@767 84 int delay = 700 * int(m_aligners.size());
Chris@761 85 if (delay > 3500) {
Chris@761 86 delay = 3500;
Chris@761 87 }
Chris@767 88 if (!addAligner(doc, reference, toAlign)) {
Chris@767 89 return;
Chris@767 90 }
Chris@761 91 SVCERR << "Align::scheduleAlignment: delaying " << delay << "ms" << endl;
Chris@761 92 QTimer::singleShot(delay, m_aligners[toAlign].get(), SLOT(begin()));
Chris@761 93 }
Chris@761 94
Chris@767 95 bool
Chris@761 96 Align::addAligner(Document *doc,
Chris@761 97 ModelId reference,
Chris@761 98 ModelId toAlign)
Chris@753 99 {
Chris@767 100 QString additionalData;
Chris@767 101 AlignmentType type = getAlignmentPreference(additionalData);
Chris@753 102
Chris@753 103 std::shared_ptr<Aligner> aligner;
Chris@753 104
Chris@773 105 if (m_aligners.find(toAlign) != m_aligners.end()) {
Chris@773 106 // We don't want a callback on removeAligner to happen during
Chris@773 107 // our own call to addAligner! Disconnect and delete the old
Chris@773 108 // aligner first
Chris@773 109 disconnect(m_aligners[toAlign].get(), nullptr, this, nullptr);
Chris@773 110 m_aligners.erase(toAlign);
Chris@773 111 }
Chris@773 112
Chris@753 113 {
Chris@753 114 // Replace the aligner with a new one. This also stops any
Chris@753 115 // previously-running alignment, when the old entry is
Chris@753 116 // replaced and its aligner destroyed.
Chris@753 117
Chris@753 118 QMutexLocker locker(&m_mutex);
Chris@767 119
Chris@767 120 switch (type) {
Chris@767 121
Chris@767 122 case NoAlignment:
Chris@767 123 return false;
Chris@767 124
Chris@767 125 case LinearAlignment:
Chris@767 126 case TrimmedLinearAlignment: {
Chris@767 127 bool trimmed = (type == TrimmedLinearAlignment);
Chris@767 128 aligner = make_shared<LinearAligner>(doc,
Chris@767 129 reference,
Chris@767 130 toAlign,
Chris@767 131 trimmed);
Chris@767 132 break;
Chris@753 133 }
Chris@753 134
Chris@767 135 case MATCHAlignment:
Chris@767 136 case MATCHAlignmentWithPitchCompare: {
Chris@767 137
Chris@767 138 bool withTuningDifference =
Chris@767 139 (type == MATCHAlignmentWithPitchCompare);
Chris@767 140
Chris@767 141 aligner = make_shared<TransformAligner>(doc,
Chris@767 142 reference,
Chris@767 143 toAlign,
Chris@767 144 withTuningDifference);
Chris@767 145 break;
Chris@767 146 }
Chris@767 147
Chris@771 148 case SungNoteContourAlignment:
Chris@767 149 {
Chris@767 150 auto refModel = ModelById::get(reference);
Chris@767 151 if (!refModel) return false;
Chris@771 152
Chris@767 153 Transform transform = TransformFactory::getInstance()->
Chris@771 154 getDefaultTransformFor("vamp:pyin:pyin:notes",
Chris@767 155 refModel->getSampleRate());
Chris@767 156
Chris@767 157 aligner = make_shared<TransformDTWAligner>
Chris@767 158 (doc,
Chris@767 159 reference,
Chris@767 160 toAlign,
Chris@767 161 transform,
Chris@771 162 [](double prev, double curr) {
Chris@771 163 RiseFallDTW::Value v;
Chris@771 164 if (curr <= 0.0) {
Chris@771 165 v = { RiseFallDTW::Direction::None, 0.0 };
Chris@771 166 } else if (prev <= 0.0) {
Chris@771 167 v = { RiseFallDTW::Direction::Up, 0.0 };
Chris@768 168 } else {
Chris@771 169 double prevP = Pitch::getPitchForFrequency(prev);
Chris@771 170 double currP = Pitch::getPitchForFrequency(curr);
Chris@771 171 if (currP >= prevP) {
Chris@771 172 v = { RiseFallDTW::Direction::Up, currP - prevP };
Chris@771 173 } else {
Chris@771 174 v = { RiseFallDTW::Direction::Down, prevP - currP };
Chris@771 175 }
Chris@768 176 }
Chris@771 177 return v;
Chris@768 178 });
Chris@767 179 break;
Chris@767 180 }
Chris@767 181
Chris@767 182 case TransformDrivenDTWAlignment:
Chris@767 183 throw std::logic_error("Not yet implemented"); //!!!
Chris@767 184
Chris@767 185 case ExternalProgramAlignment: {
Chris@767 186 aligner = make_shared<ExternalProgramAligner>(doc,
Chris@767 187 reference,
Chris@767 188 toAlign,
Chris@767 189 additionalData);
Chris@767 190 }
Chris@767 191 }
Chris@767 192
Chris@767 193 m_aligners[toAlign] = aligner;
Chris@753 194 }
Chris@753 195
Chris@753 196 connect(aligner.get(), SIGNAL(complete(ModelId)),
Chris@753 197 this, SLOT(alignerComplete(ModelId)));
Chris@761 198
Chris@761 199 connect(aligner.get(), SIGNAL(failed(ModelId, QString)),
Chris@761 200 this, SLOT(alignerFailed(ModelId, QString)));
Chris@767 201
Chris@767 202 return true;
Chris@767 203 }
Chris@767 204
Chris@767 205 Align::AlignmentType
Chris@767 206 Align::getAlignmentPreference(QString &additionalData)
Chris@767 207 {
Chris@767 208 QSettings settings;
Chris@767 209 settings.beginGroup("Alignment");
Chris@767 210
Chris@767 211 QString tag = settings.value
Chris@767 212 ("alignment-type", getAlignmentTypeTag(MATCHAlignment)).toString();
Chris@767 213
Chris@767 214 AlignmentType type = getAlignmentTypeForTag(tag);
Chris@767 215
Chris@767 216 if (type == TransformDrivenDTWAlignment) {
Chris@767 217 additionalData = settings.value("alignment-transform", "").toString();
Chris@767 218 } else if (type == ExternalProgramAlignment) {
Chris@767 219 additionalData = settings.value("alignment-program", "").toString();
Chris@767 220 }
Chris@767 221
Chris@767 222 settings.endGroup();
Chris@767 223 return type;
Chris@753 224 }
Chris@753 225
Chris@753 226 void
Chris@767 227 Align::setAlignmentPreference(AlignmentType type, QString additionalData)
Chris@422 228 {
Chris@422 229 QSettings settings;
Chris@767 230 settings.beginGroup("Alignment");
Chris@767 231
Chris@767 232 QString tag = getAlignmentTypeTag(type);
Chris@767 233 settings.setValue("alignment-type", tag);
Chris@767 234
Chris@767 235 if (type == TransformDrivenDTWAlignment) {
Chris@767 236 settings.setValue("alignment-transform", additionalData);
Chris@767 237 } else if (type == ExternalProgramAlignment) {
Chris@767 238 settings.setValue("alignment-program", additionalData);
Chris@767 239 }
Chris@767 240
Chris@422 241 settings.endGroup();
Chris@670 242 }
Chris@670 243
Chris@428 244 bool
Chris@428 245 Align::canAlign()
Chris@428 246 {
Chris@767 247 QString additionalData;
Chris@767 248 AlignmentType type = getAlignmentPreference(additionalData);
Chris@753 249
Chris@767 250 if (type == ExternalProgramAlignment) {
Chris@767 251 return ExternalProgramAligner::isAvailable(additionalData);
Chris@753 252 } else {
Chris@753 253 return TransformAligner::isAvailable();
Chris@753 254 }
Chris@428 255 }
Chris@428 256
Chris@702 257 void
Chris@753 258 Align::alignerComplete(ModelId alignmentModel)
Chris@702 259 {
Chris@761 260 removeAligner(sender());
Chris@761 261 emit alignmentComplete(alignmentModel);
Chris@761 262 }
Chris@761 263
Chris@761 264 void
Chris@761 265 Align::alignerFailed(ModelId toAlign, QString error)
Chris@761 266 {
Chris@761 267 removeAligner(sender());
Chris@761 268 emit alignmentFailed(toAlign, error);
Chris@761 269 }
Chris@761 270
Chris@761 271 void
Chris@761 272 Align::removeAligner(QObject *obj)
Chris@761 273 {
Chris@761 274 Aligner *aligner = qobject_cast<Aligner *>(obj);
Chris@753 275 if (!aligner) {
Chris@761 276 SVCERR << "ERROR: Align::removeAligner: Not an Aligner" << endl;
Chris@702 277 return;
Chris@702 278 }
Chris@702 279
Chris@761 280 QMutexLocker locker (&m_mutex);
Chris@702 281
Chris@761 282 for (auto p: m_aligners) {
Chris@761 283 if (aligner == p.second.get()) {
Chris@761 284 m_aligners.erase(p.first);
Chris@761 285 break;
Chris@702 286 }
Chris@702 287 }
Chris@761 288 }
Chris@702 289