1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam and QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#ifndef SV_DOCUMENT_H
#define SV_DOCUMENT_H
#include "layer/LayerFactory.h"
#include "transform/Transform.h"
#include "transform/ModelTransformer.h"
#include "transform/FeatureExtractionModelTransformer.h"
#include "base/Command.h"
#include <map>
#include <set>
namespace sv {
class Model;
class Layer;
class View;
class WaveFileModel;
class AggregateWaveModel;
class AdditionalModelConverter;
class Align;
/**
* A Sonic Visualiser document consists of a set of data models, and
* also the visualisation layers used to display them. Changes to the
* layers and their layout need to be stored and managed in much the
* same way as changes to the underlying data.
*
* The document manages:
*
* - A main data Model, which provides the underlying sample rate and
* such like. This must be a WaveFileModel.
*
* - Any number of imported Model objects, which contain data without any
* requirement to remember where the data came from or how to
* regenerate it.
*
* - Any number of Model objects that were generated by a Transformer
* such as FeatureExtractionModelTransformer. For these, we also
* record the source model and the name of the transform used to
* generate the model so that we can regenerate it (potentially
* from a different source) on demand.
*
* - A flat list of Layer objects. Elsewhere, the GUI may distribute these
* across any number of View widgets. A layer may be viewable on more
* than one view at once, in principle. A layer refers to one model,
* but the same model can be in use in more than one layer.
*
* The document does *not* manage the existence or structure of Pane
* and other view widgets. However, it does provide convenience
* methods for reference-counted command-based management of the
* association between layers and views (addLayerToView,
* removeLayerFromView).
*/
class Document : public QObject,
public XmlExportable
{
Q_OBJECT
public:
Document();
virtual ~Document();
/**
* Create and return a new layer of the given type, associated
* with no model. The caller may set any model on this layer, but
* the model must also be registered with the document via the
* add-model methods below.
*/
Layer *createLayer(LayerFactory::LayerType);
/**
* Create and return a new layer of the given type, associated
* with the current main model (if appropriate to the layer type).
*/
Layer *createMainModelLayer(LayerFactory::LayerType);
/**
* Create and return a new layer associated with the given model,
* and register the model as an imported model.
*/
Layer *createImportedLayer(ModelId);
/**
* Create and return a new layer of the given type, with an
* appropriate empty model. If the given type is not one for
* which an empty model can meaningfully be created, return 0.
*/
Layer *createEmptyLayer(LayerFactory::LayerType);
/**
* Create and return a new layer of the given type, associated
* with the given transform name. This method does not run the
* transform itself, nor create a model. The caller can safely
* add a model to the layer later, but note that all models used
* by a transform layer _must_ be registered with the document
* using addDerivedModel below.
*/
Layer *createDerivedLayer(LayerFactory::LayerType, TransformId);
/**
* Create and return a suitable layer for the given transform,
* running the transform and associating the resulting model with
* the new layer.
*/
Layer *createDerivedLayer(const Transform &,
const ModelTransformer::Input &);
/**
* Create and return suitable layers for the given transforms,
* which must be identical apart from the output (i.e. must use
* the same plugin and configuration). The layers are returned in
* the same order as the transforms are supplied.
*/
std::vector<Layer *> createDerivedLayers(const Transforms &,
const ModelTransformer::Input &);
typedef void *LayerCreationAsyncHandle;
class LayerCreationHandler {
public:
virtual ~LayerCreationHandler() { }
/**
* The primary layers are those corresponding 1-1 to the input
* models, listed in the same order as the input models. The
* additional layers vector contains any layers (from all
* models) that were returned separately at the end of
* processing. The handle indicates which async process this
* callback was initiated by. It must not be used again after
* this function returns.
*/
virtual void layersCreated(LayerCreationAsyncHandle handle,
std::vector<Layer *> primary,
std::vector<Layer *> additional) = 0;
};
/**
* Create suitable layers for the given transforms, which must be
* identical apart from the output (i.e. must use the same plugin
* and configuration). This method returns after initialising the
* transformer process, and the layers are returned through a
* subsequent call to the provided handler (which must be
* non-null). The handle returned will be passed through to the
* handler callback.
*/
LayerCreationAsyncHandle createDerivedLayersAsync(const Transforms &,
const ModelTransformer::Input &,
LayerCreationHandler *handler);
/**
* Indicate that the async layer creation task associated with the
* given handle should be cancelled. There is no guarantee about
* what this will mean, and the handler callback may still be
* called.
*/
void cancelAsyncLayerCreation(LayerCreationAsyncHandle handle);
/**
* Delete the given layer, and also its associated model if no
* longer used by any other layer. In general, this should be the
* only method used to delete layers -- doing so directly is a bit
* of a social gaffe.
*/
void deleteLayer(Layer *, bool force = false);
/**
* Set the main model (the source for playback sample rate, etc)
* to the given wave file model. This will regenerate any derived
* models that were based on the previous main model. The model
* must have been added to ModelById already, and Document will
* take responsibility for releasing it later.
*/
void setMainModel(ModelId); // a WaveFileModel
/**
* Set the main model (the source for playback sample rate, etc)
* to the given wave file model. The given model must be one that
* has already been added to the Document. The old main model, if
* there was one, will not be removed or altered - it just won't
* be the main model any more.
*/
void switchMainModel(ModelId); // a WaveFileModel
/**
* Get the main model (the source for playback sample rate, etc).
*/
ModelId getMainModel() { return m_mainModel; }
/**
* Regenerate any derived models that are based on the given
* model. This could be because the model has changed, for example
* because it was being recorded and recording has now finished.
*/
void refreshModel(ModelId); // a WaveFileModel
std::vector<ModelId> getTransformInputModels();
/**
* Return true if the model id is known to be the main model or
* one of the other existing models that can be shown in a new
* layer.
*/
bool isKnownModel(ModelId) const;
/**
* Add a derived model associated with the given transform,
* running the transform and returning the resulting model.
* The model is added to ModelById before returning.
*
* If acceptExistingModel is true and an existing model is found
* with the same source, transform, and channel, return that model
* instead of re-running the transform.
*/
ModelId addDerivedModel(const Transform &transform,
const ModelTransformer::Input &input,
QString &returnedMessage,
bool acceptExistingModel = true);
/**
* Add derived models associated with the given set of related
* transforms, running the transforms and returning the resulting
* models. The models are added to ModelById before returning.
*/
friend class AdditionalModelConverter;
std::vector<ModelId> addDerivedModels(const Transforms &transforms,
const ModelTransformer::Input &input,
QString &returnedMessage,
AdditionalModelConverter *);
/**
* Add a derived model associated with the given transform. This
* is necessary to register any derived model that was not created
* by the document using createDerivedModel or
* createDerivedLayer. Document will take responsibility for
* releasing the model later.
*/
void addAlreadyDerivedModel(const Transform &transform,
const ModelTransformer::Input &input,
ModelId outputModelToAdd);
/**
* Add an imported model, i.e. any model (other than the main
* model) that has been created by any means other than as the
* output of a transform. This is necessary to register any
* imported model that is to be associated with a layer, and also
* to make sure that the model is released by the Document
* later. Aggregate models, alignment models, and miscellaneous
* temporary models should also be added in this way, unless the
* temporary models are large enough to need managing in a way
* that guarantees the shortest possible lifespan.
*/
void addNonDerivedModel(ModelId);
/**
* Associate the given model with the given layer. The model must
* have already been registered using one of the addXXModel
* methods above.
*/
void setModel(Layer *, ModelId);
/**
* Set the given layer to use the given channel of its model (-1
* means all available channels).
*/
void setChannel(Layer *, int);
/**
* Add the given layer to the given view. If the layer is
* intended to show a particular model, the model should normally
* be set using setModel before this method is called.
*/
void addLayerToView(View *, Layer *);
/**
* Remove the given layer from the given view. Ownership of the
* layer is transferred to a command object on the undo stack, and
* the layer will be deleted when the undo stack is pruned.
*/
void removeLayerFromView(View *, Layer *);
/**
* Return true if alignment is supported (i.e. if the necessary
* plugin(s) are found).
*/
bool canAlign();
/**
* Specify whether models added via addImportedModel should be
* automatically aligned against the main model if appropriate.
*/
void setAutoAlignment(bool on) { m_autoAlignment = on; }
/**
* Generate alignments for all appropriate models against the main
* model. Existing alignments will not be re-calculated unless
* the main model has changed since they were calculated.
*/
void alignModels();
/**
* Re-generate alignments for all appropriate models against the
* main model. Existing alignments will be re-calculated.
*/
void realignModels();
/**
* Return true if any external files (most obviously audio) failed
* to be found on load, so that the document is incomplete
* compared to its saved description.
*/
bool isIncomplete() const { return m_isIncomplete; }
void setIncomplete(bool i) { m_isIncomplete = i; }
void toXml(QTextStream &, QString indent, QString extraAttributes) const override;
void toXmlAsTemplate(QTextStream &, QString indent, QString extraAttributes) const;
/** Return all model IDs, including the main model. */
std::set<ModelId> getModels() const;
/** Return pointers to all current layers. */
std::set<Layer *> getLayers() const;
signals:
void layerAdded(Layer *);
void layerRemoved(Layer *);
void layerAboutToBeDeleted(Layer *);
// Emitted when a layer is first added to a view, or when it is
// last removed from a view
void layerInAView(Layer *, bool);
void modelAdded(ModelId);
void mainModelChanged(ModelId); // a WaveFileModel; emitted after modelAdded
void modelGenerationFailed(QString transformName, QString message);
void modelGenerationWarning(QString transformName, QString message);
void modelRegenerationFailed(QString layerName, QString transformName,
QString message);
void modelRegenerationWarning(QString layerName, QString transformName,
QString message);
void alignmentComplete(ModelId); // an AlignmentModel
void alignmentFailed(ModelId, QString message); // an AlignmentModel
void activity(QString);
protected slots:
void performDeferredAlignment(ModelId);
protected:
void releaseModel(ModelId model);
/**
* Replace model "from" with "to", re-running transforms that were
* based on "from" as appropriate. If "from" is the same as "to",
* transforms based on "from" will be re-run with their existing
* sources. The new model should be known to the document already
* (it should have either been set as m_mainModel already or be
* present in m_models).
*
* This is used by setMainModel and refreshModel.
*/
void replaceModel(ModelId from, ModelId to);
/**
* If model is suitable for alignment, align it against the main
* model and store the alignment in the model. If the model has an
* alignment already for the current main model, leave it
* unchanged unless forceRecalculate is true.
*/
void alignModel(ModelId, bool forceRecalculate = false);
/*
* Every model that is in use by a layer in the document must be
* found in either m_mainModel or m_models. We own and control
* the lifespan of all of these models.
*/
/**
* The model that provides the underlying sample rate, etc. This
* model is not reference counted for layers, and is not freed
* unless it is replaced or the document is deleted.
*/
ModelId m_mainModel; // a WaveFileModel
struct ModelRecord
{
// Information associated with a non-main model. If this
// model is derived from another, then source will be
// something other than None and the transform name will be
// set appropriately. If the transform is set but source is
// None, then there was a transform involved but the (target)
// model has been modified since being generated from it.
// This does not use ModelTransformer::Input, because it would
// be confusing to have Input objects hanging around with None
// models in them.
ModelId source; // may be None
int channel;
Transform transform;
bool additional;
};
// These must be stored in increasing order of id (as in the
// ordered std::map), to ensure repeatability for automated tests
std::map<ModelId, ModelRecord> m_models;
std::set<ModelId> m_aggregateModels;
std::set<ModelId> m_alignmentModels;
/**
* Add an extra derived model (returned at the end of processing a
* transform).
*/
void addAdditionalModel(ModelId);
class AddLayerCommand : public Command
{
public:
AddLayerCommand(Document *d, View *view, Layer *layer);
virtual ~AddLayerCommand();
void execute() override;
void unexecute() override;
QString getName() const override;
protected:
Document *m_d;
View *m_view; // I don't own this
Layer *m_layer; // Document owns this, but I determine its lifespan
QString m_name;
bool m_added;
};
class RemoveLayerCommand : public Command
{
public:
RemoveLayerCommand(Document *d, View *view, Layer *layer);
virtual ~RemoveLayerCommand();
void execute() override;
void unexecute() override;
QString getName() const override;
protected:
Document *m_d;
View *m_view; // I don't own this
Layer *m_layer; // Document owns this, but I determine its lifespan
bool m_wasDormant;
QString m_name;
bool m_added;
};
typedef std::map<Layer *, std::set<View *> > LayerViewMap;
LayerViewMap m_layerViewMap;
void addToLayerViewMap(Layer *, View *);
void removeFromLayerViewMap(Layer *, View *);
QString getUniqueLayerName(QString candidate);
void writeBackwardCompatibleDerivation(QTextStream &, QString, ModelId,
const ModelRecord &) const;
void toXml(QTextStream &, QString, QString, bool asTemplate) const;
void writePlaceholderMainModel(QTextStream &, QString) const;
std::vector<Layer *> createLayersForDerivedModels(std::vector<ModelId>,
QStringList names);
/**
* And these are the layers. We also control the lifespans of
* these (usually through the commands used to add and remove them).
*/
typedef std::vector<Layer *> LayerList;
LayerList m_layers;
bool m_autoAlignment;
Align *m_align;
bool m_isIncomplete;
};
} // end namespace sv
#endif
|