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@150: } Chris@150: Chris@150: void Chris@1735: Model::setSourceModel(ModelId modelId) Chris@319: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1798: Chris@1735: m_sourceModel = modelId; Chris@319: Chris@1735: auto model = ModelById::get(m_sourceModel); Chris@1735: if (model) { Chris@1752: connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)), Chris@1752: this, SIGNAL(alignmentCompletionChanged(ModelId))); Chris@1735: } Chris@319: } Chris@319: Chris@319: void Chris@1735: Model::setAlignment(ModelId alignmentModel) Chris@319: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1798: Chris@1668: SVDEBUG << "Model(" << this << "): accepting alignment model " Chris@1735: << alignmentModel << endl; Chris@1761: Chris@1761: if (auto model = ModelById::get(m_alignmentModel)) { Chris@1761: disconnect(model.get(), SIGNAL(completionChanged(ModelId)), Chris@1767: this, SLOT(alignmentModelCompletionChanged(ModelId))); Chris@319: } Chris@1018: Chris@1735: m_alignmentModel = alignmentModel; Chris@1018: Chris@1761: if (auto model = ModelById::get(m_alignmentModel)) { Chris@1758: connect(model.get(), SIGNAL(completionChanged(ModelId)), Chris@1767: this, SLOT(alignmentModelCompletionChanged(ModelId))); Chris@1018: } Chris@319: } Chris@319: Chris@1767: void Chris@1767: Model::alignmentModelCompletionChanged(ModelId) Chris@1767: { Chris@1767: emit alignmentCompletionChanged(getId()); Chris@1767: } Chris@1767: Chris@1735: const ModelId Chris@407: Model::getAlignment() const Chris@407: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1735: return m_alignmentModel; Chris@407: } Chris@407: Chris@1735: const ModelId Chris@319: Model::getAlignmentReference() const Chris@319: { Chris@1798: QMutexLocker locker(&m_mutex); 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@1798: ModelId alignmentModelId, sourceModelId; Chris@1798: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1798: alignmentModelId = m_alignmentModel; Chris@1798: sourceModelId = m_sourceModel; Chris@1798: } Chris@1798: Chris@1798: auto alignmentModel = ModelById::getAs(alignmentModelId); Chris@1739: Chris@1739: if (!alignmentModel) { Chris@1798: auto sourceModel = ModelById::get(sourceModelId); 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@1798: ModelId alignmentModelId, sourceModelId; Chris@1798: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1798: alignmentModelId = m_alignmentModel; Chris@1798: sourceModelId = m_sourceModel; Chris@1798: } Chris@1798: Chris@1798: auto alignmentModel = ModelById::getAs(alignmentModelId); Chris@1798: Chris@1739: if (!alignmentModel) { Chris@1798: auto sourceModel = ModelById::get(sourceModelId); 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@1798: ModelId alignmentModelId, sourceModelId; Chris@1798: { Chris@1798: QMutexLocker locker(&m_mutex); Chris@1798: alignmentModelId = m_alignmentModel; Chris@1798: sourceModelId = m_sourceModel; Chris@1798: } Chris@1798: 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@1798: auto alignmentModel = ModelById::getAs(alignmentModelId); Chris@1739: Chris@1739: if (!alignmentModel) { Chris@1798: auto sourceModel = ModelById::get(sourceModelId); 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@1798: QMutexLocker locker(&m_mutex); 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@1798: QMutexLocker locker(&m_mutex); 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@1798: QMutexLocker locker(&m_mutex); 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: