annotate data/model/Model.cpp @ 398:be49bf95d4a5

* Fix hang when using more than one consecutive coded audio file reader in decode-at-once mode
author Chris Cannam
date Wed, 26 Mar 2008 14:35:03 +0000
parents 5858cc462d0a
children 88ad01799040
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@150 23 const int Model::COMPLETION_UNKNOWN = -1;
Chris@150 24
Chris@150 25 Model::~Model()
Chris@150 26 {
Chris@360 27 std::cerr << "Model::~Model(" << this << ")" << std::endl;
Chris@150 28
Chris@319 29 if (!m_aboutToDelete) {
Chris@319 30 std::cerr << "NOTE: Model::~Model(" << this << ", \""
Chris@319 31 << objectName().toStdString() << "\"): Model deleted "
Chris@319 32 << "with no aboutToDelete notification" << std::endl;
Chris@319 33 }
Chris@319 34
Chris@319 35 if (m_alignment) {
Chris@319 36 m_alignment->aboutToDelete();
Chris@319 37 delete m_alignment;
Chris@319 38 }
Chris@150 39 }
Chris@150 40
Chris@150 41 void
Chris@319 42 Model::setSourceModel(Model *model)
Chris@319 43 {
Chris@319 44 if (m_sourceModel) {
Chris@319 45 disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 46 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 47 }
Chris@319 48
Chris@319 49 m_sourceModel = model;
Chris@319 50
Chris@319 51 if (m_sourceModel) {
Chris@333 52 connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()),
Chris@333 53 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 54 connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 55 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 56 }
Chris@319 57 }
Chris@319 58
Chris@319 59 void
Chris@319 60 Model::aboutToDelete()
Chris@319 61 {
Chris@360 62 std::cerr << "Model(" << this << ")::aboutToDelete()" << std::endl;
Chris@360 63
Chris@319 64 if (m_aboutToDelete) {
Chris@319 65 std::cerr << "WARNING: Model(" << this << ", \""
Chris@319 66 << objectName().toStdString() << "\")::aboutToDelete: "
Chris@319 67 << "aboutToDelete called more than once for the same model"
Chris@319 68 << std::endl;
Chris@319 69 }
Chris@319 70
Chris@319 71 emit aboutToBeDeleted();
Chris@319 72 m_aboutToDelete = true;
Chris@319 73 }
Chris@319 74
Chris@319 75 void
Chris@319 76 Model::sourceModelAboutToBeDeleted()
Chris@319 77 {
Chris@319 78 m_sourceModel = 0;
Chris@319 79 }
Chris@319 80
Chris@319 81 void
Chris@319 82 Model::setAlignment(AlignmentModel *alignment)
Chris@319 83 {
Chris@319 84 if (m_alignment) {
Chris@319 85 m_alignment->aboutToDelete();
Chris@319 86 delete m_alignment;
Chris@319 87 }
Chris@319 88 m_alignment = alignment;
Chris@319 89 connect(m_alignment, SIGNAL(completionChanged()),
Chris@319 90 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 91 }
Chris@319 92
Chris@319 93 const Model *
Chris@319 94 Model::getAlignmentReference() const
Chris@319 95 {
Chris@333 96 if (!m_alignment) {
Chris@333 97 if (m_sourceModel) return m_sourceModel->getAlignmentReference();
Chris@371 98 return 0;
Chris@333 99 }
Chris@319 100 return m_alignment->getReferenceModel();
Chris@319 101 }
Chris@319 102
Chris@319 103 size_t
Chris@319 104 Model::alignToReference(size_t frame) const
Chris@319 105 {
Chris@333 106 if (!m_alignment) {
Chris@333 107 if (m_sourceModel) return m_sourceModel->alignToReference(frame);
Chris@333 108 else return frame;
Chris@333 109 }
Chris@333 110 size_t refFrame = m_alignment->toReference(frame);
Chris@340 111 const Model *m = m_alignment->getReferenceModel();
Chris@340 112 if (m && refFrame > m->getEndFrame()) refFrame = m->getEndFrame();
Chris@333 113 return refFrame;
Chris@319 114 }
Chris@319 115
Chris@319 116 size_t
Chris@319 117 Model::alignFromReference(size_t refFrame) const
Chris@319 118 {
Chris@333 119 if (!m_alignment) {
Chris@333 120 if (m_sourceModel) return m_sourceModel->alignFromReference(refFrame);
Chris@333 121 else return refFrame;
Chris@333 122 }
Chris@333 123 size_t frame = m_alignment->fromReference(refFrame);
Chris@340 124 if (frame > getEndFrame()) frame = getEndFrame();
Chris@333 125 return frame;
Chris@319 126 }
Chris@319 127
Chris@319 128 int
Chris@319 129 Model::getAlignmentCompletion() const
Chris@319 130 {
Chris@323 131 // std::cerr << "Model::getAlignmentCompletion" << std::endl;
Chris@333 132 if (!m_alignment) {
Chris@333 133 if (m_sourceModel) return m_sourceModel->getAlignmentCompletion();
Chris@333 134 else return 100;
Chris@333 135 }
Chris@319 136 int completion = 0;
Chris@319 137 (void)m_alignment->isReady(&completion);
Chris@323 138 // std::cerr << " -> " << completion << std::endl;
Chris@319 139 return completion;
Chris@319 140 }
Chris@319 141
Chris@333 142 QString
Chris@333 143 Model::getTitle() const
Chris@333 144 {
Chris@333 145 if (m_sourceModel) return m_sourceModel->getTitle();
Chris@345 146 else return "";
Chris@333 147 }
Chris@333 148
Chris@333 149 QString
Chris@333 150 Model::getMaker() const
Chris@333 151 {
Chris@333 152 if (m_sourceModel) return m_sourceModel->getMaker();
Chris@345 153 else return "";
Chris@345 154 }
Chris@345 155
Chris@345 156 QString
Chris@345 157 Model::getLocation() const
Chris@345 158 {
Chris@345 159 if (m_sourceModel) return m_sourceModel->getLocation();
Chris@345 160 else return "";
Chris@333 161 }
Chris@333 162
Chris@319 163 void
Chris@150 164 Model::toXml(QTextStream &stream, QString indent,
Chris@150 165 QString extraAttributes) const
Chris@150 166 {
Chris@150 167 stream << indent;
Chris@150 168 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@150 169 .arg(getObjectExportId(this))
Chris@150 170 .arg(encodeEntities(objectName()))
Chris@150 171 .arg(getSampleRate())
Chris@150 172 .arg(getStartFrame())
Chris@150 173 .arg(getEndFrame())
Chris@150 174 .arg(extraAttributes);
Chris@150 175 }
Chris@150 176
Chris@150 177