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