annotate framework/Document.cpp @ 72:4aa40182321f

* Merge from transforms branch -- switch over to using Transform object properly
author Chris Cannam
date Fri, 07 Dec 2007 16:47:31 +0000
parents 716e9d2f91c7
children 0535c49069ba
rev   line source
Chris@45 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@45 2
Chris@45 3 /*
Chris@45 4 Sonic Visualiser
Chris@45 5 An audio file viewer and annotation editor.
Chris@45 6 Centre for Digital Music, Queen Mary, University of London.
Chris@45 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@45 8
Chris@45 9 This program is free software; you can redistribute it and/or
Chris@45 10 modify it under the terms of the GNU General Public License as
Chris@45 11 published by the Free Software Foundation; either version 2 of the
Chris@45 12 License, or (at your option) any later version. See the file
Chris@45 13 COPYING included with this distribution for more information.
Chris@45 14 */
Chris@45 15
Chris@45 16 #include "Document.h"
Chris@45 17
Chris@45 18 #include "data/model/WaveFileModel.h"
Chris@45 19 #include "data/model/WritableWaveFileModel.h"
Chris@45 20 #include "data/model/DenseThreeDimensionalModel.h"
Chris@45 21 #include "data/model/DenseTimeValueModel.h"
Chris@45 22 #include "layer/Layer.h"
Chris@45 23 #include "base/CommandHistory.h"
Chris@45 24 #include "base/Command.h"
Chris@45 25 #include "view/View.h"
Chris@45 26 #include "base/PlayParameterRepository.h"
Chris@45 27 #include "base/PlayParameters.h"
Chris@54 28 #include "plugin/transform/TransformFactory.h"
Chris@54 29 #include "plugin/transform/ModelTransformerFactory.h"
Chris@45 30 #include <QApplication>
Chris@45 31 #include <QTextStream>
Chris@45 32 #include <iostream>
Chris@45 33
Chris@45 34 // For alignment:
Chris@45 35 #include "data/model/AggregateWaveModel.h"
Chris@45 36 #include "data/model/SparseTimeValueModel.h"
Chris@45 37 #include "data/model/AlignmentModel.h"
Chris@45 38
Chris@45 39 //!!! still need to handle command history, documentRestored/documentModified
Chris@45 40
Chris@45 41 Document::Document() :
Chris@47 42 m_mainModel(0),
Chris@47 43 m_autoAlignment(false)
Chris@45 44 {
Chris@45 45 connect(this, SIGNAL(modelAboutToBeDeleted(Model *)),
Chris@54 46 ModelTransformerFactory::getInstance(),
Chris@45 47 SLOT(modelAboutToBeDeleted(Model *)));
Chris@45 48 }
Chris@45 49
Chris@45 50 Document::~Document()
Chris@45 51 {
Chris@45 52 //!!! Document should really own the command history. atm we
Chris@45 53 //still refer to it in various places that don't have access to
Chris@45 54 //the document, be nice to fix that
Chris@45 55
Chris@45 56 // std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl;
Chris@45 57 CommandHistory::getInstance()->clear();
Chris@45 58
Chris@52 59 std::cerr << "Document::~Document: about to delete layers" << std::endl;
Chris@45 60 while (!m_layers.empty()) {
Chris@45 61 deleteLayer(*m_layers.begin(), true);
Chris@45 62 }
Chris@45 63
Chris@45 64 if (!m_models.empty()) {
Chris@45 65 std::cerr << "Document::~Document: WARNING: "
Chris@45 66 << m_models.size() << " model(s) still remain -- "
Chris@45 67 << "should have been garbage collected when deleting layers"
Chris@45 68 << std::endl;
Chris@45 69 while (!m_models.empty()) {
Chris@45 70 Model *model = m_models.begin()->first;
Chris@45 71 if (model == m_mainModel) {
Chris@45 72 // just in case!
Chris@45 73 std::cerr << "Document::~Document: WARNING: Main model is also"
Chris@45 74 << " in models list!" << std::endl;
Chris@45 75 } else if (model) {
Chris@45 76 emit modelAboutToBeDeleted(model);
Chris@45 77 model->aboutToDelete();
Chris@45 78 delete model;
Chris@45 79 }
Chris@45 80 m_models.erase(m_models.begin());
Chris@45 81 }
Chris@45 82 }
Chris@45 83
Chris@45 84 // std::cerr << "Document::~Document: About to get rid of main model"
Chris@45 85 // << std::endl;
Chris@45 86 if (m_mainModel) {
Chris@45 87 emit modelAboutToBeDeleted(m_mainModel);
Chris@45 88 m_mainModel->aboutToDelete();
Chris@45 89 }
Chris@45 90
Chris@45 91 emit mainModelChanged(0);
Chris@45 92 delete m_mainModel;
Chris@45 93
Chris@45 94 }
Chris@45 95
Chris@45 96 Layer *
Chris@45 97 Document::createLayer(LayerFactory::LayerType type)
Chris@45 98 {
Chris@45 99 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 100 if (!newLayer) return 0;
Chris@45 101
Chris@45 102 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 103
Chris@45 104 m_layers.insert(newLayer);
Chris@52 105
Chris@65 106 // std::cerr << "Document::createLayer: Added layer of type " << type
Chris@65 107 // << ", now have " << m_layers.size() << " layers" << std::endl;
Chris@52 108
Chris@45 109 emit layerAdded(newLayer);
Chris@45 110
Chris@45 111 return newLayer;
Chris@45 112 }
Chris@45 113
Chris@45 114 Layer *
Chris@45 115 Document::createMainModelLayer(LayerFactory::LayerType type)
Chris@45 116 {
Chris@45 117 Layer *newLayer = createLayer(type);
Chris@45 118 if (!newLayer) return 0;
Chris@45 119 setModel(newLayer, m_mainModel);
Chris@45 120 return newLayer;
Chris@45 121 }
Chris@45 122
Chris@45 123 Layer *
Chris@45 124 Document::createImportedLayer(Model *model)
Chris@45 125 {
Chris@45 126 LayerFactory::LayerTypeSet types =
Chris@45 127 LayerFactory::getInstance()->getValidLayerTypes(model);
Chris@45 128
Chris@45 129 if (types.empty()) {
Chris@45 130 std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl;
Chris@45 131 return 0;
Chris@45 132 }
Chris@45 133
Chris@45 134 //!!! for now, just use the first suitable layer type
Chris@45 135 LayerFactory::LayerType type = *types.begin();
Chris@45 136
Chris@45 137 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 138 if (!newLayer) return 0;
Chris@45 139
Chris@45 140 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 141
Chris@45 142 addImportedModel(model);
Chris@45 143 setModel(newLayer, model);
Chris@45 144
Chris@45 145 //!!! and all channels
Chris@45 146 setChannel(newLayer, -1);
Chris@45 147
Chris@45 148 m_layers.insert(newLayer);
Chris@52 149
Chris@52 150 std::cerr << "Document::createImportedLayer: Added layer of type " << type
Chris@52 151 << ", now have " << m_layers.size() << " layers" << std::endl;
Chris@52 152
Chris@45 153 emit layerAdded(newLayer);
Chris@45 154 return newLayer;
Chris@45 155 }
Chris@45 156
Chris@45 157 Layer *
Chris@45 158 Document::createEmptyLayer(LayerFactory::LayerType type)
Chris@45 159 {
Chris@61 160 if (!m_mainModel) return 0;
Chris@61 161
Chris@45 162 Model *newModel =
Chris@45 163 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
Chris@45 164 if (!newModel) return 0;
Chris@45 165
Chris@45 166 Layer *newLayer = createLayer(type);
Chris@45 167 if (!newLayer) {
Chris@45 168 delete newModel;
Chris@45 169 return 0;
Chris@45 170 }
Chris@45 171
Chris@45 172 addImportedModel(newModel);
Chris@45 173 setModel(newLayer, newModel);
Chris@45 174
Chris@45 175 return newLayer;
Chris@45 176 }
Chris@45 177
Chris@45 178 Layer *
Chris@45 179 Document::createDerivedLayer(LayerFactory::LayerType type,
Chris@54 180 TransformId transform)
Chris@45 181 {
Chris@45 182 Layer *newLayer = createLayer(type);
Chris@45 183 if (!newLayer) return 0;
Chris@45 184
Chris@45 185 newLayer->setObjectName(getUniqueLayerName
Chris@54 186 (TransformFactory::getInstance()->
Chris@54 187 getTransformFriendlyName(transform)));
Chris@45 188
Chris@45 189 return newLayer;
Chris@45 190 }
Chris@45 191
Chris@45 192 Layer *
Chris@72 193 Document::createDerivedLayer(const Transform &transform,
Chris@72 194 const ModelTransformer::Input &input)
Chris@45 195 {
Chris@72 196 Model *newModel = addDerivedModel(transform, input);
Chris@45 197 if (!newModel) {
Chris@45 198 // error already printed to stderr by addDerivedModel
Chris@72 199 emit modelGenerationFailed(transform.getIdentifier());
Chris@45 200 return 0;
Chris@45 201 }
Chris@45 202
Chris@45 203 LayerFactory::LayerTypeSet types =
Chris@45 204 LayerFactory::getInstance()->getValidLayerTypes(newModel);
Chris@45 205
Chris@45 206 if (types.empty()) {
Chris@72 207 std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.getIdentifier().toStdString() << std::endl;
Chris@45 208 delete newModel;
Chris@45 209 return 0;
Chris@45 210 }
Chris@45 211
Chris@45 212 //!!! for now, just use the first suitable layer type
Chris@45 213
Chris@45 214 Layer *newLayer = createLayer(*types.begin());
Chris@45 215 setModel(newLayer, newModel);
Chris@45 216
Chris@45 217 //!!! We need to clone the model when adding the layer, so that it
Chris@45 218 //can be edited without affecting other layers that are based on
Chris@45 219 //the same model. Unfortunately we can't just clone it now,
Chris@45 220 //because it probably hasn't been completed yet -- the transform
Chris@45 221 //runs in the background. Maybe the transform has to handle
Chris@45 222 //cloning and cacheing models itself.
Chris@45 223 //
Chris@45 224 // Once we do clone models here, of course, we'll have to avoid
Chris@45 225 // leaking them too.
Chris@45 226 //
Chris@45 227 // We want the user to be able to add a model to a second layer
Chris@45 228 // _while it's still being calculated in the first_ and have it
Chris@45 229 // work quickly. That means we need to put the same physical
Chris@45 230 // model pointer in both layers, so they can't actually be cloned.
Chris@45 231
Chris@45 232 if (newLayer) {
Chris@45 233 newLayer->setObjectName(getUniqueLayerName
Chris@54 234 (TransformFactory::getInstance()->
Chris@72 235 getTransformFriendlyName
Chris@72 236 (transform.getIdentifier())));
Chris@45 237 }
Chris@45 238
Chris@45 239 emit layerAdded(newLayer);
Chris@45 240 return newLayer;
Chris@45 241 }
Chris@45 242
Chris@45 243 void
Chris@45 244 Document::setMainModel(WaveFileModel *model)
Chris@45 245 {
Chris@45 246 Model *oldMainModel = m_mainModel;
Chris@45 247 m_mainModel = model;
Chris@45 248
Chris@45 249 emit modelAdded(m_mainModel);
Chris@45 250
Chris@45 251 std::vector<Layer *> obsoleteLayers;
Chris@53 252 std::set<QString> failedTransformers;
Chris@45 253
Chris@45 254 // We need to ensure that no layer is left using oldMainModel or
Chris@45 255 // any of the old derived models as its model. Either replace the
Chris@45 256 // model, or delete the layer for each layer that is currently
Chris@45 257 // using one of these. Carry out this replacement before we
Chris@45 258 // delete any of the models.
Chris@45 259
Chris@70 260 // std::cerr << "Document::setMainModel: Have "
Chris@70 261 // << m_layers.size() << " layers" << std::endl;
Chris@52 262
Chris@45 263 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 264
Chris@45 265 Layer *layer = *i;
Chris@45 266 Model *model = layer->getModel();
Chris@45 267
Chris@45 268 // std::cerr << "Document::setMainModel: inspecting model "
Chris@45 269 // << (model ? model->objectName().toStdString() : "(null)") << " in layer "
Chris@45 270 // << layer->objectName().toStdString() << std::endl;
Chris@45 271
Chris@70 272 if (model == oldMainModel) {
Chris@45 273 // std::cerr << "... it uses the old main model, replacing" << std::endl;
Chris@45 274 LayerFactory::getInstance()->setModel(layer, m_mainModel);
Chris@45 275 continue;
Chris@45 276 }
Chris@45 277
Chris@70 278 if (model && (m_models.find(model) == m_models.end())) {
Chris@45 279 std::cerr << "WARNING: Document::setMainModel: Unknown model "
Chris@45 280 << model << " in layer " << layer << std::endl;
Chris@45 281 // get rid of this hideous degenerate
Chris@45 282 obsoleteLayers.push_back(layer);
Chris@45 283 continue;
Chris@45 284 }
Chris@45 285
Chris@70 286 if (m_models[model].source &&
Chris@70 287 (m_models[model].source == oldMainModel)) {
Chris@45 288
Chris@45 289 // std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl;
Chris@45 290
Chris@45 291 // This model was derived from the previous main
Chris@45 292 // model: regenerate it.
Chris@45 293
Chris@72 294 const Transform &transform = m_models[model].transform;
Chris@72 295 QString transformId = transform.getIdentifier();
Chris@45 296
Chris@72 297 //!!! We have a problem here if the number of channels in
Chris@72 298 //the main model has changed.
Chris@72 299
Chris@45 300 Model *replacementModel =
Chris@45 301 addDerivedModel(transform,
Chris@72 302 ModelTransformer::Input
Chris@72 303 (m_mainModel,
Chris@72 304 m_models[model].channel));
Chris@45 305
Chris@45 306 if (!replacementModel) {
Chris@45 307 std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
Chris@72 308 << transformId.toStdString() << "\"" << " in layer " << layer << std::endl;
Chris@72 309 if (failedTransformers.find(transformId)
Chris@72 310 == failedTransformers.end()) {
Chris@45 311 emit modelRegenerationFailed(layer->objectName(),
Chris@72 312 transformId);
Chris@72 313 failedTransformers.insert(transformId);
Chris@45 314 }
Chris@45 315 obsoleteLayers.push_back(layer);
Chris@45 316 } else {
Chris@70 317 // std::cerr << "Replacing model " << model << " (type "
Chris@70 318 // << typeid(*model).name() << ") with model "
Chris@70 319 // << replacementModel << " (type "
Chris@70 320 // << typeid(*replacementModel).name() << ") in layer "
Chris@70 321 // << layer << " (name " << layer->objectName().toStdString() << ")"
Chris@70 322 // << std::endl;
Chris@45 323 RangeSummarisableTimeValueModel *rm =
Chris@45 324 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
Chris@45 325 if (rm) {
Chris@45 326 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl;
Chris@45 327 } else {
Chris@45 328 std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl;
Chris@45 329 }
Chris@45 330 setModel(layer, replacementModel);
Chris@45 331 }
Chris@45 332 }
Chris@45 333 }
Chris@45 334
Chris@45 335 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
Chris@45 336 deleteLayer(obsoleteLayers[k], true);
Chris@45 337 }
Chris@45 338
Chris@48 339 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@68 340 if (oldMainModel &&
Chris@68 341 (i->first->getAlignmentReference() == oldMainModel)) {
Chris@48 342 alignModel(i->first);
Chris@48 343 }
Chris@48 344 }
Chris@48 345
Chris@45 346 emit mainModelChanged(m_mainModel);
Chris@45 347
Chris@45 348 // we already emitted modelAboutToBeDeleted for this
Chris@45 349 delete oldMainModel;
Chris@45 350 }
Chris@45 351
Chris@45 352 void
Chris@72 353 Document::addDerivedModel(const Transform &transform,
Chris@72 354 const ModelTransformer::Input &input,
Chris@72 355 Model *outputModelToAdd)
Chris@45 356 {
Chris@45 357 if (m_models.find(outputModelToAdd) != m_models.end()) {
Chris@45 358 std::cerr << "WARNING: Document::addDerivedModel: Model already added"
Chris@45 359 << std::endl;
Chris@45 360 return;
Chris@45 361 }
Chris@45 362
Chris@45 363 // std::cerr << "Document::addDerivedModel: source is " << inputModel << " \"" << inputModel->objectName().toStdString() << "\"" << std::endl;
Chris@45 364
Chris@45 365 ModelRecord rec;
Chris@72 366 rec.source = input.getModel();
Chris@72 367 rec.channel = input.getChannel();
Chris@45 368 rec.transform = transform;
Chris@45 369 rec.refcount = 0;
Chris@45 370
Chris@72 371 outputModelToAdd->setSourceModel(input.getModel());
Chris@45 372
Chris@45 373 m_models[outputModelToAdd] = rec;
Chris@45 374
Chris@45 375 emit modelAdded(outputModelToAdd);
Chris@45 376 }
Chris@45 377
Chris@45 378
Chris@45 379 void
Chris@45 380 Document::addImportedModel(Model *model)
Chris@45 381 {
Chris@45 382 if (m_models.find(model) != m_models.end()) {
Chris@45 383 std::cerr << "WARNING: Document::addImportedModel: Model already added"
Chris@45 384 << std::endl;
Chris@45 385 return;
Chris@45 386 }
Chris@45 387
Chris@45 388 ModelRecord rec;
Chris@45 389 rec.source = 0;
Chris@45 390 rec.refcount = 0;
Chris@45 391
Chris@45 392 m_models[model] = rec;
Chris@45 393
Chris@47 394 if (m_autoAlignment) alignModel(model);
Chris@47 395
Chris@45 396 emit modelAdded(model);
Chris@45 397 }
Chris@45 398
Chris@45 399 Model *
Chris@72 400 Document::addDerivedModel(const Transform &transform,
Chris@72 401 const ModelTransformer::Input &input)
Chris@45 402 {
Chris@45 403 Model *model = 0;
Chris@45 404
Chris@45 405 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 406 if (i->second.transform == transform &&
Chris@72 407 i->second.source == input.getModel() &&
Chris@72 408 i->second.channel == input.getChannel()) {
Chris@45 409 return i->first;
Chris@45 410 }
Chris@45 411 }
Chris@45 412
Chris@72 413 model = ModelTransformerFactory::getInstance()->transform(transform, input);
Chris@45 414
Chris@45 415 if (!model) {
Chris@72 416 std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.getIdentifier().toStdString() << std::endl;
Chris@45 417 } else {
Chris@72 418 addDerivedModel(transform, input, model);
Chris@45 419 }
Chris@45 420
Chris@45 421 return model;
Chris@45 422 }
Chris@45 423
Chris@45 424 void
Chris@45 425 Document::releaseModel(Model *model) // Will _not_ release main model!
Chris@45 426 {
Chris@45 427 if (model == 0) {
Chris@45 428 return;
Chris@45 429 }
Chris@45 430
Chris@45 431 if (model == m_mainModel) {
Chris@45 432 return;
Chris@45 433 }
Chris@45 434
Chris@45 435 bool toDelete = false;
Chris@45 436
Chris@45 437 if (m_models.find(model) != m_models.end()) {
Chris@45 438
Chris@45 439 if (m_models[model].refcount == 0) {
Chris@45 440 std::cerr << "WARNING: Document::releaseModel: model " << model
Chris@45 441 << " reference count is zero already!" << std::endl;
Chris@45 442 } else {
Chris@45 443 if (--m_models[model].refcount == 0) {
Chris@45 444 toDelete = true;
Chris@45 445 }
Chris@45 446 }
Chris@45 447 } else {
Chris@45 448 std::cerr << "WARNING: Document::releaseModel: Unfound model "
Chris@45 449 << model << std::endl;
Chris@45 450 toDelete = true;
Chris@45 451 }
Chris@45 452
Chris@45 453 if (toDelete) {
Chris@45 454
Chris@45 455 int sourceCount = 0;
Chris@45 456
Chris@45 457 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 458 if (i->second.source == model) {
Chris@45 459 ++sourceCount;
Chris@45 460 i->second.source = 0;
Chris@45 461 }
Chris@45 462 }
Chris@45 463
Chris@45 464 if (sourceCount > 0) {
Chris@45 465 std::cerr << "Document::releaseModel: Deleting model "
Chris@45 466 << model << " even though it is source for "
Chris@45 467 << sourceCount << " other derived model(s) -- resetting "
Chris@45 468 << "their source fields appropriately" << std::endl;
Chris@45 469 }
Chris@45 470
Chris@45 471 emit modelAboutToBeDeleted(model);
Chris@45 472 model->aboutToDelete();
Chris@45 473 m_models.erase(model);
Chris@45 474 delete model;
Chris@45 475 }
Chris@45 476 }
Chris@45 477
Chris@45 478 void
Chris@45 479 Document::deleteLayer(Layer *layer, bool force)
Chris@45 480 {
Chris@45 481 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
Chris@45 482 m_layerViewMap[layer].size() > 0) {
Chris@45 483
Chris@45 484 std::cerr << "WARNING: Document::deleteLayer: Layer "
Chris@45 485 << layer << " [" << layer->objectName().toStdString() << "]"
Chris@45 486 << " is still used in " << m_layerViewMap[layer].size()
Chris@45 487 << " views!" << std::endl;
Chris@45 488
Chris@45 489 if (force) {
Chris@45 490
Chris@45 491 std::cerr << "(force flag set -- deleting from all views)" << std::endl;
Chris@45 492
Chris@45 493 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
Chris@45 494 j != m_layerViewMap[layer].end(); ++j) {
Chris@45 495 // don't use removeLayerFromView, as it issues a command
Chris@45 496 layer->setLayerDormant(*j, true);
Chris@45 497 (*j)->removeLayer(layer);
Chris@45 498 }
Chris@45 499
Chris@45 500 m_layerViewMap.erase(layer);
Chris@45 501
Chris@45 502 } else {
Chris@45 503 return;
Chris@45 504 }
Chris@45 505 }
Chris@45 506
Chris@45 507 if (m_layers.find(layer) == m_layers.end()) {
Chris@45 508 std::cerr << "Document::deleteLayer: Layer "
Chris@45 509 << layer << " does not exist, or has already been deleted "
Chris@45 510 << "(this may not be as serious as it sounds)" << std::endl;
Chris@45 511 return;
Chris@45 512 }
Chris@45 513
Chris@45 514 m_layers.erase(layer);
Chris@45 515
Chris@52 516 std::cerr << "Document::deleteLayer: Removing, now have "
Chris@52 517 << m_layers.size() << " layers" << std::endl;
Chris@52 518
Chris@45 519 releaseModel(layer->getModel());
Chris@45 520 emit layerRemoved(layer);
Chris@45 521 emit layerAboutToBeDeleted(layer);
Chris@45 522 delete layer;
Chris@45 523 }
Chris@45 524
Chris@45 525 void
Chris@45 526 Document::setModel(Layer *layer, Model *model)
Chris@45 527 {
Chris@45 528 if (model &&
Chris@45 529 model != m_mainModel &&
Chris@45 530 m_models.find(model) == m_models.end()) {
Chris@45 531 std::cerr << "ERROR: Document::setModel: Layer " << layer
Chris@45 532 << " (\"" << layer->objectName().toStdString()
Chris@45 533 << "\") wants to use unregistered model " << model
Chris@45 534 << ": register the layer's model before setting it!"
Chris@45 535 << std::endl;
Chris@45 536 return;
Chris@45 537 }
Chris@45 538
Chris@45 539 Model *previousModel = layer->getModel();
Chris@45 540
Chris@45 541 if (previousModel == model) {
Chris@45 542 std::cerr << "WARNING: Document::setModel: Layer " << layer << " (\""
Chris@45 543 << layer->objectName().toStdString()
Chris@45 544 << "\") is already set to model "
Chris@45 545 << model << " (\""
Chris@45 546 << (model ? model->objectName().toStdString() : "(null)")
Chris@45 547 << "\")" << std::endl;
Chris@45 548 return;
Chris@45 549 }
Chris@45 550
Chris@45 551 if (model && model != m_mainModel) {
Chris@45 552 m_models[model].refcount ++;
Chris@45 553 }
Chris@45 554
Chris@45 555 if (model && previousModel) {
Chris@45 556 PlayParameterRepository::getInstance()->copyParameters
Chris@45 557 (previousModel, model);
Chris@45 558 }
Chris@45 559
Chris@45 560 LayerFactory::getInstance()->setModel(layer, model);
Chris@45 561
Chris@45 562 if (previousModel) {
Chris@45 563 releaseModel(previousModel);
Chris@45 564 }
Chris@45 565 }
Chris@45 566
Chris@45 567 void
Chris@45 568 Document::setChannel(Layer *layer, int channel)
Chris@45 569 {
Chris@45 570 LayerFactory::getInstance()->setChannel(layer, channel);
Chris@45 571 }
Chris@45 572
Chris@45 573 void
Chris@45 574 Document::addLayerToView(View *view, Layer *layer)
Chris@45 575 {
Chris@45 576 Model *model = layer->getModel();
Chris@45 577 if (!model) {
Chris@45 578 // std::cerr << "Document::addLayerToView: Layer (\""
Chris@45 579 // << layer->objectName().toStdString()
Chris@45 580 // << "\") with no model being added to view: "
Chris@45 581 // << "normally you want to set the model first" << std::endl;
Chris@45 582 } else {
Chris@45 583 if (model != m_mainModel &&
Chris@45 584 m_models.find(model) == m_models.end()) {
Chris@45 585 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer
Chris@45 586 << " has unregistered model " << model
Chris@45 587 << " -- register the layer's model before adding the layer!" << std::endl;
Chris@45 588 return;
Chris@45 589 }
Chris@45 590 }
Chris@45 591
Chris@45 592 CommandHistory::getInstance()->addCommand
Chris@45 593 (new Document::AddLayerCommand(this, view, layer));
Chris@45 594 }
Chris@45 595
Chris@45 596 void
Chris@45 597 Document::removeLayerFromView(View *view, Layer *layer)
Chris@45 598 {
Chris@45 599 CommandHistory::getInstance()->addCommand
Chris@45 600 (new Document::RemoveLayerCommand(this, view, layer));
Chris@45 601 }
Chris@45 602
Chris@45 603 void
Chris@45 604 Document::addToLayerViewMap(Layer *layer, View *view)
Chris@45 605 {
Chris@45 606 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
Chris@45 607 m_layerViewMap[layer].empty());
Chris@45 608
Chris@45 609 if (m_layerViewMap[layer].find(view) !=
Chris@45 610 m_layerViewMap[layer].end()) {
Chris@45 611 std::cerr << "WARNING: Document::addToLayerViewMap:"
Chris@45 612 << " Layer " << layer << " -> view " << view << " already in"
Chris@45 613 << " layer view map -- internal inconsistency" << std::endl;
Chris@45 614 }
Chris@45 615
Chris@45 616 m_layerViewMap[layer].insert(view);
Chris@45 617
Chris@45 618 if (firstView) emit layerInAView(layer, true);
Chris@45 619 }
Chris@45 620
Chris@45 621 void
Chris@45 622 Document::removeFromLayerViewMap(Layer *layer, View *view)
Chris@45 623 {
Chris@45 624 if (m_layerViewMap[layer].find(view) ==
Chris@45 625 m_layerViewMap[layer].end()) {
Chris@45 626 std::cerr << "WARNING: Document::removeFromLayerViewMap:"
Chris@45 627 << " Layer " << layer << " -> view " << view << " not in"
Chris@45 628 << " layer view map -- internal inconsistency" << std::endl;
Chris@45 629 }
Chris@45 630
Chris@45 631 m_layerViewMap[layer].erase(view);
Chris@45 632
Chris@45 633 if (m_layerViewMap[layer].empty()) {
Chris@45 634 m_layerViewMap.erase(layer);
Chris@45 635 emit layerInAView(layer, false);
Chris@45 636 }
Chris@45 637 }
Chris@45 638
Chris@45 639 QString
Chris@45 640 Document::getUniqueLayerName(QString candidate)
Chris@45 641 {
Chris@45 642 for (int count = 1; ; ++count) {
Chris@45 643
Chris@45 644 QString adjusted =
Chris@45 645 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
Chris@45 646 candidate);
Chris@45 647
Chris@45 648 bool duplicate = false;
Chris@45 649
Chris@45 650 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 651 if ((*i)->objectName() == adjusted) {
Chris@45 652 duplicate = true;
Chris@45 653 break;
Chris@45 654 }
Chris@45 655 }
Chris@45 656
Chris@45 657 if (!duplicate) return adjusted;
Chris@45 658 }
Chris@45 659 }
Chris@45 660
Chris@45 661 std::vector<Model *>
Chris@72 662 Document::getTransformInputModels()
Chris@45 663 {
Chris@45 664 std::vector<Model *> models;
Chris@45 665
Chris@45 666 if (!m_mainModel) return models;
Chris@45 667
Chris@45 668 models.push_back(m_mainModel);
Chris@45 669
Chris@45 670 //!!! This will pick up all models, including those that aren't visible...
Chris@45 671
Chris@45 672 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 673
Chris@45 674 Model *model = i->first;
Chris@45 675 if (!model || model == m_mainModel) continue;
Chris@45 676 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@45 677
Chris@45 678 if (dtvm) {
Chris@45 679 models.push_back(dtvm);
Chris@45 680 }
Chris@45 681 }
Chris@45 682
Chris@45 683 return models;
Chris@45 684 }
Chris@45 685
Chris@50 686 bool
Chris@51 687 Document::canAlign()
Chris@50 688 {
Chris@54 689 TransformId id = "vamp:match-vamp-plugin:match:path";
Chris@54 690 TransformFactory *factory = TransformFactory::getInstance();
Chris@54 691 return factory->haveTransform(id);
Chris@50 692 }
Chris@50 693
Chris@45 694 void
Chris@45 695 Document::alignModel(Model *model)
Chris@45 696 {
Chris@45 697 if (!m_mainModel || model == m_mainModel) return;
Chris@45 698
Chris@45 699 RangeSummarisableTimeValueModel *rm =
Chris@45 700 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
Chris@45 701 if (!rm) return;
Chris@48 702
Chris@48 703 if (rm->getAlignmentReference() == m_mainModel) return;
Chris@45 704
Chris@45 705 // This involves creating three new models:
Chris@45 706
Chris@45 707 // 1. an AggregateWaveModel to provide the mixdowns of the main
Chris@45 708 // model and the new model in its two channels, as input to the
Chris@45 709 // MATCH plugin
Chris@45 710
Chris@45 711 // 2. a SparseTimeValueModel, which is the model automatically
Chris@53 712 // created by FeatureExtractionPluginTransformer when running the
Chris@45 713 // MATCH plugin (thus containing the alignment path)
Chris@45 714
Chris@45 715 // 3. an AlignmentModel, which stores the path model and carries
Chris@45 716 // out alignment lookups on it.
Chris@45 717
Chris@45 718 // The first two of these are provided as arguments to the
Chris@45 719 // constructor for the third, which takes responsibility for
Chris@45 720 // deleting them. The AlignmentModel, meanwhile, is passed to the
Chris@45 721 // new model we are aligning, which also takes responsibility for
Chris@45 722 // it. We should not have to delete any of these new models here.
Chris@45 723
Chris@45 724 AggregateWaveModel::ChannelSpecList components;
Chris@45 725
Chris@45 726 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 727 (m_mainModel, -1));
Chris@45 728
Chris@45 729 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 730 (rm, -1));
Chris@45 731
Chris@45 732 Model *aggregate = new AggregateWaveModel(components);
Chris@45 733
Chris@72 734 TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure
Chris@45 735
Chris@72 736 TransformFactory *tf = TransformFactory::getInstance();
Chris@45 737
Chris@72 738 Transform transform = tf->getDefaultTransformFor
Chris@72 739 (id, aggregate->getSampleRate());
Chris@57 740
Chris@72 741 transform.setStepSize(transform.getBlockSize()/2);
Chris@72 742 transform.setParameter("serialise", 1);
Chris@64 743
Chris@72 744 //!!! QString args = "<plugin param-serialise=\"1\"/>";
Chris@72 745 // Model *transformOutput = factory->transform(id, aggregate, context, args);
Chris@72 746
Chris@72 747 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@72 748
Chris@72 749 Model *transformOutput = mtf->transform(transform, aggregate);
Chris@64 750
Chris@64 751 if (!transformOutput) {
Chris@72 752 transform.setStepSize(0);
Chris@72 753 transformOutput = mtf->transform(transform, aggregate);
Chris@64 754 }
Chris@45 755
Chris@45 756 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
Chris@45 757 (transformOutput);
Chris@45 758
Chris@45 759 if (!path) {
Chris@45 760 std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl;
Chris@45 761 delete transformOutput;
Chris@45 762 delete aggregate;
Chris@45 763 return;
Chris@45 764 }
Chris@45 765
Chris@45 766 AlignmentModel *alignmentModel = new AlignmentModel
Chris@45 767 (m_mainModel, model, aggregate, path);
Chris@45 768
Chris@45 769 rm->setAlignment(alignmentModel);
Chris@45 770 }
Chris@45 771
Chris@45 772 void
Chris@45 773 Document::alignModels()
Chris@45 774 {
Chris@45 775 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 776 alignModel(i->first);
Chris@45 777 }
Chris@45 778 }
Chris@45 779
Chris@45 780 Document::AddLayerCommand::AddLayerCommand(Document *d,
Chris@45 781 View *view,
Chris@45 782 Layer *layer) :
Chris@45 783 m_d(d),
Chris@45 784 m_view(view),
Chris@45 785 m_layer(layer),
Chris@45 786 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
Chris@45 787 m_added(false)
Chris@45 788 {
Chris@45 789 }
Chris@45 790
Chris@45 791 Document::AddLayerCommand::~AddLayerCommand()
Chris@45 792 {
Chris@45 793 // std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl;
Chris@45 794 if (!m_added) {
Chris@45 795 m_d->deleteLayer(m_layer);
Chris@45 796 }
Chris@45 797 }
Chris@45 798
Chris@45 799 void
Chris@45 800 Document::AddLayerCommand::execute()
Chris@45 801 {
Chris@45 802 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 803 if (m_view->getLayer(i) == m_layer) {
Chris@45 804 // already there
Chris@45 805 m_layer->setLayerDormant(m_view, false);
Chris@45 806 m_added = true;
Chris@45 807 return;
Chris@45 808 }
Chris@45 809 }
Chris@45 810
Chris@45 811 m_view->addLayer(m_layer);
Chris@45 812 m_layer->setLayerDormant(m_view, false);
Chris@45 813
Chris@45 814 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 815 m_added = true;
Chris@45 816 }
Chris@45 817
Chris@45 818 void
Chris@45 819 Document::AddLayerCommand::unexecute()
Chris@45 820 {
Chris@45 821 m_view->removeLayer(m_layer);
Chris@45 822 m_layer->setLayerDormant(m_view, true);
Chris@45 823
Chris@45 824 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 825 m_added = false;
Chris@45 826 }
Chris@45 827
Chris@45 828 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
Chris@45 829 View *view,
Chris@45 830 Layer *layer) :
Chris@45 831 m_d(d),
Chris@45 832 m_view(view),
Chris@45 833 m_layer(layer),
Chris@45 834 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
Chris@45 835 m_added(true)
Chris@45 836 {
Chris@45 837 }
Chris@45 838
Chris@45 839 Document::RemoveLayerCommand::~RemoveLayerCommand()
Chris@45 840 {
Chris@45 841 // std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl;
Chris@45 842 if (!m_added) {
Chris@45 843 m_d->deleteLayer(m_layer);
Chris@45 844 }
Chris@45 845 }
Chris@45 846
Chris@45 847 void
Chris@45 848 Document::RemoveLayerCommand::execute()
Chris@45 849 {
Chris@45 850 bool have = false;
Chris@45 851 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 852 if (m_view->getLayer(i) == m_layer) {
Chris@45 853 have = true;
Chris@45 854 break;
Chris@45 855 }
Chris@45 856 }
Chris@45 857
Chris@45 858 if (!have) { // not there!
Chris@45 859 m_layer->setLayerDormant(m_view, true);
Chris@45 860 m_added = false;
Chris@45 861 return;
Chris@45 862 }
Chris@45 863
Chris@45 864 m_view->removeLayer(m_layer);
Chris@45 865 m_layer->setLayerDormant(m_view, true);
Chris@45 866
Chris@45 867 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 868 m_added = false;
Chris@45 869 }
Chris@45 870
Chris@45 871 void
Chris@45 872 Document::RemoveLayerCommand::unexecute()
Chris@45 873 {
Chris@45 874 m_view->addLayer(m_layer);
Chris@45 875 m_layer->setLayerDormant(m_view, false);
Chris@45 876
Chris@45 877 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 878 m_added = true;
Chris@45 879 }
Chris@45 880
Chris@45 881 void
Chris@45 882 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
Chris@45 883 {
Chris@45 884 out << indent + QString("<data%1%2>\n")
Chris@45 885 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
Chris@45 886
Chris@45 887 if (m_mainModel) {
Chris@45 888 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
Chris@45 889 }
Chris@45 890
Chris@45 891 // Models that are not used in a layer that is in a view should
Chris@45 892 // not be written. Get our list of required models first.
Chris@45 893
Chris@45 894 std::set<const Model *> used;
Chris@45 895
Chris@45 896 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
Chris@45 897 i != m_layerViewMap.end(); ++i) {
Chris@45 898
Chris@45 899 if (i->first && !i->second.empty() && i->first->getModel()) {
Chris@45 900 used.insert(i->first->getModel());
Chris@45 901 }
Chris@45 902 }
Chris@45 903
Chris@45 904 for (ModelMap::const_iterator i = m_models.begin();
Chris@45 905 i != m_models.end(); ++i) {
Chris@45 906
Chris@45 907 const Model *model = i->first;
Chris@45 908 const ModelRecord &rec = i->second;
Chris@45 909
Chris@45 910 if (used.find(model) == used.end()) continue;
Chris@45 911
Chris@45 912 // We need an intelligent way to determine which models need
Chris@45 913 // to be streamed (i.e. have been edited, or are small) and
Chris@45 914 // which should not be (i.e. remain as generated by a
Chris@45 915 // transform, and are large).
Chris@45 916 //
Chris@45 917 // At the moment we can get away with deciding not to stream
Chris@45 918 // dense 3d models or writable wave file models, provided they
Chris@45 919 // were generated from a transform, because at the moment there
Chris@45 920 // is no way to edit those model types so it should be safe to
Chris@45 921 // regenerate them. That won't always work in future though.
Chris@45 922 // It would be particularly nice to be able to ask the user,
Chris@45 923 // as well as making an intelligent guess.
Chris@45 924
Chris@45 925 bool writeModel = true;
Chris@45 926 bool haveDerivation = false;
Chris@45 927
Chris@72 928 if (rec.source && rec.transform.getIdentifier() != "") {
Chris@45 929 haveDerivation = true;
Chris@45 930 }
Chris@45 931
Chris@45 932 if (haveDerivation) {
Chris@45 933 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
Chris@45 934 writeModel = false;
Chris@45 935 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
Chris@45 936 writeModel = false;
Chris@45 937 }
Chris@45 938 }
Chris@45 939
Chris@45 940 if (writeModel) {
Chris@45 941 i->first->toXml(out, indent + " ");
Chris@45 942 }
Chris@45 943
Chris@45 944 if (haveDerivation) {
Chris@72 945 writeBackwardCompatibleDerivation(out, indent + " ",
Chris@72 946 i->first, rec);
Chris@45 947 }
Chris@45 948
Chris@45 949 //!!! We should probably own the PlayParameterRepository
Chris@45 950 PlayParameters *playParameters =
Chris@45 951 PlayParameterRepository::getInstance()->getPlayParameters(i->first);
Chris@45 952 if (playParameters) {
Chris@45 953 playParameters->toXml
Chris@45 954 (out, indent + " ",
Chris@45 955 QString("model=\"%1\"")
Chris@45 956 .arg(XmlExportable::getObjectExportId(i->first)));
Chris@45 957 }
Chris@45 958 }
Chris@45 959
Chris@45 960 for (LayerSet::const_iterator i = m_layers.begin();
Chris@45 961 i != m_layers.end(); ++i) {
Chris@45 962
Chris@45 963 (*i)->toXml(out, indent + " ");
Chris@45 964 }
Chris@45 965
Chris@45 966 out << indent + "</data>\n";
Chris@45 967 }
Chris@45 968
Chris@72 969 void
Chris@72 970 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
Chris@72 971 Model *targetModel,
Chris@72 972 const ModelRecord &rec) const
Chris@72 973 {
Chris@72 974 // There is a lot of redundancy in the XML we output here, because
Chris@72 975 // we want it to work with older SV session file reading code as
Chris@72 976 // well.
Chris@72 977 //
Chris@72 978 // Formerly, a transform was described using a derivation element
Chris@72 979 // which set out the source and target models, execution context
Chris@72 980 // (step size, input channel etc) and transform id, containing a
Chris@72 981 // plugin element which set out the transform parameters and so
Chris@72 982 // on. (The plugin element came from a "configurationXml" string
Chris@72 983 // obtained from PluginXml.)
Chris@72 984 //
Chris@72 985 // This has been replaced by a derivation element setting out the
Chris@72 986 // source and target models and input channel, containing a
Chris@72 987 // transform element which sets out everything in the Transform.
Chris@72 988 //
Chris@72 989 // In order to retain compatibility with older SV code, however,
Chris@72 990 // we have to write out the same stuff into the derivation as
Chris@72 991 // before, and manufacture an appropriate plugin element as well
Chris@72 992 // as the transform element. In order that newer code knows it's
Chris@72 993 // dealing with a newer format, we will also write an attribute
Chris@72 994 // 'type="transform"' in the derivation element.
Chris@45 995
Chris@72 996 const Transform &transform = rec.transform;
Chris@72 997
Chris@72 998 // Just for reference, this is what we would write if we didn't
Chris@72 999 // have to be backward compatible:
Chris@72 1000 //
Chris@72 1001 // out << indent
Chris@72 1002 // << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1003 // "model=\"%2\" channel=\"%3\">\n")
Chris@72 1004 // .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1005 // .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1006 // .arg(rec.channel);
Chris@72 1007 //
Chris@72 1008 // transform.toXml(out, indent + " ");
Chris@72 1009 //
Chris@72 1010 // out << indent << "</derivation>\n";
Chris@72 1011 //
Chris@72 1012 // Unfortunately, we can't just do that. So we do this...
Chris@72 1013
Chris@72 1014 QString extentsAttributes;
Chris@72 1015 if (transform.getStartTime() != RealTime::zeroTime ||
Chris@72 1016 transform.getDuration() != RealTime::zeroTime) {
Chris@72 1017 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
Chris@72 1018 .arg(RealTime::realTime2Frame(transform.getStartTime(),
Chris@72 1019 targetModel->getSampleRate()))
Chris@72 1020 .arg(RealTime::realTime2Frame(transform.getDuration(),
Chris@72 1021 targetModel->getSampleRate()));
Chris@72 1022 }
Chris@72 1023
Chris@72 1024 out << indent;
Chris@72 1025 out << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1026 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
Chris@72 1027 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
Chris@72 1028 "transform=\"%9\">\n")
Chris@72 1029 .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1030 .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1031 .arg(rec.channel)
Chris@72 1032 .arg(TransformFactory::getInstance()->getTransformInputDomain
Chris@72 1033 (transform.getIdentifier()))
Chris@72 1034 .arg(transform.getStepSize())
Chris@72 1035 .arg(transform.getBlockSize())
Chris@72 1036 .arg(extentsAttributes)
Chris@72 1037 .arg(int(transform.getWindowType()))
Chris@72 1038 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
Chris@72 1039
Chris@72 1040 transform.toXml(out, indent + " ");
Chris@72 1041
Chris@72 1042 out << indent << " "
Chris@72 1043 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
Chris@72 1044
Chris@72 1045 out << indent << "</derivation>\n";
Chris@72 1046 }
Chris@72 1047