Chris@45: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@45: Chris@45: /* Chris@45: Sonic Visualiser Chris@45: An audio file viewer and annotation editor. Chris@45: Centre for Digital Music, Queen Mary, University of London. Chris@45: This file copyright 2006 Chris Cannam and QMUL. Chris@45: Chris@45: This program is free software; you can redistribute it and/or Chris@45: modify it under the terms of the GNU General Public License as Chris@45: published by the Free Software Foundation; either version 2 of the Chris@45: License, or (at your option) any later version. See the file Chris@45: COPYING included with this distribution for more information. Chris@45: */ Chris@45: Chris@45: #include "Document.h" Chris@45: Chris@45: #include "data/model/WaveFileModel.h" Chris@45: #include "data/model/WritableWaveFileModel.h" Chris@45: #include "data/model/DenseThreeDimensionalModel.h" Chris@45: #include "data/model/DenseTimeValueModel.h" Chris@45: #include "layer/Layer.h" Chris@45: #include "base/CommandHistory.h" Chris@45: #include "base/Command.h" Chris@45: #include "view/View.h" Chris@45: #include "base/PlayParameterRepository.h" Chris@45: #include "base/PlayParameters.h" Chris@54: #include "plugin/transform/TransformFactory.h" Chris@54: #include "plugin/transform/ModelTransformerFactory.h" Chris@45: #include Chris@45: #include Chris@100: #include Chris@45: #include Chris@45: Chris@45: // For alignment: Chris@45: #include "data/model/AggregateWaveModel.h" Chris@45: #include "data/model/SparseTimeValueModel.h" Chris@45: #include "data/model/AlignmentModel.h" Chris@45: Chris@100: //#define DEBUG_DOCUMENT 1 Chris@100: Chris@45: //!!! still need to handle command history, documentRestored/documentModified Chris@45: Chris@45: Document::Document() : Chris@47: m_mainModel(0), Chris@47: m_autoAlignment(false) Chris@45: { Chris@45: connect(this, SIGNAL(modelAboutToBeDeleted(Model *)), Chris@54: ModelTransformerFactory::getInstance(), Chris@45: SLOT(modelAboutToBeDeleted(Model *))); Chris@45: } Chris@45: Chris@45: Document::~Document() Chris@45: { Chris@45: //!!! Document should really own the command history. atm we Chris@45: //still refer to it in various places that don't have access to Chris@45: //the document, be nice to fix that Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl; Chris@100: #endif Chris@45: CommandHistory::getInstance()->clear(); Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@52: std::cerr << "Document::~Document: about to delete layers" << std::endl; Chris@100: #endif Chris@45: while (!m_layers.empty()) { Chris@45: deleteLayer(*m_layers.begin(), true); Chris@45: } Chris@45: Chris@45: if (!m_models.empty()) { Chris@45: std::cerr << "Document::~Document: WARNING: " Chris@45: << m_models.size() << " model(s) still remain -- " Chris@45: << "should have been garbage collected when deleting layers" Chris@45: << std::endl; Chris@45: while (!m_models.empty()) { Chris@45: Model *model = m_models.begin()->first; Chris@45: if (model == m_mainModel) { Chris@45: // just in case! Chris@45: std::cerr << "Document::~Document: WARNING: Main model is also" Chris@45: << " in models list!" << std::endl; Chris@45: } else if (model) { Chris@100: model->aboutToDelete(); Chris@45: emit modelAboutToBeDeleted(model); Chris@45: delete model; Chris@45: } Chris@45: m_models.erase(m_models.begin()); Chris@45: } Chris@45: } Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::~Document: About to get rid of main model" Chris@100: << std::endl; Chris@100: #endif Chris@45: if (m_mainModel) { Chris@100: m_mainModel->aboutToDelete(); Chris@45: emit modelAboutToBeDeleted(m_mainModel); Chris@45: } Chris@45: Chris@45: emit mainModelChanged(0); Chris@45: delete m_mainModel; Chris@45: Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createLayer(LayerFactory::LayerType type) Chris@45: { Chris@45: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@45: m_layers.insert(newLayer); Chris@52: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::createLayer: Added layer of type " << type Chris@100: << ", now have " << m_layers.size() << " layers" << std::endl; Chris@100: #endif Chris@52: Chris@45: emit layerAdded(newLayer); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createMainModelLayer(LayerFactory::LayerType type) Chris@45: { Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: setModel(newLayer, m_mainModel); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createImportedLayer(Model *model) Chris@45: { Chris@45: LayerFactory::LayerTypeSet types = Chris@45: LayerFactory::getInstance()->getValidLayerTypes(model); Chris@45: Chris@45: if (types.empty()) { Chris@45: std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl; Chris@45: return 0; Chris@45: } Chris@45: Chris@45: //!!! for now, just use the first suitable layer type Chris@45: LayerFactory::LayerType type = *types.begin(); Chris@45: Chris@45: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@45: addImportedModel(model); Chris@45: setModel(newLayer, model); Chris@45: Chris@45: //!!! and all channels Chris@45: setChannel(newLayer, -1); Chris@45: Chris@45: m_layers.insert(newLayer); Chris@52: Chris@100: #ifdef DEBUG_DOCUMENT Chris@52: std::cerr << "Document::createImportedLayer: Added layer of type " << type Chris@52: << ", now have " << m_layers.size() << " layers" << std::endl; Chris@100: #endif Chris@52: Chris@45: emit layerAdded(newLayer); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createEmptyLayer(LayerFactory::LayerType type) Chris@45: { Chris@61: if (!m_mainModel) return 0; Chris@61: Chris@45: Model *newModel = Chris@45: LayerFactory::getInstance()->createEmptyModel(type, m_mainModel); Chris@45: if (!newModel) return 0; Chris@45: Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) { Chris@45: delete newModel; Chris@45: return 0; Chris@45: } Chris@45: Chris@45: addImportedModel(newModel); Chris@45: setModel(newLayer, newModel); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createDerivedLayer(LayerFactory::LayerType type, Chris@54: TransformId transform) Chris@45: { Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName Chris@54: (TransformFactory::getInstance()-> Chris@54: getTransformFriendlyName(transform))); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@100: Document::createDerivedLayer(const Transform &transform, Chris@100: const ModelTransformer::Input &input) Chris@45: { Chris@100: QString message; Chris@100: Model *newModel = addDerivedModel(transform, input, message); Chris@45: if (!newModel) { Chris@100: emit modelGenerationFailed(transform.getIdentifier(), message); Chris@45: return 0; Chris@100: } else if (message != "") { Chris@100: emit modelGenerationWarning(transform.getIdentifier(), message); Chris@45: } Chris@45: Chris@45: LayerFactory::LayerTypeSet types = Chris@45: LayerFactory::getInstance()->getValidLayerTypes(newModel); Chris@45: Chris@45: if (types.empty()) { Chris@100: std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.getIdentifier().toStdString() << std::endl; Chris@45: delete newModel; Chris@45: return 0; Chris@45: } Chris@45: Chris@45: //!!! for now, just use the first suitable layer type Chris@45: Chris@45: Layer *newLayer = createLayer(*types.begin()); Chris@45: setModel(newLayer, newModel); Chris@45: Chris@45: //!!! We need to clone the model when adding the layer, so that it Chris@45: //can be edited without affecting other layers that are based on Chris@45: //the same model. Unfortunately we can't just clone it now, Chris@45: //because it probably hasn't been completed yet -- the transform Chris@45: //runs in the background. Maybe the transform has to handle Chris@45: //cloning and cacheing models itself. Chris@45: // Chris@45: // Once we do clone models here, of course, we'll have to avoid Chris@45: // leaking them too. Chris@45: // Chris@45: // We want the user to be able to add a model to a second layer Chris@45: // _while it's still being calculated in the first_ and have it Chris@45: // work quickly. That means we need to put the same physical Chris@45: // model pointer in both layers, so they can't actually be cloned. Chris@45: Chris@45: if (newLayer) { Chris@45: newLayer->setObjectName(getUniqueLayerName Chris@54: (TransformFactory::getInstance()-> Chris@100: getTransformFriendlyName Chris@100: (transform.getIdentifier()))); Chris@45: } Chris@45: Chris@45: emit layerAdded(newLayer); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setMainModel(WaveFileModel *model) Chris@45: { Chris@45: Model *oldMainModel = m_mainModel; Chris@45: m_mainModel = model; Chris@45: Chris@45: emit modelAdded(m_mainModel); Chris@45: Chris@45: std::vector obsoleteLayers; Chris@53: std::set failedTransformers; Chris@45: Chris@45: // We need to ensure that no layer is left using oldMainModel or Chris@45: // any of the old derived models as its model. Either replace the Chris@45: // model, or delete the layer for each layer that is currently Chris@45: // using one of these. Carry out this replacement before we Chris@45: // delete any of the models. Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::setMainModel: Have " Chris@100: << m_layers.size() << " layers" << std::endl; Chris@100: #endif Chris@52: Chris@45: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@45: Chris@45: Layer *layer = *i; Chris@45: Model *model = layer->getModel(); Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::setMainModel: inspecting model " Chris@100: << (model ? model->objectName().toStdString() : "(null)") << " in layer " Chris@100: << layer->objectName().toStdString() << std::endl; Chris@100: #endif Chris@45: Chris@71: if (model == oldMainModel) { Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "... it uses the old main model, replacing" << std::endl; Chris@100: #endif Chris@45: LayerFactory::getInstance()->setModel(layer, m_mainModel); Chris@45: continue; Chris@45: } Chris@45: Chris@71: if (model && (m_models.find(model) == m_models.end())) { Chris@45: std::cerr << "WARNING: Document::setMainModel: Unknown model " Chris@45: << model << " in layer " << layer << std::endl; Chris@45: // get rid of this hideous degenerate Chris@45: obsoleteLayers.push_back(layer); Chris@45: continue; Chris@45: } Chris@45: Chris@71: if (m_models[model].source && Chris@71: (m_models[model].source == oldMainModel)) { Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl; Chris@100: #endif Chris@45: Chris@45: // This model was derived from the previous main Chris@45: // model: regenerate it. Chris@45: Chris@100: const Transform &transform = m_models[model].transform; Chris@100: QString transformId = transform.getIdentifier(); Chris@45: Chris@100: //!!! We have a problem here if the number of channels in Chris@100: //the main model has changed. Chris@100: Chris@100: QString message; Chris@45: Model *replacementModel = Chris@45: addDerivedModel(transform, Chris@100: ModelTransformer::Input Chris@100: (m_mainModel, m_models[model].channel), Chris@100: message); Chris@45: Chris@45: if (!replacementModel) { Chris@45: std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \"" Chris@100: << transformId.toStdString() << "\"" << " in layer " << layer << std::endl; Chris@100: if (failedTransformers.find(transformId) Chris@100: == failedTransformers.end()) { Chris@45: emit modelRegenerationFailed(layer->objectName(), Chris@100: transformId, Chris@100: message); Chris@100: failedTransformers.insert(transformId); Chris@45: } Chris@45: obsoleteLayers.push_back(layer); Chris@45: } else { Chris@100: if (message != "") { Chris@100: emit modelRegenerationWarning(layer->objectName(), Chris@100: transformId, Chris@100: message); Chris@100: } Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Replacing model " << model << " (type " Chris@100: << typeid(*model).name() << ") with model " Chris@100: << replacementModel << " (type " Chris@100: << typeid(*replacementModel).name() << ") in layer " Chris@100: << layer << " (name " << layer->objectName().toStdString() << ")" Chris@100: << std::endl; Chris@100: #endif Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(replacementModel); Chris@100: #ifdef DEBUG_DOCUMENT Chris@45: if (rm) { Chris@45: std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl; Chris@45: } else { Chris@45: std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl; Chris@45: } Chris@100: #endif Chris@45: setModel(layer, replacementModel); Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: for (size_t k = 0; k < obsoleteLayers.size(); ++k) { Chris@45: deleteLayer(obsoleteLayers[k], true); Chris@45: } Chris@45: Chris@48: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@100: Chris@100: if (m_autoAlignment) { Chris@100: Chris@100: alignModel(i->first); Chris@100: Chris@100: } else if (oldMainModel && Chris@100: (i->first->getAlignmentReference() == oldMainModel)) { Chris@100: Chris@48: alignModel(i->first); Chris@48: } Chris@48: } Chris@48: Chris@100: if (oldMainModel) { Chris@100: oldMainModel->aboutToDelete(); Chris@100: emit modelAboutToBeDeleted(oldMainModel); Chris@100: } Chris@100: Chris@100: if (m_autoAlignment) { Chris@100: alignModel(m_mainModel); Chris@100: } Chris@100: Chris@45: emit mainModelChanged(m_mainModel); Chris@45: Chris@45: delete oldMainModel; Chris@45: } Chris@45: Chris@45: void Chris@100: Document::addDerivedModel(const Transform &transform, Chris@100: const ModelTransformer::Input &input, Chris@100: Model *outputModelToAdd) Chris@45: { Chris@45: if (m_models.find(outputModelToAdd) != m_models.end()) { Chris@45: std::cerr << "WARNING: Document::addDerivedModel: Model already added" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName().toStdString() << "\"" << std::endl; Chris@100: #endif Chris@45: Chris@45: ModelRecord rec; Chris@100: rec.source = input.getModel(); Chris@100: rec.channel = input.getChannel(); Chris@45: rec.transform = transform; Chris@45: rec.refcount = 0; Chris@45: Chris@100: outputModelToAdd->setSourceModel(input.getModel()); Chris@45: Chris@45: m_models[outputModelToAdd] = rec; Chris@45: Chris@45: emit modelAdded(outputModelToAdd); Chris@45: } Chris@45: Chris@45: Chris@45: void Chris@45: Document::addImportedModel(Model *model) Chris@45: { Chris@45: if (m_models.find(model) != m_models.end()) { Chris@45: std::cerr << "WARNING: Document::addImportedModel: Model already added" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: ModelRecord rec; Chris@45: rec.source = 0; Chris@45: rec.refcount = 0; Chris@45: Chris@45: m_models[model] = rec; Chris@45: Chris@47: if (m_autoAlignment) alignModel(model); Chris@47: Chris@45: emit modelAdded(model); Chris@45: } Chris@45: Chris@45: Model * Chris@100: Document::addDerivedModel(const Transform &transform, Chris@100: const ModelTransformer::Input &input, Chris@100: QString &message) Chris@45: { Chris@45: Model *model = 0; Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: if (i->second.transform == transform && Chris@100: i->second.source == input.getModel() && Chris@100: i->second.channel == input.getChannel()) { Chris@45: return i->first; Chris@45: } Chris@45: } Chris@45: Chris@54: model = ModelTransformerFactory::getInstance()->transform Chris@100: (transform, input, message); Chris@100: Chris@100: // The transform we actually used was presumably identical to the Chris@100: // one asked for, except that the version of the plugin may Chris@100: // differ. It's possible that the returned message contains a Chris@100: // warning about this; that doesn't concern us here, but we do Chris@100: // need to ensure that the transform we remember is correct for Chris@100: // what was actually applied, with the current plugin version. Chris@100: Chris@100: Transform applied = transform; Chris@100: applied.setPluginVersion Chris@100: (TransformFactory::getInstance()-> Chris@100: getDefaultTransformFor(transform.getIdentifier(), Chris@100: lrintf(transform.getSampleRate())) Chris@100: .getPluginVersion()); Chris@45: Chris@45: if (!model) { Chris@100: std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.getIdentifier().toStdString() << std::endl; Chris@45: } else { Chris@100: addDerivedModel(applied, input, model); Chris@45: } Chris@45: Chris@45: return model; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::releaseModel(Model *model) // Will _not_ release main model! Chris@45: { Chris@45: if (model == 0) { Chris@45: return; Chris@45: } Chris@45: Chris@45: if (model == m_mainModel) { Chris@45: return; Chris@45: } Chris@45: Chris@45: bool toDelete = false; Chris@45: Chris@45: if (m_models.find(model) != m_models.end()) { Chris@45: Chris@45: if (m_models[model].refcount == 0) { Chris@45: std::cerr << "WARNING: Document::releaseModel: model " << model Chris@45: << " reference count is zero already!" << std::endl; Chris@45: } else { Chris@45: if (--m_models[model].refcount == 0) { Chris@45: toDelete = true; Chris@45: } Chris@45: } Chris@45: } else { Chris@45: std::cerr << "WARNING: Document::releaseModel: Unfound model " Chris@45: << model << std::endl; Chris@45: toDelete = true; Chris@45: } Chris@45: Chris@45: if (toDelete) { Chris@45: Chris@45: int sourceCount = 0; Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: if (i->second.source == model) { Chris@45: ++sourceCount; Chris@45: i->second.source = 0; Chris@45: } Chris@45: } Chris@45: Chris@45: if (sourceCount > 0) { Chris@45: std::cerr << "Document::releaseModel: Deleting model " Chris@45: << model << " even though it is source for " Chris@45: << sourceCount << " other derived model(s) -- resetting " Chris@45: << "their source fields appropriately" << std::endl; Chris@45: } Chris@45: Chris@100: model->aboutToDelete(); Chris@45: emit modelAboutToBeDeleted(model); Chris@45: m_models.erase(model); Chris@45: delete model; Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::deleteLayer(Layer *layer, bool force) Chris@45: { Chris@45: if (m_layerViewMap.find(layer) != m_layerViewMap.end() && Chris@45: m_layerViewMap[layer].size() > 0) { Chris@45: Chris@45: std::cerr << "WARNING: Document::deleteLayer: Layer " Chris@45: << layer << " [" << layer->objectName().toStdString() << "]" Chris@45: << " is still used in " << m_layerViewMap[layer].size() Chris@45: << " views!" << std::endl; Chris@45: Chris@45: if (force) { Chris@45: Chris@100: #ifdef DEBUG_DOCUMENT Chris@45: std::cerr << "(force flag set -- deleting from all views)" << std::endl; Chris@100: #endif Chris@45: Chris@45: for (std::set::iterator j = m_layerViewMap[layer].begin(); Chris@45: j != m_layerViewMap[layer].end(); ++j) { Chris@45: // don't use removeLayerFromView, as it issues a command Chris@45: layer->setLayerDormant(*j, true); Chris@45: (*j)->removeLayer(layer); Chris@45: } Chris@45: Chris@45: m_layerViewMap.erase(layer); Chris@45: Chris@45: } else { Chris@45: return; Chris@45: } Chris@45: } Chris@45: Chris@45: if (m_layers.find(layer) == m_layers.end()) { Chris@45: std::cerr << "Document::deleteLayer: Layer " Chris@45: << layer << " does not exist, or has already been deleted " Chris@45: << "(this may not be as serious as it sounds)" << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: m_layers.erase(layer); Chris@45: Chris@52: std::cerr << "Document::deleteLayer: Removing, now have " Chris@52: << m_layers.size() << " layers" << std::endl; Chris@52: Chris@45: releaseModel(layer->getModel()); Chris@45: emit layerRemoved(layer); Chris@45: emit layerAboutToBeDeleted(layer); Chris@45: delete layer; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setModel(Layer *layer, Model *model) Chris@45: { Chris@45: if (model && Chris@45: model != m_mainModel && Chris@45: m_models.find(model) == m_models.end()) { Chris@45: std::cerr << "ERROR: Document::setModel: Layer " << layer Chris@45: << " (\"" << layer->objectName().toStdString() Chris@45: << "\") wants to use unregistered model " << model Chris@45: << ": register the layer's model before setting it!" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: Model *previousModel = layer->getModel(); Chris@45: Chris@45: if (previousModel == model) { Chris@45: std::cerr << "WARNING: Document::setModel: Layer " << layer << " (\"" Chris@45: << layer->objectName().toStdString() Chris@45: << "\") is already set to model " Chris@45: << model << " (\"" Chris@45: << (model ? model->objectName().toStdString() : "(null)") Chris@45: << "\")" << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: if (model && model != m_mainModel) { Chris@45: m_models[model].refcount ++; Chris@45: } Chris@45: Chris@45: if (model && previousModel) { Chris@45: PlayParameterRepository::getInstance()->copyParameters Chris@45: (previousModel, model); Chris@45: } Chris@45: Chris@45: LayerFactory::getInstance()->setModel(layer, model); Chris@45: Chris@45: if (previousModel) { Chris@45: releaseModel(previousModel); Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setChannel(Layer *layer, int channel) Chris@45: { Chris@45: LayerFactory::getInstance()->setChannel(layer, channel); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::addLayerToView(View *view, Layer *layer) Chris@45: { Chris@45: Model *model = layer->getModel(); Chris@45: if (!model) { Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::addLayerToView: Layer (\"" Chris@100: << layer->objectName().toStdString() Chris@100: << "\") with no model being added to view: " Chris@100: << "normally you want to set the model first" << std::endl; Chris@100: #endif Chris@45: } else { Chris@45: if (model != m_mainModel && Chris@45: m_models.find(model) == m_models.end()) { Chris@45: std::cerr << "ERROR: Document::addLayerToView: Layer " << layer Chris@45: << " has unregistered model " << model Chris@45: << " -- register the layer's model before adding the layer!" << std::endl; Chris@45: return; Chris@45: } Chris@45: } Chris@45: Chris@45: CommandHistory::getInstance()->addCommand Chris@45: (new Document::AddLayerCommand(this, view, layer)); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::removeLayerFromView(View *view, Layer *layer) Chris@45: { Chris@45: CommandHistory::getInstance()->addCommand Chris@45: (new Document::RemoveLayerCommand(this, view, layer)); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::addToLayerViewMap(Layer *layer, View *view) Chris@45: { Chris@45: bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() || Chris@45: m_layerViewMap[layer].empty()); Chris@45: Chris@45: if (m_layerViewMap[layer].find(view) != Chris@45: m_layerViewMap[layer].end()) { Chris@45: std::cerr << "WARNING: Document::addToLayerViewMap:" Chris@45: << " Layer " << layer << " -> view " << view << " already in" Chris@45: << " layer view map -- internal inconsistency" << std::endl; Chris@45: } Chris@45: Chris@45: m_layerViewMap[layer].insert(view); Chris@45: Chris@45: if (firstView) emit layerInAView(layer, true); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::removeFromLayerViewMap(Layer *layer, View *view) Chris@45: { Chris@45: if (m_layerViewMap[layer].find(view) == Chris@45: m_layerViewMap[layer].end()) { Chris@45: std::cerr << "WARNING: Document::removeFromLayerViewMap:" Chris@45: << " Layer " << layer << " -> view " << view << " not in" Chris@45: << " layer view map -- internal inconsistency" << std::endl; Chris@45: } Chris@45: Chris@45: m_layerViewMap[layer].erase(view); Chris@45: Chris@45: if (m_layerViewMap[layer].empty()) { Chris@45: m_layerViewMap.erase(layer); Chris@45: emit layerInAView(layer, false); Chris@45: } Chris@45: } Chris@45: Chris@45: QString Chris@45: Document::getUniqueLayerName(QString candidate) Chris@45: { Chris@45: for (int count = 1; ; ++count) { Chris@45: Chris@45: QString adjusted = Chris@45: (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) : Chris@45: candidate); Chris@45: Chris@45: bool duplicate = false; Chris@45: Chris@45: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@45: if ((*i)->objectName() == adjusted) { Chris@45: duplicate = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!duplicate) return adjusted; Chris@45: } Chris@45: } Chris@45: Chris@45: std::vector Chris@100: Document::getTransformInputModels() Chris@45: { Chris@45: std::vector models; Chris@45: Chris@45: if (!m_mainModel) return models; Chris@45: Chris@45: models.push_back(m_mainModel); Chris@45: Chris@45: //!!! This will pick up all models, including those that aren't visible... Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: Chris@45: Model *model = i->first; Chris@45: if (!model || model == m_mainModel) continue; Chris@45: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@45: Chris@45: if (dtvm) { Chris@45: models.push_back(dtvm); Chris@45: } Chris@45: } Chris@45: Chris@45: return models; Chris@45: } Chris@45: Chris@50: bool Chris@100: Document::isKnownModel(const Model *model) const Chris@100: { Chris@100: if (model == m_mainModel) return true; Chris@100: return (m_models.find(const_cast(model)) != m_models.end()); Chris@100: } Chris@100: Chris@100: TransformId Chris@100: Document::getAlignmentTransformName() Chris@100: { Chris@100: QSettings settings; Chris@100: settings.beginGroup("Alignment"); Chris@100: TransformId id = Chris@100: settings.value("transform-id", Chris@100: "vamp:match-vamp-plugin:match:path").toString(); Chris@100: settings.endGroup(); Chris@100: return id; Chris@100: } Chris@100: Chris@100: bool Chris@51: Document::canAlign() Chris@50: { Chris@100: TransformId id = getAlignmentTransformName(); Chris@54: TransformFactory *factory = TransformFactory::getInstance(); Chris@54: return factory->haveTransform(id); Chris@50: } Chris@50: Chris@45: void Chris@45: Document::alignModel(Model *model) Chris@45: { Chris@100: if (!m_mainModel) return; Chris@45: Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(model); Chris@45: if (!rm) return; Chris@48: Chris@100: if (rm->getAlignmentReference() == m_mainModel) { Chris@100: std::cerr << "Document::alignModel: model " << rm << " is already aligned to main model " << m_mainModel << std::endl; Chris@100: return; Chris@100: } Chris@45: Chris@100: if (model == m_mainModel) { Chris@100: // The reference has an empty alignment to itself. This makes Chris@100: // it possible to distinguish between the reference and any Chris@100: // unaligned model just by looking at the model itself, Chris@100: // without also knowing what the main model is Chris@100: std::cerr << "Document::alignModel(" << model << "): is main model, setting appropriately" << std::endl; Chris@100: rm->setAlignment(new AlignmentModel(model, model, 0, 0)); Chris@100: return; Chris@100: } Chris@100: Chris@45: // This involves creating three new models: Chris@45: Chris@45: // 1. an AggregateWaveModel to provide the mixdowns of the main Chris@45: // model and the new model in its two channels, as input to the Chris@45: // MATCH plugin Chris@45: Chris@45: // 2. a SparseTimeValueModel, which is the model automatically Chris@53: // created by FeatureExtractionPluginTransformer when running the Chris@45: // MATCH plugin (thus containing the alignment path) Chris@45: Chris@45: // 3. an AlignmentModel, which stores the path model and carries Chris@45: // out alignment lookups on it. Chris@45: Chris@45: // The first two of these are provided as arguments to the Chris@45: // constructor for the third, which takes responsibility for Chris@45: // deleting them. The AlignmentModel, meanwhile, is passed to the Chris@45: // new model we are aligning, which also takes responsibility for Chris@45: // it. We should not have to delete any of these new models here. Chris@45: Chris@45: AggregateWaveModel::ChannelSpecList components; Chris@45: Chris@45: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@45: (m_mainModel, -1)); Chris@45: Chris@45: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@45: (rm, -1)); Chris@45: Chris@45: Model *aggregate = new AggregateWaveModel(components); Chris@45: Chris@100: TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure Chris@45: Chris@100: TransformFactory *tf = TransformFactory::getInstance(); Chris@45: Chris@100: Transform transform = tf->getDefaultTransformFor Chris@100: (id, aggregate->getSampleRate()); Chris@57: Chris@100: transform.setStepSize(transform.getBlockSize()/2); Chris@100: transform.setParameter("serialise", 1); Chris@64: Chris@100: std::cerr << "Document::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << std::endl; Chris@100: Chris@100: ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance(); Chris@100: Chris@100: QString message; Chris@100: Model *transformOutput = mtf->transform(transform, aggregate, message); Chris@64: Chris@64: if (!transformOutput) { Chris@100: transform.setStepSize(0); Chris@100: transformOutput = mtf->transform(transform, aggregate, message); Chris@64: } Chris@45: Chris@45: SparseTimeValueModel *path = dynamic_cast Chris@45: (transformOutput); Chris@45: Chris@45: if (!path) { Chris@45: std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl; Chris@100: emit alignmentFailed(id, message); Chris@45: delete transformOutput; Chris@45: delete aggregate; Chris@45: return; Chris@45: } Chris@45: Chris@45: AlignmentModel *alignmentModel = new AlignmentModel Chris@45: (m_mainModel, model, aggregate, path); Chris@45: Chris@45: rm->setAlignment(alignmentModel); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::alignModels() Chris@45: { Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: alignModel(i->first); Chris@45: } Chris@100: alignModel(m_mainModel); Chris@45: } Chris@45: Chris@45: Document::AddLayerCommand::AddLayerCommand(Document *d, Chris@45: View *view, Chris@45: Layer *layer) : Chris@45: m_d(d), Chris@45: m_view(view), Chris@45: m_layer(layer), Chris@45: m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())), Chris@45: m_added(false) Chris@45: { Chris@45: } Chris@45: Chris@45: Document::AddLayerCommand::~AddLayerCommand() Chris@45: { Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl; Chris@100: #endif Chris@45: if (!m_added) { Chris@45: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::AddLayerCommand::execute() Chris@45: { Chris@45: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@45: if (m_view->getLayer(i) == m_layer) { Chris@45: // already there Chris@45: m_layer->setLayerDormant(m_view, false); Chris@45: m_added = true; Chris@45: return; Chris@45: } Chris@45: } Chris@45: Chris@45: m_view->addLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, false); Chris@45: Chris@45: m_d->addToLayerViewMap(m_layer, m_view); Chris@45: m_added = true; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::AddLayerCommand::unexecute() Chris@45: { Chris@45: m_view->removeLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: Chris@45: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@45: m_added = false; Chris@45: } Chris@45: Chris@45: Document::RemoveLayerCommand::RemoveLayerCommand(Document *d, Chris@45: View *view, Chris@45: Layer *layer) : Chris@45: m_d(d), Chris@45: m_view(view), Chris@45: m_layer(layer), Chris@45: m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())), Chris@45: m_added(true) Chris@45: { Chris@45: } Chris@45: Chris@45: Document::RemoveLayerCommand::~RemoveLayerCommand() Chris@45: { Chris@100: #ifdef DEBUG_DOCUMENT Chris@100: std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl; Chris@100: #endif Chris@45: if (!m_added) { Chris@45: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::RemoveLayerCommand::execute() Chris@45: { Chris@45: bool have = false; Chris@45: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@45: if (m_view->getLayer(i) == m_layer) { Chris@45: have = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!have) { // not there! Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: m_added = false; Chris@45: return; Chris@45: } Chris@45: Chris@45: m_view->removeLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: Chris@45: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@45: m_added = false; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::RemoveLayerCommand::unexecute() Chris@45: { Chris@45: m_view->addLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, false); Chris@45: Chris@45: m_d->addToLayerViewMap(m_layer, m_view); Chris@45: m_added = true; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const Chris@45: { Chris@45: out << indent + QString("\n") Chris@45: .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes); Chris@45: Chris@45: if (m_mainModel) { Chris@45: m_mainModel->toXml(out, indent + " ", "mainModel=\"true\""); Chris@45: } Chris@45: Chris@45: // Models that are not used in a layer that is in a view should Chris@45: // not be written. Get our list of required models first. Chris@45: Chris@45: std::set used; Chris@45: Chris@45: for (LayerViewMap::const_iterator i = m_layerViewMap.begin(); Chris@45: i != m_layerViewMap.end(); ++i) { Chris@45: Chris@45: if (i->first && !i->second.empty() && i->first->getModel()) { Chris@45: used.insert(i->first->getModel()); Chris@45: } Chris@45: } Chris@45: Chris@45: for (ModelMap::const_iterator i = m_models.begin(); Chris@45: i != m_models.end(); ++i) { Chris@45: Chris@45: const Model *model = i->first; Chris@45: const ModelRecord &rec = i->second; Chris@45: Chris@45: if (used.find(model) == used.end()) continue; Chris@45: Chris@45: // We need an intelligent way to determine which models need Chris@45: // to be streamed (i.e. have been edited, or are small) and Chris@45: // which should not be (i.e. remain as generated by a Chris@45: // transform, and are large). Chris@45: // Chris@45: // At the moment we can get away with deciding not to stream Chris@45: // dense 3d models or writable wave file models, provided they Chris@45: // were generated from a transform, because at the moment there Chris@45: // is no way to edit those model types so it should be safe to Chris@45: // regenerate them. That won't always work in future though. Chris@45: // It would be particularly nice to be able to ask the user, Chris@45: // as well as making an intelligent guess. Chris@45: Chris@45: bool writeModel = true; Chris@45: bool haveDerivation = false; Chris@45: Chris@100: if (rec.source && rec.transform.getIdentifier() != "") { Chris@45: haveDerivation = true; Chris@45: } Chris@45: Chris@45: if (haveDerivation) { Chris@45: if (dynamic_cast(model)) { Chris@45: writeModel = false; Chris@45: } else if (dynamic_cast(model)) { Chris@45: writeModel = false; Chris@45: } Chris@45: } Chris@45: Chris@45: if (writeModel) { Chris@45: i->first->toXml(out, indent + " "); Chris@45: } Chris@45: Chris@45: if (haveDerivation) { Chris@100: writeBackwardCompatibleDerivation(out, indent + " ", Chris@100: i->first, rec); Chris@45: } Chris@45: Chris@45: //!!! We should probably own the PlayParameterRepository Chris@45: PlayParameters *playParameters = Chris@45: PlayParameterRepository::getInstance()->getPlayParameters(i->first); Chris@45: if (playParameters) { Chris@45: playParameters->toXml Chris@45: (out, indent + " ", Chris@45: QString("model=\"%1\"") Chris@45: .arg(XmlExportable::getObjectExportId(i->first))); Chris@45: } Chris@45: } Chris@45: Chris@45: for (LayerSet::const_iterator i = m_layers.begin(); Chris@45: i != m_layers.end(); ++i) { Chris@45: Chris@45: (*i)->toXml(out, indent + " "); Chris@45: } Chris@45: Chris@45: out << indent + "\n"; Chris@45: } Chris@45: Chris@100: void Chris@100: Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent, Chris@100: Model *targetModel, Chris@100: const ModelRecord &rec) const Chris@100: { Chris@100: // There is a lot of redundancy in the XML we output here, because Chris@100: // we want it to work with older SV session file reading code as Chris@100: // well. Chris@100: // Chris@100: // Formerly, a transform was described using a derivation element Chris@100: // which set out the source and target models, execution context Chris@100: // (step size, input channel etc) and transform id, containing a Chris@100: // plugin element which set out the transform parameters and so Chris@100: // on. (The plugin element came from a "configurationXml" string Chris@100: // obtained from PluginXml.) Chris@100: // Chris@100: // This has been replaced by a derivation element setting out the Chris@100: // source and target models and input channel, containing a Chris@100: // transform element which sets out everything in the Transform. Chris@100: // Chris@100: // In order to retain compatibility with older SV code, however, Chris@100: // we have to write out the same stuff into the derivation as Chris@100: // before, and manufacture an appropriate plugin element as well Chris@100: // as the transform element. In order that newer code knows it's Chris@100: // dealing with a newer format, we will also write an attribute Chris@100: // 'type="transform"' in the derivation element. Chris@45: Chris@100: const Transform &transform = rec.transform; Chris@100: Chris@100: // Just for reference, this is what we would write if we didn't Chris@100: // have to be backward compatible: Chris@100: // Chris@100: // out << indent Chris@100: // << QString("\n") Chris@100: // .arg(XmlExportable::getObjectExportId(rec.source)) Chris@100: // .arg(XmlExportable::getObjectExportId(targetModel)) Chris@100: // .arg(rec.channel); Chris@100: // Chris@100: // transform.toXml(out, indent + " "); Chris@100: // Chris@100: // out << indent << "\n"; Chris@100: // Chris@100: // Unfortunately, we can't just do that. So we do this... Chris@100: Chris@100: QString extentsAttributes; Chris@100: if (transform.getStartTime() != RealTime::zeroTime || Chris@100: transform.getDuration() != RealTime::zeroTime) { Chris@100: extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ") Chris@100: .arg(RealTime::realTime2Frame(transform.getStartTime(), Chris@100: targetModel->getSampleRate())) Chris@100: .arg(RealTime::realTime2Frame(transform.getDuration(), Chris@100: targetModel->getSampleRate())); Chris@100: } Chris@100: Chris@100: out << indent; Chris@100: out << QString("\n") Chris@100: .arg(XmlExportable::getObjectExportId(rec.source)) Chris@100: .arg(XmlExportable::getObjectExportId(targetModel)) Chris@100: .arg(rec.channel) Chris@100: .arg(TransformFactory::getInstance()->getTransformInputDomain Chris@100: (transform.getIdentifier())) Chris@100: .arg(transform.getStepSize()) Chris@100: .arg(transform.getBlockSize()) Chris@100: .arg(extentsAttributes) Chris@100: .arg(int(transform.getWindowType())) Chris@100: .arg(XmlExportable::encodeEntities(transform.getIdentifier())); Chris@100: Chris@100: transform.toXml(out, indent + " "); Chris@100: Chris@100: out << indent << " " Chris@100: << TransformFactory::getInstance()->getPluginConfigurationXml(transform); Chris@100: Chris@100: out << indent << "\n"; Chris@100: } Chris@100: