Chris@150: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@150: Chris@150: /* Chris@150: Sonic Visualiser Chris@150: An audio file viewer and annotation editor. Chris@150: Centre for Digital Music, Queen Mary, University of London. Chris@150: This file copyright 2006 Chris Cannam. Chris@150: Chris@150: This program is free software; you can redistribute it and/or Chris@150: modify it under the terms of the GNU General Public License as Chris@150: published by the Free Software Foundation; either version 2 of the Chris@150: License, or (at your option) any later version. See the file Chris@150: COPYING included with this distribution for more information. Chris@150: */ Chris@150: Chris@150: #include "Model.h" Chris@319: #include "AlignmentModel.h" Chris@150: Chris@150: #include Chris@150: Chris@150: #include Chris@150: Chris@1696: //#define DEBUG_COMPLETION 1 Chris@1696: Chris@150: Model::~Model() Chris@150: { Chris@1739: SVDEBUG << "Model::~Model: " << this << " with id " << getId() << endl; Chris@1739: /*!!! Chris@319: if (!m_aboutToDelete) { Chris@1384: SVDEBUG << "NOTE: Model(" << this << ", \"" Chris@1384: << objectName() << "\", type uri <" Chris@1384: << m_typeUri << ">)::~Model(): Model deleted " Chris@1384: << "with no aboutToDelete notification" Chris@1384: << endl; Chris@319: } Chris@1739: */ Chris@1739: //!!! see notes in header - sort this out Chris@1739: /* Chris@1735: if (!m_alignmentModel.isNone()) { Chris@1735: ModelById::release(m_alignmentModel); Chris@319: } Chris@1739: */ Chris@150: } Chris@150: Chris@150: void Chris@1735: Model::setSourceModel(ModelId modelId) Chris@319: { Chris@1735: /*!!! Chris@319: if (m_sourceModel) { Chris@319: disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()), Chris@319: this, SLOT(sourceModelAboutToBeDeleted())); Chris@319: } Chris@1735: */ Chris@1735: Chris@1735: m_sourceModel = modelId; Chris@319: Chris@1735: auto model = ModelById::get(m_sourceModel); Chris@1735: if (model) { Chris@1735: connect(model.get(), SIGNAL(alignmentCompletionChanged()), Chris@1735: this, SIGNAL(alignmentCompletionChanged())); Chris@1735: } Chris@1735: Chris@1735: Chris@1735: /* Chris@319: if (m_sourceModel) { Chris@333: connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()), Chris@333: this, SIGNAL(alignmentCompletionChanged())); Chris@319: connect(m_sourceModel, SIGNAL(aboutToBeDeleted()), Chris@319: this, SLOT(sourceModelAboutToBeDeleted())); Chris@319: } Chris@1735: */ Chris@319: } Chris@1735: /*!!! Chris@319: void Chris@319: Model::aboutToDelete() Chris@319: { Chris@1668: SVDEBUG << "Model(" << this << ", \"" Chris@1668: << objectName() << "\", type name \"" Chris@1668: << getTypeName() << "\", type uri <" Chris@1668: << m_typeUri << ">)::aboutToDelete()" << endl; Chris@360: Chris@319: if (m_aboutToDelete) { Chris@1384: SVDEBUG << "WARNING: Model(" << this << ", \"" Chris@1384: << objectName() << "\", type uri <" Chris@1384: << m_typeUri << ">)::aboutToDelete: " Chris@1384: << "aboutToDelete called more than once for the same model" Chris@1384: << endl; Chris@319: } Chris@319: Chris@319: emit aboutToBeDeleted(); Chris@319: m_aboutToDelete = true; Chris@319: } Chris@319: Chris@319: void Chris@319: Model::sourceModelAboutToBeDeleted() Chris@319: { Chris@1582: m_sourceModel = nullptr; Chris@319: } Chris@1735: */ Chris@319: void Chris@1735: Model::setAlignment(ModelId alignmentModel) Chris@319: { Chris@1668: SVDEBUG << "Model(" << this << "): accepting alignment model " Chris@1735: << alignmentModel << endl; Chris@1668: Chris@1735: if (!m_alignmentModel.isNone()) { Chris@1735: ModelById::release(m_alignmentModel); Chris@319: } Chris@1018: Chris@1735: m_alignmentModel = alignmentModel; Chris@1018: Chris@1735: auto model = ModelById::get(m_alignmentModel); Chris@1735: if (model) { Chris@1735: connect(model.get(), SIGNAL(completionChanged()), Chris@1018: this, SIGNAL(alignmentCompletionChanged())); Chris@1018: } Chris@319: } Chris@319: Chris@1735: const ModelId Chris@407: Model::getAlignment() const Chris@407: { Chris@1735: return m_alignmentModel; Chris@407: } Chris@407: Chris@1735: const ModelId Chris@319: Model::getAlignmentReference() const Chris@319: { Chris@1735: auto model = ModelById::getAs(m_alignmentModel); Chris@1735: if (model) return model->getReferenceModel(); Chris@1735: else return {}; Chris@319: } Chris@319: Chris@1038: sv_frame_t Chris@1038: Model::alignToReference(sv_frame_t frame) const Chris@319: { Chris@1739: auto alignmentModel = ModelById::getAs(m_alignmentModel); Chris@1739: Chris@1739: if (!alignmentModel) { Chris@1739: auto sourceModel = ModelById::get(m_sourceModel); Chris@1739: if (sourceModel) { Chris@1739: return sourceModel->alignToReference(frame); Chris@1739: } Chris@1739: return frame; Chris@333: } Chris@1739: Chris@1739: sv_frame_t refFrame = alignmentModel->toReference(frame); Chris@1739: auto refModel = ModelById::get(alignmentModel->getReferenceModel()); Chris@1739: if (refModel && refFrame > refModel->getEndFrame()) { Chris@1739: refFrame = refModel->getEndFrame(); Chris@1739: } Chris@333: return refFrame; Chris@319: } Chris@319: Chris@1038: sv_frame_t Chris@1038: Model::alignFromReference(sv_frame_t refFrame) const Chris@319: { Chris@1739: auto alignmentModel = ModelById::getAs(m_alignmentModel); Chris@1739: Chris@1739: if (!alignmentModel) { Chris@1739: auto sourceModel = ModelById::get(m_sourceModel); Chris@1739: if (sourceModel) { Chris@1739: return sourceModel->alignFromReference(refFrame); Chris@1739: } Chris@1739: return refFrame; Chris@333: } Chris@1739: Chris@1739: sv_frame_t frame = alignmentModel->fromReference(refFrame); Chris@340: if (frame > getEndFrame()) frame = getEndFrame(); Chris@333: return frame; Chris@319: } Chris@319: Chris@319: int Chris@319: Model::getAlignmentCompletion() const Chris@319: { Chris@1696: #ifdef DEBUG_COMPLETION Chris@1739: SVCERR << "Model(" << this Chris@1739: << ")::getAlignmentCompletion: m_alignmentModel = " Chris@1739: << m_alignmentModel << endl; Chris@1696: #endif Chris@1739: Chris@1739: auto alignmentModel = ModelById::getAs(m_alignmentModel); Chris@1739: Chris@1739: if (!alignmentModel) { Chris@1739: auto sourceModel = ModelById::get(m_sourceModel); Chris@1739: if (sourceModel) { Chris@1739: return sourceModel->getAlignmentCompletion(); Chris@1739: } Chris@1739: return 100; Chris@333: } Chris@1739: Chris@319: int completion = 0; Chris@1739: (void)alignmentModel->isReady(&completion); Chris@1696: #ifdef DEBUG_COMPLETION Chris@1739: SVCERR << "Model(" << this Chris@1739: << ")::getAlignmentCompletion: completion = " << completion Chris@1696: << endl; Chris@1696: #endif Chris@319: return completion; Chris@319: } Chris@319: Chris@333: QString Chris@333: Model::getTitle() const Chris@333: { Chris@1739: auto source = ModelById::get(m_sourceModel); Chris@1739: if (source) return source->getTitle(); Chris@345: else return ""; Chris@333: } Chris@333: Chris@333: QString Chris@333: Model::getMaker() const Chris@333: { Chris@1739: auto source = ModelById::get(m_sourceModel); Chris@1739: if (source) return source->getMaker(); Chris@345: else return ""; Chris@345: } Chris@345: Chris@345: QString Chris@345: Model::getLocation() const Chris@345: { Chris@1739: auto source = ModelById::get(m_sourceModel); Chris@1739: if (source) return source->getLocation(); Chris@345: else return ""; Chris@333: } Chris@333: Chris@319: void Chris@150: Model::toXml(QTextStream &stream, QString indent, Chris@150: QString extraAttributes) const Chris@150: { Chris@150: stream << indent; Chris@150: stream << QString("\n") Chris@1677: .arg(getExportId()) Chris@1429: .arg(encodeEntities(objectName())) Chris@1429: .arg(getSampleRate()) Chris@1429: .arg(getStartFrame()) Chris@1429: .arg(getEndFrame()) Chris@1429: .arg(extraAttributes); Chris@150: } Chris@150: Chris@150: