annotate data/model/Model.cpp @ 1705:28f9ff7864c6 single-point

Add error field to alignment model
author Chris Cannam
date Wed, 15 May 2019 17:51:43 +0100
parents 187c76c40c6f
children 601851995f4b
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@1668 27 SVDEBUG << "Model::~Model(" << this << ")" << endl;
Chris@150 28
Chris@319 29 if (!m_aboutToDelete) {
Chris@1384 30 SVDEBUG << "NOTE: Model(" << this << ", \""
Chris@1384 31 << objectName() << "\", type uri <"
Chris@1384 32 << m_typeUri << ">)::~Model(): Model deleted "
Chris@1384 33 << "with no aboutToDelete notification"
Chris@1384 34 << endl;
Chris@319 35 }
Chris@319 36
Chris@319 37 if (m_alignment) {
Chris@319 38 m_alignment->aboutToDelete();
Chris@319 39 delete m_alignment;
Chris@319 40 }
Chris@150 41 }
Chris@150 42
Chris@1500 43 int
Chris@1500 44 Model::getNextId()
Chris@1500 45 {
Chris@1500 46 static int nextId = 0;
Chris@1500 47 static QMutex mutex;
Chris@1500 48 QMutexLocker locker(&mutex);
Chris@1500 49 int i = nextId;
Chris@1500 50 if (nextId == INT_MAX) {
Chris@1500 51 nextId = INT_MIN;
Chris@1500 52 }
Chris@1500 53 ++nextId;
Chris@1500 54 return i;
Chris@1500 55 }
Chris@1500 56
Chris@150 57 void
Chris@319 58 Model::setSourceModel(Model *model)
Chris@319 59 {
Chris@319 60 if (m_sourceModel) {
Chris@319 61 disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 62 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 63 }
Chris@319 64
Chris@319 65 m_sourceModel = model;
Chris@319 66
Chris@319 67 if (m_sourceModel) {
Chris@333 68 connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()),
Chris@333 69 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 70 connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 71 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 72 }
Chris@319 73 }
Chris@319 74
Chris@319 75 void
Chris@319 76 Model::aboutToDelete()
Chris@319 77 {
Chris@1668 78 SVDEBUG << "Model(" << this << ", \""
Chris@1668 79 << objectName() << "\", type name \""
Chris@1668 80 << getTypeName() << "\", type uri <"
Chris@1668 81 << m_typeUri << ">)::aboutToDelete()" << endl;
Chris@360 82
Chris@319 83 if (m_aboutToDelete) {
Chris@1384 84 SVDEBUG << "WARNING: Model(" << this << ", \""
Chris@1384 85 << objectName() << "\", type uri <"
Chris@1384 86 << m_typeUri << ">)::aboutToDelete: "
Chris@1384 87 << "aboutToDelete called more than once for the same model"
Chris@1384 88 << endl;
Chris@319 89 }
Chris@319 90
Chris@319 91 emit aboutToBeDeleted();
Chris@319 92 m_aboutToDelete = true;
Chris@319 93 }
Chris@319 94
Chris@319 95 void
Chris@319 96 Model::sourceModelAboutToBeDeleted()
Chris@319 97 {
Chris@1582 98 m_sourceModel = nullptr;
Chris@319 99 }
Chris@319 100
Chris@319 101 void
Chris@319 102 Model::setAlignment(AlignmentModel *alignment)
Chris@319 103 {
Chris@1668 104 SVDEBUG << "Model(" << this << "): accepting alignment model "
Chris@1668 105 << alignment << endl;
Chris@1668 106
Chris@319 107 if (m_alignment) {
Chris@319 108 m_alignment->aboutToDelete();
Chris@319 109 delete m_alignment;
Chris@319 110 }
Chris@1018 111
Chris@319 112 m_alignment = alignment;
Chris@1018 113
Chris@1018 114 if (m_alignment) {
Chris@1018 115 connect(m_alignment, SIGNAL(completionChanged()),
Chris@1018 116 this, SIGNAL(alignmentCompletionChanged()));
Chris@1018 117 }
Chris@319 118 }
Chris@319 119
Chris@407 120 const AlignmentModel *
Chris@407 121 Model::getAlignment() const
Chris@407 122 {
Chris@407 123 return m_alignment;
Chris@407 124 }
Chris@407 125
Chris@319 126 const Model *
Chris@319 127 Model::getAlignmentReference() const
Chris@319 128 {
Chris@333 129 if (!m_alignment) {
Chris@333 130 if (m_sourceModel) return m_sourceModel->getAlignmentReference();
Chris@1582 131 return nullptr;
Chris@333 132 }
Chris@319 133 return m_alignment->getReferenceModel();
Chris@319 134 }
Chris@319 135
Chris@1038 136 sv_frame_t
Chris@1038 137 Model::alignToReference(sv_frame_t frame) const
Chris@319 138 {
Chris@951 139 // cerr << "Model(" << this << ")::alignToReference(" << frame << ")" << endl;
Chris@333 140 if (!m_alignment) {
Chris@333 141 if (m_sourceModel) return m_sourceModel->alignToReference(frame);
Chris@333 142 else return frame;
Chris@333 143 }
Chris@1038 144 sv_frame_t refFrame = m_alignment->toReference(frame);
Chris@340 145 const Model *m = m_alignment->getReferenceModel();
Chris@340 146 if (m && refFrame > m->getEndFrame()) refFrame = m->getEndFrame();
Chris@951 147 // cerr << "have alignment, aligned is " << refFrame << endl;
Chris@333 148 return refFrame;
Chris@319 149 }
Chris@319 150
Chris@1038 151 sv_frame_t
Chris@1038 152 Model::alignFromReference(sv_frame_t refFrame) const
Chris@319 153 {
Chris@951 154 // cerr << "Model(" << this << ")::alignFromReference(" << refFrame << ")" << endl;
Chris@333 155 if (!m_alignment) {
Chris@333 156 if (m_sourceModel) return m_sourceModel->alignFromReference(refFrame);
Chris@333 157 else return refFrame;
Chris@333 158 }
Chris@1038 159 sv_frame_t frame = m_alignment->fromReference(refFrame);
Chris@340 160 if (frame > getEndFrame()) frame = getEndFrame();
Chris@951 161 // cerr << "have alignment, aligned is " << frame << endl;
Chris@333 162 return frame;
Chris@319 163 }
Chris@319 164
Chris@319 165 int
Chris@319 166 Model::getAlignmentCompletion() const
Chris@319 167 {
Chris@1696 168 #ifdef DEBUG_COMPLETION
Chris@1696 169 SVCERR << "Model(" << this << ")::getAlignmentCompletion: m_alignment = "
Chris@1696 170 << m_alignment << endl;
Chris@1696 171 #endif
Chris@333 172 if (!m_alignment) {
Chris@333 173 if (m_sourceModel) return m_sourceModel->getAlignmentCompletion();
Chris@333 174 else return 100;
Chris@333 175 }
Chris@319 176 int completion = 0;
Chris@319 177 (void)m_alignment->isReady(&completion);
Chris@1696 178 #ifdef DEBUG_COMPLETION
Chris@1696 179 SVCERR << "Model(" << this << ")::getAlignmentCompletion: completion = " << completion
Chris@1696 180 << endl;
Chris@1696 181 #endif
Chris@319 182 return completion;
Chris@319 183 }
Chris@319 184
Chris@333 185 QString
Chris@333 186 Model::getTitle() const
Chris@333 187 {
Chris@333 188 if (m_sourceModel) return m_sourceModel->getTitle();
Chris@345 189 else return "";
Chris@333 190 }
Chris@333 191
Chris@333 192 QString
Chris@333 193 Model::getMaker() const
Chris@333 194 {
Chris@333 195 if (m_sourceModel) return m_sourceModel->getMaker();
Chris@345 196 else return "";
Chris@345 197 }
Chris@345 198
Chris@345 199 QString
Chris@345 200 Model::getLocation() const
Chris@345 201 {
Chris@345 202 if (m_sourceModel) return m_sourceModel->getLocation();
Chris@345 203 else return "";
Chris@333 204 }
Chris@333 205
Chris@319 206 void
Chris@150 207 Model::toXml(QTextStream &stream, QString indent,
Chris@150 208 QString extraAttributes) const
Chris@150 209 {
Chris@150 210 stream << indent;
Chris@150 211 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@1677 212 .arg(getExportId())
Chris@1429 213 .arg(encodeEntities(objectName()))
Chris@1429 214 .arg(getSampleRate())
Chris@1429 215 .arg(getStartFrame())
Chris@1429 216 .arg(getEndFrame())
Chris@1429 217 .arg(extraAttributes);
Chris@150 218 }
Chris@150 219
Chris@150 220