annotate framework/Document.cpp @ 313:58582119c92a tonioni

Add a basic continuous synth implementation (simple sinusoids only, no gaps)
author Chris Cannam
date Wed, 08 Jan 2014 13:07:22 +0000
parents 1e61f0c26593
children f72d58d1ccb0
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"
gyorgyf@270 22 #include "data/model/FlexiNoteModel.h"
gyorgyf@270 23
Chris@45 24 #include "layer/Layer.h"
Chris@105 25 #include "widgets/CommandHistory.h"
Chris@45 26 #include "base/Command.h"
Chris@45 27 #include "view/View.h"
Chris@45 28 #include "base/PlayParameterRepository.h"
Chris@45 29 #include "base/PlayParameters.h"
Chris@106 30 #include "transform/TransformFactory.h"
Chris@106 31 #include "transform/ModelTransformerFactory.h"
gyorgyf@270 32 #include "transform/FeatureExtractionModelTransformer.h"
Chris@45 33 #include <QApplication>
Chris@45 34 #include <QTextStream>
Chris@90 35 #include <QSettings>
Chris@45 36 #include <iostream>
Chris@212 37 #include <typeinfo>
Chris@45 38
Chris@45 39 // For alignment:
Chris@45 40 #include "data/model/AggregateWaveModel.h"
Chris@45 41 #include "data/model/SparseTimeValueModel.h"
Chris@45 42 #include "data/model/AlignmentModel.h"
Chris@45 43
Chris@297 44 using std::vector;
Chris@297 45
Chris@138 46 //#define DEBUG_DOCUMENT 1
Chris@77 47
Chris@45 48 //!!! still need to handle command history, documentRestored/documentModified
Chris@45 49
Chris@45 50 Document::Document() :
Chris@47 51 m_mainModel(0),
Chris@47 52 m_autoAlignment(false)
Chris@45 53 {
Chris@45 54 connect(this, SIGNAL(modelAboutToBeDeleted(Model *)),
Chris@54 55 ModelTransformerFactory::getInstance(),
Chris@45 56 SLOT(modelAboutToBeDeleted(Model *)));
Chris@45 57 }
Chris@45 58
Chris@45 59 Document::~Document()
Chris@45 60 {
Chris@45 61 //!!! Document should really own the command history. atm we
Chris@45 62 //still refer to it in various places that don't have access to
Chris@45 63 //the document, be nice to fix that
Chris@45 64
Chris@77 65 #ifdef DEBUG_DOCUMENT
Chris@293 66 cerr << "\n\nDocument::~Document: about to clear command history" << endl;
Chris@77 67 #endif
Chris@45 68 CommandHistory::getInstance()->clear();
Chris@45 69
Chris@77 70 #ifdef DEBUG_DOCUMENT
Chris@233 71 SVDEBUG << "Document::~Document: about to delete layers" << endl;
Chris@77 72 #endif
Chris@45 73 while (!m_layers.empty()) {
Chris@45 74 deleteLayer(*m_layers.begin(), true);
Chris@45 75 }
Chris@45 76
Chris@45 77 if (!m_models.empty()) {
Chris@233 78 SVDEBUG << "Document::~Document: WARNING: "
Chris@45 79 << m_models.size() << " model(s) still remain -- "
Chris@45 80 << "should have been garbage collected when deleting layers"
Chris@229 81 << endl;
Chris@45 82 while (!m_models.empty()) {
Chris@45 83 Model *model = m_models.begin()->first;
Chris@45 84 if (model == m_mainModel) {
Chris@45 85 // just in case!
Chris@233 86 SVDEBUG << "Document::~Document: WARNING: Main model is also"
Chris@229 87 << " in models list!" << endl;
Chris@45 88 } else if (model) {
Chris@79 89 model->aboutToDelete();
Chris@45 90 emit modelAboutToBeDeleted(model);
Chris@45 91 delete model;
Chris@45 92 }
Chris@45 93 m_models.erase(m_models.begin());
Chris@45 94 }
Chris@45 95 }
Chris@45 96
Chris@77 97 #ifdef DEBUG_DOCUMENT
Chris@233 98 SVDEBUG << "Document::~Document: About to get rid of main model"
Chris@229 99 << endl;
Chris@77 100 #endif
Chris@45 101 if (m_mainModel) {
Chris@79 102 m_mainModel->aboutToDelete();
Chris@45 103 emit modelAboutToBeDeleted(m_mainModel);
Chris@45 104 }
Chris@45 105
Chris@45 106 emit mainModelChanged(0);
Chris@45 107 delete m_mainModel;
Chris@45 108
Chris@45 109 }
Chris@45 110
Chris@45 111 Layer *
Chris@45 112 Document::createLayer(LayerFactory::LayerType type)
Chris@45 113 {
Chris@45 114 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 115 if (!newLayer) return 0;
Chris@45 116
Chris@45 117 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 118
Chris@45 119 m_layers.insert(newLayer);
Chris@52 120
Chris@77 121 #ifdef DEBUG_DOCUMENT
Chris@233 122 SVDEBUG << "Document::createLayer: Added layer of type " << type
Chris@229 123 << ", now have " << m_layers.size() << " layers" << endl;
Chris@77 124 #endif
Chris@52 125
Chris@45 126 emit layerAdded(newLayer);
Chris@45 127
Chris@45 128 return newLayer;
Chris@45 129 }
Chris@45 130
Chris@45 131 Layer *
Chris@45 132 Document::createMainModelLayer(LayerFactory::LayerType type)
Chris@45 133 {
Chris@45 134 Layer *newLayer = createLayer(type);
Chris@45 135 if (!newLayer) return 0;
Chris@45 136 setModel(newLayer, m_mainModel);
Chris@45 137 return newLayer;
Chris@45 138 }
Chris@45 139
Chris@45 140 Layer *
Chris@45 141 Document::createImportedLayer(Model *model)
Chris@45 142 {
Chris@45 143 LayerFactory::LayerTypeSet types =
Chris@45 144 LayerFactory::getInstance()->getValidLayerTypes(model);
Chris@45 145
Chris@45 146 if (types.empty()) {
Chris@293 147 cerr << "WARNING: Document::importLayer: no valid display layer for model" << endl;
Chris@45 148 return 0;
Chris@45 149 }
Chris@45 150
Chris@45 151 //!!! for now, just use the first suitable layer type
Chris@45 152 LayerFactory::LayerType type = *types.begin();
Chris@45 153
Chris@45 154 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 155 if (!newLayer) return 0;
Chris@45 156
Chris@45 157 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 158
Chris@45 159 addImportedModel(model);
Chris@45 160 setModel(newLayer, model);
Chris@45 161
Chris@45 162 //!!! and all channels
Chris@45 163 setChannel(newLayer, -1);
Chris@45 164
Chris@45 165 m_layers.insert(newLayer);
Chris@52 166
Chris@77 167 #ifdef DEBUG_DOCUMENT
Chris@233 168 SVDEBUG << "Document::createImportedLayer: Added layer of type " << type
Chris@229 169 << ", now have " << m_layers.size() << " layers" << endl;
Chris@77 170 #endif
Chris@52 171
Chris@45 172 emit layerAdded(newLayer);
Chris@45 173 return newLayer;
Chris@45 174 }
Chris@45 175
Chris@45 176 Layer *
Chris@45 177 Document::createEmptyLayer(LayerFactory::LayerType type)
Chris@45 178 {
Chris@61 179 if (!m_mainModel) return 0;
Chris@61 180
Chris@45 181 Model *newModel =
Chris@45 182 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
Chris@45 183 if (!newModel) return 0;
Chris@45 184
Chris@45 185 Layer *newLayer = createLayer(type);
Chris@45 186 if (!newLayer) {
Chris@45 187 delete newModel;
Chris@45 188 return 0;
Chris@45 189 }
Chris@45 190
Chris@45 191 addImportedModel(newModel);
Chris@45 192 setModel(newLayer, newModel);
Chris@45 193
Chris@45 194 return newLayer;
Chris@45 195 }
Chris@45 196
Chris@45 197 Layer *
Chris@45 198 Document::createDerivedLayer(LayerFactory::LayerType type,
Chris@54 199 TransformId transform)
Chris@45 200 {
Chris@45 201 Layer *newLayer = createLayer(type);
Chris@45 202 if (!newLayer) return 0;
Chris@45 203
Chris@45 204 newLayer->setObjectName(getUniqueLayerName
Chris@54 205 (TransformFactory::getInstance()->
Chris@54 206 getTransformFriendlyName(transform)));
Chris@45 207
Chris@45 208 return newLayer;
Chris@45 209 }
Chris@297 210
Chris@45 211 Layer *
Chris@72 212 Document::createDerivedLayer(const Transform &transform,
Chris@72 213 const ModelTransformer::Input &input)
Chris@45 214 {
Chris@297 215 Transforms transforms;
Chris@297 216 transforms.push_back(transform);
Chris@297 217 vector<Layer *> layers = createDerivedLayers(transforms, input);
Chris@297 218 if (layers.empty()) return 0;
Chris@297 219 else return layers[0];
Chris@297 220 }
Chris@297 221
Chris@297 222 vector<Layer *>
Chris@297 223 Document::createDerivedLayers(const Transforms &transforms,
Chris@297 224 const ModelTransformer::Input &input)
Chris@297 225 {
Chris@78 226 QString message;
Chris@297 227 vector<Model *> newModels = addDerivedModels(transforms, input, message);
Chris@297 228
Chris@297 229 if (newModels.empty()) {
Chris@297 230 //!!! This identifier may be wrong!
Chris@297 231 emit modelGenerationFailed(transforms[0].getIdentifier(), message);
Chris@297 232 return vector<Layer *>();
Chris@78 233 } else if (message != "") {
Chris@297 234 //!!! This identifier may be wrong!
Chris@297 235 emit modelGenerationWarning(transforms[0].getIdentifier(), message);
Chris@45 236 }
Chris@45 237
Chris@297 238 vector<Layer *> layers;
Chris@45 239
Chris@297 240 for (int i = 0; i < (int)newModels.size(); ++i) {
Chris@297 241
Chris@297 242 Model *newModel = newModels[i];
Chris@297 243
Chris@297 244 LayerFactory::LayerTypeSet types =
Chris@297 245 LayerFactory::getInstance()->getValidLayerTypes(newModel);
Chris@297 246
Chris@297 247 if (types.empty()) {
Chris@297 248 cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transforms[i].getIdentifier() << endl;
Chris@297 249 //!!! inadequate cleanup:
Chris@297 250 newModel->aboutToDelete();
Chris@297 251 emit modelAboutToBeDeleted(newModel);
Chris@297 252 m_models.erase(newModel);
Chris@297 253 delete newModel;
Chris@297 254 return vector<Layer *>();
Chris@297 255 }
Chris@297 256
Chris@297 257 //!!! for now, just use the first suitable layer type
Chris@297 258
Chris@297 259 Layer *newLayer = createLayer(*types.begin());
Chris@297 260 setModel(newLayer, newModel);
Chris@297 261
Chris@297 262 //!!! We need to clone the model when adding the layer, so that it
Chris@297 263 //can be edited without affecting other layers that are based on
Chris@297 264 //the same model. Unfortunately we can't just clone it now,
Chris@297 265 //because it probably hasn't been completed yet -- the transform
Chris@297 266 //runs in the background. Maybe the transform has to handle
Chris@297 267 //cloning and cacheing models itself.
Chris@297 268 //
Chris@297 269 // Once we do clone models here, of course, we'll have to avoid
Chris@297 270 // leaking them too.
Chris@297 271 //
Chris@297 272 // We want the user to be able to add a model to a second layer
Chris@297 273 // _while it's still being calculated in the first_ and have it
Chris@297 274 // work quickly. That means we need to put the same physical
Chris@297 275 // model pointer in both layers, so they can't actually be cloned.
Chris@297 276
Chris@297 277 if (newLayer) {
Chris@297 278 newLayer->setObjectName(getUniqueLayerName
Chris@297 279 (TransformFactory::getInstance()->
Chris@297 280 getTransformFriendlyName
Chris@297 281 (transforms[i].getIdentifier())));
Chris@297 282 }
Chris@297 283
Chris@297 284 emit layerAdded(newLayer);
Chris@297 285 layers.push_back(newLayer);
Chris@45 286 }
Chris@45 287
Chris@297 288 return layers;
Chris@45 289 }
Chris@45 290
Chris@45 291 void
Chris@45 292 Document::setMainModel(WaveFileModel *model)
Chris@45 293 {
Chris@45 294 Model *oldMainModel = m_mainModel;
Chris@45 295 m_mainModel = model;
Chris@160 296
Chris@45 297 emit modelAdded(m_mainModel);
Chris@160 298 if (model) {
Chris@160 299 emit activity(tr("Set main model to %1").arg(model->objectName()));
Chris@160 300 } else {
Chris@160 301 emit activity(tr("Clear main model"));
Chris@160 302 }
Chris@45 303
Chris@45 304 std::vector<Layer *> obsoleteLayers;
Chris@53 305 std::set<QString> failedTransformers;
Chris@45 306
Chris@45 307 // We need to ensure that no layer is left using oldMainModel or
Chris@45 308 // any of the old derived models as its model. Either replace the
Chris@45 309 // model, or delete the layer for each layer that is currently
Chris@45 310 // using one of these. Carry out this replacement before we
Chris@45 311 // delete any of the models.
Chris@45 312
Chris@77 313 #ifdef DEBUG_DOCUMENT
Chris@293 314 cerr << "Document::setMainModel: Have "
Chris@293 315 << m_layers.size() << " layers" << endl;
Chris@293 316 cerr << "Models now: ";
Chris@137 317 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@293 318 cerr << i->first << " ";
Chris@137 319 }
Chris@293 320 cerr << endl;
Chris@293 321 cerr << "Old main model: " << oldMainModel << endl;
Chris@77 322 #endif
Chris@52 323
Chris@45 324 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 325
Chris@45 326 Layer *layer = *i;
Chris@45 327 Model *model = layer->getModel();
Chris@45 328
Chris@77 329 #ifdef DEBUG_DOCUMENT
Chris@293 330 cerr << "Document::setMainModel: inspecting model "
Chris@229 331 << (model ? model->objectName(): "(null)") << " in layer "
Chris@293 332 << layer->objectName() << endl;
Chris@77 333 #endif
Chris@45 334
Chris@70 335 if (model == oldMainModel) {
Chris@77 336 #ifdef DEBUG_DOCUMENT
Chris@293 337 cerr << "... it uses the old main model, replacing" << endl;
Chris@77 338 #endif
Chris@45 339 LayerFactory::getInstance()->setModel(layer, m_mainModel);
Chris@45 340 continue;
Chris@45 341 }
Chris@45 342
Chris@137 343 if (!model) {
Chris@293 344 cerr << "WARNING: Document::setMainModel: Null model in layer "
Chris@293 345 << layer << endl;
Chris@137 346 // get rid of this hideous degenerate
Chris@137 347 obsoleteLayers.push_back(layer);
Chris@137 348 continue;
Chris@137 349 }
Chris@137 350
Chris@137 351 if (m_models.find(model) == m_models.end()) {
Chris@293 352 cerr << "WARNING: Document::setMainModel: Unknown model "
Chris@293 353 << model << " in layer " << layer << endl;
Chris@137 354 // and this one
Chris@45 355 obsoleteLayers.push_back(layer);
Chris@45 356 continue;
Chris@45 357 }
Chris@45 358
Chris@70 359 if (m_models[model].source &&
Chris@70 360 (m_models[model].source == oldMainModel)) {
Chris@45 361
Chris@77 362 #ifdef DEBUG_DOCUMENT
Chris@293 363 cerr << "... it uses a model derived from the old main model, regenerating" << endl;
Chris@77 364 #endif
Chris@45 365
Chris@45 366 // This model was derived from the previous main
Chris@45 367 // model: regenerate it.
Chris@45 368
Chris@72 369 const Transform &transform = m_models[model].transform;
Chris@72 370 QString transformId = transform.getIdentifier();
Chris@45 371
Chris@72 372 //!!! We have a problem here if the number of channels in
Chris@72 373 //the main model has changed.
Chris@72 374
Chris@78 375 QString message;
Chris@45 376 Model *replacementModel =
Chris@45 377 addDerivedModel(transform,
Chris@72 378 ModelTransformer::Input
Chris@78 379 (m_mainModel, m_models[model].channel),
Chris@78 380 message);
Chris@45 381
Chris@45 382 if (!replacementModel) {
Chris@293 383 cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
Chris@293 384 << transformId << "\"" << " in layer " << layer << endl;
Chris@72 385 if (failedTransformers.find(transformId)
Chris@72 386 == failedTransformers.end()) {
Chris@45 387 emit modelRegenerationFailed(layer->objectName(),
Chris@78 388 transformId,
Chris@78 389 message);
Chris@72 390 failedTransformers.insert(transformId);
Chris@45 391 }
Chris@45 392 obsoleteLayers.push_back(layer);
Chris@45 393 } else {
Chris@78 394 if (message != "") {
Chris@78 395 emit modelRegenerationWarning(layer->objectName(),
Chris@78 396 transformId,
Chris@78 397 message);
Chris@78 398 }
Chris@77 399 #ifdef DEBUG_DOCUMENT
Chris@293 400 cerr << "Replacing model " << model << " (type "
Chris@77 401 << typeid(*model).name() << ") with model "
Chris@77 402 << replacementModel << " (type "
Chris@77 403 << typeid(*replacementModel).name() << ") in layer "
Chris@228 404 << layer << " (name " << layer->objectName() << ")"
Chris@293 405 << endl;
Chris@77 406 #endif
Chris@45 407 RangeSummarisableTimeValueModel *rm =
Chris@45 408 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
Chris@77 409 #ifdef DEBUG_DOCUMENT
Chris@45 410 if (rm) {
Chris@293 411 cerr << "new model has " << rm->getChannelCount() << " channels " << endl;
Chris@45 412 } else {
Chris@293 413 cerr << "new model " << replacementModel << " is not a RangeSummarisableTimeValueModel!" << endl;
Chris@45 414 }
Chris@77 415 #endif
Chris@45 416 setModel(layer, replacementModel);
Chris@45 417 }
Chris@45 418 }
Chris@45 419 }
Chris@45 420
Chris@45 421 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
Chris@45 422 deleteLayer(obsoleteLayers[k], true);
Chris@45 423 }
Chris@45 424
Chris@48 425 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@86 426
Chris@137 427 Model *m = i->first;
Chris@137 428
Chris@137 429 #ifdef DEBUG_DOCUMENT
Chris@233 430 SVDEBUG << "considering alignment for model " << m << " (name \""
Chris@229 431 << m->objectName() << "\")" << endl;
Chris@137 432 #endif
Chris@137 433
Chris@86 434 if (m_autoAlignment) {
Chris@86 435
Chris@137 436 alignModel(m);
Chris@86 437
Chris@86 438 } else if (oldMainModel &&
Chris@137 439 (m->getAlignmentReference() == oldMainModel)) {
Chris@86 440
Chris@137 441 alignModel(m);
Chris@48 442 }
Chris@48 443 }
Chris@48 444
Chris@77 445 if (oldMainModel) {
Chris@79 446 oldMainModel->aboutToDelete();
Chris@77 447 emit modelAboutToBeDeleted(oldMainModel);
Chris@77 448 }
Chris@77 449
Chris@86 450 if (m_autoAlignment) {
Chris@86 451 alignModel(m_mainModel);
Chris@86 452 }
Chris@86 453
Chris@45 454 emit mainModelChanged(m_mainModel);
Chris@45 455
Chris@45 456 delete oldMainModel;
Chris@45 457 }
Chris@45 458
Chris@45 459 void
Chris@72 460 Document::addDerivedModel(const Transform &transform,
Chris@72 461 const ModelTransformer::Input &input,
Chris@72 462 Model *outputModelToAdd)
Chris@45 463 {
Chris@45 464 if (m_models.find(outputModelToAdd) != m_models.end()) {
Chris@293 465 cerr << "WARNING: Document::addDerivedModel: Model already added"
Chris@293 466 << endl;
Chris@45 467 return;
Chris@45 468 }
Chris@45 469
Chris@77 470 #ifdef DEBUG_DOCUMENT
Chris@248 471 if (input.getModel()) {
Chris@293 472 cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << endl;
Chris@248 473 } else {
Chris@293 474 cerr << "Document::addDerivedModel: source is " << input.getModel() << endl;
Chris@248 475 }
Chris@77 476 #endif
Chris@45 477
Chris@45 478 ModelRecord rec;
Chris@72 479 rec.source = input.getModel();
Chris@72 480 rec.channel = input.getChannel();
Chris@45 481 rec.transform = transform;
Chris@45 482 rec.refcount = 0;
Chris@45 483
Chris@72 484 outputModelToAdd->setSourceModel(input.getModel());
Chris@45 485
Chris@45 486 m_models[outputModelToAdd] = rec;
Chris@45 487
Chris@137 488 #ifdef DEBUG_DOCUMENT
Chris@233 489 SVDEBUG << "Document::addDerivedModel: Added model " << outputModelToAdd << endl;
Chris@293 490 cerr << "Models now: ";
Chris@137 491 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@293 492 cerr << i->first << " ";
Chris@137 493 }
Chris@293 494 cerr << endl;
Chris@137 495 #endif
Chris@137 496
Chris@45 497 emit modelAdded(outputModelToAdd);
Chris@45 498 }
Chris@45 499
Chris@45 500
Chris@45 501 void
Chris@45 502 Document::addImportedModel(Model *model)
Chris@45 503 {
Chris@45 504 if (m_models.find(model) != m_models.end()) {
Chris@293 505 cerr << "WARNING: Document::addImportedModel: Model already added"
Chris@293 506 << endl;
Chris@45 507 return;
Chris@45 508 }
Chris@45 509
Chris@45 510 ModelRecord rec;
Chris@45 511 rec.source = 0;
Chris@45 512 rec.refcount = 0;
Chris@45 513
Chris@45 514 m_models[model] = rec;
Chris@45 515
Chris@137 516 #ifdef DEBUG_DOCUMENT
Chris@233 517 SVDEBUG << "Document::addImportedModel: Added model " << model << endl;
Chris@293 518 cerr << "Models now: ";
Chris@137 519 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@293 520 cerr << i->first << " ";
Chris@137 521 }
Chris@293 522 cerr << endl;
Chris@137 523 #endif
Chris@137 524
Chris@47 525 if (m_autoAlignment) alignModel(model);
Chris@47 526
Chris@45 527 emit modelAdded(model);
Chris@45 528 }
Chris@45 529
Chris@45 530 Model *
Chris@72 531 Document::addDerivedModel(const Transform &transform,
Chris@78 532 const ModelTransformer::Input &input,
Chris@296 533 QString &message)
Chris@45 534 {
Chris@45 535 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@297 536 if (i->second.transform == transform &&
Chris@297 537 i->second.source == input.getModel() &&
Chris@72 538 i->second.channel == input.getChannel()) {
Chris@297 539 std::cerr << "derived model taken from map " << std::endl;
Chris@297 540 return i->first;
Chris@297 541 }
Chris@45 542 }
Chris@45 543
Chris@297 544 Transforms tt;
Chris@297 545 tt.push_back(transform);
Chris@297 546 vector<Model *> mm = addDerivedModels(tt, input, message);
Chris@297 547 if (mm.empty()) return 0;
Chris@297 548 else return mm[0];
Chris@297 549 }
Chris@45 550
Chris@297 551 vector<Model *>
Chris@297 552 Document::addDerivedModels(const Transforms &transforms,
Chris@297 553 const ModelTransformer::Input &input,
Chris@297 554 QString &message)
Chris@297 555 {
Chris@297 556 vector<Model *> mm =
Chris@297 557 ModelTransformerFactory::getInstance()->transformMultiple
Chris@297 558 (transforms, input, message);
Chris@83 559
Chris@297 560 for (int j = 0; j < (int)mm.size(); ++j) {
Chris@83 561
Chris@297 562 Model *model = mm[j];
Chris@297 563
Chris@297 564 // The transform we actually used was presumably identical to
Chris@297 565 // the one asked for, except that the version of the plugin
Chris@297 566 // may differ. It's possible that the returned message
Chris@297 567 // contains a warning about this; that doesn't concern us
Chris@297 568 // here, but we do need to ensure that the transform we
Chris@297 569 // remember is correct for what was actually applied, with the
Chris@297 570 // current plugin version.
Chris@297 571
Chris@297 572 Transform applied = transforms[j];
Chris@297 573 applied.setPluginVersion
Chris@297 574 (TransformFactory::getInstance()->
Chris@297 575 getDefaultTransformFor(applied.getIdentifier(),
Chris@297 576 lrintf(applied.getSampleRate()))
Chris@297 577 .getPluginVersion());
Chris@297 578
Chris@297 579 if (!model) {
Chris@297 580 cerr << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl;
Chris@297 581 } else {
Chris@297 582 addDerivedModel(applied, input, model);
Chris@297 583 }
Chris@45 584 }
gyorgyf@270 585
Chris@297 586 return mm;
Chris@45 587 }
Chris@45 588
Chris@45 589 void
Chris@45 590 Document::releaseModel(Model *model) // Will _not_ release main model!
Chris@45 591 {
Chris@45 592 if (model == 0) {
Chris@45 593 return;
Chris@45 594 }
Chris@45 595
Chris@45 596 if (model == m_mainModel) {
Chris@45 597 return;
Chris@45 598 }
Chris@45 599
Chris@45 600 bool toDelete = false;
Chris@45 601
Chris@45 602 if (m_models.find(model) != m_models.end()) {
Chris@45 603
Chris@45 604 if (m_models[model].refcount == 0) {
Chris@293 605 cerr << "WARNING: Document::releaseModel: model " << model
Chris@293 606 << " reference count is zero already!" << endl;
Chris@45 607 } else {
Chris@45 608 if (--m_models[model].refcount == 0) {
Chris@45 609 toDelete = true;
Chris@45 610 }
Chris@45 611 }
Chris@45 612 } else {
Chris@293 613 cerr << "WARNING: Document::releaseModel: Unfound model "
Chris@293 614 << model << endl;
Chris@45 615 toDelete = true;
Chris@45 616 }
Chris@45 617
Chris@45 618 if (toDelete) {
Chris@45 619
Chris@45 620 int sourceCount = 0;
Chris@45 621
Chris@45 622 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 623 if (i->second.source == model) {
Chris@45 624 ++sourceCount;
Chris@45 625 i->second.source = 0;
Chris@45 626 }
Chris@45 627 }
Chris@45 628
Chris@45 629 if (sourceCount > 0) {
Chris@233 630 SVDEBUG << "Document::releaseModel: Deleting model "
Chris@45 631 << model << " even though it is source for "
Chris@45 632 << sourceCount << " other derived model(s) -- resetting "
Chris@229 633 << "their source fields appropriately" << endl;
Chris@45 634 }
Chris@45 635
Chris@79 636 model->aboutToDelete();
Chris@45 637 emit modelAboutToBeDeleted(model);
Chris@45 638 m_models.erase(model);
Chris@137 639
Chris@137 640 #ifdef DEBUG_DOCUMENT
Chris@233 641 SVDEBUG << "Document::releaseModel: Deleted model " << model << endl;
Chris@293 642 cerr << "Models now: ";
Chris@137 643 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@293 644 cerr << i->first << " ";
Chris@137 645 }
Chris@293 646 cerr << endl;
Chris@137 647 #endif
Chris@137 648
Chris@45 649 delete model;
Chris@45 650 }
Chris@45 651 }
Chris@45 652
Chris@45 653 void
Chris@45 654 Document::deleteLayer(Layer *layer, bool force)
Chris@45 655 {
Chris@45 656 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
Chris@45 657 m_layerViewMap[layer].size() > 0) {
Chris@45 658
Chris@293 659 cerr << "WARNING: Document::deleteLayer: Layer "
Chris@228 660 << layer << " [" << layer->objectName() << "]"
Chris@45 661 << " is still used in " << m_layerViewMap[layer].size()
Chris@293 662 << " views!" << endl;
Chris@45 663
Chris@45 664 if (force) {
Chris@45 665
Chris@77 666 #ifdef DEBUG_DOCUMENT
Chris@293 667 cerr << "(force flag set -- deleting from all views)" << endl;
Chris@77 668 #endif
Chris@45 669
Chris@45 670 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
Chris@45 671 j != m_layerViewMap[layer].end(); ++j) {
Chris@45 672 // don't use removeLayerFromView, as it issues a command
Chris@45 673 layer->setLayerDormant(*j, true);
Chris@45 674 (*j)->removeLayer(layer);
Chris@45 675 }
Chris@45 676
Chris@45 677 m_layerViewMap.erase(layer);
Chris@45 678
Chris@45 679 } else {
Chris@45 680 return;
Chris@45 681 }
Chris@45 682 }
Chris@45 683
Chris@45 684 if (m_layers.find(layer) == m_layers.end()) {
Chris@233 685 SVDEBUG << "Document::deleteLayer: Layer "
Chris@212 686 << layer << " (" << typeid(layer).name() <<
Chris@212 687 ") does not exist, or has already been deleted "
Chris@229 688 << "(this may not be as serious as it sounds)" << endl;
Chris@45 689 return;
Chris@45 690 }
Chris@45 691
Chris@45 692 m_layers.erase(layer);
Chris@45 693
Chris@132 694 #ifdef DEBUG_DOCUMENT
Chris@233 695 SVDEBUG << "Document::deleteLayer: Removing, now have "
Chris@229 696 << m_layers.size() << " layers" << endl;
Chris@132 697 #endif
Chris@52 698
Chris@45 699 releaseModel(layer->getModel());
Chris@45 700 emit layerRemoved(layer);
Chris@45 701 emit layerAboutToBeDeleted(layer);
Chris@45 702 delete layer;
Chris@45 703 }
Chris@45 704
Chris@45 705 void
Chris@45 706 Document::setModel(Layer *layer, Model *model)
Chris@45 707 {
Chris@45 708 if (model &&
Chris@45 709 model != m_mainModel &&
Chris@45 710 m_models.find(model) == m_models.end()) {
Chris@293 711 cerr << "ERROR: Document::setModel: Layer " << layer
Chris@294 712 << " (\"" << layer->objectName()
Chris@45 713 << "\") wants to use unregistered model " << model
Chris@45 714 << ": register the layer's model before setting it!"
Chris@293 715 << endl;
Chris@45 716 return;
Chris@45 717 }
Chris@45 718
Chris@45 719 Model *previousModel = layer->getModel();
Chris@45 720
Chris@45 721 if (previousModel == model) {
Chris@233 722 SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\""
Chris@229 723 << layer->objectName() << "\") is already set to model "
Chris@45 724 << model << " (\""
Chris@229 725 << (model ? model->objectName(): "(null)")
Chris@229 726 << "\")" << endl;
Chris@45 727 return;
Chris@45 728 }
Chris@45 729
Chris@45 730 if (model && model != m_mainModel) {
Chris@45 731 m_models[model].refcount ++;
Chris@45 732 }
Chris@45 733
Chris@45 734 if (model && previousModel) {
Chris@45 735 PlayParameterRepository::getInstance()->copyParameters
Chris@45 736 (previousModel, model);
Chris@45 737 }
Chris@45 738
Chris@45 739 LayerFactory::getInstance()->setModel(layer, model);
gyorgyf@272 740 // std::cerr << "layer type: " << LayerFactory::getInstance()->getLayerTypeName(LayerFactory::getInstance()->getLayerType(layer)) << std::endl;
Chris@45 741
Chris@45 742 if (previousModel) {
Chris@45 743 releaseModel(previousModel);
Chris@45 744 }
Chris@45 745 }
Chris@45 746
Chris@45 747 void
Chris@45 748 Document::setChannel(Layer *layer, int channel)
Chris@45 749 {
Chris@45 750 LayerFactory::getInstance()->setChannel(layer, channel);
Chris@45 751 }
Chris@45 752
Chris@45 753 void
Chris@45 754 Document::addLayerToView(View *view, Layer *layer)
Chris@45 755 {
Chris@45 756 Model *model = layer->getModel();
Chris@45 757 if (!model) {
Chris@77 758 #ifdef DEBUG_DOCUMENT
Chris@233 759 SVDEBUG << "Document::addLayerToView: Layer (\""
Chris@229 760 << layer->objectName() << "\") with no model being added to view: "
Chris@229 761 << "normally you want to set the model first" << endl;
Chris@77 762 #endif
Chris@45 763 } else {
Chris@45 764 if (model != m_mainModel &&
Chris@45 765 m_models.find(model) == m_models.end()) {
Chris@293 766 cerr << "ERROR: Document::addLayerToView: Layer " << layer
Chris@45 767 << " has unregistered model " << model
Chris@293 768 << " -- register the layer's model before adding the layer!" << endl;
Chris@45 769 return;
Chris@45 770 }
Chris@45 771 }
Chris@45 772
Chris@45 773 CommandHistory::getInstance()->addCommand
Chris@45 774 (new Document::AddLayerCommand(this, view, layer));
Chris@45 775 }
Chris@45 776
Chris@45 777 void
Chris@45 778 Document::removeLayerFromView(View *view, Layer *layer)
Chris@45 779 {
Chris@45 780 CommandHistory::getInstance()->addCommand
Chris@45 781 (new Document::RemoveLayerCommand(this, view, layer));
Chris@45 782 }
Chris@45 783
Chris@45 784 void
Chris@45 785 Document::addToLayerViewMap(Layer *layer, View *view)
Chris@45 786 {
Chris@45 787 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
Chris@45 788 m_layerViewMap[layer].empty());
Chris@45 789
Chris@45 790 if (m_layerViewMap[layer].find(view) !=
Chris@45 791 m_layerViewMap[layer].end()) {
Chris@293 792 cerr << "WARNING: Document::addToLayerViewMap:"
Chris@45 793 << " Layer " << layer << " -> view " << view << " already in"
Chris@293 794 << " layer view map -- internal inconsistency" << endl;
Chris@45 795 }
Chris@45 796
Chris@45 797 m_layerViewMap[layer].insert(view);
Chris@45 798
Chris@45 799 if (firstView) emit layerInAView(layer, true);
Chris@45 800 }
Chris@45 801
Chris@45 802 void
Chris@45 803 Document::removeFromLayerViewMap(Layer *layer, View *view)
Chris@45 804 {
Chris@45 805 if (m_layerViewMap[layer].find(view) ==
Chris@45 806 m_layerViewMap[layer].end()) {
Chris@293 807 cerr << "WARNING: Document::removeFromLayerViewMap:"
Chris@45 808 << " Layer " << layer << " -> view " << view << " not in"
Chris@293 809 << " layer view map -- internal inconsistency" << endl;
Chris@45 810 }
Chris@45 811
Chris@45 812 m_layerViewMap[layer].erase(view);
Chris@45 813
Chris@45 814 if (m_layerViewMap[layer].empty()) {
Chris@45 815 m_layerViewMap.erase(layer);
Chris@45 816 emit layerInAView(layer, false);
Chris@45 817 }
Chris@45 818 }
Chris@45 819
Chris@45 820 QString
Chris@45 821 Document::getUniqueLayerName(QString candidate)
Chris@45 822 {
Chris@45 823 for (int count = 1; ; ++count) {
Chris@45 824
Chris@45 825 QString adjusted =
Chris@45 826 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
Chris@45 827 candidate);
Chris@45 828
Chris@45 829 bool duplicate = false;
Chris@45 830
Chris@45 831 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 832 if ((*i)->objectName() == adjusted) {
Chris@45 833 duplicate = true;
Chris@45 834 break;
Chris@45 835 }
Chris@45 836 }
Chris@45 837
Chris@45 838 if (!duplicate) return adjusted;
Chris@45 839 }
Chris@45 840 }
Chris@45 841
Chris@45 842 std::vector<Model *>
Chris@72 843 Document::getTransformInputModels()
Chris@45 844 {
Chris@45 845 std::vector<Model *> models;
Chris@45 846
Chris@45 847 if (!m_mainModel) return models;
Chris@45 848
Chris@45 849 models.push_back(m_mainModel);
Chris@45 850
Chris@45 851 //!!! This will pick up all models, including those that aren't visible...
Chris@45 852
Chris@45 853 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 854
Chris@45 855 Model *model = i->first;
Chris@45 856 if (!model || model == m_mainModel) continue;
Chris@45 857 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@45 858
Chris@45 859 if (dtvm) {
Chris@45 860 models.push_back(dtvm);
Chris@45 861 }
Chris@45 862 }
Chris@45 863
Chris@45 864 return models;
Chris@45 865 }
Chris@45 866
Chris@50 867 bool
Chris@77 868 Document::isKnownModel(const Model *model) const
Chris@77 869 {
Chris@77 870 if (model == m_mainModel) return true;
Chris@77 871 return (m_models.find(const_cast<Model *>(model)) != m_models.end());
Chris@77 872 }
Chris@77 873
Chris@90 874 TransformId
Chris@90 875 Document::getAlignmentTransformName()
Chris@90 876 {
Chris@90 877 QSettings settings;
Chris@90 878 settings.beginGroup("Alignment");
Chris@90 879 TransformId id =
Chris@90 880 settings.value("transform-id",
Chris@90 881 "vamp:match-vamp-plugin:match:path").toString();
Chris@90 882 settings.endGroup();
Chris@90 883 return id;
Chris@90 884 }
Chris@90 885
Chris@77 886 bool
Chris@51 887 Document::canAlign()
Chris@50 888 {
Chris@90 889 TransformId id = getAlignmentTransformName();
Chris@54 890 TransformFactory *factory = TransformFactory::getInstance();
Chris@54 891 return factory->haveTransform(id);
Chris@50 892 }
Chris@50 893
Chris@45 894 void
Chris@45 895 Document::alignModel(Model *model)
Chris@45 896 {
Chris@86 897 if (!m_mainModel) return;
Chris@45 898
Chris@45 899 RangeSummarisableTimeValueModel *rm =
Chris@45 900 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
Chris@45 901 if (!rm) return;
Chris@48 902
Chris@86 903 if (rm->getAlignmentReference() == m_mainModel) {
Chris@233 904 SVDEBUG << "Document::alignModel: model " << rm << " is already aligned to main model " << m_mainModel << endl;
Chris@86 905 return;
Chris@86 906 }
Chris@45 907
Chris@86 908 if (model == m_mainModel) {
Chris@86 909 // The reference has an empty alignment to itself. This makes
Chris@86 910 // it possible to distinguish between the reference and any
Chris@86 911 // unaligned model just by looking at the model itself,
Chris@86 912 // without also knowing what the main model is
Chris@233 913 SVDEBUG << "Document::alignModel(" << model << "): is main model, setting appropriately" << endl;
Chris@86 914 rm->setAlignment(new AlignmentModel(model, model, 0, 0));
Chris@86 915 return;
Chris@86 916 }
Chris@86 917
Chris@45 918 // This involves creating three new models:
Chris@45 919
Chris@45 920 // 1. an AggregateWaveModel to provide the mixdowns of the main
Chris@45 921 // model and the new model in its two channels, as input to the
Chris@45 922 // MATCH plugin
Chris@45 923
Chris@45 924 // 2. a SparseTimeValueModel, which is the model automatically
Chris@53 925 // created by FeatureExtractionPluginTransformer when running the
Chris@45 926 // MATCH plugin (thus containing the alignment path)
Chris@45 927
Chris@45 928 // 3. an AlignmentModel, which stores the path model and carries
Chris@45 929 // out alignment lookups on it.
Chris@45 930
Chris@45 931 // The first two of these are provided as arguments to the
Chris@45 932 // constructor for the third, which takes responsibility for
Chris@45 933 // deleting them. The AlignmentModel, meanwhile, is passed to the
Chris@45 934 // new model we are aligning, which also takes responsibility for
Chris@45 935 // it. We should not have to delete any of these new models here.
Chris@45 936
Chris@45 937 AggregateWaveModel::ChannelSpecList components;
Chris@45 938
Chris@45 939 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 940 (m_mainModel, -1));
Chris@45 941
Chris@45 942 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 943 (rm, -1));
Chris@45 944
Chris@112 945 Model *aggregateModel = new AggregateWaveModel(components);
Chris@112 946 ModelTransformer::Input aggregate(aggregateModel);
Chris@45 947
Chris@72 948 TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure
Chris@45 949
Chris@72 950 TransformFactory *tf = TransformFactory::getInstance();
Chris@45 951
Chris@72 952 Transform transform = tf->getDefaultTransformFor
Chris@112 953 (id, aggregateModel->getSampleRate());
Chris@57 954
Chris@72 955 transform.setStepSize(transform.getBlockSize()/2);
Chris@72 956 transform.setParameter("serialise", 1);
Chris@64 957
Chris@233 958 SVDEBUG << "Document::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@81 959
Chris@72 960 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@72 961
Chris@78 962 QString message;
Chris@78 963 Model *transformOutput = mtf->transform(transform, aggregate, message);
Chris@64 964
Chris@64 965 if (!transformOutput) {
Chris@72 966 transform.setStepSize(0);
Chris@78 967 transformOutput = mtf->transform(transform, aggregate, message);
Chris@64 968 }
Chris@45 969
Chris@45 970 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
Chris@45 971 (transformOutput);
Chris@45 972
Chris@45 973 if (!path) {
Chris@293 974 cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << endl;
Chris@78 975 emit alignmentFailed(id, message);
Chris@45 976 delete transformOutput;
Chris@112 977 delete aggregateModel;
Chris@45 978 return;
Chris@45 979 }
Chris@45 980
Chris@112 981 path->setCompletion(0);
Chris@112 982
Chris@45 983 AlignmentModel *alignmentModel = new AlignmentModel
Chris@112 984 (m_mainModel, model, aggregateModel, path);
Chris@45 985
Chris@45 986 rm->setAlignment(alignmentModel);
Chris@45 987 }
Chris@45 988
Chris@45 989 void
Chris@45 990 Document::alignModels()
Chris@45 991 {
Chris@45 992 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 993 alignModel(i->first);
Chris@45 994 }
Chris@86 995 alignModel(m_mainModel);
Chris@45 996 }
Chris@45 997
Chris@45 998 Document::AddLayerCommand::AddLayerCommand(Document *d,
Chris@45 999 View *view,
Chris@45 1000 Layer *layer) :
Chris@45 1001 m_d(d),
Chris@45 1002 m_view(view),
Chris@45 1003 m_layer(layer),
Chris@45 1004 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
Chris@45 1005 m_added(false)
Chris@45 1006 {
Chris@45 1007 }
Chris@45 1008
Chris@45 1009 Document::AddLayerCommand::~AddLayerCommand()
Chris@45 1010 {
Chris@77 1011 #ifdef DEBUG_DOCUMENT
Chris@233 1012 SVDEBUG << "Document::AddLayerCommand::~AddLayerCommand" << endl;
Chris@77 1013 #endif
Chris@45 1014 if (!m_added) {
Chris@45 1015 m_d->deleteLayer(m_layer);
Chris@45 1016 }
Chris@45 1017 }
Chris@45 1018
Chris@159 1019 QString
Chris@159 1020 Document::AddLayerCommand::getName() const
Chris@159 1021 {
Chris@165 1022 #ifdef DEBUG_DOCUMENT
Chris@233 1023 SVDEBUG << "Document::AddLayerCommand::getName(): Name is "
Chris@229 1024 << m_name << endl;
Chris@165 1025 #endif
Chris@159 1026 return m_name;
Chris@159 1027 }
Chris@159 1028
Chris@45 1029 void
Chris@45 1030 Document::AddLayerCommand::execute()
Chris@45 1031 {
Chris@45 1032 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 1033 if (m_view->getLayer(i) == m_layer) {
Chris@45 1034 // already there
Chris@45 1035 m_layer->setLayerDormant(m_view, false);
Chris@45 1036 m_added = true;
Chris@45 1037 return;
Chris@45 1038 }
Chris@45 1039 }
Chris@45 1040
Chris@45 1041 m_view->addLayer(m_layer);
Chris@45 1042 m_layer->setLayerDormant(m_view, false);
Chris@45 1043
Chris@45 1044 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 1045 m_added = true;
Chris@45 1046 }
Chris@45 1047
Chris@45 1048 void
Chris@45 1049 Document::AddLayerCommand::unexecute()
Chris@45 1050 {
Chris@45 1051 m_view->removeLayer(m_layer);
Chris@45 1052 m_layer->setLayerDormant(m_view, true);
Chris@45 1053
Chris@45 1054 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 1055 m_added = false;
Chris@45 1056 }
Chris@45 1057
Chris@45 1058 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
Chris@45 1059 View *view,
Chris@45 1060 Layer *layer) :
Chris@45 1061 m_d(d),
Chris@45 1062 m_view(view),
Chris@45 1063 m_layer(layer),
Chris@45 1064 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
Chris@45 1065 m_added(true)
Chris@45 1066 {
Chris@45 1067 }
Chris@45 1068
Chris@45 1069 Document::RemoveLayerCommand::~RemoveLayerCommand()
Chris@45 1070 {
Chris@77 1071 #ifdef DEBUG_DOCUMENT
Chris@233 1072 SVDEBUG << "Document::RemoveLayerCommand::~RemoveLayerCommand" << endl;
Chris@77 1073 #endif
Chris@45 1074 if (!m_added) {
Chris@45 1075 m_d->deleteLayer(m_layer);
Chris@45 1076 }
Chris@45 1077 }
Chris@45 1078
Chris@159 1079 QString
Chris@159 1080 Document::RemoveLayerCommand::getName() const
Chris@159 1081 {
Chris@171 1082 #ifdef DEBUG_DOCUMENT
Chris@233 1083 SVDEBUG << "Document::RemoveLayerCommand::getName(): Name is "
Chris@229 1084 << m_name << endl;
Chris@171 1085 #endif
Chris@159 1086 return m_name;
Chris@159 1087 }
Chris@159 1088
Chris@45 1089 void
Chris@45 1090 Document::RemoveLayerCommand::execute()
Chris@45 1091 {
Chris@45 1092 bool have = false;
Chris@45 1093 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 1094 if (m_view->getLayer(i) == m_layer) {
Chris@45 1095 have = true;
Chris@45 1096 break;
Chris@45 1097 }
Chris@45 1098 }
Chris@45 1099
Chris@45 1100 if (!have) { // not there!
Chris@45 1101 m_layer->setLayerDormant(m_view, true);
Chris@45 1102 m_added = false;
Chris@45 1103 return;
Chris@45 1104 }
Chris@45 1105
Chris@45 1106 m_view->removeLayer(m_layer);
Chris@45 1107 m_layer->setLayerDormant(m_view, true);
Chris@45 1108
Chris@45 1109 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 1110 m_added = false;
Chris@45 1111 }
Chris@45 1112
Chris@45 1113 void
Chris@45 1114 Document::RemoveLayerCommand::unexecute()
Chris@45 1115 {
Chris@45 1116 m_view->addLayer(m_layer);
Chris@45 1117 m_layer->setLayerDormant(m_view, false);
Chris@45 1118
Chris@45 1119 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 1120 m_added = true;
Chris@45 1121 }
Chris@45 1122
Chris@45 1123 void
Chris@45 1124 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
Chris@45 1125 {
Chris@226 1126 toXml(out, indent, extraAttributes, false);
Chris@226 1127 }
Chris@226 1128
Chris@226 1129 void
Chris@226 1130 Document::toXmlAsTemplate(QTextStream &out, QString indent, QString extraAttributes) const
Chris@226 1131 {
Chris@226 1132 toXml(out, indent, extraAttributes, true);
Chris@226 1133 }
Chris@226 1134
Chris@226 1135 void
Chris@226 1136 Document::toXml(QTextStream &out, QString indent, QString extraAttributes,
Chris@226 1137 bool asTemplate) const
Chris@226 1138 {
Chris@45 1139 out << indent + QString("<data%1%2>\n")
Chris@45 1140 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
Chris@45 1141
Chris@45 1142 if (m_mainModel) {
Chris@108 1143
Chris@226 1144 if (asTemplate) {
Chris@226 1145 writePlaceholderMainModel(out, indent + " ");
Chris@226 1146 } else {
Chris@226 1147 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
Chris@226 1148 }
Chris@108 1149
Chris@108 1150 PlayParameters *playParameters =
Chris@108 1151 PlayParameterRepository::getInstance()->getPlayParameters(m_mainModel);
Chris@108 1152 if (playParameters) {
Chris@108 1153 playParameters->toXml
Chris@108 1154 (out, indent + " ",
Chris@108 1155 QString("model=\"%1\"")
Chris@108 1156 .arg(XmlExportable::getObjectExportId(m_mainModel)));
Chris@108 1157 }
Chris@45 1158 }
Chris@45 1159
Chris@45 1160 // Models that are not used in a layer that is in a view should
Chris@45 1161 // not be written. Get our list of required models first.
Chris@45 1162
Chris@45 1163 std::set<const Model *> used;
Chris@45 1164
Chris@45 1165 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
Chris@45 1166 i != m_layerViewMap.end(); ++i) {
Chris@45 1167
Chris@45 1168 if (i->first && !i->second.empty() && i->first->getModel()) {
Chris@45 1169 used.insert(i->first->getModel());
Chris@45 1170 }
Chris@45 1171 }
Chris@45 1172
Chris@111 1173 std::set<Model *> written;
Chris@111 1174
Chris@45 1175 for (ModelMap::const_iterator i = m_models.begin();
Chris@45 1176 i != m_models.end(); ++i) {
Chris@45 1177
Chris@111 1178 Model *model = i->first;
Chris@45 1179 const ModelRecord &rec = i->second;
Chris@45 1180
Chris@45 1181 if (used.find(model) == used.end()) continue;
Chris@45 1182
Chris@45 1183 // We need an intelligent way to determine which models need
Chris@45 1184 // to be streamed (i.e. have been edited, or are small) and
Chris@45 1185 // which should not be (i.e. remain as generated by a
Chris@45 1186 // transform, and are large).
Chris@45 1187 //
Chris@45 1188 // At the moment we can get away with deciding not to stream
Chris@45 1189 // dense 3d models or writable wave file models, provided they
Chris@45 1190 // were generated from a transform, because at the moment there
Chris@45 1191 // is no way to edit those model types so it should be safe to
Chris@45 1192 // regenerate them. That won't always work in future though.
Chris@45 1193 // It would be particularly nice to be able to ask the user,
Chris@45 1194 // as well as making an intelligent guess.
Chris@45 1195
Chris@45 1196 bool writeModel = true;
Chris@45 1197 bool haveDerivation = false;
Chris@45 1198
Chris@72 1199 if (rec.source && rec.transform.getIdentifier() != "") {
Chris@45 1200 haveDerivation = true;
Chris@45 1201 }
Chris@45 1202
Chris@45 1203 if (haveDerivation) {
Chris@45 1204 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
Chris@45 1205 writeModel = false;
Chris@45 1206 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
Chris@45 1207 writeModel = false;
Chris@45 1208 }
Chris@45 1209 }
Chris@45 1210
Chris@45 1211 if (writeModel) {
Chris@111 1212 model->toXml(out, indent + " ");
Chris@111 1213 written.insert(model);
Chris@45 1214 }
Chris@45 1215
Chris@45 1216 if (haveDerivation) {
Chris@72 1217 writeBackwardCompatibleDerivation(out, indent + " ",
Chris@111 1218 model, rec);
Chris@45 1219 }
Chris@45 1220
Chris@45 1221 //!!! We should probably own the PlayParameterRepository
Chris@45 1222 PlayParameters *playParameters =
Chris@111 1223 PlayParameterRepository::getInstance()->getPlayParameters(model);
Chris@45 1224 if (playParameters) {
Chris@45 1225 playParameters->toXml
Chris@45 1226 (out, indent + " ",
Chris@45 1227 QString("model=\"%1\"")
Chris@111 1228 .arg(XmlExportable::getObjectExportId(model)));
Chris@45 1229 }
Chris@45 1230 }
Chris@45 1231
Chris@111 1232 //!!!
Chris@111 1233
Chris@111 1234 // We should write out the alignment models here. AlignmentModel
Chris@111 1235 // needs a toXml that writes out the export IDs of its reference
Chris@111 1236 // and aligned models, and then streams its path model. Note that
Chris@111 1237 // this will only work when the alignment is complete, so we
Chris@111 1238 // should probably wait for it if it isn't already by this point.
Chris@111 1239
Chris@111 1240 for (std::set<Model *>::const_iterator i = written.begin();
Chris@111 1241 i != written.end(); ++i) {
Chris@111 1242
Chris@111 1243 const Model *model = *i;
Chris@111 1244 const AlignmentModel *alignment = model->getAlignment();
Chris@111 1245 if (!alignment) continue;
Chris@111 1246
Chris@111 1247 alignment->toXml(out, indent + " ");
Chris@111 1248 }
Chris@111 1249
Chris@45 1250 for (LayerSet::const_iterator i = m_layers.begin();
Chris@45 1251 i != m_layers.end(); ++i) {
Chris@45 1252
Chris@45 1253 (*i)->toXml(out, indent + " ");
Chris@45 1254 }
Chris@45 1255
Chris@45 1256 out << indent + "</data>\n";
Chris@45 1257 }
Chris@45 1258
Chris@72 1259 void
Chris@226 1260 Document::writePlaceholderMainModel(QTextStream &out, QString indent) const
Chris@226 1261 {
Chris@226 1262 out << indent;
Chris@226 1263 out << QString("<model id=\"%1\" name=\"placeholder\" sampleRate=\"%2\" type=\"wavefile\" file=\":samples/silent.wav\" mainModel=\"true\"/>\n")
Chris@226 1264 .arg(getObjectExportId(m_mainModel))
Chris@226 1265 .arg(m_mainModel->getSampleRate());
Chris@226 1266 }
Chris@226 1267
Chris@226 1268 void
Chris@72 1269 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
Chris@72 1270 Model *targetModel,
Chris@72 1271 const ModelRecord &rec) const
Chris@72 1272 {
Chris@72 1273 // There is a lot of redundancy in the XML we output here, because
Chris@72 1274 // we want it to work with older SV session file reading code as
Chris@72 1275 // well.
Chris@72 1276 //
Chris@72 1277 // Formerly, a transform was described using a derivation element
Chris@72 1278 // which set out the source and target models, execution context
Chris@72 1279 // (step size, input channel etc) and transform id, containing a
Chris@72 1280 // plugin element which set out the transform parameters and so
Chris@72 1281 // on. (The plugin element came from a "configurationXml" string
Chris@72 1282 // obtained from PluginXml.)
Chris@72 1283 //
Chris@72 1284 // This has been replaced by a derivation element setting out the
Chris@72 1285 // source and target models and input channel, containing a
Chris@72 1286 // transform element which sets out everything in the Transform.
Chris@72 1287 //
Chris@72 1288 // In order to retain compatibility with older SV code, however,
Chris@72 1289 // we have to write out the same stuff into the derivation as
Chris@72 1290 // before, and manufacture an appropriate plugin element as well
Chris@72 1291 // as the transform element. In order that newer code knows it's
Chris@72 1292 // dealing with a newer format, we will also write an attribute
Chris@72 1293 // 'type="transform"' in the derivation element.
Chris@45 1294
Chris@72 1295 const Transform &transform = rec.transform;
Chris@72 1296
Chris@72 1297 // Just for reference, this is what we would write if we didn't
Chris@72 1298 // have to be backward compatible:
Chris@72 1299 //
Chris@72 1300 // out << indent
Chris@72 1301 // << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1302 // "model=\"%2\" channel=\"%3\">\n")
Chris@72 1303 // .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1304 // .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1305 // .arg(rec.channel);
Chris@72 1306 //
Chris@72 1307 // transform.toXml(out, indent + " ");
Chris@72 1308 //
Chris@72 1309 // out << indent << "</derivation>\n";
Chris@72 1310 //
Chris@72 1311 // Unfortunately, we can't just do that. So we do this...
Chris@72 1312
Chris@72 1313 QString extentsAttributes;
Chris@72 1314 if (transform.getStartTime() != RealTime::zeroTime ||
Chris@72 1315 transform.getDuration() != RealTime::zeroTime) {
Chris@72 1316 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
Chris@72 1317 .arg(RealTime::realTime2Frame(transform.getStartTime(),
Chris@72 1318 targetModel->getSampleRate()))
Chris@72 1319 .arg(RealTime::realTime2Frame(transform.getDuration(),
Chris@72 1320 targetModel->getSampleRate()));
Chris@72 1321 }
Chris@72 1322
Chris@72 1323 out << indent;
Chris@72 1324 out << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1325 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
Chris@72 1326 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
Chris@72 1327 "transform=\"%9\">\n")
Chris@72 1328 .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1329 .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1330 .arg(rec.channel)
Chris@72 1331 .arg(TransformFactory::getInstance()->getTransformInputDomain
Chris@72 1332 (transform.getIdentifier()))
Chris@72 1333 .arg(transform.getStepSize())
Chris@72 1334 .arg(transform.getBlockSize())
Chris@72 1335 .arg(extentsAttributes)
Chris@72 1336 .arg(int(transform.getWindowType()))
Chris@72 1337 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
Chris@72 1338
Chris@72 1339 transform.toXml(out, indent + " ");
Chris@72 1340
Chris@72 1341 out << indent << " "
Chris@72 1342 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
Chris@72 1343
Chris@72 1344 out << indent << "</derivation>\n";
Chris@72 1345 }
Chris@72 1346