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@59
|
4 Sonic Visualiser
|
Chris@59
|
5 An audio file viewer and annotation editor.
|
Chris@59
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@59
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@59
|
9 This program is free software; you can redistribute it and/or
|
Chris@59
|
10 modify it under the terms of the GNU General Public License as
|
Chris@59
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@59
|
12 License, or (at your option) any later version. See the file
|
Chris@59
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "WaveformLayer.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include "base/AudioLevel.h"
|
Chris@0
|
19 #include "base/View.h"
|
Chris@0
|
20 #include "base/Profiler.h"
|
Chris@0
|
21
|
Chris@0
|
22 #include <QPainter>
|
Chris@0
|
23 #include <QPixmap>
|
Chris@0
|
24
|
Chris@0
|
25 #include <iostream>
|
Chris@0
|
26 #include <cmath>
|
Chris@0
|
27
|
Chris@4
|
28 //#define DEBUG_WAVEFORM_PAINT 1
|
Chris@4
|
29
|
Chris@0
|
30 using std::cerr;
|
Chris@0
|
31 using std::endl;
|
Chris@0
|
32
|
Chris@44
|
33 WaveformLayer::WaveformLayer() :
|
Chris@44
|
34 Layer(),
|
Chris@0
|
35 m_model(0),
|
Chris@0
|
36 m_gain(1.0f),
|
Chris@67
|
37 m_autoNormalize(false),
|
Chris@0
|
38 m_colour(Qt::black),
|
Chris@0
|
39 m_showMeans(true),
|
Chris@0
|
40 m_greyscale(true),
|
Chris@0
|
41 m_channelMode(SeparateChannels),
|
Chris@0
|
42 m_channel(-1),
|
Chris@0
|
43 m_scale(LinearScale),
|
Chris@0
|
44 m_aggressive(false),
|
Chris@0
|
45 m_cache(0),
|
Chris@0
|
46 m_cacheValid(false)
|
Chris@0
|
47 {
|
Chris@44
|
48
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 WaveformLayer::~WaveformLayer()
|
Chris@0
|
52 {
|
Chris@0
|
53 delete m_cache;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 void
|
Chris@0
|
57 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
|
Chris@0
|
58 {
|
Chris@69
|
59 bool channelsChanged = false;
|
Chris@69
|
60 if (m_channel == -1) {
|
Chris@69
|
61 if (!m_model) {
|
Chris@69
|
62 if (model) {
|
Chris@69
|
63 channelsChanged = true;
|
Chris@69
|
64 }
|
Chris@69
|
65 } else {
|
Chris@69
|
66 if (model &&
|
Chris@69
|
67 m_model->getChannelCount() != model->getChannelCount()) {
|
Chris@69
|
68 channelsChanged = true;
|
Chris@69
|
69 }
|
Chris@69
|
70 }
|
Chris@69
|
71 }
|
Chris@69
|
72
|
Chris@0
|
73 m_model = model;
|
Chris@0
|
74 m_cacheValid = false;
|
Chris@0
|
75 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
76
|
Chris@0
|
77 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@0
|
78 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
79 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@0
|
80
|
Chris@0
|
81 connect(m_model, SIGNAL(completionChanged()),
|
Chris@0
|
82 this, SIGNAL(modelCompletionChanged()));
|
Chris@0
|
83
|
Chris@0
|
84 emit modelReplaced();
|
Chris@69
|
85
|
Chris@69
|
86 if (channelsChanged) emit layerParametersChanged();
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 Layer::PropertyList
|
Chris@0
|
90 WaveformLayer::getProperties() const
|
Chris@0
|
91 {
|
Chris@0
|
92 PropertyList list;
|
Chris@0
|
93 list.push_back(tr("Colour"));
|
Chris@0
|
94 list.push_back(tr("Scale"));
|
Chris@0
|
95 list.push_back(tr("Gain"));
|
Chris@67
|
96 list.push_back(tr("Normalize Visible Area"));
|
Chris@68
|
97
|
Chris@68
|
98 if (m_model && m_model->getChannelCount() > 1 && m_channel == -1) {
|
Chris@68
|
99 list.push_back(tr("Channels"));
|
Chris@68
|
100 }
|
Chris@68
|
101
|
Chris@0
|
102 return list;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 Layer::PropertyType
|
Chris@0
|
106 WaveformLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
107 {
|
Chris@0
|
108 if (name == tr("Gain")) return RangeProperty;
|
Chris@67
|
109 if (name == tr("Normalize Visible Area")) return ToggleProperty;
|
Chris@0
|
110 if (name == tr("Colour")) return ValueProperty;
|
Chris@67
|
111 if (name == tr("Channels")) return ValueProperty;
|
Chris@0
|
112 if (name == tr("Scale")) return ValueProperty;
|
Chris@0
|
113 return InvalidProperty;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 QString
|
Chris@0
|
117 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@0
|
118 {
|
Chris@0
|
119 if (name == tr("Gain") ||
|
Chris@67
|
120 name == tr("Normalize Visible Area") ||
|
Chris@0
|
121 name == tr("Scale")) return tr("Scale");
|
Chris@0
|
122 return QString();
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 int
|
Chris@0
|
126 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@67
|
127 int *min, int *max) const
|
Chris@0
|
128 {
|
Chris@0
|
129 int deft = 0;
|
Chris@0
|
130
|
Chris@56
|
131 int garbage0, garbage1;
|
Chris@56
|
132 if (!min) min = &garbage0;
|
Chris@56
|
133 if (!max) max = &garbage1;
|
Chris@10
|
134
|
Chris@0
|
135 if (name == tr("Gain")) {
|
Chris@0
|
136
|
Chris@0
|
137 *min = -50;
|
Chris@0
|
138 *max = 50;
|
Chris@0
|
139
|
Chris@34
|
140 deft = lrint(log10(m_gain) * 20.0);
|
Chris@0
|
141 if (deft < *min) deft = *min;
|
Chris@0
|
142 if (deft > *max) deft = *max;
|
Chris@0
|
143
|
Chris@67
|
144 } else if (name == tr("Normalize Visible Area")) {
|
Chris@67
|
145
|
Chris@67
|
146 deft = (m_autoNormalize ? 1 : 0);
|
Chris@67
|
147
|
Chris@0
|
148 } else if (name == tr("Colour")) {
|
Chris@0
|
149
|
Chris@0
|
150 *min = 0;
|
Chris@0
|
151 *max = 5;
|
Chris@0
|
152
|
Chris@0
|
153 if (m_colour == Qt::black) deft = 0;
|
Chris@0
|
154 else if (m_colour == Qt::darkRed) deft = 1;
|
Chris@0
|
155 else if (m_colour == Qt::darkBlue) deft = 2;
|
Chris@0
|
156 else if (m_colour == Qt::darkGreen) deft = 3;
|
Chris@0
|
157 else if (m_colour == QColor(200, 50, 255)) deft = 4;
|
Chris@0
|
158 else if (m_colour == QColor(255, 150, 50)) deft = 5;
|
Chris@0
|
159
|
Chris@67
|
160 } else if (name == tr("Channels")) {
|
Chris@0
|
161
|
Chris@67
|
162 *min = 0;
|
Chris@67
|
163 *max = 2;
|
Chris@67
|
164 if (m_channelMode == MixChannels) deft = 1;
|
Chris@67
|
165 else if (m_channelMode == MergeChannels) deft = 2;
|
Chris@67
|
166 else deft = 0;
|
Chris@0
|
167
|
Chris@0
|
168 } else if (name == tr("Scale")) {
|
Chris@0
|
169
|
Chris@0
|
170 *min = 0;
|
Chris@0
|
171 *max = 2;
|
Chris@0
|
172
|
Chris@0
|
173 deft = (int)m_scale;
|
Chris@0
|
174
|
Chris@0
|
175 } else {
|
Chris@0
|
176 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 return deft;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 QString
|
Chris@0
|
183 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
184 int value) const
|
Chris@0
|
185 {
|
Chris@0
|
186 if (name == tr("Colour")) {
|
Chris@0
|
187 switch (value) {
|
Chris@0
|
188 default:
|
Chris@0
|
189 case 0: return tr("Black");
|
Chris@0
|
190 case 1: return tr("Red");
|
Chris@0
|
191 case 2: return tr("Blue");
|
Chris@0
|
192 case 3: return tr("Green");
|
Chris@0
|
193 case 4: return tr("Purple");
|
Chris@0
|
194 case 5: return tr("Orange");
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|
Chris@0
|
197 if (name == tr("Scale")) {
|
Chris@0
|
198 switch (value) {
|
Chris@0
|
199 default:
|
Chris@0
|
200 case 0: return tr("Linear");
|
Chris@0
|
201 case 1: return tr("Meter");
|
Chris@0
|
202 case 2: return tr("dB");
|
Chris@0
|
203 }
|
Chris@0
|
204 }
|
Chris@67
|
205 if (name == tr("Channels")) {
|
Chris@67
|
206 switch (value) {
|
Chris@67
|
207 default:
|
Chris@67
|
208 case 0: return tr("Separate");
|
Chris@67
|
209 case 1: return tr("Mean");
|
Chris@67
|
210 case 2: return tr("Butterfly");
|
Chris@67
|
211 }
|
Chris@67
|
212 }
|
Chris@0
|
213 return tr("<unknown>");
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 void
|
Chris@0
|
217 WaveformLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
218 {
|
Chris@0
|
219 if (name == tr("Gain")) {
|
Chris@0
|
220 setGain(pow(10, float(value)/20.0));
|
Chris@67
|
221 } else if (name == tr("Normalize Visible Area")) {
|
Chris@67
|
222 setAutoNormalize(value ? true : false);
|
Chris@0
|
223 } else if (name == tr("Colour")) {
|
Chris@0
|
224 switch (value) {
|
Chris@0
|
225 default:
|
Chris@0
|
226 case 0: setBaseColour(Qt::black); break;
|
Chris@0
|
227 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@0
|
228 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@0
|
229 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@0
|
230 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@0
|
231 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@0
|
232 }
|
Chris@67
|
233 } else if (name == tr("Channels")) {
|
Chris@67
|
234 if (value == 1) setChannelMode(MixChannels);
|
Chris@67
|
235 else if (value == 2) setChannelMode(MergeChannels);
|
Chris@67
|
236 else setChannelMode(SeparateChannels);
|
Chris@0
|
237 } else if (name == tr("Scale")) {
|
Chris@0
|
238 switch (value) {
|
Chris@0
|
239 default:
|
Chris@0
|
240 case 0: setScale(LinearScale); break;
|
Chris@0
|
241 case 1: setScale(MeterScale); break;
|
Chris@0
|
242 case 2: setScale(dBScale); break;
|
Chris@0
|
243 }
|
Chris@0
|
244 }
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 void
|
Chris@67
|
248 WaveformLayer::setGain(float gain)
|
Chris@0
|
249 {
|
Chris@0
|
250 if (m_gain == gain) return;
|
Chris@0
|
251 m_gain = gain;
|
Chris@0
|
252 m_cacheValid = false;
|
Chris@0
|
253 emit layerParametersChanged();
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 void
|
Chris@67
|
257 WaveformLayer::setAutoNormalize(bool autoNormalize)
|
Chris@67
|
258 {
|
Chris@67
|
259 if (m_autoNormalize == autoNormalize) return;
|
Chris@67
|
260 m_autoNormalize = autoNormalize;
|
Chris@67
|
261 m_cacheValid = false;
|
Chris@67
|
262 emit layerParametersChanged();
|
Chris@67
|
263 }
|
Chris@67
|
264
|
Chris@67
|
265 void
|
Chris@0
|
266 WaveformLayer::setBaseColour(QColor colour)
|
Chris@0
|
267 {
|
Chris@0
|
268 if (m_colour == colour) return;
|
Chris@0
|
269 m_colour = colour;
|
Chris@0
|
270 m_cacheValid = false;
|
Chris@0
|
271 emit layerParametersChanged();
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 void
|
Chris@0
|
275 WaveformLayer::setShowMeans(bool showMeans)
|
Chris@0
|
276 {
|
Chris@0
|
277 if (m_showMeans == showMeans) return;
|
Chris@0
|
278 m_showMeans = showMeans;
|
Chris@0
|
279 m_cacheValid = false;
|
Chris@0
|
280 emit layerParametersChanged();
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 void
|
Chris@0
|
284 WaveformLayer::setUseGreyscale(bool useGreyscale)
|
Chris@0
|
285 {
|
Chris@0
|
286 if (m_greyscale == useGreyscale) return;
|
Chris@0
|
287 m_greyscale = useGreyscale;
|
Chris@0
|
288 m_cacheValid = false;
|
Chris@0
|
289 emit layerParametersChanged();
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 void
|
Chris@0
|
293 WaveformLayer::setChannelMode(ChannelMode channelMode)
|
Chris@0
|
294 {
|
Chris@0
|
295 if (m_channelMode == channelMode) return;
|
Chris@0
|
296 m_channelMode = channelMode;
|
Chris@0
|
297 m_cacheValid = false;
|
Chris@0
|
298 emit layerParametersChanged();
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 void
|
Chris@0
|
302 WaveformLayer::setChannel(int channel)
|
Chris@0
|
303 {
|
Chris@0
|
304 std::cerr << "WaveformLayer::setChannel(" << channel << ")" << std::endl;
|
Chris@0
|
305
|
Chris@0
|
306 if (m_channel == channel) return;
|
Chris@0
|
307 m_channel = channel;
|
Chris@0
|
308 m_cacheValid = false;
|
Chris@0
|
309 emit layerParametersChanged();
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 void
|
Chris@0
|
313 WaveformLayer::setScale(Scale scale)
|
Chris@0
|
314 {
|
Chris@0
|
315 if (m_scale == scale) return;
|
Chris@0
|
316 m_scale = scale;
|
Chris@0
|
317 m_cacheValid = false;
|
Chris@0
|
318 emit layerParametersChanged();
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 void
|
Chris@0
|
322 WaveformLayer::setAggressiveCacheing(bool aggressive)
|
Chris@0
|
323 {
|
Chris@0
|
324 if (m_aggressive == aggressive) return;
|
Chris@0
|
325 m_aggressive = aggressive;
|
Chris@0
|
326 m_cacheValid = false;
|
Chris@0
|
327 emit layerParametersChanged();
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 int
|
Chris@0
|
331 WaveformLayer::getCompletion() const
|
Chris@0
|
332 {
|
Chris@0
|
333 int completion = 100;
|
Chris@0
|
334 if (!m_model || !m_model->isOK()) return completion;
|
Chris@0
|
335 if (m_model->isReady(&completion)) return 100;
|
Chris@0
|
336 return completion;
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@79
|
339 bool
|
Chris@79
|
340 WaveformLayer::getValueExtents(float &min, float &max, QString &unit) const
|
Chris@79
|
341 {
|
Chris@79
|
342 if (m_scale == LinearScale) {
|
Chris@79
|
343 min = 0.0;
|
Chris@79
|
344 max = 1.0;
|
Chris@79
|
345 unit = "V";
|
Chris@79
|
346 } else if (m_scale == MeterScale) {
|
Chris@79
|
347 return false; //!!!
|
Chris@79
|
348 } else {
|
Chris@79
|
349 min = AudioLevel::multiplier_to_dB(0.0);
|
Chris@79
|
350 max = AudioLevel::multiplier_to_dB(1.0);
|
Chris@79
|
351 unit = "dB";
|
Chris@79
|
352 }
|
Chris@79
|
353 return true;
|
Chris@79
|
354 }
|
Chris@79
|
355
|
Chris@0
|
356 int
|
Chris@0
|
357 WaveformLayer::dBscale(float sample, int m) const
|
Chris@0
|
358 {
|
Chris@67
|
359 //!!! if (sample < 0.0) return -dBscale(-sample, m);
|
Chris@67
|
360 if (sample < 0.0) return dBscale(-sample, m);
|
Chris@0
|
361 float dB = AudioLevel::multiplier_to_dB(sample);
|
Chris@0
|
362 if (dB < -50.0) return 0;
|
Chris@0
|
363 if (dB > 0.0) return m;
|
Chris@0
|
364 return int(((dB + 50.0) * m) / 50.0 + 0.1);
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 size_t
|
Chris@67
|
368 WaveformLayer::getChannelArrangement(size_t &min, size_t &max,
|
Chris@67
|
369 bool &merging, bool &mixing)
|
Chris@0
|
370 const
|
Chris@0
|
371 {
|
Chris@0
|
372 if (!m_model || !m_model->isOK()) return 0;
|
Chris@0
|
373
|
Chris@0
|
374 size_t channels = m_model->getChannelCount();
|
Chris@0
|
375 if (channels == 0) return 0;
|
Chris@0
|
376
|
Chris@0
|
377 size_t rawChannels = channels;
|
Chris@0
|
378
|
Chris@0
|
379 if (m_channel == -1) {
|
Chris@0
|
380 min = 0;
|
Chris@67
|
381 if (m_channelMode == MergeChannels ||
|
Chris@67
|
382 m_channelMode == MixChannels) {
|
Chris@0
|
383 max = 0;
|
Chris@0
|
384 channels = 1;
|
Chris@0
|
385 } else {
|
Chris@0
|
386 max = channels - 1;
|
Chris@0
|
387 }
|
Chris@0
|
388 } else {
|
Chris@0
|
389 min = m_channel;
|
Chris@0
|
390 max = m_channel;
|
Chris@0
|
391 rawChannels = 1;
|
Chris@0
|
392 channels = 1;
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 merging = (m_channelMode == MergeChannels && rawChannels > 1);
|
Chris@67
|
396 mixing = (m_channelMode == MixChannels && rawChannels > 1);
|
Chris@0
|
397
|
Chris@0
|
398 // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl;
|
Chris@0
|
399
|
Chris@0
|
400 return channels;
|
Chris@0
|
401 }
|
Chris@0
|
402
|
Chris@67
|
403 bool
|
Chris@67
|
404 WaveformLayer::isLayerScrollable(const View *) const
|
Chris@67
|
405 {
|
Chris@67
|
406 return !m_autoNormalize;
|
Chris@67
|
407 }
|
Chris@67
|
408
|
Chris@68
|
409 static float meterdbs[] = { -40, -30, -20, -15, -10,
|
Chris@68
|
410 -5, -3, -2, -1, -0.5, 0 };
|
Chris@68
|
411
|
Chris@0
|
412 void
|
Chris@44
|
413 WaveformLayer::paint(View *v, QPainter &viewPainter, QRect rect) const
|
Chris@0
|
414 {
|
Chris@0
|
415 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
416 return;
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@44
|
419 long startFrame = v->getStartFrame();
|
Chris@44
|
420 int zoomLevel = v->getZoomLevel();
|
Chris@0
|
421
|
Chris@2
|
422 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@0
|
423 Profiler profiler("WaveformLayer::paint", true);
|
Chris@0
|
424 std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
|
Chris@0
|
425 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl;
|
Chris@2
|
426 #endif
|
Chris@0
|
427
|
Chris@0
|
428 size_t channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
429 bool mergingChannels = false, mixingChannels = false;
|
Chris@0
|
430
|
Chris@67
|
431 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
432 mergingChannels, mixingChannels);
|
Chris@0
|
433 if (channels == 0) return;
|
Chris@0
|
434
|
Chris@44
|
435 int w = v->width();
|
Chris@44
|
436 int h = v->height();
|
Chris@0
|
437
|
Chris@0
|
438 bool ready = m_model->isReady();
|
Chris@0
|
439 QPainter *paint;
|
Chris@0
|
440
|
Chris@0
|
441 if (m_aggressive) {
|
Chris@0
|
442
|
Chris@0
|
443 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
|
Chris@0
|
444 m_cacheValid = false;
|
Chris@0
|
445 }
|
Chris@0
|
446
|
Chris@0
|
447 if (m_cacheValid) {
|
Chris@0
|
448 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@0
|
449 return;
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
|
Chris@0
|
453 delete m_cache;
|
Chris@0
|
454 m_cache = new QPixmap(w, h);
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 paint = new QPainter(m_cache);
|
Chris@0
|
458
|
Chris@0
|
459 paint->setPen(Qt::NoPen);
|
Chris@44
|
460 paint->setBrush(v->palette().background());
|
Chris@0
|
461 paint->drawRect(rect);
|
Chris@0
|
462
|
Chris@0
|
463 paint->setPen(Qt::black);
|
Chris@0
|
464 paint->setBrush(Qt::NoBrush);
|
Chris@0
|
465
|
Chris@0
|
466 } else {
|
Chris@0
|
467 paint = &viewPainter;
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@28
|
470 paint->setRenderHint(QPainter::Antialiasing, false);
|
Chris@28
|
471
|
Chris@0
|
472 int x0 = 0, x1 = w - 1;
|
Chris@0
|
473 int y0 = 0, y1 = h - 1;
|
Chris@0
|
474
|
Chris@0
|
475 x0 = rect.left();
|
Chris@0
|
476 x1 = rect.right();
|
Chris@0
|
477 y0 = rect.top();
|
Chris@0
|
478 y1 = rect.bottom();
|
Chris@0
|
479
|
Chris@28
|
480 if (x0 > 0) --x0;
|
Chris@44
|
481 if (x1 < v->width()) ++x1;
|
Chris@28
|
482
|
Chris@44
|
483 long frame0 = v->getFrameForX(x0);
|
Chris@44
|
484 long frame1 = v->getFrameForX(x1 + 1);
|
Chris@0
|
485
|
Chris@4
|
486 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@4
|
487 std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl;
|
Chris@4
|
488 #endif
|
Chris@0
|
489
|
Chris@0
|
490 RangeSummarisableTimeValueModel::RangeBlock ranges;
|
Chris@0
|
491 RangeSummarisableTimeValueModel::RangeBlock otherChannelRanges;
|
Chris@0
|
492 RangeSummarisableTimeValueModel::Range range;
|
Chris@0
|
493
|
Chris@0
|
494 QColor greys[3];
|
Chris@0
|
495 if (m_colour == Qt::black) {
|
Chris@27
|
496 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
|
Chris@0
|
497 int level = 192 - 64 * i;
|
Chris@0
|
498 greys[i] = QColor(level, level, level);
|
Chris@0
|
499 }
|
Chris@0
|
500 } else {
|
Chris@44
|
501 int hue, sat, val;
|
Chris@44
|
502 m_colour.getHsv(&hue, &sat, &val);
|
Chris@27
|
503 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
|
Chris@44
|
504 if (v->hasLightBackground()) {
|
Chris@44
|
505 greys[i] = QColor::fromHsv(hue, sat * (i + 1) / 4, val);
|
Chris@27
|
506 } else {
|
Chris@44
|
507 greys[i] = QColor::fromHsv(hue, sat * (3 - i) / 4, val);
|
Chris@27
|
508 }
|
Chris@27
|
509 }
|
Chris@0
|
510 }
|
Chris@0
|
511
|
Chris@0
|
512 QColor midColour = m_colour;
|
Chris@0
|
513 if (midColour == Qt::black) {
|
Chris@0
|
514 midColour = Qt::gray;
|
Chris@44
|
515 } else if (v->hasLightBackground()) {
|
Chris@0
|
516 midColour = midColour.light(150);
|
Chris@0
|
517 } else {
|
Chris@0
|
518 midColour = midColour.light(50);
|
Chris@0
|
519 }
|
Chris@0
|
520
|
Chris@67
|
521 while (m_effectiveGains.size() <= maxChannel) {
|
Chris@67
|
522 m_effectiveGains.push_back(m_gain);
|
Chris@67
|
523 }
|
Chris@67
|
524
|
Chris@0
|
525 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@0
|
526
|
Chris@0
|
527 int prevRangeBottom = -1, prevRangeTop = -1;
|
Chris@28
|
528 QColor prevRangeBottomColour = m_colour, prevRangeTopColour = m_colour;
|
Chris@0
|
529
|
Chris@67
|
530 m_effectiveGains[ch] = m_gain;
|
Chris@67
|
531
|
Chris@67
|
532 if (m_autoNormalize) {
|
Chris@67
|
533 RangeSummarisableTimeValueModel::Range range =
|
Chris@67
|
534 m_model->getRange(ch, startFrame < 0 ? 0 : startFrame,
|
Chris@67
|
535 v->getEndFrame());
|
Chris@67
|
536 if (mergingChannels || mixingChannels) {
|
Chris@67
|
537 RangeSummarisableTimeValueModel::Range otherRange =
|
Chris@67
|
538 m_model->getRange(1, startFrame < 0 ? 0 : startFrame,
|
Chris@67
|
539 v->getEndFrame());
|
Chris@67
|
540 range.max = std::max(range.max, otherRange.max);
|
Chris@67
|
541 range.min = std::min(range.min, otherRange.min);
|
Chris@67
|
542 range.absmean = std::min(range.absmean, otherRange.absmean);
|
Chris@67
|
543 }
|
Chris@67
|
544 m_effectiveGains[ch] = 1.0 / std::max(fabsf(range.max),
|
Chris@67
|
545 fabsf(range.min));
|
Chris@67
|
546 }
|
Chris@67
|
547
|
Chris@67
|
548 float gain = m_effectiveGains[ch];
|
Chris@67
|
549
|
Chris@68
|
550 int m = (h / channels) / 2;
|
Chris@68
|
551 int my = m + (((ch - minChannel) * h) / channels);
|
Chris@68
|
552
|
Chris@68
|
553 // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl;
|
Chris@68
|
554
|
Chris@68
|
555 if (my - m > y1 || my + m < y0) continue;
|
Chris@68
|
556
|
Chris@68
|
557 if ((m_scale == dBScale || m_scale == MeterScale) &&
|
Chris@68
|
558 m_channelMode != MergeChannels) {
|
Chris@68
|
559 m = (h / channels);
|
Chris@68
|
560 my = m + (((ch - minChannel) * h) / channels);
|
Chris@68
|
561 }
|
Chris@68
|
562
|
Chris@68
|
563 paint->setPen(greys[0]);
|
Chris@68
|
564 paint->drawLine(x0, my, x1, my);
|
Chris@68
|
565
|
Chris@68
|
566 int n = 10;
|
Chris@68
|
567 int py = -1;
|
Chris@68
|
568
|
Chris@68
|
569 if (v->hasLightBackground()) {
|
Chris@68
|
570
|
Chris@68
|
571 paint->setPen(QColor(240, 240, 240));
|
Chris@68
|
572
|
Chris@68
|
573 for (int i = 1; i < n; ++i) {
|
Chris@68
|
574
|
Chris@68
|
575 float val = 0.0, nval = 0.0;
|
Chris@68
|
576
|
Chris@68
|
577 switch (m_scale) {
|
Chris@68
|
578
|
Chris@68
|
579 case LinearScale:
|
Chris@68
|
580 val = (i * gain) / n;
|
Chris@68
|
581 if (i > 0) nval = -val;
|
Chris@68
|
582 break;
|
Chris@68
|
583
|
Chris@68
|
584 case MeterScale:
|
Chris@68
|
585 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
|
Chris@68
|
586 break;
|
Chris@68
|
587
|
Chris@68
|
588 case dBScale:
|
Chris@68
|
589 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
|
Chris@68
|
590 break;
|
Chris@68
|
591 }
|
Chris@68
|
592
|
Chris@68
|
593 if (val < -1.0 || val > 1.0) continue;
|
Chris@68
|
594
|
Chris@68
|
595 int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel);
|
Chris@68
|
596
|
Chris@68
|
597 if (py >= 0 && abs(y - py) < 10) continue;
|
Chris@68
|
598 else py = y;
|
Chris@68
|
599
|
Chris@68
|
600 int ny = y;
|
Chris@68
|
601 if (nval != 0.0) {
|
Chris@68
|
602 ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel);
|
Chris@68
|
603 }
|
Chris@68
|
604
|
Chris@68
|
605 paint->drawLine(x0, y, x1, y);
|
Chris@68
|
606 if (ny != y) {
|
Chris@68
|
607 paint->drawLine(x0, ny, x1, ny);
|
Chris@68
|
608 }
|
Chris@68
|
609 }
|
Chris@68
|
610 }
|
Chris@68
|
611
|
Chris@68
|
612 if (frame1 <= 0) continue;
|
Chris@68
|
613
|
Chris@68
|
614 size_t modelZoomLevel = zoomLevel;
|
Chris@68
|
615
|
Chris@68
|
616 ranges = m_model->getRanges
|
Chris@68
|
617 (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
|
Chris@68
|
618
|
Chris@68
|
619 if (mergingChannels || mixingChannels) {
|
Chris@71
|
620 if (m_model->getChannelCount() > 1) {
|
Chris@71
|
621 otherChannelRanges = m_model->getRanges
|
Chris@71
|
622 (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
|
Chris@71
|
623 } else {
|
Chris@71
|
624 otherChannelRanges = ranges;
|
Chris@71
|
625 }
|
Chris@68
|
626 }
|
Chris@68
|
627
|
Chris@0
|
628 for (int x = x0; x <= x1; ++x) {
|
Chris@0
|
629
|
Chris@0
|
630 range = RangeSummarisableTimeValueModel::Range();
|
Chris@0
|
631 size_t index = x - x0;
|
Chris@0
|
632 size_t maxIndex = index;
|
Chris@0
|
633
|
Chris@0
|
634 if (frame0 < 0) {
|
Chris@0
|
635 if (index < size_t(-frame0 / zoomLevel)) {
|
Chris@0
|
636 continue;
|
Chris@0
|
637 } else {
|
Chris@0
|
638 index -= -frame0 / zoomLevel;
|
Chris@0
|
639 maxIndex = index;
|
Chris@0
|
640 }
|
Chris@0
|
641 }
|
Chris@0
|
642
|
Chris@0
|
643 if (int(modelZoomLevel) != zoomLevel) {
|
Chris@0
|
644
|
Chris@0
|
645 index = size_t((double(index) * zoomLevel) / modelZoomLevel);
|
Chris@0
|
646
|
Chris@0
|
647 if (int(modelZoomLevel) < zoomLevel) {
|
Chris@0
|
648 // Peaks may be missed! The model should avoid
|
Chris@0
|
649 // this by rounding zoom levels up rather than
|
Chris@0
|
650 // down, but we'd better cope in case it doesn't
|
Chris@0
|
651 maxIndex = index;
|
Chris@0
|
652 } else {
|
Chris@0
|
653 maxIndex = size_t((double(index + 1) * zoomLevel)
|
Chris@0
|
654 / modelZoomLevel) - 1;
|
Chris@0
|
655 }
|
Chris@0
|
656 }
|
Chris@0
|
657
|
Chris@0
|
658 if (index < ranges.size()) {
|
Chris@0
|
659
|
Chris@0
|
660 range = ranges[index];
|
Chris@0
|
661
|
Chris@0
|
662 if (maxIndex > index && maxIndex < ranges.size()) {
|
Chris@0
|
663 range.max = std::max(range.max, ranges[maxIndex].max);
|
Chris@0
|
664 range.min = std::min(range.min, ranges[maxIndex].min);
|
Chris@0
|
665 range.absmean = (range.absmean +
|
Chris@0
|
666 ranges[maxIndex].absmean) / 2;
|
Chris@0
|
667 }
|
Chris@0
|
668
|
Chris@0
|
669 } else {
|
Chris@0
|
670 continue;
|
Chris@0
|
671 }
|
Chris@0
|
672
|
Chris@0
|
673 int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
|
Chris@0
|
674
|
Chris@0
|
675 if (mergingChannels) {
|
Chris@0
|
676
|
Chris@0
|
677 if (index < otherChannelRanges.size()) {
|
Chris@0
|
678
|
Chris@0
|
679 range.max = fabsf(range.max);
|
Chris@0
|
680 range.min = -fabsf(otherChannelRanges[index].max);
|
Chris@0
|
681 range.absmean = (range.absmean +
|
Chris@0
|
682 otherChannelRanges[index].absmean) / 2;
|
Chris@0
|
683
|
Chris@0
|
684 if (maxIndex > index && maxIndex < ranges.size()) {
|
Chris@0
|
685 // let's not concern ourselves about the mean
|
Chris@0
|
686 range.min = std::min
|
Chris@0
|
687 (range.min,
|
Chris@0
|
688 -fabsf(otherChannelRanges[maxIndex].max));
|
Chris@0
|
689 }
|
Chris@0
|
690 }
|
Chris@67
|
691
|
Chris@67
|
692 } else if (mixingChannels) {
|
Chris@67
|
693
|
Chris@67
|
694 if (index < otherChannelRanges.size()) {
|
Chris@67
|
695
|
Chris@67
|
696 range.max = (range.max + otherChannelRanges[index].max) / 2;
|
Chris@67
|
697 range.min = (range.min + otherChannelRanges[index].min) / 2;
|
Chris@67
|
698 range.absmean = (range.absmean + otherChannelRanges[index].absmean) / 2;
|
Chris@67
|
699 }
|
Chris@67
|
700 }
|
Chris@0
|
701
|
Chris@0
|
702 int greyLevels = 1;
|
Chris@0
|
703 if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4;
|
Chris@0
|
704
|
Chris@0
|
705 switch (m_scale) {
|
Chris@0
|
706
|
Chris@0
|
707 case LinearScale:
|
Chris@67
|
708 rangeBottom = int( m * greyLevels * range.min * gain);
|
Chris@67
|
709 rangeTop = int( m * greyLevels * range.max * gain);
|
Chris@67
|
710 meanBottom = int(-m * range.absmean * gain);
|
Chris@67
|
711 meanTop = int( m * range.absmean * gain);
|
Chris@0
|
712 break;
|
Chris@0
|
713
|
Chris@0
|
714 case dBScale:
|
Chris@67
|
715 if (!mergingChannels) {
|
Chris@67
|
716 int db0 = dBscale(range.min * gain, m);
|
Chris@67
|
717 int db1 = dBscale(range.max * gain, m);
|
Chris@67
|
718 rangeTop = std::max(db0, db1);
|
Chris@67
|
719 meanTop = std::min(db0, db1);
|
Chris@67
|
720 if (mixingChannels) rangeBottom = meanTop;
|
Chris@67
|
721 else rangeBottom = dBscale(range.absmean * gain, m);
|
Chris@67
|
722 meanBottom = rangeBottom;
|
Chris@67
|
723 } else {
|
Chris@67
|
724 rangeBottom = -dBscale(range.min * gain, m * greyLevels);
|
Chris@67
|
725 rangeTop = dBscale(range.max * gain, m * greyLevels);
|
Chris@67
|
726 meanBottom = -dBscale(range.absmean * gain, m);
|
Chris@67
|
727 meanTop = dBscale(range.absmean * gain, m);
|
Chris@67
|
728 }
|
Chris@0
|
729 break;
|
Chris@0
|
730
|
Chris@0
|
731 case MeterScale:
|
Chris@67
|
732 if (!mergingChannels) {
|
Chris@67
|
733 int r0 = abs(AudioLevel::multiplier_to_preview(range.min * gain, m));
|
Chris@67
|
734 int r1 = abs(AudioLevel::multiplier_to_preview(range.max * gain, m));
|
Chris@67
|
735 rangeTop = std::max(r0, r1);
|
Chris@67
|
736 meanTop = std::min(r0, r1);
|
Chris@67
|
737 if (mixingChannels) rangeBottom = meanTop;
|
Chris@67
|
738 else rangeBottom = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
|
Chris@67
|
739 meanBottom = rangeBottom;
|
Chris@67
|
740 } else {
|
Chris@67
|
741 rangeBottom = AudioLevel::multiplier_to_preview(range.min * gain, m * greyLevels);
|
Chris@67
|
742 rangeTop = AudioLevel::multiplier_to_preview(range.max * gain, m * greyLevels);
|
Chris@67
|
743 meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * gain, m);
|
Chris@67
|
744 meanTop = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
|
Chris@67
|
745 }
|
Chris@67
|
746 break;
|
Chris@0
|
747 }
|
Chris@0
|
748
|
Chris@27
|
749 rangeBottom = my * greyLevels - rangeBottom;
|
Chris@27
|
750 rangeTop = my * greyLevels - rangeTop;
|
Chris@27
|
751 meanBottom = my - meanBottom;
|
Chris@27
|
752 meanTop = my - meanTop;
|
Chris@27
|
753
|
Chris@27
|
754 int topFill = (rangeTop % greyLevels);
|
Chris@27
|
755 if (topFill > 0) topFill = greyLevels - topFill;
|
Chris@27
|
756
|
Chris@27
|
757 int bottomFill = (rangeBottom % greyLevels);
|
Chris@27
|
758
|
Chris@0
|
759 rangeTop = rangeTop / greyLevels;
|
Chris@0
|
760 rangeBottom = rangeBottom / greyLevels;
|
Chris@0
|
761
|
Chris@0
|
762 bool clipped = false;
|
Chris@27
|
763
|
Chris@27
|
764 if (rangeTop < my - m) { rangeTop = my - m; }
|
Chris@27
|
765 if (rangeTop > my + m) { rangeTop = my + m; }
|
Chris@27
|
766 if (rangeBottom < my - m) { rangeBottom = my - m; }
|
Chris@27
|
767 if (rangeBottom > my + m) { rangeBottom = my + m; }
|
Chris@27
|
768
|
Chris@67
|
769 if (range.max <= -1.0 ||
|
Chris@67
|
770 range.max >= 1.0) clipped = true;
|
Chris@0
|
771
|
Chris@0
|
772 if (meanBottom > rangeBottom) meanBottom = rangeBottom;
|
Chris@0
|
773 if (meanTop < rangeTop) meanTop = rangeTop;
|
Chris@0
|
774
|
Chris@0
|
775 bool drawMean = m_showMeans;
|
Chris@0
|
776 if (meanTop == rangeTop) {
|
Chris@0
|
777 if (meanTop < meanBottom) ++meanTop;
|
Chris@0
|
778 else drawMean = false;
|
Chris@0
|
779 }
|
Chris@67
|
780 if (meanBottom == rangeBottom && m_scale == LinearScale) {
|
Chris@0
|
781 if (meanBottom > meanTop) --meanBottom;
|
Chris@0
|
782 else drawMean = false;
|
Chris@0
|
783 }
|
Chris@0
|
784
|
Chris@0
|
785 if (x != x0 && prevRangeBottom != -1) {
|
Chris@0
|
786 if (prevRangeBottom > rangeBottom &&
|
Chris@0
|
787 prevRangeTop > rangeBottom) {
|
Chris@28
|
788 // paint->setPen(midColour);
|
Chris@28
|
789 paint->setPen(m_colour);
|
Chris@0
|
790 paint->drawLine(x-1, prevRangeTop, x, rangeBottom);
|
Chris@28
|
791 paint->setPen(prevRangeTopColour);
|
Chris@0
|
792 paint->drawPoint(x-1, prevRangeTop);
|
Chris@0
|
793 } else if (prevRangeBottom < rangeTop &&
|
Chris@0
|
794 prevRangeTop < rangeTop) {
|
Chris@28
|
795 // paint->setPen(midColour);
|
Chris@28
|
796 paint->setPen(m_colour);
|
Chris@0
|
797 paint->drawLine(x-1, prevRangeBottom, x, rangeTop);
|
Chris@28
|
798 paint->setPen(prevRangeBottomColour);
|
Chris@0
|
799 paint->drawPoint(x-1, prevRangeBottom);
|
Chris@0
|
800 }
|
Chris@0
|
801 }
|
Chris@0
|
802
|
Chris@0
|
803 if (ready) {
|
Chris@67
|
804 if (clipped /*!!! ||
|
Chris@67
|
805 range.min * gain <= -1.0 ||
|
Chris@67
|
806 range.max * gain >= 1.0 */) {
|
Chris@0
|
807 paint->setPen(Qt::red);
|
Chris@0
|
808 } else {
|
Chris@0
|
809 paint->setPen(m_colour);
|
Chris@0
|
810 }
|
Chris@0
|
811 } else {
|
Chris@0
|
812 paint->setPen(midColour);
|
Chris@0
|
813 }
|
Chris@0
|
814
|
Chris@0
|
815 paint->drawLine(x, rangeBottom, x, rangeTop);
|
Chris@0
|
816
|
Chris@28
|
817 prevRangeTopColour = m_colour;
|
Chris@28
|
818 prevRangeBottomColour = m_colour;
|
Chris@28
|
819
|
Chris@0
|
820 if (m_greyscale && (m_scale == LinearScale) && ready) {
|
Chris@0
|
821 if (!clipped) {
|
Chris@0
|
822 if (rangeTop < rangeBottom) {
|
Chris@0
|
823 if (topFill > 0 &&
|
Chris@0
|
824 (!drawMean || (rangeTop < meanTop - 1))) {
|
Chris@0
|
825 paint->setPen(greys[topFill - 1]);
|
Chris@27
|
826 paint->drawPoint(x, rangeTop);
|
Chris@28
|
827 prevRangeTopColour = greys[topFill - 1];
|
Chris@0
|
828 }
|
Chris@0
|
829 if (bottomFill > 0 &&
|
Chris@0
|
830 (!drawMean || (rangeBottom > meanBottom + 1))) {
|
Chris@0
|
831 paint->setPen(greys[bottomFill - 1]);
|
Chris@27
|
832 paint->drawPoint(x, rangeBottom);
|
Chris@28
|
833 prevRangeBottomColour = greys[bottomFill - 1];
|
Chris@0
|
834 }
|
Chris@0
|
835 }
|
Chris@0
|
836 }
|
Chris@0
|
837 }
|
Chris@0
|
838
|
Chris@0
|
839 if (drawMean) {
|
Chris@0
|
840 paint->setPen(midColour);
|
Chris@0
|
841 paint->drawLine(x, meanBottom, x, meanTop);
|
Chris@0
|
842 }
|
Chris@0
|
843
|
Chris@0
|
844 prevRangeBottom = rangeBottom;
|
Chris@0
|
845 prevRangeTop = rangeTop;
|
Chris@0
|
846 }
|
Chris@0
|
847 }
|
Chris@0
|
848
|
Chris@0
|
849 if (m_aggressive) {
|
Chris@41
|
850
|
Chris@44
|
851 if (ready && rect == v->rect()) {
|
Chris@0
|
852 m_cacheValid = true;
|
Chris@0
|
853 m_cacheZoomLevel = zoomLevel;
|
Chris@0
|
854 }
|
Chris@0
|
855 paint->end();
|
Chris@0
|
856 delete paint;
|
Chris@0
|
857 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@0
|
858 }
|
Chris@0
|
859 }
|
Chris@0
|
860
|
Chris@25
|
861 QString
|
Chris@44
|
862 WaveformLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@25
|
863 {
|
Chris@25
|
864 int x = pos.x();
|
Chris@25
|
865
|
Chris@25
|
866 if (!m_model || !m_model->isOK()) return "";
|
Chris@25
|
867
|
Chris@44
|
868 long f0 = v->getFrameForX(x);
|
Chris@44
|
869 long f1 = v->getFrameForX(x + 1);
|
Chris@25
|
870
|
Chris@25
|
871 if (f0 < 0) f0 = 0;
|
Chris@25
|
872 if (f1 <= f0) return "";
|
Chris@25
|
873
|
Chris@25
|
874 QString text;
|
Chris@25
|
875
|
Chris@25
|
876 RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate());
|
Chris@25
|
877 RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate());
|
Chris@25
|
878
|
Chris@25
|
879 if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) {
|
Chris@25
|
880 text += tr("Time:\t%1 - %2")
|
Chris@25
|
881 .arg(rt0.toText(true).c_str())
|
Chris@25
|
882 .arg(rt1.toText(true).c_str());
|
Chris@25
|
883 } else {
|
Chris@25
|
884 text += tr("Time:\t%1")
|
Chris@25
|
885 .arg(rt0.toText(true).c_str());
|
Chris@25
|
886 }
|
Chris@25
|
887
|
Chris@25
|
888 size_t channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
889 bool mergingChannels = false, mixingChannels = false;
|
Chris@25
|
890
|
Chris@67
|
891 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
892 mergingChannels, mixingChannels);
|
Chris@25
|
893 if (channels == 0) return "";
|
Chris@25
|
894
|
Chris@25
|
895 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@25
|
896
|
Chris@44
|
897 size_t blockSize = v->getZoomLevel();
|
Chris@25
|
898 RangeSummarisableTimeValueModel::RangeBlock ranges =
|
Chris@25
|
899 m_model->getRanges(ch, f0, f1, blockSize);
|
Chris@25
|
900
|
Chris@25
|
901 if (ranges.empty()) continue;
|
Chris@25
|
902
|
Chris@25
|
903 RangeSummarisableTimeValueModel::Range range = ranges[0];
|
Chris@25
|
904
|
Chris@25
|
905 QString label = tr("Level:");
|
Chris@25
|
906 if (minChannel != maxChannel) {
|
Chris@25
|
907 if (ch == 0) label = tr("Left:");
|
Chris@25
|
908 else if (ch == 1) label = tr("Right:");
|
Chris@25
|
909 else label = tr("Channel %1").arg(ch + 1);
|
Chris@25
|
910 }
|
Chris@25
|
911
|
Chris@76
|
912 bool singleValue = false;
|
Chris@76
|
913 float min, max;
|
Chris@76
|
914
|
Chris@76
|
915 if (fabs(range.min) < 0.01) {
|
Chris@76
|
916 min = range.min;
|
Chris@76
|
917 max = range.max;
|
Chris@76
|
918 singleValue = (min == max);
|
Chris@76
|
919 } else {
|
Chris@76
|
920 int imin = int(range.min * 1000);
|
Chris@76
|
921 int imax = int(range.max * 1000);
|
Chris@76
|
922 singleValue = (imin == imax);
|
Chris@76
|
923 min = float(imin)/1000;
|
Chris@76
|
924 max = float(imax)/1000;
|
Chris@76
|
925 }
|
Chris@76
|
926
|
Chris@25
|
927 int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min),
|
Chris@25
|
928 fabsf(range.max)))
|
Chris@25
|
929 * 100);
|
Chris@25
|
930
|
Chris@76
|
931 if (!singleValue) {
|
Chris@25
|
932 text += tr("\n%1\t%2 - %3 (%4 dB peak)")
|
Chris@76
|
933 .arg(label).arg(min).arg(max).arg(float(db)/100);
|
Chris@25
|
934 } else {
|
Chris@25
|
935 text += tr("\n%1\t%2 (%3 dB peak)")
|
Chris@76
|
936 .arg(label).arg(min).arg(float(db)/100);
|
Chris@25
|
937 }
|
Chris@25
|
938 }
|
Chris@25
|
939
|
Chris@25
|
940 return text;
|
Chris@25
|
941 }
|
Chris@25
|
942
|
Chris@0
|
943 int
|
Chris@68
|
944 WaveformLayer::getYForValue(View *v, Scale scale, float value, size_t channel,
|
Chris@68
|
945 size_t minChannel, size_t maxChannel) const
|
Chris@68
|
946 {
|
Chris@68
|
947 if (maxChannel < minChannel || channel < minChannel) return 0;
|
Chris@68
|
948
|
Chris@68
|
949 int w = v->width();
|
Chris@68
|
950 int h = v->height();
|
Chris@68
|
951
|
Chris@68
|
952 int channels = maxChannel - minChannel + 1;
|
Chris@68
|
953 int m = (h / channels) / 2;
|
Chris@68
|
954 int my = m + (((channel - minChannel) * h) / channels);
|
Chris@68
|
955
|
Chris@68
|
956 if ((m_scale == dBScale || m_scale == MeterScale) &&
|
Chris@68
|
957 m_channelMode != MergeChannels) {
|
Chris@68
|
958 m = (h / channels);
|
Chris@68
|
959 my = m + (((channel - minChannel) * h) / channels);
|
Chris@68
|
960 }
|
Chris@68
|
961
|
Chris@68
|
962 int vy = 0;
|
Chris@68
|
963
|
Chris@68
|
964 switch (scale) {
|
Chris@68
|
965
|
Chris@68
|
966 case LinearScale:
|
Chris@68
|
967 vy = int(m * value);
|
Chris@68
|
968 break;
|
Chris@68
|
969
|
Chris@68
|
970 case MeterScale:
|
Chris@68
|
971 vy = AudioLevel::multiplier_to_preview(value, m);
|
Chris@68
|
972 break;
|
Chris@68
|
973
|
Chris@68
|
974 case dBScale:
|
Chris@68
|
975 vy = dBscale(value, m);
|
Chris@68
|
976 break;
|
Chris@68
|
977 }
|
Chris@68
|
978
|
Chris@68
|
979 return my - vy;
|
Chris@68
|
980 }
|
Chris@68
|
981
|
Chris@68
|
982 int
|
Chris@44
|
983 WaveformLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
|
Chris@0
|
984 {
|
Chris@0
|
985 if (m_scale == LinearScale) {
|
Chris@0
|
986 return paint.fontMetrics().width("0.0") + 13;
|
Chris@0
|
987 } else {
|
Chris@0
|
988 return std::max(paint.fontMetrics().width(tr("0dB")),
|
Chris@0
|
989 paint.fontMetrics().width(tr("-Inf"))) + 13;
|
Chris@0
|
990 }
|
Chris@0
|
991 }
|
Chris@0
|
992
|
Chris@0
|
993 void
|
Chris@44
|
994 WaveformLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
995 {
|
Chris@0
|
996 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
997 return;
|
Chris@0
|
998 }
|
Chris@0
|
999
|
Chris@0
|
1000 size_t channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
1001 bool mergingChannels = false, mixingChannels = false;
|
Chris@0
|
1002
|
Chris@67
|
1003 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
1004 mergingChannels, mixingChannels);
|
Chris@0
|
1005 if (channels == 0) return;
|
Chris@0
|
1006
|
Chris@0
|
1007 int h = rect.height(), w = rect.width();
|
Chris@0
|
1008 int textHeight = paint.fontMetrics().height();
|
Chris@0
|
1009 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
|
Chris@0
|
1010
|
Chris@67
|
1011 float gain = m_gain;
|
Chris@67
|
1012
|
Chris@0
|
1013 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@0
|
1014
|
Chris@68
|
1015 int lastLabelledY = -1;
|
Chris@0
|
1016
|
Chris@67
|
1017 if (ch < m_effectiveGains.size()) gain = m_effectiveGains[ch];
|
Chris@67
|
1018
|
Chris@68
|
1019 int n = 10;
|
Chris@0
|
1020
|
Chris@68
|
1021 for (int i = 0; i <= n; ++i) {
|
Chris@68
|
1022
|
Chris@68
|
1023 float val = 0.0, nval = 0.0;
|
Chris@0
|
1024 QString text = "";
|
Chris@0
|
1025
|
Chris@68
|
1026 switch (m_scale) {
|
Chris@68
|
1027
|
Chris@68
|
1028 case LinearScale:
|
Chris@68
|
1029 val = (i * gain) / n;
|
Chris@68
|
1030 text = QString("%1").arg(float(i) / n);
|
Chris@68
|
1031 if (i == 0) text = "0.0";
|
Chris@68
|
1032 else {
|
Chris@68
|
1033 nval = -val;
|
Chris@68
|
1034 if (i == n) text = "1.0";
|
Chris@68
|
1035 }
|
Chris@68
|
1036 break;
|
Chris@0
|
1037
|
Chris@68
|
1038 case MeterScale:
|
Chris@68
|
1039 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
|
Chris@68
|
1040 text = QString("%1").arg(meterdbs[i]);
|
Chris@68
|
1041 if (i == n) text = tr("0dB");
|
Chris@68
|
1042 if (i == 0) {
|
Chris@68
|
1043 text = tr("-Inf");
|
Chris@68
|
1044 val = 0.0;
|
Chris@68
|
1045 }
|
Chris@68
|
1046 break;
|
Chris@0
|
1047
|
Chris@68
|
1048 case dBScale:
|
Chris@68
|
1049 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
|
Chris@68
|
1050 text = QString("%1").arg(-(10*n) + i * 10);
|
Chris@68
|
1051 if (i == n) text = tr("0dB");
|
Chris@68
|
1052 if (i == 0) {
|
Chris@68
|
1053 text = tr("-Inf");
|
Chris@68
|
1054 val = 0.0;
|
Chris@68
|
1055 }
|
Chris@68
|
1056 break;
|
Chris@68
|
1057 }
|
Chris@0
|
1058
|
Chris@68
|
1059 if (val < -1.0 || val > 1.0) continue;
|
Chris@0
|
1060
|
Chris@68
|
1061 int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel);
|
Chris@0
|
1062
|
Chris@68
|
1063 int ny = y;
|
Chris@68
|
1064 if (nval != 0.0) {
|
Chris@68
|
1065 ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel);
|
Chris@68
|
1066 }
|
Chris@0
|
1067
|
Chris@68
|
1068 bool spaceForLabel = (i == 0 ||
|
Chris@68
|
1069 abs(y - lastLabelledY) >= textHeight - 1);
|
Chris@0
|
1070
|
Chris@68
|
1071 if (spaceForLabel) {
|
Chris@0
|
1072
|
Chris@68
|
1073 int tx = 3;
|
Chris@68
|
1074 if (m_scale != LinearScale) {
|
Chris@68
|
1075 tx = w - 10 - paint.fontMetrics().width(text);
|
Chris@68
|
1076 }
|
Chris@68
|
1077
|
Chris@68
|
1078 int ty = y;
|
Chris@68
|
1079 if (ty < paint.fontMetrics().ascent()) {
|
Chris@68
|
1080 ty = paint.fontMetrics().ascent();
|
Chris@68
|
1081 } else if (ty > h - paint.fontMetrics().descent()) {
|
Chris@68
|
1082 ty = h - paint.fontMetrics().descent();
|
Chris@68
|
1083 } else {
|
Chris@68
|
1084 ty += toff;
|
Chris@68
|
1085 }
|
Chris@68
|
1086 paint.drawText(tx, ty, text);
|
Chris@0
|
1087
|
Chris@68
|
1088 lastLabelledY = ty - toff;
|
Chris@67
|
1089
|
Chris@68
|
1090 if (ny != y) {
|
Chris@68
|
1091 ty = ny;
|
Chris@68
|
1092 if (ty < paint.fontMetrics().ascent()) {
|
Chris@68
|
1093 ty = paint.fontMetrics().ascent();
|
Chris@68
|
1094 } else if (ty > h - paint.fontMetrics().descent()) {
|
Chris@68
|
1095 ty = h - paint.fontMetrics().descent();
|
Chris@68
|
1096 } else {
|
Chris@68
|
1097 ty += toff;
|
Chris@68
|
1098 }
|
Chris@68
|
1099 paint.drawText(tx, ty, text);
|
Chris@68
|
1100 }
|
Chris@68
|
1101
|
Chris@68
|
1102 paint.drawLine(w - 7, y, w, y);
|
Chris@68
|
1103 if (ny != y) paint.drawLine(w - 7, ny, w, ny);
|
Chris@68
|
1104
|
Chris@68
|
1105 } else {
|
Chris@68
|
1106
|
Chris@68
|
1107 paint.drawLine(w - 4, y, w, y);
|
Chris@68
|
1108 if (ny != y) paint.drawLine(w - 4, ny, w, ny);
|
Chris@68
|
1109 }
|
Chris@0
|
1110 }
|
Chris@0
|
1111 }
|
Chris@0
|
1112 }
|
Chris@0
|
1113
|
Chris@6
|
1114 QString
|
Chris@6
|
1115 WaveformLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@6
|
1116 {
|
Chris@6
|
1117 QString s;
|
Chris@6
|
1118
|
Chris@6
|
1119 s += QString("gain=\"%1\" "
|
Chris@6
|
1120 "colour=\"%2\" "
|
Chris@6
|
1121 "showMeans=\"%3\" "
|
Chris@6
|
1122 "greyscale=\"%4\" "
|
Chris@6
|
1123 "channelMode=\"%5\" "
|
Chris@6
|
1124 "channel=\"%6\" "
|
Chris@6
|
1125 "scale=\"%7\" "
|
Chris@67
|
1126 "aggressive=\"%8\" "
|
Chris@67
|
1127 "autoNormalize=\"%9\"")
|
Chris@6
|
1128 .arg(m_gain)
|
Chris@6
|
1129 .arg(encodeColour(m_colour))
|
Chris@6
|
1130 .arg(m_showMeans)
|
Chris@6
|
1131 .arg(m_greyscale)
|
Chris@6
|
1132 .arg(m_channelMode)
|
Chris@6
|
1133 .arg(m_channel)
|
Chris@6
|
1134 .arg(m_scale)
|
Chris@67
|
1135 .arg(m_aggressive)
|
Chris@67
|
1136 .arg(m_autoNormalize);
|
Chris@6
|
1137
|
Chris@6
|
1138 return Layer::toXmlString(indent, extraAttributes + " " + s);
|
Chris@6
|
1139 }
|
Chris@6
|
1140
|
Chris@11
|
1141 void
|
Chris@11
|
1142 WaveformLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
1143 {
|
Chris@11
|
1144 bool ok = false;
|
Chris@11
|
1145
|
Chris@11
|
1146 float gain = attributes.value("gain").toFloat(&ok);
|
Chris@11
|
1147 if (ok) setGain(gain);
|
Chris@11
|
1148
|
Chris@11
|
1149 QString colourSpec = attributes.value("colour");
|
Chris@11
|
1150 if (colourSpec != "") {
|
Chris@11
|
1151 QColor colour(colourSpec);
|
Chris@11
|
1152 if (colour.isValid()) {
|
Chris@11
|
1153 setBaseColour(QColor(colourSpec));
|
Chris@11
|
1154 }
|
Chris@11
|
1155 }
|
Chris@11
|
1156
|
Chris@11
|
1157 bool showMeans = (attributes.value("showMeans") == "1" ||
|
Chris@11
|
1158 attributes.value("showMeans") == "true");
|
Chris@11
|
1159 setShowMeans(showMeans);
|
Chris@11
|
1160
|
Chris@11
|
1161 bool greyscale = (attributes.value("greyscale") == "1" ||
|
Chris@11
|
1162 attributes.value("greyscale") == "true");
|
Chris@11
|
1163 setUseGreyscale(greyscale);
|
Chris@11
|
1164
|
Chris@11
|
1165 ChannelMode channelMode = (ChannelMode)
|
Chris@11
|
1166 attributes.value("channelMode").toInt(&ok);
|
Chris@11
|
1167 if (ok) setChannelMode(channelMode);
|
Chris@11
|
1168
|
Chris@11
|
1169 int channel = attributes.value("channel").toInt(&ok);
|
Chris@11
|
1170 if (ok) setChannel(channel);
|
Chris@11
|
1171
|
Chris@11
|
1172 Scale scale = (Scale)
|
Chris@11
|
1173 attributes.value("scale").toInt(&ok);
|
Chris@11
|
1174 if (ok) setScale(scale);
|
Chris@11
|
1175
|
Chris@11
|
1176 bool aggressive = (attributes.value("aggressive") == "1" ||
|
Chris@11
|
1177 attributes.value("aggressive") == "true");
|
Chris@11
|
1178 setUseGreyscale(aggressive);
|
Chris@67
|
1179
|
Chris@67
|
1180 bool autoNormalize = (attributes.value("autoNormalize") == "1" ||
|
Chris@67
|
1181 attributes.value("autoNormalize") == "true");
|
Chris@67
|
1182 setAutoNormalize(autoNormalize);
|
Chris@11
|
1183 }
|
Chris@11
|
1184
|
Chris@0
|
1185 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
1186 #include "WaveformLayer.moc.cpp"
|
Chris@0
|
1187 #endif
|
Chris@0
|
1188
|