Chris@0
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 A waveform viewer and audio annotation editor.
|
Chris@5
|
5 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@0
|
6
|
Chris@0
|
7 This is experimental software. Not for distribution.
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 #include "LayerFactory.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "WaveformLayer.h"
|
Chris@0
|
13 #include "SpectrogramLayer.h"
|
Chris@0
|
14 #include "TimeRulerLayer.h"
|
Chris@0
|
15 #include "TimeInstantLayer.h"
|
Chris@0
|
16 #include "TimeValueLayer.h"
|
Chris@30
|
17 #include "NoteLayer.h"
|
Chris@35
|
18 #include "TextLayer.h"
|
Chris@0
|
19 #include "Colour3DPlotLayer.h"
|
Chris@0
|
20
|
Chris@0
|
21 #include "model/RangeSummarisableTimeValueModel.h"
|
Chris@0
|
22 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
23 #include "model/SparseOneDimensionalModel.h"
|
Chris@0
|
24 #include "model/SparseTimeValueModel.h"
|
Chris@30
|
25 #include "model/NoteModel.h"
|
Chris@35
|
26 #include "model/TextModel.h"
|
Chris@0
|
27 #include "model/DenseThreeDimensionalModel.h"
|
Chris@0
|
28
|
Chris@0
|
29 LayerFactory *
|
Chris@0
|
30 LayerFactory::m_instance = new LayerFactory;
|
Chris@0
|
31
|
Chris@0
|
32 LayerFactory *
|
Chris@0
|
33 LayerFactory::instance()
|
Chris@0
|
34 {
|
Chris@0
|
35 return m_instance;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 LayerFactory::~LayerFactory()
|
Chris@0
|
39 {
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 QString
|
Chris@0
|
43 LayerFactory::getLayerPresentationName(LayerType type)
|
Chris@0
|
44 {
|
Chris@0
|
45 switch (type) {
|
Chris@0
|
46 case Waveform: return Layer::tr("Waveform");
|
Chris@0
|
47 case Spectrogram: return Layer::tr("Spectrogram");
|
Chris@0
|
48 case TimeRuler: return Layer::tr("Ruler");
|
Chris@0
|
49 case TimeInstants: return Layer::tr("Time Instants");
|
Chris@0
|
50 case TimeValues: return Layer::tr("Time Values");
|
Chris@30
|
51 case Notes: return Layer::tr("Notes");
|
Chris@35
|
52 case Text: return Layer::tr("Text");
|
Chris@0
|
53 case Colour3DPlot: return Layer::tr("Colour 3D Plot");
|
Chris@0
|
54
|
Chris@0
|
55 case MelodicRangeSpectrogram:
|
Chris@0
|
56 // The user can change all the parameters of this after the
|
Chris@0
|
57 // fact -- there's nothing permanently melodic-range about it
|
Chris@0
|
58 // that should be encoded in its name
|
Chris@0
|
59 return Layer::tr("Spectrogram");
|
Chris@11
|
60
|
Chris@11
|
61 default: break;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 return Layer::tr("Layer");
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 LayerFactory::LayerTypeSet
|
Chris@0
|
68 LayerFactory::getValidLayerTypes(Model *model)
|
Chris@0
|
69 {
|
Chris@0
|
70 LayerTypeSet types;
|
Chris@0
|
71
|
Chris@0
|
72 if (dynamic_cast<DenseThreeDimensionalModel *>(model)) {
|
Chris@0
|
73 types.insert(Colour3DPlot);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 if (dynamic_cast<DenseTimeValueModel *>(model)) {
|
Chris@0
|
77 types.insert(Spectrogram);
|
Chris@0
|
78 types.insert(MelodicRangeSpectrogram);
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 if (dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
|
Chris@0
|
82 types.insert(Waveform);
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 if (dynamic_cast<SparseOneDimensionalModel *>(model)) {
|
Chris@0
|
86 types.insert(TimeInstants);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 if (dynamic_cast<SparseTimeValueModel *>(model)) {
|
Chris@0
|
90 types.insert(TimeValues);
|
Chris@35
|
91
|
Chris@35
|
92 }
|
Chris@35
|
93 if (dynamic_cast<NoteModel *>(model)) {
|
Chris@35
|
94 types.insert(Notes);
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@35
|
97 if (dynamic_cast<TextModel *>(model)) {
|
Chris@35
|
98 types.insert(Text);
|
Chris@30
|
99 }
|
Chris@30
|
100
|
Chris@0
|
101 // We don't count TimeRuler here as it doesn't actually display
|
Chris@0
|
102 // the data, although it can be backed by any model
|
Chris@0
|
103
|
Chris@0
|
104 return types;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@17
|
107 LayerFactory::LayerTypeSet
|
Chris@17
|
108 LayerFactory::getValidEmptyLayerTypes()
|
Chris@17
|
109 {
|
Chris@17
|
110 LayerTypeSet types;
|
Chris@17
|
111 types.insert(TimeInstants);
|
Chris@17
|
112 types.insert(TimeValues);
|
Chris@30
|
113 types.insert(Notes);
|
Chris@35
|
114 types.insert(Text);
|
Chris@17
|
115 //!!! and in principle Colour3DPlot -- now that's a challenge
|
Chris@17
|
116 return types;
|
Chris@17
|
117 }
|
Chris@17
|
118
|
Chris@0
|
119 LayerFactory::LayerType
|
Chris@6
|
120 LayerFactory::getLayerType(const Layer *layer)
|
Chris@0
|
121 {
|
Chris@6
|
122 if (dynamic_cast<const WaveformLayer *>(layer)) return Waveform;
|
Chris@6
|
123 if (dynamic_cast<const SpectrogramLayer *>(layer)) return Spectrogram;
|
Chris@6
|
124 if (dynamic_cast<const TimeRulerLayer *>(layer)) return TimeRuler;
|
Chris@6
|
125 if (dynamic_cast<const TimeInstantLayer *>(layer)) return TimeInstants;
|
Chris@6
|
126 if (dynamic_cast<const TimeValueLayer *>(layer)) return TimeValues;
|
Chris@30
|
127 if (dynamic_cast<const NoteLayer *>(layer)) return Notes;
|
Chris@35
|
128 if (dynamic_cast<const TextLayer *>(layer)) return Text;
|
Chris@6
|
129 if (dynamic_cast<const Colour3DPlotLayer *>(layer)) return Colour3DPlot;
|
Chris@6
|
130 return UnknownLayer;
|
Chris@6
|
131 }
|
Chris@6
|
132
|
Chris@6
|
133 QString
|
Chris@17
|
134 LayerFactory::getLayerIconName(LayerType type)
|
Chris@17
|
135 {
|
Chris@17
|
136 switch (type) {
|
Chris@17
|
137 case Waveform: return "waveform";
|
Chris@17
|
138 case Spectrogram: return "spectrogram";
|
Chris@17
|
139 case TimeRuler: return "timeruler";
|
Chris@17
|
140 case TimeInstants: return "instants";
|
Chris@17
|
141 case TimeValues: return "values";
|
Chris@30
|
142 case Notes: return "notes";
|
Chris@35
|
143 case Text: return "text";
|
Chris@17
|
144 case Colour3DPlot: return "colour3d";
|
Chris@17
|
145 default: return "unknown";
|
Chris@17
|
146 }
|
Chris@17
|
147 }
|
Chris@17
|
148
|
Chris@17
|
149 QString
|
Chris@6
|
150 LayerFactory::getLayerTypeName(LayerType type)
|
Chris@6
|
151 {
|
Chris@6
|
152 switch (type) {
|
Chris@6
|
153 case Waveform: return "waveform";
|
Chris@6
|
154 case Spectrogram: return "spectrogram";
|
Chris@6
|
155 case TimeRuler: return "timeruler";
|
Chris@6
|
156 case TimeInstants: return "timeinstants";
|
Chris@6
|
157 case TimeValues: return "timevalues";
|
Chris@30
|
158 case Notes: return "notes";
|
Chris@35
|
159 case Text: return "text";
|
Chris@6
|
160 case Colour3DPlot: return "colour3dplot";
|
Chris@6
|
161 default: return "unknown";
|
Chris@6
|
162 }
|
Chris@6
|
163 }
|
Chris@6
|
164
|
Chris@6
|
165 LayerFactory::LayerType
|
Chris@6
|
166 LayerFactory::getLayerTypeForName(QString name)
|
Chris@6
|
167 {
|
Chris@6
|
168 if (name == "waveform") return Waveform;
|
Chris@6
|
169 if (name == "spectrogram") return Spectrogram;
|
Chris@6
|
170 if (name == "timeruler") return TimeRuler;
|
Chris@6
|
171 if (name == "timeinstants") return TimeInstants;
|
Chris@6
|
172 if (name == "timevalues") return TimeValues;
|
Chris@30
|
173 if (name == "notes") return Notes;
|
Chris@35
|
174 if (name == "text") return Text;
|
Chris@6
|
175 if (name == "colour3dplot") return Colour3DPlot;
|
Chris@0
|
176 return UnknownLayer;
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 void
|
Chris@0
|
180 LayerFactory::setModel(Layer *layer, Model *model)
|
Chris@0
|
181 {
|
Chris@0
|
182 if (trySetModel<WaveformLayer, RangeSummarisableTimeValueModel>(layer, model))
|
Chris@0
|
183 return;
|
Chris@0
|
184
|
Chris@0
|
185 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
|
Chris@0
|
186 return;
|
Chris@0
|
187
|
Chris@0
|
188 if (trySetModel<TimeRulerLayer, Model>(layer, model))
|
Chris@0
|
189 return;
|
Chris@0
|
190
|
Chris@0
|
191 if (trySetModel<TimeInstantLayer, SparseOneDimensionalModel>(layer, model))
|
Chris@0
|
192 return;
|
Chris@0
|
193
|
Chris@0
|
194 if (trySetModel<TimeValueLayer, SparseTimeValueModel>(layer, model))
|
Chris@0
|
195 return;
|
Chris@0
|
196
|
Chris@30
|
197 if (trySetModel<NoteLayer, NoteModel>(layer, model))
|
Chris@30
|
198 return;
|
Chris@30
|
199
|
Chris@35
|
200 if (trySetModel<TextLayer, TextModel>(layer, model))
|
Chris@35
|
201 return;
|
Chris@35
|
202
|
Chris@0
|
203 if (trySetModel<Colour3DPlotLayer, DenseThreeDimensionalModel>(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
|
Chris@17
|
210 Model *
|
Chris@17
|
211 LayerFactory::createEmptyModel(LayerType layerType, Model *baseModel)
|
Chris@17
|
212 {
|
Chris@17
|
213 if (layerType == TimeInstants) {
|
Chris@17
|
214 return new SparseOneDimensionalModel(baseModel->getSampleRate(), 1);
|
Chris@17
|
215 } else if (layerType == TimeValues) {
|
Chris@17
|
216 return new SparseTimeValueModel(baseModel->getSampleRate(), 1,
|
Chris@17
|
217 0.0, 0.0, true);
|
Chris@30
|
218 } else if (layerType == Notes) {
|
Chris@30
|
219 return new NoteModel(baseModel->getSampleRate(), 1,
|
Chris@30
|
220 0.0, 0.0, true);
|
Chris@35
|
221 } else if (layerType == Text) {
|
Chris@35
|
222 return new TextModel(baseModel->getSampleRate(), 1, true);
|
Chris@17
|
223 } else {
|
Chris@17
|
224 return 0;
|
Chris@17
|
225 }
|
Chris@17
|
226 }
|
Chris@17
|
227
|
Chris@0
|
228 Layer *
|
Chris@0
|
229 LayerFactory::createLayer(LayerType type, View *view,
|
Chris@0
|
230 Model *model, int channel)
|
Chris@0
|
231 {
|
Chris@0
|
232 Layer *layer = 0;
|
Chris@0
|
233
|
Chris@0
|
234 switch (type) {
|
Chris@0
|
235
|
Chris@0
|
236 case Waveform:
|
Chris@0
|
237 layer = new WaveformLayer(view);
|
Chris@0
|
238 static_cast<WaveformLayer *>(layer)->setChannel(channel);
|
Chris@0
|
239 break;
|
Chris@0
|
240
|
Chris@0
|
241 case Spectrogram:
|
Chris@0
|
242 layer = new SpectrogramLayer(view);
|
Chris@0
|
243 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@0
|
244 break;
|
Chris@0
|
245
|
Chris@0
|
246 case TimeRuler:
|
Chris@0
|
247 layer = new TimeRulerLayer(view);
|
Chris@0
|
248 break;
|
Chris@0
|
249
|
Chris@0
|
250 case TimeInstants:
|
Chris@0
|
251 layer = new TimeInstantLayer(view);
|
Chris@0
|
252 break;
|
Chris@0
|
253
|
Chris@0
|
254 case TimeValues:
|
Chris@0
|
255 layer = new TimeValueLayer(view);
|
Chris@0
|
256 break;
|
Chris@0
|
257
|
Chris@30
|
258 case Notes:
|
Chris@30
|
259 layer = new NoteLayer(view);
|
Chris@30
|
260 break;
|
Chris@30
|
261
|
Chris@35
|
262 case Text:
|
Chris@35
|
263 layer = new TextLayer(view);
|
Chris@35
|
264 break;
|
Chris@35
|
265
|
Chris@0
|
266 case Colour3DPlot:
|
Chris@0
|
267 layer = new Colour3DPlotLayer(view);
|
Chris@0
|
268 break;
|
Chris@0
|
269
|
Chris@0
|
270 case MelodicRangeSpectrogram:
|
Chris@0
|
271 layer = new SpectrogramLayer(view, SpectrogramLayer::MelodicRange);
|
Chris@0
|
272 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@0
|
273 break;
|
Chris@11
|
274
|
Chris@11
|
275 default: break;
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 if (!layer) {
|
Chris@0
|
279 std::cerr << "LayerFactory::createLayer: Unknown layer type "
|
Chris@0
|
280 << type << std::endl;
|
Chris@0
|
281 } else {
|
Chris@8
|
282 if (model) setModel(layer, model);
|
Chris@0
|
283 std::cerr << "LayerFactory::createLayer: Setting object name "
|
Chris@0
|
284 << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl;
|
Chris@0
|
285 layer->setObjectName(getLayerPresentationName(type));
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 return layer;
|
Chris@0
|
289 }
|
Chris@0
|
290
|