annotate data/model/Model.cpp @ 1784:4eac4bf35b45

More graceful handling of failure to construct FFT models in the case where the source model has already been deleted before this occurs
author Chris Cannam
date Tue, 17 Sep 2019 11:21:33 +0100
parents aa0b56d72f27
children 13bd41bd8a17
rev   line source
Chris@150 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@150 2
Chris@150 3 /*
Chris@150 4 Sonic Visualiser
Chris@150 5 An audio file viewer and annotation editor.
Chris@150 6 Centre for Digital Music, Queen Mary, University of London.
Chris@150 7 This file copyright 2006 Chris Cannam.
Chris@150 8
Chris@150 9 This program is free software; you can redistribute it and/or
Chris@150 10 modify it under the terms of the GNU General Public License as
Chris@150 11 published by the Free Software Foundation; either version 2 of the
Chris@150 12 License, or (at your option) any later version. See the file
Chris@150 13 COPYING included with this distribution for more information.
Chris@150 14 */
Chris@150 15
Chris@150 16 #include "Model.h"
Chris@319 17 #include "AlignmentModel.h"
Chris@150 18
Chris@150 19 #include <QTextStream>
Chris@150 20
Chris@150 21 #include <iostream>
Chris@150 22
Chris@1696 23 //#define DEBUG_COMPLETION 1
Chris@1696 24
Chris@150 25 Model::~Model()
Chris@150 26 {
Chris@1739 27 SVDEBUG << "Model::~Model: " << this << " with id " << getId() << endl;
Chris@150 28 }
Chris@150 29
Chris@150 30 void
Chris@1735 31 Model::setSourceModel(ModelId modelId)
Chris@319 32 {
Chris@1735 33 m_sourceModel = modelId;
Chris@319 34
Chris@1735 35 auto model = ModelById::get(m_sourceModel);
Chris@1735 36 if (model) {
Chris@1752 37 connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)),
Chris@1752 38 this, SIGNAL(alignmentCompletionChanged(ModelId)));
Chris@1735 39 }
Chris@319 40 }
Chris@319 41
Chris@319 42 void
Chris@1735 43 Model::setAlignment(ModelId alignmentModel)
Chris@319 44 {
Chris@1668 45 SVDEBUG << "Model(" << this << "): accepting alignment model "
Chris@1735 46 << alignmentModel << endl;
Chris@1761 47
Chris@1761 48 if (auto model = ModelById::get(m_alignmentModel)) {
Chris@1761 49 disconnect(model.get(), SIGNAL(completionChanged(ModelId)),
Chris@1767 50 this, SLOT(alignmentModelCompletionChanged(ModelId)));
Chris@319 51 }
Chris@1018 52
Chris@1735 53 m_alignmentModel = alignmentModel;
Chris@1018 54
Chris@1761 55 if (auto model = ModelById::get(m_alignmentModel)) {
Chris@1758 56 connect(model.get(), SIGNAL(completionChanged(ModelId)),
Chris@1767 57 this, SLOT(alignmentModelCompletionChanged(ModelId)));
Chris@1018 58 }
Chris@319 59 }
Chris@319 60
Chris@1767 61 void
Chris@1767 62 Model::alignmentModelCompletionChanged(ModelId)
Chris@1767 63 {
Chris@1767 64 emit alignmentCompletionChanged(getId());
Chris@1767 65 }
Chris@1767 66
Chris@1735 67 const ModelId
Chris@407 68 Model::getAlignment() const
Chris@407 69 {
Chris@1735 70 return m_alignmentModel;
Chris@407 71 }
Chris@407 72
Chris@1735 73 const ModelId
Chris@319 74 Model::getAlignmentReference() const
Chris@319 75 {
Chris@1735 76 auto model = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1735 77 if (model) return model->getReferenceModel();
Chris@1735 78 else return {};
Chris@319 79 }
Chris@319 80
Chris@1038 81 sv_frame_t
Chris@1038 82 Model::alignToReference(sv_frame_t frame) const
Chris@319 83 {
Chris@1739 84 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 85
Chris@1739 86 if (!alignmentModel) {
Chris@1739 87 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 88 if (sourceModel) {
Chris@1739 89 return sourceModel->alignToReference(frame);
Chris@1739 90 }
Chris@1739 91 return frame;
Chris@333 92 }
Chris@1739 93
Chris@1739 94 sv_frame_t refFrame = alignmentModel->toReference(frame);
Chris@1739 95 auto refModel = ModelById::get(alignmentModel->getReferenceModel());
Chris@1739 96 if (refModel && refFrame > refModel->getEndFrame()) {
Chris@1739 97 refFrame = refModel->getEndFrame();
Chris@1739 98 }
Chris@333 99 return refFrame;
Chris@319 100 }
Chris@319 101
Chris@1038 102 sv_frame_t
Chris@1038 103 Model::alignFromReference(sv_frame_t refFrame) const
Chris@319 104 {
Chris@1739 105 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 106
Chris@1739 107 if (!alignmentModel) {
Chris@1739 108 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 109 if (sourceModel) {
Chris@1739 110 return sourceModel->alignFromReference(refFrame);
Chris@1739 111 }
Chris@1739 112 return refFrame;
Chris@333 113 }
Chris@1739 114
Chris@1739 115 sv_frame_t frame = alignmentModel->fromReference(refFrame);
Chris@340 116 if (frame > getEndFrame()) frame = getEndFrame();
Chris@333 117 return frame;
Chris@319 118 }
Chris@319 119
Chris@319 120 int
Chris@319 121 Model::getAlignmentCompletion() const
Chris@319 122 {
Chris@1696 123 #ifdef DEBUG_COMPLETION
Chris@1739 124 SVCERR << "Model(" << this
Chris@1739 125 << ")::getAlignmentCompletion: m_alignmentModel = "
Chris@1739 126 << m_alignmentModel << endl;
Chris@1696 127 #endif
Chris@1739 128
Chris@1739 129 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 130
Chris@1739 131 if (!alignmentModel) {
Chris@1739 132 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 133 if (sourceModel) {
Chris@1739 134 return sourceModel->getAlignmentCompletion();
Chris@1739 135 }
Chris@1739 136 return 100;
Chris@333 137 }
Chris@1739 138
Chris@319 139 int completion = 0;
Chris@1739 140 (void)alignmentModel->isReady(&completion);
Chris@1696 141 #ifdef DEBUG_COMPLETION
Chris@1739 142 SVCERR << "Model(" << this
Chris@1739 143 << ")::getAlignmentCompletion: completion = " << completion
Chris@1696 144 << endl;
Chris@1696 145 #endif
Chris@319 146 return completion;
Chris@319 147 }
Chris@319 148
Chris@333 149 QString
Chris@333 150 Model::getTitle() const
Chris@333 151 {
Chris@1739 152 auto source = ModelById::get(m_sourceModel);
Chris@1739 153 if (source) return source->getTitle();
Chris@345 154 else return "";
Chris@333 155 }
Chris@333 156
Chris@333 157 QString
Chris@333 158 Model::getMaker() const
Chris@333 159 {
Chris@1739 160 auto source = ModelById::get(m_sourceModel);
Chris@1739 161 if (source) return source->getMaker();
Chris@345 162 else return "";
Chris@345 163 }
Chris@345 164
Chris@345 165 QString
Chris@345 166 Model::getLocation() const
Chris@345 167 {
Chris@1739 168 auto source = ModelById::get(m_sourceModel);
Chris@1739 169 if (source) return source->getLocation();
Chris@345 170 else return "";
Chris@333 171 }
Chris@333 172
Chris@319 173 void
Chris@150 174 Model::toXml(QTextStream &stream, QString indent,
Chris@150 175 QString extraAttributes) const
Chris@150 176 {
Chris@150 177 stream << indent;
Chris@150 178 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@1677 179 .arg(getExportId())
Chris@1429 180 .arg(encodeEntities(objectName()))
Chris@1429 181 .arg(getSampleRate())
Chris@1429 182 .arg(getStartFrame())
Chris@1429 183 .arg(getEndFrame())
Chris@1429 184 .arg(extraAttributes);
Chris@150 185 }
Chris@150 186
Chris@150 187