annotate data/model/Model.cpp @ 1754:8529763e2258 by-id

Tidy
author Chris Cannam
date Fri, 05 Jul 2019 16:55:27 +0100
parents 6d09d68165a4
children 83178b4bb698
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@1739 28 //!!! see notes in header - sort this out
Chris@1739 29 /*
Chris@1735 30 if (!m_alignmentModel.isNone()) {
Chris@1735 31 ModelById::release(m_alignmentModel);
Chris@319 32 }
Chris@1739 33 */
Chris@150 34 }
Chris@150 35
Chris@150 36 void
Chris@1735 37 Model::setSourceModel(ModelId modelId)
Chris@319 38 {
Chris@1735 39 m_sourceModel = modelId;
Chris@319 40
Chris@1735 41 auto model = ModelById::get(m_sourceModel);
Chris@1735 42 if (model) {
Chris@1752 43 connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)),
Chris@1752 44 this, SIGNAL(alignmentCompletionChanged(ModelId)));
Chris@1735 45 }
Chris@319 46 }
Chris@319 47
Chris@319 48 void
Chris@1735 49 Model::setAlignment(ModelId alignmentModel)
Chris@319 50 {
Chris@1668 51 SVDEBUG << "Model(" << this << "): accepting alignment model "
Chris@1735 52 << alignmentModel << endl;
Chris@1668 53
Chris@1735 54 if (!m_alignmentModel.isNone()) {
Chris@1735 55 ModelById::release(m_alignmentModel);
Chris@319 56 }
Chris@1018 57
Chris@1735 58 m_alignmentModel = alignmentModel;
Chris@1018 59
Chris@1735 60 auto model = ModelById::get(m_alignmentModel);
Chris@1735 61 if (model) {
Chris@1735 62 connect(model.get(), SIGNAL(completionChanged()),
Chris@1018 63 this, SIGNAL(alignmentCompletionChanged()));
Chris@1018 64 }
Chris@319 65 }
Chris@319 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