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