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@4
|
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 "WaveformLayer.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "base/AudioLevel.h"
|
Chris@0
|
13 #include "base/View.h"
|
Chris@0
|
14 #include "base/Profiler.h"
|
Chris@0
|
15
|
Chris@0
|
16 #include <QPainter>
|
Chris@0
|
17 #include <QPixmap>
|
Chris@0
|
18
|
Chris@0
|
19 #include <iostream>
|
Chris@0
|
20 #include <cmath>
|
Chris@0
|
21
|
Chris@4
|
22 //#define DEBUG_WAVEFORM_PAINT 1
|
Chris@4
|
23
|
Chris@0
|
24 using std::cerr;
|
Chris@0
|
25 using std::endl;
|
Chris@0
|
26
|
Chris@0
|
27 WaveformLayer::WaveformLayer(View *w) :
|
Chris@0
|
28 Layer(w),
|
Chris@0
|
29 m_model(0),
|
Chris@0
|
30 m_gain(1.0f),
|
Chris@0
|
31 m_colour(Qt::black),
|
Chris@0
|
32 m_showMeans(true),
|
Chris@0
|
33 m_greyscale(true),
|
Chris@0
|
34 m_channelMode(SeparateChannels),
|
Chris@0
|
35 m_channel(-1),
|
Chris@0
|
36 m_scale(LinearScale),
|
Chris@0
|
37 m_aggressive(false),
|
Chris@0
|
38 m_cache(0),
|
Chris@0
|
39 m_cacheValid(false)
|
Chris@0
|
40 {
|
Chris@0
|
41 m_view->addLayer(this);
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 WaveformLayer::~WaveformLayer()
|
Chris@0
|
45 {
|
Chris@0
|
46 delete m_cache;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 void
|
Chris@0
|
50 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
|
Chris@0
|
51 {
|
Chris@0
|
52 m_model = model;
|
Chris@0
|
53 m_cacheValid = false;
|
Chris@0
|
54 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
55
|
Chris@0
|
56 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@0
|
57 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
58 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@0
|
59
|
Chris@0
|
60 connect(m_model, SIGNAL(completionChanged()),
|
Chris@0
|
61 this, SIGNAL(modelCompletionChanged()));
|
Chris@0
|
62
|
Chris@0
|
63 emit modelReplaced();
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 Layer::PropertyList
|
Chris@0
|
67 WaveformLayer::getProperties() const
|
Chris@0
|
68 {
|
Chris@0
|
69 PropertyList list;
|
Chris@0
|
70 list.push_back(tr("Colour"));
|
Chris@0
|
71 list.push_back(tr("Scale"));
|
Chris@0
|
72 list.push_back(tr("Gain"));
|
Chris@0
|
73 list.push_back(tr("Merge Channels"));
|
Chris@0
|
74 return list;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 Layer::PropertyType
|
Chris@0
|
78 WaveformLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
79 {
|
Chris@0
|
80 if (name == tr("Gain")) return RangeProperty;
|
Chris@0
|
81 if (name == tr("Colour")) return ValueProperty;
|
Chris@0
|
82 if (name == tr("Merge Channels")) return ToggleProperty;
|
Chris@0
|
83 if (name == tr("Scale")) return ValueProperty;
|
Chris@0
|
84 return InvalidProperty;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 QString
|
Chris@0
|
88 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@0
|
89 {
|
Chris@0
|
90 if (name == tr("Gain") ||
|
Chris@0
|
91 name == tr("Scale")) return tr("Scale");
|
Chris@0
|
92 return QString();
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 int
|
Chris@0
|
96 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@0
|
97 int *min, int *max) const
|
Chris@0
|
98 {
|
Chris@0
|
99 int deft = 0;
|
Chris@0
|
100
|
Chris@0
|
101 if (name == tr("Gain")) {
|
Chris@0
|
102
|
Chris@0
|
103 *min = -50;
|
Chris@0
|
104 *max = 50;
|
Chris@0
|
105
|
Chris@0
|
106 deft = int(nearbyint(log10(m_gain) * 20.0));
|
Chris@0
|
107 if (deft < *min) deft = *min;
|
Chris@0
|
108 if (deft > *max) deft = *max;
|
Chris@0
|
109
|
Chris@0
|
110 } else if (name == tr("Colour")) {
|
Chris@0
|
111
|
Chris@0
|
112 *min = 0;
|
Chris@0
|
113 *max = 5;
|
Chris@0
|
114
|
Chris@0
|
115 if (m_colour == Qt::black) deft = 0;
|
Chris@0
|
116 else if (m_colour == Qt::darkRed) deft = 1;
|
Chris@0
|
117 else if (m_colour == Qt::darkBlue) deft = 2;
|
Chris@0
|
118 else if (m_colour == Qt::darkGreen) deft = 3;
|
Chris@0
|
119 else if (m_colour == QColor(200, 50, 255)) deft = 4;
|
Chris@0
|
120 else if (m_colour == QColor(255, 150, 50)) deft = 5;
|
Chris@0
|
121
|
Chris@0
|
122 } else if (name == tr("Merge Channels")) {
|
Chris@0
|
123
|
Chris@0
|
124 deft = ((m_channelMode == MergeChannels) ? 1 : 0);
|
Chris@0
|
125
|
Chris@0
|
126 } else if (name == tr("Scale")) {
|
Chris@0
|
127
|
Chris@0
|
128 *min = 0;
|
Chris@0
|
129 *max = 2;
|
Chris@0
|
130
|
Chris@0
|
131 deft = (int)m_scale;
|
Chris@0
|
132
|
Chris@0
|
133 } else {
|
Chris@0
|
134 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 return deft;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 QString
|
Chris@0
|
141 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
142 int value) const
|
Chris@0
|
143 {
|
Chris@0
|
144 if (name == tr("Colour")) {
|
Chris@0
|
145 switch (value) {
|
Chris@0
|
146 default:
|
Chris@0
|
147 case 0: return tr("Black");
|
Chris@0
|
148 case 1: return tr("Red");
|
Chris@0
|
149 case 2: return tr("Blue");
|
Chris@0
|
150 case 3: return tr("Green");
|
Chris@0
|
151 case 4: return tr("Purple");
|
Chris@0
|
152 case 5: return tr("Orange");
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155 if (name == tr("Scale")) {
|
Chris@0
|
156 switch (value) {
|
Chris@0
|
157 default:
|
Chris@0
|
158 case 0: return tr("Linear");
|
Chris@0
|
159 case 1: return tr("Meter");
|
Chris@0
|
160 case 2: return tr("dB");
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163 return tr("<unknown>");
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 void
|
Chris@0
|
167 WaveformLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
168 {
|
Chris@0
|
169 if (name == tr("Gain")) {
|
Chris@0
|
170 setGain(pow(10, float(value)/20.0));
|
Chris@0
|
171 } else if (name == tr("Colour")) {
|
Chris@0
|
172 switch (value) {
|
Chris@0
|
173 default:
|
Chris@0
|
174 case 0: setBaseColour(Qt::black); break;
|
Chris@0
|
175 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@0
|
176 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@0
|
177 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@0
|
178 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@0
|
179 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@0
|
180 }
|
Chris@0
|
181 } else if (name == tr("Merge Channels")) {
|
Chris@0
|
182 setChannelMode(value ? MergeChannels : SeparateChannels);
|
Chris@0
|
183 } else if (name == tr("Scale")) {
|
Chris@0
|
184 switch (value) {
|
Chris@0
|
185 default:
|
Chris@0
|
186 case 0: setScale(LinearScale); break;
|
Chris@0
|
187 case 1: setScale(MeterScale); break;
|
Chris@0
|
188 case 2: setScale(dBScale); break;
|
Chris@0
|
189 }
|
Chris@0
|
190 }
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /*
|
Chris@0
|
194
|
Chris@0
|
195 int
|
Chris@0
|
196 WaveformLayer::getProperty(const PropertyName &name)
|
Chris@0
|
197 {
|
Chris@0
|
198 if (name == "Gain") {
|
Chris@0
|
199 return int((getGain() - 1.0) * 10.0 + 0.01);
|
Chris@0
|
200 }
|
Chris@0
|
201 if (name == "Colour") {
|
Chris@0
|
202
|
Chris@0
|
203 */
|
Chris@0
|
204
|
Chris@0
|
205 void
|
Chris@0
|
206 WaveformLayer::setGain(float gain) //!!! inadequate for floats!
|
Chris@0
|
207 {
|
Chris@0
|
208 if (m_gain == gain) return;
|
Chris@0
|
209 m_gain = gain;
|
Chris@0
|
210 m_cacheValid = false;
|
Chris@0
|
211 emit layerParametersChanged();
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 void
|
Chris@0
|
215 WaveformLayer::setBaseColour(QColor colour)
|
Chris@0
|
216 {
|
Chris@0
|
217 if (m_colour == colour) return;
|
Chris@0
|
218 m_colour = colour;
|
Chris@0
|
219 m_cacheValid = false;
|
Chris@0
|
220 emit layerParametersChanged();
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 void
|
Chris@0
|
224 WaveformLayer::setShowMeans(bool showMeans)
|
Chris@0
|
225 {
|
Chris@0
|
226 if (m_showMeans == showMeans) return;
|
Chris@0
|
227 m_showMeans = showMeans;
|
Chris@0
|
228 m_cacheValid = false;
|
Chris@0
|
229 emit layerParametersChanged();
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 void
|
Chris@0
|
233 WaveformLayer::setUseGreyscale(bool useGreyscale)
|
Chris@0
|
234 {
|
Chris@0
|
235 if (m_greyscale == useGreyscale) return;
|
Chris@0
|
236 m_greyscale = useGreyscale;
|
Chris@0
|
237 m_cacheValid = false;
|
Chris@0
|
238 emit layerParametersChanged();
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 void
|
Chris@0
|
242 WaveformLayer::setChannelMode(ChannelMode channelMode)
|
Chris@0
|
243 {
|
Chris@0
|
244 if (m_channelMode == channelMode) return;
|
Chris@0
|
245 m_channelMode = channelMode;
|
Chris@0
|
246 m_cacheValid = false;
|
Chris@0
|
247 emit layerParametersChanged();
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 void
|
Chris@0
|
251 WaveformLayer::setChannel(int channel)
|
Chris@0
|
252 {
|
Chris@0
|
253 std::cerr << "WaveformLayer::setChannel(" << channel << ")" << std::endl;
|
Chris@0
|
254
|
Chris@0
|
255 if (m_channel == channel) return;
|
Chris@0
|
256 m_channel = channel;
|
Chris@0
|
257 m_cacheValid = false;
|
Chris@0
|
258 emit layerParametersChanged();
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 void
|
Chris@0
|
262 WaveformLayer::setScale(Scale scale)
|
Chris@0
|
263 {
|
Chris@0
|
264 if (m_scale == scale) return;
|
Chris@0
|
265 m_scale = scale;
|
Chris@0
|
266 m_cacheValid = false;
|
Chris@0
|
267 emit layerParametersChanged();
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 void
|
Chris@0
|
271 WaveformLayer::setAggressiveCacheing(bool aggressive)
|
Chris@0
|
272 {
|
Chris@0
|
273 if (m_aggressive == aggressive) return;
|
Chris@0
|
274 m_aggressive = aggressive;
|
Chris@0
|
275 m_cacheValid = false;
|
Chris@0
|
276 emit layerParametersChanged();
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 int
|
Chris@0
|
280 WaveformLayer::getCompletion() const
|
Chris@0
|
281 {
|
Chris@0
|
282 int completion = 100;
|
Chris@0
|
283 if (!m_model || !m_model->isOK()) return completion;
|
Chris@0
|
284 if (m_model->isReady(&completion)) return 100;
|
Chris@0
|
285 return completion;
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 int
|
Chris@0
|
289 WaveformLayer::dBscale(float sample, int m) const
|
Chris@0
|
290 {
|
Chris@0
|
291 if (sample < 0.0) return -dBscale(-sample, m);
|
Chris@0
|
292 float dB = AudioLevel::multiplier_to_dB(sample);
|
Chris@0
|
293 if (dB < -50.0) return 0;
|
Chris@0
|
294 if (dB > 0.0) return m;
|
Chris@0
|
295 return int(((dB + 50.0) * m) / 50.0 + 0.1);
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 size_t
|
Chris@0
|
299 WaveformLayer::getChannelArrangement(size_t &min, size_t &max, bool &merging)
|
Chris@0
|
300 const
|
Chris@0
|
301 {
|
Chris@0
|
302 if (!m_model || !m_model->isOK()) return 0;
|
Chris@0
|
303
|
Chris@0
|
304 size_t channels = m_model->getChannelCount();
|
Chris@0
|
305 if (channels == 0) return 0;
|
Chris@0
|
306
|
Chris@0
|
307 size_t rawChannels = channels;
|
Chris@0
|
308
|
Chris@0
|
309 if (m_channel == -1) {
|
Chris@0
|
310 min = 0;
|
Chris@0
|
311 if (m_channelMode == MergeChannels) {
|
Chris@0
|
312 max = 0;
|
Chris@0
|
313 channels = 1;
|
Chris@0
|
314 } else {
|
Chris@0
|
315 max = channels - 1;
|
Chris@0
|
316 }
|
Chris@0
|
317 } else {
|
Chris@0
|
318 min = m_channel;
|
Chris@0
|
319 max = m_channel;
|
Chris@0
|
320 rawChannels = 1;
|
Chris@0
|
321 channels = 1;
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 merging = (m_channelMode == MergeChannels && rawChannels > 1);
|
Chris@0
|
325
|
Chris@0
|
326 // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl;
|
Chris@0
|
327
|
Chris@0
|
328 return channels;
|
Chris@0
|
329 }
|
Chris@0
|
330
|
Chris@0
|
331 void
|
Chris@0
|
332 WaveformLayer::paint(QPainter &viewPainter, QRect rect) const
|
Chris@0
|
333 {
|
Chris@0
|
334 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
335 return;
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 long startFrame = m_view->getStartFrame();
|
Chris@0
|
339 int zoomLevel = m_view->getZoomLevel();
|
Chris@0
|
340
|
Chris@2
|
341 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@0
|
342 Profiler profiler("WaveformLayer::paint", true);
|
Chris@0
|
343 std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
|
Chris@0
|
344 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl;
|
Chris@2
|
345 #endif
|
Chris@0
|
346
|
Chris@0
|
347 size_t channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@0
|
348 bool mergingChannels = false;
|
Chris@0
|
349
|
Chris@0
|
350 channels = getChannelArrangement(minChannel, maxChannel, mergingChannels);
|
Chris@0
|
351 if (channels == 0) return;
|
Chris@0
|
352
|
Chris@0
|
353 int w = m_view->width();
|
Chris@0
|
354 int h = m_view->height();
|
Chris@0
|
355
|
Chris@0
|
356 bool ready = m_model->isReady();
|
Chris@0
|
357 QPainter *paint;
|
Chris@0
|
358
|
Chris@0
|
359 if (m_aggressive) {
|
Chris@0
|
360
|
Chris@0
|
361 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
|
Chris@0
|
362 m_cacheValid = false;
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 if (m_cacheValid) {
|
Chris@0
|
366 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@0
|
367 return;
|
Chris@0
|
368 }
|
Chris@0
|
369
|
Chris@0
|
370 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
|
Chris@0
|
371 delete m_cache;
|
Chris@0
|
372 m_cache = new QPixmap(w, h);
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 paint = new QPainter(m_cache);
|
Chris@0
|
376
|
Chris@0
|
377 paint->setPen(Qt::NoPen);
|
Chris@0
|
378 paint->setBrush(m_view->palette().background());
|
Chris@0
|
379 paint->drawRect(rect);
|
Chris@0
|
380
|
Chris@0
|
381 paint->setPen(Qt::black);
|
Chris@0
|
382 paint->setBrush(Qt::NoBrush);
|
Chris@0
|
383
|
Chris@0
|
384 } else {
|
Chris@0
|
385 paint = &viewPainter;
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@0
|
388 int x0 = 0, x1 = w - 1;
|
Chris@0
|
389 int y0 = 0, y1 = h - 1;
|
Chris@0
|
390
|
Chris@0
|
391 x0 = rect.left();
|
Chris@0
|
392 x1 = rect.right();
|
Chris@0
|
393 y0 = rect.top();
|
Chris@0
|
394 y1 = rect.bottom();
|
Chris@0
|
395
|
Chris@0
|
396 long frame0 = startFrame + x0 * zoomLevel;
|
Chris@0
|
397 long frame1 = startFrame + (x1 + 1) * zoomLevel;
|
Chris@0
|
398
|
Chris@4
|
399 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@4
|
400 std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl;
|
Chris@4
|
401 #endif
|
Chris@0
|
402
|
Chris@0
|
403 RangeSummarisableTimeValueModel::RangeBlock ranges;
|
Chris@0
|
404 RangeSummarisableTimeValueModel::RangeBlock otherChannelRanges;
|
Chris@0
|
405 RangeSummarisableTimeValueModel::Range range;
|
Chris@0
|
406
|
Chris@0
|
407 QColor greys[3];
|
Chris@0
|
408 if (m_colour == Qt::black) {
|
Chris@0
|
409 for (int i = 0; i < 3; ++i) {
|
Chris@0
|
410 int level = 192 - 64 * i;
|
Chris@0
|
411 greys[i] = QColor(level, level, level);
|
Chris@0
|
412 }
|
Chris@0
|
413 } else {
|
Chris@0
|
414 int factor = (m_view->hasLightBackground() ? 120 : 80);
|
Chris@0
|
415 greys[2] = m_colour.light(factor);
|
Chris@0
|
416 greys[1] = greys[2].light(factor);
|
Chris@0
|
417 greys[0] = greys[1].light(factor);
|
Chris@0
|
418 }
|
Chris@0
|
419
|
Chris@0
|
420 QColor midColour = m_colour;
|
Chris@0
|
421 if (midColour == Qt::black) {
|
Chris@0
|
422 midColour = Qt::gray;
|
Chris@0
|
423 } else if (m_view->hasLightBackground()) {
|
Chris@0
|
424 midColour = midColour.light(150);
|
Chris@0
|
425 } else {
|
Chris@0
|
426 midColour = midColour.light(50);
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@0
|
430
|
Chris@0
|
431 int prevRangeBottom = -1, prevRangeTop = -1;
|
Chris@0
|
432
|
Chris@0
|
433 int m = (h / channels) / 2;
|
Chris@0
|
434 int my = m + (((ch - minChannel) * h) / channels);
|
Chris@0
|
435
|
Chris@0
|
436 // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl;
|
Chris@0
|
437
|
Chris@0
|
438 if (my - m > y1 || my + m < y0) continue;
|
Chris@0
|
439
|
Chris@0
|
440 paint->setPen(greys[0]);
|
Chris@0
|
441 paint->drawLine(x0, my, x1, my);
|
Chris@0
|
442
|
Chris@0
|
443 if (frame1 <= 0) continue;
|
Chris@0
|
444
|
Chris@0
|
445 size_t modelZoomLevel = zoomLevel;
|
Chris@0
|
446
|
Chris@0
|
447 ranges = m_model->getRanges
|
Chris@0
|
448 (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
|
Chris@0
|
449
|
Chris@0
|
450 if (mergingChannels) {
|
Chris@0
|
451 otherChannelRanges = m_model->getRanges
|
Chris@0
|
452 (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 for (int x = x0; x <= x1; ++x) {
|
Chris@0
|
456
|
Chris@0
|
457 range = RangeSummarisableTimeValueModel::Range();
|
Chris@0
|
458 size_t index = x - x0;
|
Chris@0
|
459 size_t maxIndex = index;
|
Chris@0
|
460
|
Chris@0
|
461 if (frame0 < 0) {
|
Chris@0
|
462 if (index < size_t(-frame0 / zoomLevel)) {
|
Chris@0
|
463 continue;
|
Chris@0
|
464 } else {
|
Chris@0
|
465 index -= -frame0 / zoomLevel;
|
Chris@0
|
466 maxIndex = index;
|
Chris@0
|
467 }
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 if (int(modelZoomLevel) != zoomLevel) {
|
Chris@0
|
471
|
Chris@0
|
472 index = size_t((double(index) * zoomLevel) / modelZoomLevel);
|
Chris@0
|
473
|
Chris@0
|
474 if (int(modelZoomLevel) < zoomLevel) {
|
Chris@0
|
475 // Peaks may be missed! The model should avoid
|
Chris@0
|
476 // this by rounding zoom levels up rather than
|
Chris@0
|
477 // down, but we'd better cope in case it doesn't
|
Chris@0
|
478 maxIndex = index;
|
Chris@0
|
479 } else {
|
Chris@0
|
480 maxIndex = size_t((double(index + 1) * zoomLevel)
|
Chris@0
|
481 / modelZoomLevel) - 1;
|
Chris@0
|
482 }
|
Chris@0
|
483 }
|
Chris@0
|
484
|
Chris@0
|
485 if (index < ranges.size()) {
|
Chris@0
|
486
|
Chris@0
|
487 range = ranges[index];
|
Chris@0
|
488
|
Chris@0
|
489 if (maxIndex > index && maxIndex < ranges.size()) {
|
Chris@0
|
490 range.max = std::max(range.max, ranges[maxIndex].max);
|
Chris@0
|
491 range.min = std::min(range.min, ranges[maxIndex].min);
|
Chris@0
|
492 range.absmean = (range.absmean +
|
Chris@0
|
493 ranges[maxIndex].absmean) / 2;
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 } else {
|
Chris@0
|
497 continue;
|
Chris@0
|
498 }
|
Chris@0
|
499
|
Chris@0
|
500 int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
|
Chris@0
|
501
|
Chris@0
|
502 if (mergingChannels) {
|
Chris@0
|
503
|
Chris@0
|
504 if (index < otherChannelRanges.size()) {
|
Chris@0
|
505
|
Chris@0
|
506 range.max = fabsf(range.max);
|
Chris@0
|
507 range.min = -fabsf(otherChannelRanges[index].max);
|
Chris@0
|
508 range.absmean = (range.absmean +
|
Chris@0
|
509 otherChannelRanges[index].absmean) / 2;
|
Chris@0
|
510
|
Chris@0
|
511 if (maxIndex > index && maxIndex < ranges.size()) {
|
Chris@0
|
512 // let's not concern ourselves about the mean
|
Chris@0
|
513 range.min = std::min
|
Chris@0
|
514 (range.min,
|
Chris@0
|
515 -fabsf(otherChannelRanges[maxIndex].max));
|
Chris@0
|
516 }
|
Chris@0
|
517 }
|
Chris@0
|
518 }
|
Chris@0
|
519
|
Chris@0
|
520 int greyLevels = 1;
|
Chris@0
|
521 if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4;
|
Chris@0
|
522
|
Chris@0
|
523 switch (m_scale) {
|
Chris@0
|
524
|
Chris@0
|
525 case LinearScale:
|
Chris@0
|
526 rangeBottom = int( m * greyLevels * range.min * m_gain);
|
Chris@0
|
527 rangeTop = int( m * greyLevels * range.max * m_gain);
|
Chris@0
|
528 meanBottom = int(-m * range.absmean * m_gain);
|
Chris@0
|
529 meanTop = int( m * range.absmean * m_gain);
|
Chris@0
|
530 break;
|
Chris@0
|
531
|
Chris@0
|
532 case dBScale:
|
Chris@0
|
533 rangeBottom = dBscale(range.min * m_gain, m * greyLevels);
|
Chris@0
|
534 rangeTop = dBscale(range.max * m_gain, m * greyLevels);
|
Chris@0
|
535 meanBottom = -dBscale(range.absmean * m_gain, m);
|
Chris@0
|
536 meanTop = dBscale(range.absmean * m_gain, m);
|
Chris@0
|
537 break;
|
Chris@0
|
538
|
Chris@0
|
539 case MeterScale:
|
Chris@0
|
540 rangeBottom = AudioLevel::multiplier_to_preview(range.min * m_gain, m * greyLevels);
|
Chris@0
|
541 rangeTop = AudioLevel::multiplier_to_preview(range.max * m_gain, m * greyLevels);
|
Chris@0
|
542 meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * m_gain, m);
|
Chris@0
|
543 meanTop = AudioLevel::multiplier_to_preview(range.absmean * m_gain, m);
|
Chris@0
|
544 }
|
Chris@0
|
545
|
Chris@0
|
546 int topFill = (rangeTop < 0 ? -rangeTop : rangeTop) % greyLevels;
|
Chris@0
|
547 int bottomFill = (rangeBottom < 0 ? -rangeBottom : rangeBottom) % greyLevels;
|
Chris@0
|
548 rangeTop = rangeTop / greyLevels;
|
Chris@0
|
549 rangeBottom = rangeBottom / greyLevels;
|
Chris@0
|
550
|
Chris@0
|
551 bool clipped = false;
|
Chris@0
|
552 if (rangeTop < -m) { rangeTop = -m; clipped = true; }
|
Chris@0
|
553 if (rangeTop > m) { rangeTop = m; clipped = true; }
|
Chris@0
|
554 if (rangeBottom < -m) { rangeBottom = -m; clipped = true; }
|
Chris@0
|
555 if (rangeBottom > m) { rangeBottom = m; clipped = true; }
|
Chris@0
|
556
|
Chris@0
|
557 rangeBottom = my - rangeBottom;
|
Chris@0
|
558 rangeTop = my - rangeTop;
|
Chris@0
|
559 meanBottom = my - meanBottom;
|
Chris@0
|
560 meanTop = my - meanTop;
|
Chris@0
|
561
|
Chris@0
|
562 if (meanBottom > rangeBottom) meanBottom = rangeBottom;
|
Chris@0
|
563 if (meanTop < rangeTop) meanTop = rangeTop;
|
Chris@0
|
564
|
Chris@0
|
565 bool drawMean = m_showMeans;
|
Chris@0
|
566 if (meanTop == rangeTop) {
|
Chris@0
|
567 if (meanTop < meanBottom) ++meanTop;
|
Chris@0
|
568 else drawMean = false;
|
Chris@0
|
569 }
|
Chris@0
|
570 if (meanBottom == rangeBottom) {
|
Chris@0
|
571 if (meanBottom > meanTop) --meanBottom;
|
Chris@0
|
572 else drawMean = false;
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 if (x != x0 && prevRangeBottom != -1) {
|
Chris@0
|
576 if (prevRangeBottom > rangeBottom &&
|
Chris@0
|
577 prevRangeTop > rangeBottom) {
|
Chris@0
|
578 paint->setPen(midColour);
|
Chris@0
|
579 paint->drawLine(x-1, prevRangeTop, x, rangeBottom);
|
Chris@0
|
580 paint->setPen(m_colour);
|
Chris@0
|
581 paint->drawPoint(x-1, prevRangeTop);
|
Chris@0
|
582 } else if (prevRangeBottom < rangeTop &&
|
Chris@0
|
583 prevRangeTop < rangeTop) {
|
Chris@0
|
584 paint->setPen(midColour);
|
Chris@0
|
585 paint->drawLine(x-1, prevRangeBottom, x, rangeTop);
|
Chris@0
|
586 paint->setPen(m_colour);
|
Chris@0
|
587 paint->drawPoint(x-1, prevRangeBottom);
|
Chris@0
|
588 }
|
Chris@0
|
589 }
|
Chris@0
|
590
|
Chris@0
|
591 if (ready) {
|
Chris@0
|
592 if (clipped ||
|
Chris@0
|
593 range.min * m_gain <= -1.0 ||
|
Chris@0
|
594 range.max * m_gain >= 1.0) {
|
Chris@0
|
595 paint->setPen(Qt::red);
|
Chris@0
|
596 } else {
|
Chris@0
|
597 paint->setPen(m_colour);
|
Chris@0
|
598 }
|
Chris@0
|
599 } else {
|
Chris@0
|
600 paint->setPen(midColour);
|
Chris@0
|
601 }
|
Chris@0
|
602
|
Chris@0
|
603 paint->drawLine(x, rangeBottom, x, rangeTop);
|
Chris@0
|
604
|
Chris@0
|
605 if (m_greyscale && (m_scale == LinearScale) && ready) {
|
Chris@0
|
606 if (!clipped) {
|
Chris@0
|
607 if (rangeTop < rangeBottom) {
|
Chris@0
|
608 if (topFill > 0 &&
|
Chris@0
|
609 (!drawMean || (rangeTop < meanTop - 1))) {
|
Chris@0
|
610 paint->setPen(greys[topFill - 1]);
|
Chris@0
|
611 paint->drawPoint(x, rangeTop - 1);
|
Chris@0
|
612 }
|
Chris@0
|
613 if (bottomFill > 0 &&
|
Chris@0
|
614 (!drawMean || (rangeBottom > meanBottom + 1))) {
|
Chris@0
|
615 paint->setPen(greys[bottomFill - 1]);
|
Chris@0
|
616 paint->drawPoint(x, rangeBottom + 1);
|
Chris@0
|
617 }
|
Chris@0
|
618 }
|
Chris@0
|
619 }
|
Chris@0
|
620 }
|
Chris@0
|
621
|
Chris@0
|
622 if (drawMean) {
|
Chris@0
|
623 paint->setPen(midColour);
|
Chris@0
|
624 paint->drawLine(x, meanBottom, x, meanTop);
|
Chris@0
|
625 }
|
Chris@0
|
626
|
Chris@0
|
627 prevRangeBottom = rangeBottom;
|
Chris@0
|
628 prevRangeTop = rangeTop;
|
Chris@0
|
629 }
|
Chris@0
|
630 }
|
Chris@0
|
631
|
Chris@0
|
632 if (m_aggressive) {
|
Chris@0
|
633 if (ready && rect == m_view->rect()) {
|
Chris@0
|
634 m_cacheValid = true;
|
Chris@0
|
635 m_cacheZoomLevel = zoomLevel;
|
Chris@0
|
636 }
|
Chris@0
|
637 paint->end();
|
Chris@0
|
638 delete paint;
|
Chris@0
|
639 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@0
|
640 }
|
Chris@0
|
641 }
|
Chris@0
|
642
|
Chris@0
|
643 int
|
Chris@0
|
644 WaveformLayer::getVerticalScaleWidth(QPainter &paint) const
|
Chris@0
|
645 {
|
Chris@0
|
646 if (m_scale == LinearScale) {
|
Chris@0
|
647 return paint.fontMetrics().width("0.0") + 13;
|
Chris@0
|
648 } else {
|
Chris@0
|
649 return std::max(paint.fontMetrics().width(tr("0dB")),
|
Chris@0
|
650 paint.fontMetrics().width(tr("-Inf"))) + 13;
|
Chris@0
|
651 }
|
Chris@0
|
652 }
|
Chris@0
|
653
|
Chris@0
|
654 void
|
Chris@0
|
655 WaveformLayer::paintVerticalScale(QPainter &paint, QRect rect) const
|
Chris@0
|
656 {
|
Chris@0
|
657 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
658 return;
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 size_t channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@0
|
662 bool mergingChannels = false;
|
Chris@0
|
663
|
Chris@0
|
664 channels = getChannelArrangement(minChannel, maxChannel, mergingChannels);
|
Chris@0
|
665 if (channels == 0) return;
|
Chris@0
|
666
|
Chris@0
|
667 int h = rect.height(), w = rect.width();
|
Chris@0
|
668 int textHeight = paint.fontMetrics().height();
|
Chris@0
|
669 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
|
Chris@0
|
670
|
Chris@0
|
671 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@0
|
672
|
Chris@0
|
673 int m = (h / channels) / 2;
|
Chris@0
|
674 int my = m + (((ch - minChannel) * h) / channels);
|
Chris@0
|
675 int py = -1;
|
Chris@0
|
676
|
Chris@0
|
677 for (int i = 0; i <= 10; ++i) {
|
Chris@0
|
678
|
Chris@0
|
679 int vy = 0;
|
Chris@0
|
680 QString text = "";
|
Chris@0
|
681
|
Chris@0
|
682 if (m_scale == LinearScale) {
|
Chris@0
|
683
|
Chris@0
|
684 vy = int((m * i * m_gain) / 10);
|
Chris@0
|
685
|
Chris@0
|
686 text = QString("%1").arg(float(i) / 10.0);
|
Chris@0
|
687 if (i == 0) text = "0.0";
|
Chris@0
|
688 if (i == 10) text = "1.0";
|
Chris@0
|
689
|
Chris@0
|
690 } else {
|
Chris@0
|
691
|
Chris@0
|
692 int db;
|
Chris@0
|
693 bool minvalue = false;
|
Chris@0
|
694
|
Chris@0
|
695 if (m_scale == MeterScale) {
|
Chris@0
|
696 static int dbs[] = { -50, -40, -30, -20, -15,
|
Chris@0
|
697 -10, -5, -3, -2, -1, 0 };
|
Chris@0
|
698 db = dbs[i];
|
Chris@0
|
699 if (db == -50) minvalue = true;
|
Chris@0
|
700 vy = AudioLevel::multiplier_to_preview
|
Chris@0
|
701 (AudioLevel::dB_to_multiplier(db) * m_gain, m);
|
Chris@0
|
702 } else {
|
Chris@0
|
703 db = -100 + i * 10;
|
Chris@0
|
704 if (db == -100) minvalue = true;
|
Chris@0
|
705 vy = dBscale
|
Chris@0
|
706 (AudioLevel::dB_to_multiplier(db) * m_gain, m);
|
Chris@0
|
707 }
|
Chris@0
|
708
|
Chris@0
|
709 text = QString("%1").arg(db);
|
Chris@0
|
710 if (db == 0) text = tr("0dB");
|
Chris@0
|
711 if (minvalue) {
|
Chris@0
|
712 text = tr("-Inf");
|
Chris@0
|
713 vy = 0;
|
Chris@0
|
714 }
|
Chris@0
|
715 }
|
Chris@0
|
716
|
Chris@0
|
717 if (vy < 0) vy = -vy;
|
Chris@0
|
718 if (vy >= m - 1) continue;
|
Chris@0
|
719
|
Chris@0
|
720 if (py >= 0 && (vy - py) < textHeight - 1) {
|
Chris@0
|
721 paint.drawLine(w - 4, my - vy, w, my - vy);
|
Chris@0
|
722 if (vy > 0) paint.drawLine(w - 4, my + vy, w, my + vy);
|
Chris@0
|
723 continue;
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 paint.drawLine(w - 7, my - vy, w, my - vy);
|
Chris@0
|
727 if (vy > 0) paint.drawLine(w - 7, my + vy, w, my + vy);
|
Chris@0
|
728
|
Chris@0
|
729 int tx = 3;
|
Chris@0
|
730 if (m_scale != LinearScale) {
|
Chris@0
|
731 tx = w - 10 - paint.fontMetrics().width(text);
|
Chris@0
|
732 }
|
Chris@0
|
733
|
Chris@0
|
734 paint.drawText(tx, my - vy + toff, text);
|
Chris@0
|
735 if (vy > 0) paint.drawText(tx, my + vy + toff, text);
|
Chris@0
|
736
|
Chris@0
|
737 py = vy;
|
Chris@0
|
738 }
|
Chris@0
|
739 }
|
Chris@0
|
740 }
|
Chris@0
|
741
|
Chris@0
|
742 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
743 #include "WaveformLayer.moc.cpp"
|
Chris@0
|
744 #endif
|
Chris@0
|
745
|