annotate data/model/Model.cpp @ 1761:ee7fd2c01d87 by-id

Work on management of alignment-related models
author Chris Cannam
date Fri, 12 Jul 2019 13:57:42 +0100
parents 83178b4bb698
children aa0b56d72f27
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@1761 50 this, SIGNAL(alignmentCompletionChanged(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@1758 57 this, SIGNAL(alignmentCompletionChanged(ModelId)));
Chris@1018 58 }
Chris@319 59 }
Chris@319 60
Chris@1735 61 const ModelId
Chris@407 62 Model::getAlignment() const
Chris@407 63 {
Chris@1735 64 return m_alignmentModel;
Chris@407 65 }
Chris@407 66
Chris@1735 67 const ModelId
Chris@319 68 Model::getAlignmentReference() const
Chris@319 69 {
Chris@1735 70 auto model = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1735 71 if (model) return model->getReferenceModel();
Chris@1735 72 else return {};
Chris@319 73 }
Chris@319 74
Chris@1038 75 sv_frame_t
Chris@1038 76 Model::alignToReference(sv_frame_t frame) const
Chris@319 77 {
Chris@1739 78 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 79
Chris@1739 80 if (!alignmentModel) {
Chris@1739 81 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 82 if (sourceModel) {
Chris@1739 83 return sourceModel->alignToReference(frame);
Chris@1739 84 }
Chris@1739 85 return frame;
Chris@333 86 }
Chris@1739 87
Chris@1739 88 sv_frame_t refFrame = alignmentModel->toReference(frame);
Chris@1739 89 auto refModel = ModelById::get(alignmentModel->getReferenceModel());
Chris@1739 90 if (refModel && refFrame > refModel->getEndFrame()) {
Chris@1739 91 refFrame = refModel->getEndFrame();
Chris@1739 92 }
Chris@333 93 return refFrame;
Chris@319 94 }
Chris@319 95
Chris@1038 96 sv_frame_t
Chris@1038 97 Model::alignFromReference(sv_frame_t refFrame) const
Chris@319 98 {
Chris@1739 99 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 100
Chris@1739 101 if (!alignmentModel) {
Chris@1739 102 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 103 if (sourceModel) {
Chris@1739 104 return sourceModel->alignFromReference(refFrame);
Chris@1739 105 }
Chris@1739 106 return refFrame;
Chris@333 107 }
Chris@1739 108
Chris@1739 109 sv_frame_t frame = alignmentModel->fromReference(refFrame);
Chris@340 110 if (frame > getEndFrame()) frame = getEndFrame();
Chris@333 111 return frame;
Chris@319 112 }
Chris@319 113
Chris@319 114 int
Chris@319 115 Model::getAlignmentCompletion() const
Chris@319 116 {
Chris@1696 117 #ifdef DEBUG_COMPLETION
Chris@1739 118 SVCERR << "Model(" << this
Chris@1739 119 << ")::getAlignmentCompletion: m_alignmentModel = "
Chris@1739 120 << m_alignmentModel << endl;
Chris@1696 121 #endif
Chris@1739 122
Chris@1739 123 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 124
Chris@1739 125 if (!alignmentModel) {
Chris@1739 126 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 127 if (sourceModel) {
Chris@1739 128 return sourceModel->getAlignmentCompletion();
Chris@1739 129 }
Chris@1739 130 return 100;
Chris@333 131 }
Chris@1739 132
Chris@319 133 int completion = 0;
Chris@1739 134 (void)alignmentModel->isReady(&completion);
Chris@1696 135 #ifdef DEBUG_COMPLETION
Chris@1739 136 SVCERR << "Model(" << this
Chris@1739 137 << ")::getAlignmentCompletion: completion = " << completion
Chris@1696 138 << endl;
Chris@1696 139 #endif
Chris@319 140 return completion;
Chris@319 141 }
Chris@319 142
Chris@333 143 QString
Chris@333 144 Model::getTitle() const
Chris@333 145 {
Chris@1739 146 auto source = ModelById::get(m_sourceModel);
Chris@1739 147 if (source) return source->getTitle();
Chris@345 148 else return "";
Chris@333 149 }
Chris@333 150
Chris@333 151 QString
Chris@333 152 Model::getMaker() const
Chris@333 153 {
Chris@1739 154 auto source = ModelById::get(m_sourceModel);
Chris@1739 155 if (source) return source->getMaker();
Chris@345 156 else return "";
Chris@345 157 }
Chris@345 158
Chris@345 159 QString
Chris@345 160 Model::getLocation() const
Chris@345 161 {
Chris@1739 162 auto source = ModelById::get(m_sourceModel);
Chris@1739 163 if (source) return source->getLocation();
Chris@345 164 else return "";
Chris@333 165 }
Chris@333 166
Chris@319 167 void
Chris@150 168 Model::toXml(QTextStream &stream, QString indent,
Chris@150 169 QString extraAttributes) const
Chris@150 170 {
Chris@150 171 stream << indent;
Chris@150 172 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@1677 173 .arg(getExportId())
Chris@1429 174 .arg(encodeEntities(objectName()))
Chris@1429 175 .arg(getSampleRate())
Chris@1429 176 .arg(getStartFrame())
Chris@1429 177 .arg(getEndFrame())
Chris@1429 178 .arg(extraAttributes);
Chris@150 179 }
Chris@150 180
Chris@150 181