annotate layer/LayerFactory.cpp @ 214:8520b7918104

* Fix overzealous cacheing in waveform layer that was leading to inaccurate positioning of view rects & playback pointer on overview widget sometimes
author Chris Cannam
date Thu, 01 Mar 2007 17:12:50 +0000
parents 57c2350a8c40
children 447cb52adc6b
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@59 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "LayerFactory.h"
Chris@0 17
Chris@0 18 #include "WaveformLayer.h"
Chris@0 19 #include "SpectrogramLayer.h"
Chris@0 20 #include "TimeRulerLayer.h"
Chris@0 21 #include "TimeInstantLayer.h"
Chris@0 22 #include "TimeValueLayer.h"
Chris@30 23 #include "NoteLayer.h"
Chris@35 24 #include "TextLayer.h"
Chris@0 25 #include "Colour3DPlotLayer.h"
Chris@133 26 #include "SpectrumLayer.h"
Chris@193 27 #include "SliceLayer.h"
Chris@193 28 #include "SliceableLayer.h"
Chris@0 29
Chris@128 30 #include "data/model/RangeSummarisableTimeValueModel.h"
Chris@128 31 #include "data/model/DenseTimeValueModel.h"
Chris@128 32 #include "data/model/SparseOneDimensionalModel.h"
Chris@128 33 #include "data/model/SparseTimeValueModel.h"
Chris@128 34 #include "data/model/NoteModel.h"
Chris@128 35 #include "data/model/TextModel.h"
Chris@128 36 #include "data/model/DenseThreeDimensionalModel.h"
Chris@156 37 #include "data/model/WaveFileModel.h"
Chris@156 38 #include "data/model/WritableWaveFileModel.h"
Chris@0 39
Chris@0 40 LayerFactory *
Chris@0 41 LayerFactory::m_instance = new LayerFactory;
Chris@0 42
Chris@0 43 LayerFactory *
Chris@125 44 LayerFactory::getInstance()
Chris@0 45 {
Chris@0 46 return m_instance;
Chris@0 47 }
Chris@0 48
Chris@0 49 LayerFactory::~LayerFactory()
Chris@0 50 {
Chris@0 51 }
Chris@0 52
Chris@0 53 QString
Chris@0 54 LayerFactory::getLayerPresentationName(LayerType type)
Chris@0 55 {
Chris@0 56 switch (type) {
Chris@0 57 case Waveform: return Layer::tr("Waveform");
Chris@0 58 case Spectrogram: return Layer::tr("Spectrogram");
Chris@0 59 case TimeRuler: return Layer::tr("Ruler");
Chris@0 60 case TimeInstants: return Layer::tr("Time Instants");
Chris@0 61 case TimeValues: return Layer::tr("Time Values");
Chris@30 62 case Notes: return Layer::tr("Notes");
Chris@35 63 case Text: return Layer::tr("Text");
Chris@0 64 case Colour3DPlot: return Layer::tr("Colour 3D Plot");
Chris@133 65 case Spectrum: return Layer::tr("Spectrum");
Chris@193 66 case Slice: return Layer::tr("Time Slice");
Chris@0 67
Chris@0 68 case MelodicRangeSpectrogram:
Chris@0 69 // The user can change all the parameters of this after the
Chris@0 70 // fact -- there's nothing permanently melodic-range about it
Chris@0 71 // that should be encoded in its name
Chris@0 72 return Layer::tr("Spectrogram");
Chris@11 73
Chris@37 74 case PeakFrequencySpectrogram:
Chris@37 75 // likewise
Chris@37 76 return Layer::tr("Spectrogram");
Chris@37 77
Chris@11 78 default: break;
Chris@0 79 }
Chris@0 80
Chris@0 81 return Layer::tr("Layer");
Chris@0 82 }
Chris@0 83
Chris@193 84 bool
Chris@193 85 LayerFactory::isLayerSliceable(const Layer *layer)
Chris@193 86 {
Chris@193 87 if (dynamic_cast<const SliceableLayer *>(layer)) {
Chris@193 88 if (dynamic_cast<const SpectrogramLayer *>(layer)) {
Chris@193 89
Chris@193 90 //!!! We can create slices of spectrograms, but there's a
Chris@193 91 // problem managing the models. The source model for the
Chris@193 92 // slice layer has to be one of the spectrogram's FFT
Chris@193 93 // models -- that's fine, except that we can't store &
Chris@193 94 // recall the slice layer with a reference to that model
Chris@193 95 // because the model is internal to the spectrogram layer
Chris@193 96 // and the document has no record of it. We would need
Chris@193 97 // some other way of managing models that are used in this
Chris@193 98 // way. For the moment we just don't allow slices of
Chris@193 99 // spectrograms -- and provide a spectrum layer for this
Chris@193 100 // instead.
Chris@193 101 //
Chris@193 102 // This business needs a bit more thought -- either come
Chris@193 103 // up with a sensible way to deal with that stuff, or
Chris@193 104 // simplify the existing slice layer logic so that it
Chris@193 105 // doesn't have to deal with models disappearing on it at
Chris@193 106 // all (and use the normal Document setModel mechanism to
Chris@193 107 // set its sliceable model instead of the fancy pants
Chris@193 108 // nonsense it's doing at the moment).
Chris@193 109
Chris@193 110 return false;
Chris@193 111 }
Chris@193 112 return true;
Chris@193 113 }
Chris@193 114 return false;
Chris@193 115 }
Chris@193 116
Chris@0 117 LayerFactory::LayerTypeSet
Chris@0 118 LayerFactory::getValidLayerTypes(Model *model)
Chris@0 119 {
Chris@0 120 LayerTypeSet types;
Chris@0 121
Chris@0 122 if (dynamic_cast<DenseThreeDimensionalModel *>(model)) {
Chris@0 123 types.insert(Colour3DPlot);
Chris@193 124 types.insert(Slice);
Chris@193 125 }
Chris@193 126
Chris@193 127 if (dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
Chris@193 128 types.insert(Waveform);
Chris@0 129 }
Chris@0 130
Chris@0 131 if (dynamic_cast<DenseTimeValueModel *>(model)) {
Chris@0 132 types.insert(Spectrogram);
Chris@0 133 types.insert(MelodicRangeSpectrogram);
Chris@37 134 types.insert(PeakFrequencySpectrogram);
Chris@0 135 }
Chris@0 136
Chris@0 137 if (dynamic_cast<SparseOneDimensionalModel *>(model)) {
Chris@0 138 types.insert(TimeInstants);
Chris@0 139 }
Chris@0 140
Chris@0 141 if (dynamic_cast<SparseTimeValueModel *>(model)) {
Chris@0 142 types.insert(TimeValues);
Chris@35 143
Chris@35 144 }
Chris@35 145 if (dynamic_cast<NoteModel *>(model)) {
Chris@35 146 types.insert(Notes);
Chris@0 147 }
Chris@0 148
Chris@35 149 if (dynamic_cast<TextModel *>(model)) {
Chris@35 150 types.insert(Text);
Chris@30 151 }
Chris@30 152
Chris@133 153 if (dynamic_cast<DenseTimeValueModel *>(model)) {
Chris@133 154 types.insert(Spectrum);
Chris@133 155 }
Chris@133 156
Chris@0 157 // We don't count TimeRuler here as it doesn't actually display
Chris@0 158 // the data, although it can be backed by any model
Chris@0 159
Chris@0 160 return types;
Chris@0 161 }
Chris@0 162
Chris@17 163 LayerFactory::LayerTypeSet
Chris@17 164 LayerFactory::getValidEmptyLayerTypes()
Chris@17 165 {
Chris@17 166 LayerTypeSet types;
Chris@17 167 types.insert(TimeInstants);
Chris@17 168 types.insert(TimeValues);
Chris@30 169 types.insert(Notes);
Chris@35 170 types.insert(Text);
Chris@17 171 //!!! and in principle Colour3DPlot -- now that's a challenge
Chris@17 172 return types;
Chris@17 173 }
Chris@17 174
Chris@0 175 LayerFactory::LayerType
Chris@6 176 LayerFactory::getLayerType(const Layer *layer)
Chris@0 177 {
Chris@6 178 if (dynamic_cast<const WaveformLayer *>(layer)) return Waveform;
Chris@6 179 if (dynamic_cast<const SpectrogramLayer *>(layer)) return Spectrogram;
Chris@6 180 if (dynamic_cast<const TimeRulerLayer *>(layer)) return TimeRuler;
Chris@6 181 if (dynamic_cast<const TimeInstantLayer *>(layer)) return TimeInstants;
Chris@6 182 if (dynamic_cast<const TimeValueLayer *>(layer)) return TimeValues;
Chris@30 183 if (dynamic_cast<const NoteLayer *>(layer)) return Notes;
Chris@35 184 if (dynamic_cast<const TextLayer *>(layer)) return Text;
Chris@6 185 if (dynamic_cast<const Colour3DPlotLayer *>(layer)) return Colour3DPlot;
Chris@133 186 if (dynamic_cast<const SpectrumLayer *>(layer)) return Spectrum;
Chris@193 187 if (dynamic_cast<const SliceLayer *>(layer)) return Slice;
Chris@6 188 return UnknownLayer;
Chris@6 189 }
Chris@6 190
Chris@6 191 QString
Chris@17 192 LayerFactory::getLayerIconName(LayerType type)
Chris@17 193 {
Chris@17 194 switch (type) {
Chris@17 195 case Waveform: return "waveform";
Chris@17 196 case Spectrogram: return "spectrogram";
Chris@17 197 case TimeRuler: return "timeruler";
Chris@17 198 case TimeInstants: return "instants";
Chris@17 199 case TimeValues: return "values";
Chris@30 200 case Notes: return "notes";
Chris@35 201 case Text: return "text";
Chris@17 202 case Colour3DPlot: return "colour3d";
Chris@133 203 case Spectrum: return "spectrum";
Chris@193 204 case Slice: return "spectrum";
Chris@17 205 default: return "unknown";
Chris@17 206 }
Chris@17 207 }
Chris@17 208
Chris@17 209 QString
Chris@6 210 LayerFactory::getLayerTypeName(LayerType type)
Chris@6 211 {
Chris@6 212 switch (type) {
Chris@6 213 case Waveform: return "waveform";
Chris@6 214 case Spectrogram: return "spectrogram";
Chris@6 215 case TimeRuler: return "timeruler";
Chris@6 216 case TimeInstants: return "timeinstants";
Chris@6 217 case TimeValues: return "timevalues";
Chris@30 218 case Notes: return "notes";
Chris@35 219 case Text: return "text";
Chris@6 220 case Colour3DPlot: return "colour3dplot";
Chris@133 221 case Spectrum: return "spectrum";
Chris@193 222 case Slice: return "slice";
Chris@6 223 default: return "unknown";
Chris@6 224 }
Chris@6 225 }
Chris@6 226
Chris@6 227 LayerFactory::LayerType
Chris@6 228 LayerFactory::getLayerTypeForName(QString name)
Chris@6 229 {
Chris@6 230 if (name == "waveform") return Waveform;
Chris@6 231 if (name == "spectrogram") return Spectrogram;
Chris@6 232 if (name == "timeruler") return TimeRuler;
Chris@6 233 if (name == "timeinstants") return TimeInstants;
Chris@6 234 if (name == "timevalues") return TimeValues;
Chris@30 235 if (name == "notes") return Notes;
Chris@35 236 if (name == "text") return Text;
Chris@6 237 if (name == "colour3dplot") return Colour3DPlot;
Chris@133 238 if (name == "spectrum") return Spectrum;
Chris@193 239 if (name == "slice") return Slice;
Chris@0 240 return UnknownLayer;
Chris@0 241 }
Chris@0 242
Chris@0 243 void
Chris@0 244 LayerFactory::setModel(Layer *layer, Model *model)
Chris@0 245 {
Chris@156 246 // if (trySetModel<WaveformLayer, RangeSummarisableTimeValueModel>(layer, model))
Chris@156 247 // return;
Chris@156 248
Chris@156 249 if (trySetModel<WaveformLayer, WaveFileModel>(layer, model))
Chris@156 250 return;
Chris@156 251
Chris@156 252 if (trySetModel<WaveformLayer, WritableWaveFileModel>(layer, model))
Chris@0 253 return;
Chris@0 254
Chris@0 255 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
Chris@0 256 return;
Chris@0 257
Chris@0 258 if (trySetModel<TimeRulerLayer, Model>(layer, model))
Chris@0 259 return;
Chris@0 260
Chris@0 261 if (trySetModel<TimeInstantLayer, SparseOneDimensionalModel>(layer, model))
Chris@0 262 return;
Chris@0 263
Chris@0 264 if (trySetModel<TimeValueLayer, SparseTimeValueModel>(layer, model))
Chris@0 265 return;
Chris@0 266
Chris@30 267 if (trySetModel<NoteLayer, NoteModel>(layer, model))
Chris@30 268 return;
Chris@30 269
Chris@35 270 if (trySetModel<TextLayer, TextModel>(layer, model))
Chris@35 271 return;
Chris@35 272
Chris@0 273 if (trySetModel<Colour3DPlotLayer, DenseThreeDimensionalModel>(layer, model))
Chris@0 274 return;
Chris@0 275
Chris@0 276 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
Chris@0 277 return;
Chris@133 278
Chris@133 279 if (trySetModel<SpectrumLayer, DenseTimeValueModel>(layer, model))
Chris@133 280 return;
Chris@193 281
Chris@193 282 // if (trySetModel<SliceLayer, DenseThreeDimensionalModel>(layer, model))
Chris@193 283 // return;
Chris@0 284 }
Chris@0 285
Chris@17 286 Model *
Chris@17 287 LayerFactory::createEmptyModel(LayerType layerType, Model *baseModel)
Chris@17 288 {
Chris@17 289 if (layerType == TimeInstants) {
Chris@17 290 return new SparseOneDimensionalModel(baseModel->getSampleRate(), 1);
Chris@17 291 } else if (layerType == TimeValues) {
Chris@17 292 return new SparseTimeValueModel(baseModel->getSampleRate(), 1,
Chris@17 293 0.0, 0.0, true);
Chris@30 294 } else if (layerType == Notes) {
Chris@30 295 return new NoteModel(baseModel->getSampleRate(), 1,
Chris@30 296 0.0, 0.0, true);
Chris@35 297 } else if (layerType == Text) {
Chris@35 298 return new TextModel(baseModel->getSampleRate(), 1, true);
Chris@17 299 } else {
Chris@17 300 return 0;
Chris@17 301 }
Chris@17 302 }
Chris@17 303
Chris@53 304 int
Chris@53 305 LayerFactory::getChannel(Layer *layer)
Chris@53 306 {
Chris@53 307 if (dynamic_cast<WaveformLayer *>(layer)) {
Chris@53 308 return dynamic_cast<WaveformLayer *>(layer)->getChannel();
Chris@53 309 }
Chris@53 310 if (dynamic_cast<SpectrogramLayer *>(layer)) {
Chris@53 311 return dynamic_cast<SpectrogramLayer *>(layer)->getChannel();
Chris@53 312 }
Chris@53 313 return -1;
Chris@53 314 }
Chris@53 315
Chris@53 316 void
Chris@53 317 LayerFactory::setChannel(Layer *layer, int channel)
Chris@53 318 {
Chris@53 319 if (dynamic_cast<WaveformLayer *>(layer)) {
Chris@53 320 dynamic_cast<WaveformLayer *>(layer)->setChannel(channel);
Chris@53 321 return;
Chris@53 322 }
Chris@53 323 if (dynamic_cast<SpectrogramLayer *>(layer)) {
Chris@53 324 dynamic_cast<SpectrogramLayer *>(layer)->setChannel(channel);
Chris@53 325 return;
Chris@53 326 }
Chris@53 327 }
Chris@53 328
Chris@0 329 Layer *
Chris@53 330 LayerFactory::createLayer(LayerType type)
Chris@0 331 {
Chris@0 332 Layer *layer = 0;
Chris@0 333
Chris@0 334 switch (type) {
Chris@0 335
Chris@0 336 case Waveform:
Chris@44 337 layer = new WaveformLayer;
Chris@0 338 break;
Chris@0 339
Chris@0 340 case Spectrogram:
Chris@44 341 layer = new SpectrogramLayer;
Chris@0 342 break;
Chris@0 343
Chris@0 344 case TimeRuler:
Chris@44 345 layer = new TimeRulerLayer;
Chris@0 346 break;
Chris@0 347
Chris@0 348 case TimeInstants:
Chris@44 349 layer = new TimeInstantLayer;
Chris@0 350 break;
Chris@0 351
Chris@0 352 case TimeValues:
Chris@44 353 layer = new TimeValueLayer;
Chris@0 354 break;
Chris@0 355
Chris@30 356 case Notes:
Chris@44 357 layer = new NoteLayer;
Chris@30 358 break;
Chris@30 359
Chris@35 360 case Text:
Chris@44 361 layer = new TextLayer;
Chris@35 362 break;
Chris@35 363
Chris@0 364 case Colour3DPlot:
Chris@44 365 layer = new Colour3DPlotLayer;
Chris@0 366 break;
Chris@0 367
Chris@133 368 case Spectrum:
Chris@133 369 layer = new SpectrumLayer;
Chris@133 370 break;
Chris@133 371
Chris@193 372 case Slice:
Chris@193 373 layer = new SliceLayer;
Chris@193 374 break;
Chris@193 375
Chris@0 376 case MelodicRangeSpectrogram:
Chris@44 377 layer = new SpectrogramLayer(SpectrogramLayer::MelodicRange);
Chris@0 378 break;
Chris@11 379
Chris@37 380 case PeakFrequencySpectrogram:
Chris@44 381 layer = new SpectrogramLayer(SpectrogramLayer::MelodicPeaks);
Chris@37 382 break;
Chris@37 383
Chris@11 384 default: break;
Chris@0 385 }
Chris@0 386
Chris@0 387 if (!layer) {
Chris@0 388 std::cerr << "LayerFactory::createLayer: Unknown layer type "
Chris@0 389 << type << std::endl;
Chris@0 390 } else {
Chris@101 391 // std::cerr << "LayerFactory::createLayer: Setting object name "
Chris@101 392 // << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl;
Chris@0 393 layer->setObjectName(getLayerPresentationName(type));
Chris@0 394 }
Chris@0 395
Chris@0 396 return layer;
Chris@0 397 }
Chris@0 398