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@0
|
27
|
Chris@128
|
28 #include "data/model/RangeSummarisableTimeValueModel.h"
|
Chris@128
|
29 #include "data/model/DenseTimeValueModel.h"
|
Chris@128
|
30 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@128
|
31 #include "data/model/SparseTimeValueModel.h"
|
Chris@128
|
32 #include "data/model/NoteModel.h"
|
Chris@128
|
33 #include "data/model/TextModel.h"
|
Chris@128
|
34 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@156
|
35 #include "data/model/WaveFileModel.h"
|
Chris@156
|
36 #include "data/model/WritableWaveFileModel.h"
|
Chris@0
|
37
|
Chris@0
|
38 LayerFactory *
|
Chris@0
|
39 LayerFactory::m_instance = new LayerFactory;
|
Chris@0
|
40
|
Chris@0
|
41 LayerFactory *
|
Chris@125
|
42 LayerFactory::getInstance()
|
Chris@0
|
43 {
|
Chris@0
|
44 return m_instance;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 LayerFactory::~LayerFactory()
|
Chris@0
|
48 {
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 QString
|
Chris@0
|
52 LayerFactory::getLayerPresentationName(LayerType type)
|
Chris@0
|
53 {
|
Chris@0
|
54 switch (type) {
|
Chris@0
|
55 case Waveform: return Layer::tr("Waveform");
|
Chris@0
|
56 case Spectrogram: return Layer::tr("Spectrogram");
|
Chris@0
|
57 case TimeRuler: return Layer::tr("Ruler");
|
Chris@0
|
58 case TimeInstants: return Layer::tr("Time Instants");
|
Chris@0
|
59 case TimeValues: return Layer::tr("Time Values");
|
Chris@30
|
60 case Notes: return Layer::tr("Notes");
|
Chris@35
|
61 case Text: return Layer::tr("Text");
|
Chris@0
|
62 case Colour3DPlot: return Layer::tr("Colour 3D Plot");
|
Chris@133
|
63 case Spectrum: return Layer::tr("Spectrum");
|
Chris@0
|
64
|
Chris@0
|
65 case MelodicRangeSpectrogram:
|
Chris@0
|
66 // The user can change all the parameters of this after the
|
Chris@0
|
67 // fact -- there's nothing permanently melodic-range about it
|
Chris@0
|
68 // that should be encoded in its name
|
Chris@0
|
69 return Layer::tr("Spectrogram");
|
Chris@11
|
70
|
Chris@37
|
71 case PeakFrequencySpectrogram:
|
Chris@37
|
72 // likewise
|
Chris@37
|
73 return Layer::tr("Spectrogram");
|
Chris@37
|
74
|
Chris@11
|
75 default: break;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 return Layer::tr("Layer");
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 LayerFactory::LayerTypeSet
|
Chris@0
|
82 LayerFactory::getValidLayerTypes(Model *model)
|
Chris@0
|
83 {
|
Chris@0
|
84 LayerTypeSet types;
|
Chris@0
|
85
|
Chris@0
|
86 if (dynamic_cast<DenseThreeDimensionalModel *>(model)) {
|
Chris@0
|
87 types.insert(Colour3DPlot);
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 if (dynamic_cast<DenseTimeValueModel *>(model)) {
|
Chris@0
|
91 types.insert(Spectrogram);
|
Chris@0
|
92 types.insert(MelodicRangeSpectrogram);
|
Chris@37
|
93 types.insert(PeakFrequencySpectrogram);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 if (dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
|
Chris@0
|
97 types.insert(Waveform);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 if (dynamic_cast<SparseOneDimensionalModel *>(model)) {
|
Chris@0
|
101 types.insert(TimeInstants);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 if (dynamic_cast<SparseTimeValueModel *>(model)) {
|
Chris@0
|
105 types.insert(TimeValues);
|
Chris@35
|
106
|
Chris@35
|
107 }
|
Chris@35
|
108 if (dynamic_cast<NoteModel *>(model)) {
|
Chris@35
|
109 types.insert(Notes);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@35
|
112 if (dynamic_cast<TextModel *>(model)) {
|
Chris@35
|
113 types.insert(Text);
|
Chris@30
|
114 }
|
Chris@30
|
115
|
Chris@133
|
116 if (dynamic_cast<DenseTimeValueModel *>(model)) {
|
Chris@133
|
117 types.insert(Spectrum);
|
Chris@133
|
118 }
|
Chris@133
|
119
|
Chris@0
|
120 // We don't count TimeRuler here as it doesn't actually display
|
Chris@0
|
121 // the data, although it can be backed by any model
|
Chris@0
|
122
|
Chris@0
|
123 return types;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@17
|
126 LayerFactory::LayerTypeSet
|
Chris@17
|
127 LayerFactory::getValidEmptyLayerTypes()
|
Chris@17
|
128 {
|
Chris@17
|
129 LayerTypeSet types;
|
Chris@17
|
130 types.insert(TimeInstants);
|
Chris@17
|
131 types.insert(TimeValues);
|
Chris@30
|
132 types.insert(Notes);
|
Chris@35
|
133 types.insert(Text);
|
Chris@17
|
134 //!!! and in principle Colour3DPlot -- now that's a challenge
|
Chris@17
|
135 return types;
|
Chris@17
|
136 }
|
Chris@17
|
137
|
Chris@0
|
138 LayerFactory::LayerType
|
Chris@6
|
139 LayerFactory::getLayerType(const Layer *layer)
|
Chris@0
|
140 {
|
Chris@6
|
141 if (dynamic_cast<const WaveformLayer *>(layer)) return Waveform;
|
Chris@6
|
142 if (dynamic_cast<const SpectrogramLayer *>(layer)) return Spectrogram;
|
Chris@6
|
143 if (dynamic_cast<const TimeRulerLayer *>(layer)) return TimeRuler;
|
Chris@6
|
144 if (dynamic_cast<const TimeInstantLayer *>(layer)) return TimeInstants;
|
Chris@6
|
145 if (dynamic_cast<const TimeValueLayer *>(layer)) return TimeValues;
|
Chris@30
|
146 if (dynamic_cast<const NoteLayer *>(layer)) return Notes;
|
Chris@35
|
147 if (dynamic_cast<const TextLayer *>(layer)) return Text;
|
Chris@6
|
148 if (dynamic_cast<const Colour3DPlotLayer *>(layer)) return Colour3DPlot;
|
Chris@133
|
149 if (dynamic_cast<const SpectrumLayer *>(layer)) return Spectrum;
|
Chris@6
|
150 return UnknownLayer;
|
Chris@6
|
151 }
|
Chris@6
|
152
|
Chris@6
|
153 QString
|
Chris@17
|
154 LayerFactory::getLayerIconName(LayerType type)
|
Chris@17
|
155 {
|
Chris@17
|
156 switch (type) {
|
Chris@17
|
157 case Waveform: return "waveform";
|
Chris@17
|
158 case Spectrogram: return "spectrogram";
|
Chris@17
|
159 case TimeRuler: return "timeruler";
|
Chris@17
|
160 case TimeInstants: return "instants";
|
Chris@17
|
161 case TimeValues: return "values";
|
Chris@30
|
162 case Notes: return "notes";
|
Chris@35
|
163 case Text: return "text";
|
Chris@17
|
164 case Colour3DPlot: return "colour3d";
|
Chris@133
|
165 case Spectrum: return "spectrum";
|
Chris@17
|
166 default: return "unknown";
|
Chris@17
|
167 }
|
Chris@17
|
168 }
|
Chris@17
|
169
|
Chris@17
|
170 QString
|
Chris@6
|
171 LayerFactory::getLayerTypeName(LayerType type)
|
Chris@6
|
172 {
|
Chris@6
|
173 switch (type) {
|
Chris@6
|
174 case Waveform: return "waveform";
|
Chris@6
|
175 case Spectrogram: return "spectrogram";
|
Chris@6
|
176 case TimeRuler: return "timeruler";
|
Chris@6
|
177 case TimeInstants: return "timeinstants";
|
Chris@6
|
178 case TimeValues: return "timevalues";
|
Chris@30
|
179 case Notes: return "notes";
|
Chris@35
|
180 case Text: return "text";
|
Chris@6
|
181 case Colour3DPlot: return "colour3dplot";
|
Chris@133
|
182 case Spectrum: return "spectrum";
|
Chris@6
|
183 default: return "unknown";
|
Chris@6
|
184 }
|
Chris@6
|
185 }
|
Chris@6
|
186
|
Chris@6
|
187 LayerFactory::LayerType
|
Chris@6
|
188 LayerFactory::getLayerTypeForName(QString name)
|
Chris@6
|
189 {
|
Chris@6
|
190 if (name == "waveform") return Waveform;
|
Chris@6
|
191 if (name == "spectrogram") return Spectrogram;
|
Chris@6
|
192 if (name == "timeruler") return TimeRuler;
|
Chris@6
|
193 if (name == "timeinstants") return TimeInstants;
|
Chris@6
|
194 if (name == "timevalues") return TimeValues;
|
Chris@30
|
195 if (name == "notes") return Notes;
|
Chris@35
|
196 if (name == "text") return Text;
|
Chris@6
|
197 if (name == "colour3dplot") return Colour3DPlot;
|
Chris@133
|
198 if (name == "spectrum") return Spectrum;
|
Chris@0
|
199 return UnknownLayer;
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 void
|
Chris@0
|
203 LayerFactory::setModel(Layer *layer, Model *model)
|
Chris@0
|
204 {
|
Chris@156
|
205 // if (trySetModel<WaveformLayer, RangeSummarisableTimeValueModel>(layer, model))
|
Chris@156
|
206 // return;
|
Chris@156
|
207
|
Chris@156
|
208 if (trySetModel<WaveformLayer, WaveFileModel>(layer, model))
|
Chris@156
|
209 return;
|
Chris@156
|
210
|
Chris@156
|
211 if (trySetModel<WaveformLayer, WritableWaveFileModel>(layer, model))
|
Chris@0
|
212 return;
|
Chris@0
|
213
|
Chris@0
|
214 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
|
Chris@0
|
215 return;
|
Chris@0
|
216
|
Chris@0
|
217 if (trySetModel<TimeRulerLayer, Model>(layer, model))
|
Chris@0
|
218 return;
|
Chris@0
|
219
|
Chris@0
|
220 if (trySetModel<TimeInstantLayer, SparseOneDimensionalModel>(layer, model))
|
Chris@0
|
221 return;
|
Chris@0
|
222
|
Chris@0
|
223 if (trySetModel<TimeValueLayer, SparseTimeValueModel>(layer, model))
|
Chris@0
|
224 return;
|
Chris@0
|
225
|
Chris@30
|
226 if (trySetModel<NoteLayer, NoteModel>(layer, model))
|
Chris@30
|
227 return;
|
Chris@30
|
228
|
Chris@35
|
229 if (trySetModel<TextLayer, TextModel>(layer, model))
|
Chris@35
|
230 return;
|
Chris@35
|
231
|
Chris@0
|
232 if (trySetModel<Colour3DPlotLayer, DenseThreeDimensionalModel>(layer, model))
|
Chris@0
|
233 return;
|
Chris@0
|
234
|
Chris@0
|
235 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
|
Chris@0
|
236 return;
|
Chris@133
|
237
|
Chris@133
|
238 if (trySetModel<SpectrumLayer, DenseTimeValueModel>(layer, model))
|
Chris@133
|
239 return;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@17
|
242 Model *
|
Chris@17
|
243 LayerFactory::createEmptyModel(LayerType layerType, Model *baseModel)
|
Chris@17
|
244 {
|
Chris@17
|
245 if (layerType == TimeInstants) {
|
Chris@17
|
246 return new SparseOneDimensionalModel(baseModel->getSampleRate(), 1);
|
Chris@17
|
247 } else if (layerType == TimeValues) {
|
Chris@17
|
248 return new SparseTimeValueModel(baseModel->getSampleRate(), 1,
|
Chris@17
|
249 0.0, 0.0, true);
|
Chris@30
|
250 } else if (layerType == Notes) {
|
Chris@30
|
251 return new NoteModel(baseModel->getSampleRate(), 1,
|
Chris@30
|
252 0.0, 0.0, true);
|
Chris@35
|
253 } else if (layerType == Text) {
|
Chris@35
|
254 return new TextModel(baseModel->getSampleRate(), 1, true);
|
Chris@17
|
255 } else {
|
Chris@17
|
256 return 0;
|
Chris@17
|
257 }
|
Chris@17
|
258 }
|
Chris@17
|
259
|
Chris@53
|
260 int
|
Chris@53
|
261 LayerFactory::getChannel(Layer *layer)
|
Chris@53
|
262 {
|
Chris@53
|
263 if (dynamic_cast<WaveformLayer *>(layer)) {
|
Chris@53
|
264 return dynamic_cast<WaveformLayer *>(layer)->getChannel();
|
Chris@53
|
265 }
|
Chris@53
|
266 if (dynamic_cast<SpectrogramLayer *>(layer)) {
|
Chris@53
|
267 return dynamic_cast<SpectrogramLayer *>(layer)->getChannel();
|
Chris@53
|
268 }
|
Chris@53
|
269 return -1;
|
Chris@53
|
270 }
|
Chris@53
|
271
|
Chris@53
|
272 void
|
Chris@53
|
273 LayerFactory::setChannel(Layer *layer, int channel)
|
Chris@53
|
274 {
|
Chris@53
|
275 if (dynamic_cast<WaveformLayer *>(layer)) {
|
Chris@53
|
276 dynamic_cast<WaveformLayer *>(layer)->setChannel(channel);
|
Chris@53
|
277 return;
|
Chris@53
|
278 }
|
Chris@53
|
279 if (dynamic_cast<SpectrogramLayer *>(layer)) {
|
Chris@53
|
280 dynamic_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@53
|
281 return;
|
Chris@53
|
282 }
|
Chris@53
|
283 }
|
Chris@53
|
284
|
Chris@0
|
285 Layer *
|
Chris@53
|
286 LayerFactory::createLayer(LayerType type)
|
Chris@0
|
287 {
|
Chris@0
|
288 Layer *layer = 0;
|
Chris@0
|
289
|
Chris@0
|
290 switch (type) {
|
Chris@0
|
291
|
Chris@0
|
292 case Waveform:
|
Chris@44
|
293 layer = new WaveformLayer;
|
Chris@0
|
294 break;
|
Chris@0
|
295
|
Chris@0
|
296 case Spectrogram:
|
Chris@44
|
297 layer = new SpectrogramLayer;
|
Chris@0
|
298 break;
|
Chris@0
|
299
|
Chris@0
|
300 case TimeRuler:
|
Chris@44
|
301 layer = new TimeRulerLayer;
|
Chris@0
|
302 break;
|
Chris@0
|
303
|
Chris@0
|
304 case TimeInstants:
|
Chris@44
|
305 layer = new TimeInstantLayer;
|
Chris@0
|
306 break;
|
Chris@0
|
307
|
Chris@0
|
308 case TimeValues:
|
Chris@44
|
309 layer = new TimeValueLayer;
|
Chris@0
|
310 break;
|
Chris@0
|
311
|
Chris@30
|
312 case Notes:
|
Chris@44
|
313 layer = new NoteLayer;
|
Chris@30
|
314 break;
|
Chris@30
|
315
|
Chris@35
|
316 case Text:
|
Chris@44
|
317 layer = new TextLayer;
|
Chris@35
|
318 break;
|
Chris@35
|
319
|
Chris@0
|
320 case Colour3DPlot:
|
Chris@44
|
321 layer = new Colour3DPlotLayer;
|
Chris@0
|
322 break;
|
Chris@0
|
323
|
Chris@133
|
324 case Spectrum:
|
Chris@133
|
325 layer = new SpectrumLayer;
|
Chris@133
|
326 break;
|
Chris@133
|
327
|
Chris@0
|
328 case MelodicRangeSpectrogram:
|
Chris@44
|
329 layer = new SpectrogramLayer(SpectrogramLayer::MelodicRange);
|
Chris@0
|
330 break;
|
Chris@11
|
331
|
Chris@37
|
332 case PeakFrequencySpectrogram:
|
Chris@44
|
333 layer = new SpectrogramLayer(SpectrogramLayer::MelodicPeaks);
|
Chris@37
|
334 break;
|
Chris@37
|
335
|
Chris@11
|
336 default: break;
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 if (!layer) {
|
Chris@0
|
340 std::cerr << "LayerFactory::createLayer: Unknown layer type "
|
Chris@0
|
341 << type << std::endl;
|
Chris@0
|
342 } else {
|
Chris@101
|
343 // std::cerr << "LayerFactory::createLayer: Setting object name "
|
Chris@101
|
344 // << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl;
|
Chris@0
|
345 layer->setObjectName(getLayerPresentationName(type));
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 return layer;
|
Chris@0
|
349 }
|
Chris@0
|
350
|