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