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