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