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@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@0
|
233 Layer *
|
Chris@0
|
234 LayerFactory::createLayer(LayerType type, View *view,
|
Chris@0
|
235 Model *model, int channel)
|
Chris@0
|
236 {
|
Chris@0
|
237 Layer *layer = 0;
|
Chris@0
|
238
|
Chris@0
|
239 switch (type) {
|
Chris@0
|
240
|
Chris@0
|
241 case Waveform:
|
Chris@0
|
242 layer = new WaveformLayer(view);
|
Chris@0
|
243 static_cast<WaveformLayer *>(layer)->setChannel(channel);
|
Chris@0
|
244 break;
|
Chris@0
|
245
|
Chris@0
|
246 case Spectrogram:
|
Chris@0
|
247 layer = new SpectrogramLayer(view);
|
Chris@0
|
248 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@0
|
249 break;
|
Chris@0
|
250
|
Chris@0
|
251 case TimeRuler:
|
Chris@0
|
252 layer = new TimeRulerLayer(view);
|
Chris@0
|
253 break;
|
Chris@0
|
254
|
Chris@0
|
255 case TimeInstants:
|
Chris@0
|
256 layer = new TimeInstantLayer(view);
|
Chris@0
|
257 break;
|
Chris@0
|
258
|
Chris@0
|
259 case TimeValues:
|
Chris@0
|
260 layer = new TimeValueLayer(view);
|
Chris@0
|
261 break;
|
Chris@0
|
262
|
Chris@30
|
263 case Notes:
|
Chris@30
|
264 layer = new NoteLayer(view);
|
Chris@30
|
265 break;
|
Chris@30
|
266
|
Chris@35
|
267 case Text:
|
Chris@35
|
268 layer = new TextLayer(view);
|
Chris@35
|
269 break;
|
Chris@35
|
270
|
Chris@0
|
271 case Colour3DPlot:
|
Chris@0
|
272 layer = new Colour3DPlotLayer(view);
|
Chris@0
|
273 break;
|
Chris@0
|
274
|
Chris@0
|
275 case MelodicRangeSpectrogram:
|
Chris@0
|
276 layer = new SpectrogramLayer(view, SpectrogramLayer::MelodicRange);
|
Chris@0
|
277 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@0
|
278 break;
|
Chris@11
|
279
|
Chris@37
|
280 case PeakFrequencySpectrogram:
|
Chris@37
|
281 layer = new SpectrogramLayer(view, SpectrogramLayer::MelodicPeaks);
|
Chris@37
|
282 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
|
Chris@37
|
283 break;
|
Chris@37
|
284
|
Chris@11
|
285 default: break;
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 if (!layer) {
|
Chris@0
|
289 std::cerr << "LayerFactory::createLayer: Unknown layer type "
|
Chris@0
|
290 << type << std::endl;
|
Chris@0
|
291 } else {
|
Chris@8
|
292 if (model) setModel(layer, model);
|
Chris@0
|
293 std::cerr << "LayerFactory::createLayer: Setting object name "
|
Chris@0
|
294 << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl;
|
Chris@0
|
295 layer->setObjectName(getLayerPresentationName(type));
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 return layer;
|
Chris@0
|
299 }
|
Chris@0
|
300
|