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@1147
|
22 #include "base/Strings.h"
|
Chris@1078
|
23
|
Chris@376
|
24 #include "ColourDatabase.h"
|
Chris@1078
|
25 #include "PaintAssistant.h"
|
Chris@1340
|
26
|
Chris@1340
|
27 #include "data/model/WaveformOversampler.h"
|
Chris@0
|
28
|
Chris@0
|
29 #include <QPainter>
|
Chris@0
|
30 #include <QPixmap>
|
Chris@316
|
31 #include <QTextStream>
|
Chris@0
|
32
|
Chris@0
|
33 #include <iostream>
|
Chris@0
|
34 #include <cmath>
|
Chris@0
|
35
|
Chris@1352
|
36 //#define DEBUG_WAVEFORM_PAINT 1
|
Chris@1338
|
37 //#define DEBUG_WAVEFORM_PAINT_BY_PIXEL 1
|
Chris@4
|
38
|
Chris@1333
|
39 using std::vector;
|
Chris@682
|
40
|
Chris@0
|
41
|
Chris@44
|
42 WaveformLayer::WaveformLayer() :
|
Chris@287
|
43 SingleColourLayer(),
|
Chris@0
|
44 m_model(0),
|
Chris@0
|
45 m_gain(1.0f),
|
Chris@67
|
46 m_autoNormalize(false),
|
Chris@0
|
47 m_showMeans(true),
|
Chris@0
|
48 m_channelMode(SeparateChannels),
|
Chris@0
|
49 m_channel(-1),
|
Chris@0
|
50 m_scale(LinearScale),
|
Chris@709
|
51 m_middleLineHeight(0.5),
|
Chris@0
|
52 m_aggressive(false),
|
Chris@0
|
53 m_cache(0),
|
Chris@1325
|
54 m_cacheValid(false)
|
Chris@0
|
55 {
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 WaveformLayer::~WaveformLayer()
|
Chris@0
|
59 {
|
Chris@0
|
60 delete m_cache;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 void
|
Chris@0
|
64 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
|
Chris@0
|
65 {
|
Chris@69
|
66 bool channelsChanged = false;
|
Chris@69
|
67 if (m_channel == -1) {
|
Chris@69
|
68 if (!m_model) {
|
Chris@69
|
69 if (model) {
|
Chris@69
|
70 channelsChanged = true;
|
Chris@69
|
71 }
|
Chris@69
|
72 } else {
|
Chris@69
|
73 if (model &&
|
Chris@69
|
74 m_model->getChannelCount() != model->getChannelCount()) {
|
Chris@69
|
75 channelsChanged = true;
|
Chris@69
|
76 }
|
Chris@69
|
77 }
|
Chris@69
|
78 }
|
Chris@69
|
79
|
Chris@0
|
80 m_model = model;
|
Chris@0
|
81 m_cacheValid = false;
|
Chris@0
|
82 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
83
|
Chris@320
|
84 connectSignals(m_model);
|
Chris@301
|
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@287
|
94 PropertyList list = SingleColourLayer::getProperties();
|
Chris@87
|
95 list.push_back("Scale");
|
Chris@87
|
96 list.push_back("Gain");
|
Chris@87
|
97 list.push_back("Normalize Visible Area");
|
Chris@68
|
98
|
Chris@68
|
99 if (m_model && m_model->getChannelCount() > 1 && m_channel == -1) {
|
Chris@87
|
100 list.push_back("Channels");
|
Chris@68
|
101 }
|
Chris@68
|
102
|
Chris@0
|
103 return list;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@87
|
106 QString
|
Chris@87
|
107 WaveformLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
108 {
|
Chris@87
|
109 if (name == "Scale") return tr("Scale");
|
Chris@87
|
110 if (name == "Gain") return tr("Gain");
|
Chris@87
|
111 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
|
Chris@87
|
112 if (name == "Channels") return tr("Channels");
|
Chris@287
|
113 return SingleColourLayer::getPropertyLabel(name);
|
Chris@87
|
114 }
|
Chris@87
|
115
|
Chris@335
|
116 QString
|
Chris@335
|
117 WaveformLayer::getPropertyIconName(const PropertyName &name) const
|
Chris@335
|
118 {
|
Chris@335
|
119 if (name == "Normalize Visible Area") return "normalise";
|
Chris@335
|
120 return "";
|
Chris@335
|
121 }
|
Chris@335
|
122
|
Chris@0
|
123 Layer::PropertyType
|
Chris@0
|
124 WaveformLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
125 {
|
Chris@87
|
126 if (name == "Gain") return RangeProperty;
|
Chris@87
|
127 if (name == "Normalize Visible Area") return ToggleProperty;
|
Chris@87
|
128 if (name == "Channels") return ValueProperty;
|
Chris@87
|
129 if (name == "Scale") return ValueProperty;
|
Chris@287
|
130 return SingleColourLayer::getPropertyType(name);
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 QString
|
Chris@0
|
134 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@0
|
135 {
|
Chris@87
|
136 if (name == "Gain" ||
|
Chris@87
|
137 name == "Normalize Visible Area" ||
|
Chris@1266
|
138 name == "Scale") return tr("Scale");
|
Chris@0
|
139 return QString();
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 int
|
Chris@0
|
143 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
144 int *min, int *max, int *deflt) const
|
Chris@0
|
145 {
|
Chris@216
|
146 int val = 0;
|
Chris@0
|
147
|
Chris@216
|
148 int garbage0, garbage1, garbage2;
|
Chris@56
|
149 if (!min) min = &garbage0;
|
Chris@56
|
150 if (!max) max = &garbage1;
|
Chris@216
|
151 if (!deflt) deflt = &garbage2;
|
Chris@10
|
152
|
Chris@87
|
153 if (name == "Gain") {
|
Chris@0
|
154
|
Chris@1266
|
155 *min = -50;
|
Chris@1266
|
156 *max = 50;
|
Chris@216
|
157 *deflt = 0;
|
Chris@0
|
158
|
Chris@1266
|
159 val = int(lrint(log10(m_gain) * 20.0));
|
Chris@1266
|
160 if (val < *min) val = *min;
|
Chris@1266
|
161 if (val > *max) val = *max;
|
Chris@0
|
162
|
Chris@87
|
163 } else if (name == "Normalize Visible Area") {
|
Chris@67
|
164
|
Chris@216
|
165 val = (m_autoNormalize ? 1 : 0);
|
Chris@216
|
166 *deflt = 0;
|
Chris@67
|
167
|
Chris@87
|
168 } else if (name == "Channels") {
|
Chris@0
|
169
|
Chris@67
|
170 *min = 0;
|
Chris@67
|
171 *max = 2;
|
Chris@216
|
172 *deflt = 0;
|
Chris@216
|
173 if (m_channelMode == MixChannels) val = 1;
|
Chris@216
|
174 else if (m_channelMode == MergeChannels) val = 2;
|
Chris@216
|
175 else val = 0;
|
Chris@0
|
176
|
Chris@87
|
177 } else if (name == "Scale") {
|
Chris@0
|
178
|
Chris@1266
|
179 *min = 0;
|
Chris@1266
|
180 *max = 2;
|
Chris@216
|
181 *deflt = 0;
|
Chris@0
|
182
|
Chris@1266
|
183 val = (int)m_scale;
|
Chris@0
|
184
|
Chris@0
|
185 } else {
|
Chris@1266
|
186 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@216
|
189 return val;
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 QString
|
Chris@0
|
193 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@1266
|
194 int value) const
|
Chris@0
|
195 {
|
Chris@87
|
196 if (name == "Scale") {
|
Chris@1266
|
197 switch (value) {
|
Chris@1266
|
198 default:
|
Chris@1266
|
199 case 0: return tr("Linear");
|
Chris@1266
|
200 case 1: return tr("Meter");
|
Chris@1266
|
201 case 2: return tr("dB");
|
Chris@1266
|
202 }
|
Chris@0
|
203 }
|
Chris@87
|
204 if (name == "Channels") {
|
Chris@67
|
205 switch (value) {
|
Chris@67
|
206 default:
|
Chris@67
|
207 case 0: return tr("Separate");
|
Chris@67
|
208 case 1: return tr("Mean");
|
Chris@67
|
209 case 2: return tr("Butterfly");
|
Chris@67
|
210 }
|
Chris@67
|
211 }
|
Chris@287
|
212 return SingleColourLayer::getPropertyValueLabel(name, value);
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@167
|
215 RangeMapper *
|
Chris@167
|
216 WaveformLayer::getNewPropertyRangeMapper(const PropertyName &name) const
|
Chris@167
|
217 {
|
Chris@167
|
218 if (name == "Gain") {
|
Chris@167
|
219 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
|
Chris@167
|
220 }
|
Chris@167
|
221 return 0;
|
Chris@167
|
222 }
|
Chris@167
|
223
|
Chris@0
|
224 void
|
Chris@0
|
225 WaveformLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
226 {
|
Chris@87
|
227 if (name == "Gain") {
|
Chris@1266
|
228 setGain(float(pow(10, float(value)/20.0)));
|
Chris@87
|
229 } else if (name == "Normalize Visible Area") {
|
Chris@67
|
230 setAutoNormalize(value ? true : false);
|
Chris@87
|
231 } else if (name == "Channels") {
|
Chris@67
|
232 if (value == 1) setChannelMode(MixChannels);
|
Chris@67
|
233 else if (value == 2) setChannelMode(MergeChannels);
|
Chris@67
|
234 else setChannelMode(SeparateChannels);
|
Chris@87
|
235 } else if (name == "Scale") {
|
Chris@1266
|
236 switch (value) {
|
Chris@1266
|
237 default:
|
Chris@1266
|
238 case 0: setScale(LinearScale); break;
|
Chris@1266
|
239 case 1: setScale(MeterScale); break;
|
Chris@1266
|
240 case 2: setScale(dBScale); break;
|
Chris@1266
|
241 }
|
Chris@287
|
242 } else {
|
Chris@287
|
243 SingleColourLayer::setProperty(name, value);
|
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@133
|
254 emit verticalZoomChanged();
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 void
|
Chris@67
|
258 WaveformLayer::setAutoNormalize(bool autoNormalize)
|
Chris@67
|
259 {
|
Chris@67
|
260 if (m_autoNormalize == autoNormalize) return;
|
Chris@67
|
261 m_autoNormalize = autoNormalize;
|
Chris@67
|
262 m_cacheValid = false;
|
Chris@67
|
263 emit layerParametersChanged();
|
Chris@67
|
264 }
|
Chris@67
|
265
|
Chris@67
|
266 void
|
Chris@0
|
267 WaveformLayer::setShowMeans(bool showMeans)
|
Chris@0
|
268 {
|
Chris@0
|
269 if (m_showMeans == showMeans) return;
|
Chris@0
|
270 m_showMeans = showMeans;
|
Chris@0
|
271 m_cacheValid = false;
|
Chris@0
|
272 emit layerParametersChanged();
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 void
|
Chris@0
|
276 WaveformLayer::setChannelMode(ChannelMode channelMode)
|
Chris@0
|
277 {
|
Chris@0
|
278 if (m_channelMode == channelMode) return;
|
Chris@0
|
279 m_channelMode = channelMode;
|
Chris@0
|
280 m_cacheValid = false;
|
Chris@0
|
281 emit layerParametersChanged();
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 void
|
Chris@0
|
285 WaveformLayer::setChannel(int channel)
|
Chris@0
|
286 {
|
Chris@587
|
287 // SVDEBUG << "WaveformLayer::setChannel(" << channel << ")" << endl;
|
Chris@0
|
288
|
Chris@0
|
289 if (m_channel == channel) return;
|
Chris@0
|
290 m_channel = channel;
|
Chris@0
|
291 m_cacheValid = false;
|
Chris@0
|
292 emit layerParametersChanged();
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 void
|
Chris@0
|
296 WaveformLayer::setScale(Scale scale)
|
Chris@0
|
297 {
|
Chris@0
|
298 if (m_scale == scale) return;
|
Chris@0
|
299 m_scale = scale;
|
Chris@0
|
300 m_cacheValid = false;
|
Chris@0
|
301 emit layerParametersChanged();
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 void
|
Chris@908
|
305 WaveformLayer::setMiddleLineHeight(double height)
|
Chris@709
|
306 {
|
Chris@709
|
307 if (m_middleLineHeight == height) return;
|
Chris@709
|
308 m_middleLineHeight = height;
|
Chris@709
|
309 m_cacheValid = false;
|
Chris@709
|
310 emit layerParametersChanged();
|
Chris@709
|
311 }
|
Chris@709
|
312
|
Chris@709
|
313 void
|
Chris@0
|
314 WaveformLayer::setAggressiveCacheing(bool aggressive)
|
Chris@0
|
315 {
|
Chris@0
|
316 if (m_aggressive == aggressive) return;
|
Chris@0
|
317 m_aggressive = aggressive;
|
Chris@0
|
318 m_cacheValid = false;
|
Chris@0
|
319 emit layerParametersChanged();
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 int
|
Chris@918
|
323 WaveformLayer::getCompletion(LayerGeometryProvider *) const
|
Chris@0
|
324 {
|
Chris@0
|
325 int completion = 100;
|
Chris@0
|
326 if (!m_model || !m_model->isOK()) return completion;
|
Chris@0
|
327 if (m_model->isReady(&completion)) return 100;
|
Chris@0
|
328 return completion;
|
Chris@0
|
329 }
|
Chris@0
|
330
|
Chris@79
|
331 bool
|
Chris@908
|
332 WaveformLayer::getValueExtents(double &min, double &max,
|
Chris@248
|
333 bool &, QString &unit) const
|
Chris@79
|
334 {
|
Chris@79
|
335 if (m_scale == LinearScale) {
|
Chris@79
|
336 min = 0.0;
|
Chris@79
|
337 max = 1.0;
|
Chris@79
|
338 unit = "V";
|
Chris@79
|
339 } else if (m_scale == MeterScale) {
|
Chris@79
|
340 return false; //!!!
|
Chris@79
|
341 } else {
|
Chris@79
|
342 min = AudioLevel::multiplier_to_dB(0.0);
|
Chris@79
|
343 max = AudioLevel::multiplier_to_dB(1.0);
|
Chris@79
|
344 unit = "dB";
|
Chris@79
|
345 }
|
Chris@79
|
346 return true;
|
Chris@79
|
347 }
|
Chris@79
|
348
|
Chris@1367
|
349 double
|
Chris@908
|
350 WaveformLayer::dBscale(double sample, int m) const
|
Chris@0
|
351 {
|
Chris@67
|
352 if (sample < 0.0) return dBscale(-sample, m);
|
Chris@908
|
353 double dB = AudioLevel::multiplier_to_dB(sample);
|
Chris@0
|
354 if (dB < -50.0) return 0;
|
Chris@0
|
355 if (dB > 0.0) return m;
|
Chris@1367
|
356 return ((dB + 50.0) * m) / 50.0;
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@805
|
359 int
|
Chris@805
|
360 WaveformLayer::getChannelArrangement(int &min, int &max,
|
Chris@67
|
361 bool &merging, bool &mixing)
|
Chris@0
|
362 const
|
Chris@0
|
363 {
|
Chris@0
|
364 if (!m_model || !m_model->isOK()) return 0;
|
Chris@0
|
365
|
Chris@805
|
366 int channels = m_model->getChannelCount();
|
Chris@0
|
367 if (channels == 0) return 0;
|
Chris@0
|
368
|
Chris@805
|
369 int rawChannels = channels;
|
Chris@0
|
370
|
Chris@0
|
371 if (m_channel == -1) {
|
Chris@1266
|
372 min = 0;
|
Chris@1266
|
373 if (m_channelMode == MergeChannels ||
|
Chris@67
|
374 m_channelMode == MixChannels) {
|
Chris@1266
|
375 max = 0;
|
Chris@1266
|
376 channels = 1;
|
Chris@1266
|
377 } else {
|
Chris@1266
|
378 max = channels - 1;
|
Chris@1266
|
379 }
|
Chris@0
|
380 } else {
|
Chris@1266
|
381 min = m_channel;
|
Chris@1266
|
382 max = m_channel;
|
Chris@1266
|
383 rawChannels = 1;
|
Chris@1266
|
384 channels = 1;
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 merging = (m_channelMode == MergeChannels && rawChannels > 1);
|
Chris@67
|
388 mixing = (m_channelMode == MixChannels && rawChannels > 1);
|
Chris@0
|
389
|
Chris@587
|
390 // SVDEBUG << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << endl;
|
Chris@0
|
391
|
Chris@0
|
392 return channels;
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@67
|
395 bool
|
Chris@918
|
396 WaveformLayer::isLayerScrollable(const LayerGeometryProvider *) const
|
Chris@67
|
397 {
|
Chris@67
|
398 return !m_autoNormalize;
|
Chris@67
|
399 }
|
Chris@67
|
400
|
Chris@68
|
401 static float meterdbs[] = { -40, -30, -20, -15, -10,
|
Chris@68
|
402 -5, -3, -2, -1, -0.5, 0 };
|
Chris@68
|
403
|
Chris@365
|
404 bool
|
Chris@1336
|
405 WaveformLayer::getSourceFramesForX(LayerGeometryProvider *v,
|
Chris@1336
|
406 int x, int modelZoomLevel,
|
Chris@908
|
407 sv_frame_t &f0, sv_frame_t &f1) const
|
Chris@365
|
408 {
|
Chris@908
|
409 sv_frame_t viewFrame = v->getFrameForX(x);
|
Chris@365
|
410 if (viewFrame < 0) {
|
Chris@365
|
411 f0 = 0;
|
Chris@365
|
412 f1 = 0;
|
Chris@365
|
413 return false;
|
Chris@365
|
414 }
|
Chris@365
|
415
|
Chris@365
|
416 f0 = viewFrame;
|
Chris@365
|
417 f0 = f0 / modelZoomLevel;
|
Chris@365
|
418 f0 = f0 * modelZoomLevel;
|
Chris@365
|
419
|
Chris@1341
|
420 if (v->getZoomLevel().zone == ZoomLevel::PixelsPerFrame) {
|
Chris@1341
|
421 f1 = f0 + 1;
|
Chris@1341
|
422 } else {
|
Chris@1341
|
423 viewFrame = v->getFrameForX(x + 1);
|
Chris@1341
|
424 f1 = viewFrame;
|
Chris@1341
|
425 f1 = f1 / modelZoomLevel;
|
Chris@1341
|
426 f1 = f1 * modelZoomLevel;
|
Chris@1341
|
427 }
|
Chris@365
|
428
|
Chris@365
|
429 return (f0 < m_model->getEndFrame());
|
Chris@365
|
430 }
|
Chris@365
|
431
|
Chris@365
|
432 float
|
Chris@918
|
433 WaveformLayer::getNormalizeGain(LayerGeometryProvider *v, int channel) const
|
Chris@365
|
434 {
|
Chris@908
|
435 sv_frame_t startFrame = v->getStartFrame();
|
Chris@908
|
436 sv_frame_t endFrame = v->getEndFrame();
|
Chris@365
|
437
|
Chris@908
|
438 sv_frame_t modelStart = m_model->getStartFrame();
|
Chris@908
|
439 sv_frame_t modelEnd = m_model->getEndFrame();
|
Chris@365
|
440
|
Chris@908
|
441 sv_frame_t rangeStart, rangeEnd;
|
Chris@365
|
442
|
Chris@365
|
443 if (startFrame < modelStart) rangeStart = modelStart;
|
Chris@365
|
444 else rangeStart = startFrame;
|
Chris@365
|
445
|
Chris@365
|
446 if (endFrame < 0) rangeEnd = 0;
|
Chris@365
|
447 else if (endFrame > modelEnd) rangeEnd = modelEnd;
|
Chris@365
|
448 else rangeEnd = endFrame;
|
Chris@365
|
449
|
Chris@365
|
450 if (rangeEnd < rangeStart) rangeEnd = rangeStart;
|
Chris@365
|
451
|
Chris@365
|
452 RangeSummarisableTimeValueModel::Range range =
|
Chris@365
|
453 m_model->getSummary(channel, rangeStart, rangeEnd - rangeStart);
|
Chris@365
|
454
|
Chris@805
|
455 int minChannel = 0, maxChannel = 0;
|
Chris@365
|
456 bool mergingChannels = false, mixingChannels = false;
|
Chris@365
|
457
|
Chris@859
|
458 (void)getChannelArrangement(minChannel, maxChannel,
|
Chris@859
|
459 mergingChannels, mixingChannels);
|
Chris@365
|
460
|
Chris@365
|
461 if (mergingChannels || mixingChannels) {
|
Chris@365
|
462 RangeSummarisableTimeValueModel::Range otherRange =
|
Chris@365
|
463 m_model->getSummary(1, rangeStart, rangeEnd - rangeStart);
|
Chris@386
|
464 range.setMax(std::max(range.max(), otherRange.max()));
|
Chris@386
|
465 range.setMin(std::min(range.min(), otherRange.min()));
|
Chris@386
|
466 range.setAbsmean(std::min(range.absmean(), otherRange.absmean()));
|
Chris@365
|
467 }
|
Chris@365
|
468
|
Chris@908
|
469 return float(1.0 / std::max(fabs(range.max()), fabs(range.min())));
|
Chris@365
|
470 }
|
Chris@365
|
471
|
Chris@0
|
472 void
|
Chris@916
|
473 WaveformLayer::paint(LayerGeometryProvider *v, QPainter &viewPainter, QRect rect) const
|
Chris@0
|
474 {
|
Chris@0
|
475 if (!m_model || !m_model->isOK()) {
|
Chris@1266
|
476 return;
|
Chris@0
|
477 }
|
Chris@0
|
478
|
Chris@1325
|
479 ZoomLevel zoomLevel = v->getZoomLevel();
|
Chris@0
|
480
|
Chris@2
|
481 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@0
|
482 Profiler profiler("WaveformLayer::paint", true);
|
Chris@1338
|
483 SVCERR << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
|
Chris@1266
|
484 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << endl;
|
Chris@2
|
485 #endif
|
Chris@0
|
486
|
Chris@805
|
487 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
488 bool mergingChannels = false, mixingChannels = false;
|
Chris@0
|
489
|
Chris@67
|
490 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
491 mergingChannels, mixingChannels);
|
Chris@0
|
492 if (channels == 0) return;
|
Chris@0
|
493
|
Chris@915
|
494 int w = v->getPaintWidth();
|
Chris@915
|
495 int h = v->getPaintHeight();
|
Chris@0
|
496
|
Chris@0
|
497 QPainter *paint;
|
Chris@0
|
498
|
Chris@0
|
499 if (m_aggressive) {
|
Chris@0
|
500
|
Chris@214
|
501 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
502 SVCERR << "WaveformLayer::paint: aggressive is true" << endl;
|
Chris@214
|
503 #endif
|
Chris@214
|
504
|
Chris@1325
|
505 using namespace std::rel_ops;
|
Chris@1325
|
506
|
Chris@1266
|
507 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
|
Chris@1266
|
508 m_cacheValid = false;
|
Chris@1266
|
509 }
|
Chris@0
|
510
|
Chris@1266
|
511 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
|
Chris@214
|
512 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@214
|
513 if (m_cache) {
|
Chris@1338
|
514 SVCERR << "WaveformLayer::paint: cache size " << m_cache->width() << "x" << m_cache->height() << " differs from view size " << w << "x" << h << ": regenerating aggressive cache" << endl;
|
Chris@214
|
515 }
|
Chris@214
|
516 #endif
|
Chris@1266
|
517 delete m_cache;
|
Chris@1266
|
518 m_cache = new QPixmap(w, h);
|
Chris@214
|
519 m_cacheValid = false;
|
Chris@1266
|
520 }
|
Chris@214
|
521
|
Chris@1266
|
522 if (m_cacheValid) {
|
Chris@1266
|
523 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@1266
|
524 return;
|
Chris@1266
|
525 }
|
Chris@0
|
526
|
Chris@1266
|
527 paint = new QPainter(m_cache);
|
Chris@0
|
528
|
Chris@1266
|
529 paint->setPen(Qt::NoPen);
|
Chris@1266
|
530 paint->setBrush(getBackgroundQColor(v));
|
Chris@1266
|
531 paint->drawRect(rect);
|
Chris@0
|
532
|
Chris@1266
|
533 paint->setPen(getForegroundQColor(v));
|
Chris@1266
|
534 paint->setBrush(Qt::NoBrush);
|
Chris@0
|
535
|
Chris@0
|
536 } else {
|
Chris@1266
|
537 paint = &viewPainter;
|
Chris@0
|
538 }
|
Chris@0
|
539
|
Chris@1367
|
540 paint->setRenderHint(QPainter::Antialiasing, true);
|
Chris@28
|
541
|
Chris@709
|
542 if (m_middleLineHeight != 0.5) {
|
Chris@709
|
543 paint->save();
|
Chris@908
|
544 double space = m_middleLineHeight * 2;
|
Chris@709
|
545 if (space > 1.0) space = 2.0 - space;
|
Chris@908
|
546 double yt = h * (m_middleLineHeight - space/2);
|
Chris@709
|
547 paint->translate(QPointF(0, yt));
|
Chris@709
|
548 paint->scale(1.0, space);
|
Chris@709
|
549 }
|
Chris@709
|
550
|
Chris@0
|
551 int x0 = 0, x1 = w - 1;
|
Chris@0
|
552
|
Chris@0
|
553 x0 = rect.left();
|
Chris@0
|
554 x1 = rect.right();
|
Chris@0
|
555
|
Chris@1334
|
556 if (x0 > 0) {
|
Chris@1334
|
557 rect.adjust(-1, 0, 0, 0);
|
Chris@1334
|
558 x0 = rect.left();
|
Chris@1334
|
559 }
|
Chris@1334
|
560
|
Chris@1334
|
561 if (x1 < w) {
|
Chris@1334
|
562 rect.adjust(0, 0, 1, 0);
|
Chris@1334
|
563 x1 = rect.right();
|
Chris@1334
|
564 }
|
Chris@28
|
565
|
Chris@365
|
566 // Our zoom level may differ from that at which the underlying
|
Chris@365
|
567 // model has its blocks.
|
Chris@302
|
568
|
Chris@365
|
569 // Each pixel within our visible range must always draw from
|
Chris@365
|
570 // exactly the same set of underlying audio frames, no matter what
|
Chris@365
|
571 // the range being drawn is. And that set of underlying frames
|
Chris@365
|
572 // must remain the same when we scroll one or more pixels left or
|
Chris@365
|
573 // right.
|
Chris@1325
|
574
|
Chris@1325
|
575 int desiredBlockSize = 1;
|
Chris@1325
|
576 if (zoomLevel.zone == ZoomLevel::FramesPerPixel) {
|
Chris@1325
|
577 desiredBlockSize = zoomLevel.level;
|
Chris@1325
|
578 }
|
Chris@1325
|
579 int blockSize = m_model->getSummaryBlockSize(desiredBlockSize);
|
Chris@365
|
580
|
Chris@908
|
581 sv_frame_t frame0;
|
Chris@908
|
582 sv_frame_t frame1;
|
Chris@908
|
583 sv_frame_t spare;
|
Chris@365
|
584
|
Chris@1325
|
585 getSourceFramesForX(v, x0, blockSize, frame0, spare);
|
Chris@1325
|
586 getSourceFramesForX(v, x1, blockSize, spare, frame1);
|
Chris@365
|
587
|
Chris@4
|
588 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
589 SVCERR << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << " and model zoom " << blockSize << ")" << endl;
|
Chris@4
|
590 #endif
|
Chris@0
|
591
|
Chris@1338
|
592 m_effectiveGains.clear();
|
Chris@805
|
593 while ((int)m_effectiveGains.size() <= maxChannel) {
|
Chris@67
|
594 m_effectiveGains.push_back(m_gain);
|
Chris@67
|
595 }
|
Chris@1337
|
596 if (m_autoNormalize) {
|
Chris@1337
|
597 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@1337
|
598 m_effectiveGains[ch] = getNormalizeGain(v, ch);
|
Chris@1337
|
599 }
|
Chris@1336
|
600 }
|
Chris@67
|
601
|
Chris@1338
|
602 RangeVec ranges;
|
Chris@1338
|
603
|
Chris@1336
|
604 if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) {
|
Chris@1338
|
605 getSummaryRanges(minChannel, maxChannel,
|
Chris@1338
|
606 mixingChannels || mergingChannels,
|
Chris@1338
|
607 frame0, frame1,
|
Chris@1338
|
608 blockSize, ranges);
|
Chris@1338
|
609 } else {
|
Chris@1338
|
610 getOversampledRanges(minChannel, maxChannel,
|
Chris@1338
|
611 mixingChannels || mergingChannels,
|
Chris@1338
|
612 frame0, frame1,
|
Chris@1338
|
613 v->getZoomLevel().level, ranges);
|
Chris@1338
|
614 }
|
Chris@1333
|
615
|
Chris@1343
|
616 if (!ranges.empty()) {
|
Chris@1343
|
617 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@1343
|
618 paintChannel(v, paint, rect, ch, ranges, blockSize,
|
Chris@1343
|
619 frame0, frame1);
|
Chris@1343
|
620 }
|
Chris@1333
|
621 }
|
Chris@1333
|
622
|
Chris@709
|
623 if (m_middleLineHeight != 0.5) {
|
Chris@709
|
624 paint->restore();
|
Chris@709
|
625 }
|
Chris@709
|
626
|
Chris@0
|
627 if (m_aggressive) {
|
Chris@1332
|
628 if (m_model->isReady() && rect == v->getPaintRect()) {
|
Chris@1266
|
629 m_cacheValid = true;
|
Chris@1266
|
630 m_cacheZoomLevel = zoomLevel;
|
Chris@1266
|
631 }
|
Chris@1266
|
632 paint->end();
|
Chris@1266
|
633 delete paint;
|
Chris@1266
|
634 viewPainter.drawPixmap(rect, *m_cache, rect);
|
Chris@0
|
635 }
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@1332
|
638 void
|
Chris@1338
|
639 WaveformLayer::getSummaryRanges(int minChannel, int maxChannel,
|
Chris@1338
|
640 bool mixingOrMerging,
|
Chris@1338
|
641 sv_frame_t frame0, sv_frame_t frame1,
|
Chris@1338
|
642 int blockSize, RangeVec &ranges)
|
Chris@1338
|
643 const
|
Chris@1338
|
644 {
|
Chris@1338
|
645 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@1338
|
646 ranges.push_back({});
|
Chris@1338
|
647 m_model->getSummaries(ch, frame0, frame1 - frame0,
|
Chris@1338
|
648 ranges[ch - minChannel], blockSize);
|
Chris@1338
|
649 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
650 SVCERR << "channel " << ch << ": " << ranges[ch - minChannel].size() << " ranges from " << frame0 << " to " << frame1 << " at zoom level " << blockSize << endl;
|
Chris@1338
|
651 #endif
|
Chris@1338
|
652 }
|
Chris@1338
|
653
|
Chris@1338
|
654 if (mixingOrMerging) {
|
Chris@1338
|
655 if (minChannel != 0 || maxChannel != 0) {
|
Chris@1366
|
656 throw std::logic_error("Internal error: min & max channels should be 0 when merging or mixing all channels");
|
Chris@1338
|
657 } else if (m_model->getChannelCount() > 1) {
|
Chris@1338
|
658 ranges.push_back({});
|
Chris@1338
|
659 m_model->getSummaries
|
Chris@1338
|
660 (1, frame0, frame1 - frame0, ranges[1], blockSize);
|
Chris@1338
|
661 }
|
Chris@1338
|
662 }
|
Chris@1338
|
663 }
|
Chris@1338
|
664
|
Chris@1338
|
665 void
|
Chris@1338
|
666 WaveformLayer::getOversampledRanges(int minChannel, int maxChannel,
|
Chris@1366
|
667 bool mixingOrMerging,
|
Chris@1338
|
668 sv_frame_t frame0, sv_frame_t frame1,
|
Chris@1338
|
669 int oversampleBy, RangeVec &ranges)
|
Chris@1338
|
670 const
|
Chris@1338
|
671 {
|
Chris@1366
|
672 if (mixingOrMerging) {
|
Chris@1366
|
673 if (minChannel != 0 || maxChannel != 0) {
|
Chris@1366
|
674 throw std::logic_error("Internal error: min & max channels should be 0 when merging or mixing all channels");
|
Chris@1366
|
675 }
|
Chris@1366
|
676 if (m_model->getChannelCount() > 1) {
|
Chris@1366
|
677 // call back on self for the individual channels with
|
Chris@1366
|
678 // mixingOrMerging false
|
Chris@1366
|
679 getOversampledRanges
|
Chris@1366
|
680 (0, 1, false, frame0, frame1, oversampleBy, ranges);
|
Chris@1366
|
681 return;
|
Chris@1366
|
682 }
|
Chris@1366
|
683 }
|
Chris@1366
|
684
|
Chris@1338
|
685 // These frame values, tail length, etc variables are at the model
|
Chris@1338
|
686 // sample rate, not the oversampled rate
|
Chris@1338
|
687
|
Chris@1338
|
688 sv_frame_t tail = 16;
|
Chris@1338
|
689 sv_frame_t startFrame = m_model->getStartFrame();
|
Chris@1338
|
690 sv_frame_t endFrame = m_model->getEndFrame();
|
Chris@1338
|
691
|
Chris@1338
|
692 sv_frame_t rf0 = frame0 - tail;
|
Chris@1338
|
693 if (rf0 < startFrame) {
|
Chris@1338
|
694 rf0 = 0;
|
Chris@1338
|
695 }
|
Chris@1338
|
696
|
Chris@1338
|
697 sv_frame_t rf1 = frame1 + tail;
|
Chris@1338
|
698 if (rf1 >= endFrame) {
|
Chris@1338
|
699 rf1 = endFrame - 1;
|
Chris@1338
|
700 }
|
Chris@1338
|
701 if (rf1 <= rf0) {
|
Chris@1338
|
702 SVCERR << "WARNING: getOversampledRanges: rf1 (" << rf1 << ") <= rf0 ("
|
Chris@1338
|
703 << rf0 << ")" << endl;
|
Chris@1338
|
704 return;
|
Chris@1338
|
705 }
|
Chris@1338
|
706
|
Chris@1338
|
707 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@1339
|
708 floatvec_t oversampled = WaveformOversampler::getOversampledData
|
Chris@1339
|
709 (m_model, ch, frame0, frame1 - frame0, oversampleBy);
|
Chris@1338
|
710 RangeSummarisableTimeValueModel::RangeBlock rr;
|
Chris@1339
|
711 for (float v: oversampled) {
|
Chris@1338
|
712 RangeSummarisableTimeValueModel::Range r;
|
Chris@1339
|
713 r.sample(v);
|
Chris@1338
|
714 rr.push_back(r);
|
Chris@1338
|
715 }
|
Chris@1338
|
716 ranges.push_back(rr);
|
Chris@1338
|
717
|
Chris@1338
|
718 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
719 SVCERR << "getOversampledRanges: " << frame0 << " -> " << frame1
|
Chris@1338
|
720 << " (" << frame1 - frame0 << "-frame range) at ratio "
|
Chris@1338
|
721 << oversampleBy << " with tail " << tail
|
Chris@1338
|
722 << " -> got " << oversampled.size()
|
Chris@1338
|
723 << " oversampled values for channel " << ch
|
Chris@1338
|
724 << ", from which returning " << rr.size() << " ranges" << endl;
|
Chris@1338
|
725 #endif
|
Chris@1338
|
726 }
|
Chris@1338
|
727
|
Chris@1338
|
728 return;
|
Chris@1338
|
729 }
|
Chris@1338
|
730
|
Chris@1338
|
731 void
|
Chris@1338
|
732 WaveformLayer::paintChannel(LayerGeometryProvider *v,
|
Chris@1338
|
733 QPainter *paint,
|
Chris@1338
|
734 QRect rect, int ch,
|
Chris@1338
|
735 const RangeVec &ranges,
|
Chris@1338
|
736 int blockSize,
|
Chris@1352
|
737 sv_frame_t frame0,
|
Chris@1352
|
738 sv_frame_t frame1)
|
Chris@1332
|
739 const
|
Chris@1332
|
740 {
|
Chris@1334
|
741 int x0 = rect.left();
|
Chris@1334
|
742 int y0 = rect.top();
|
Chris@1334
|
743
|
Chris@1334
|
744 int x1 = rect.right();
|
Chris@1334
|
745 int y1 = rect.bottom();
|
Chris@1334
|
746
|
Chris@1332
|
747 int h = v->getPaintHeight();
|
Chris@1332
|
748
|
Chris@1332
|
749 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@1332
|
750 bool mergingChannels = false, mixingChannels = false;
|
Chris@1332
|
751
|
Chris@1332
|
752 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@1332
|
753 mergingChannels, mixingChannels);
|
Chris@1332
|
754 if (channels == 0) return;
|
Chris@1332
|
755
|
Chris@1332
|
756 QColor baseColour = getBaseQColor();
|
Chris@1332
|
757 QColor midColour = baseColour;
|
Chris@1367
|
758
|
Chris@1332
|
759 if (midColour == Qt::black) {
|
Chris@1332
|
760 midColour = Qt::gray;
|
Chris@1332
|
761 } else if (v->hasLightBackground()) {
|
Chris@1332
|
762 midColour = midColour.light(150);
|
Chris@1332
|
763 } else {
|
Chris@1332
|
764 midColour = midColour.light(50);
|
Chris@1332
|
765 }
|
Chris@1332
|
766
|
Chris@1332
|
767 double gain = m_effectiveGains[ch];
|
Chris@1332
|
768
|
Chris@1332
|
769 int m = (h / channels) / 2;
|
Chris@1332
|
770 int my = m + (((ch - minChannel) * h) / channels);
|
Chris@1332
|
771
|
Chris@1332
|
772 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
773 SVCERR << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << endl;
|
Chris@1332
|
774 #endif
|
Chris@1332
|
775
|
Chris@1332
|
776 if (my - m > y1 || my + m < y0) return;
|
Chris@1332
|
777
|
Chris@1332
|
778 if ((m_scale == dBScale || m_scale == MeterScale) &&
|
Chris@1332
|
779 m_channelMode != MergeChannels) {
|
Chris@1332
|
780 m = (h / channels);
|
Chris@1332
|
781 my = m + (((ch - minChannel) * h) / channels);
|
Chris@1332
|
782 }
|
Chris@1332
|
783
|
Chris@1367
|
784 // Horizontal axis along middle
|
Chris@1367
|
785 paint->setPen(QPen(midColour, 0));
|
Chris@1332
|
786 paint->drawLine(x0, my, x1, my);
|
Chris@1332
|
787
|
Chris@1335
|
788 paintChannelScaleGuides(v, paint, rect, ch);
|
Chris@1332
|
789
|
Chris@1333
|
790 int rangeix = ch - minChannel;
|
Chris@1338
|
791
|
Chris@1338
|
792 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
793 SVCERR << "paint channel " << ch << ": frame0 = " << frame0 << ", frame1 = " << frame1 << ", blockSize = " << blockSize << ", have " << ranges.size() << " range blocks of which ours is index " << rangeix << " with " << ranges[rangeix].size() << " ranges in it" << endl;
|
Chris@1352
|
794 #else
|
Chris@1352
|
795 (void)frame1; // not actually used
|
Chris@1338
|
796 #endif
|
Chris@1338
|
797
|
Chris@1367
|
798 QPainterPath waveformPath;
|
Chris@1367
|
799 QPainterPath meanPath;
|
Chris@1367
|
800 QPainterPath clipPath;
|
Chris@1367
|
801 vector<QPointF> individualSamplePoints;
|
Chris@1367
|
802
|
Chris@1367
|
803 bool firstPoint = true;
|
Chris@1367
|
804
|
Chris@1332
|
805 for (int x = x0; x <= x1; ++x) {
|
Chris@1332
|
806
|
Chris@1332
|
807 sv_frame_t f0, f1;
|
Chris@1338
|
808 sv_frame_t i0, i1;
|
Chris@1338
|
809
|
Chris@1338
|
810 bool showIndividualSample = false;
|
Chris@1338
|
811
|
Chris@1338
|
812 if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) {
|
Chris@1338
|
813 if (!getSourceFramesForX(v, x, blockSize, f0, f1)) {
|
Chris@1338
|
814 continue;
|
Chris@1338
|
815 }
|
Chris@1338
|
816 f1 = f1 - 1;
|
Chris@1338
|
817 i0 = (f0 - frame0) / blockSize;
|
Chris@1338
|
818 i1 = (f1 - frame0) / blockSize;
|
Chris@1338
|
819 } else {
|
Chris@1338
|
820 int oversampleBy = v->getZoomLevel().level;
|
Chris@1338
|
821 f0 = f1 = v->getFrameForX(x);
|
Chris@1338
|
822 int xf0 = v->getXForFrame(f0);
|
Chris@1338
|
823 showIndividualSample = (x == xf0);
|
Chris@1338
|
824 i0 = i1 = (f0 - frame0) * oversampleBy + (x - xf0);
|
Chris@1338
|
825 }
|
Chris@1332
|
826
|
Chris@1332
|
827 if (f0 < frame0) {
|
Chris@1338
|
828 SVCERR << "ERROR: WaveformLayer::paint: pixel " << x << " has f0 = " << f0 << " which is less than range frame0 " << frame0 << " for x0 = " << x0 << endl;
|
Chris@1332
|
829 continue;
|
Chris@1332
|
830 }
|
Chris@1332
|
831
|
Chris@1338
|
832 #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL
|
Chris@1338
|
833 SVCERR << "WaveformLayer::paint: pixel " << x << ": i0 " << i0 << " (f " << f0 << "), i1 " << i1 << " (f " << f1 << ")" << endl;
|
Chris@1332
|
834 #endif
|
Chris@1332
|
835
|
Chris@1332
|
836 if (i1 > i0 + 1) {
|
Chris@1338
|
837 SVCERR << "WaveformLayer::paint: ERROR: i1 " << i1 << " > i0 " << i0 << " plus one (zoom = " << v->getZoomLevel() << ", model zoom = " << blockSize << ")" << endl;
|
Chris@1332
|
838 }
|
Chris@1332
|
839
|
Chris@1333
|
840 const auto &r = ranges[rangeix];
|
Chris@1333
|
841 RangeSummarisableTimeValueModel::Range range;
|
Chris@1333
|
842
|
Chris@1333
|
843 if (in_range_for(r, i0)) {
|
Chris@1332
|
844
|
Chris@1333
|
845 range = r[i0];
|
Chris@1332
|
846
|
Chris@1333
|
847 if (i1 > i0 && in_range_for(r, i1)) {
|
Chris@1333
|
848 range.setMax(std::max(range.max(), r[i1].max()));
|
Chris@1333
|
849 range.setMin(std::min(range.min(), r[i1].min()));
|
Chris@1333
|
850 range.setAbsmean((range.absmean() + r[i1].absmean()) / 2);
|
Chris@1332
|
851 }
|
Chris@1332
|
852
|
Chris@1332
|
853 } else {
|
Chris@1332
|
854 #ifdef DEBUG_WAVEFORM_PAINT
|
Chris@1338
|
855 SVCERR << "No (or not enough) ranges for index i0 = " << i0 << " (there are " << r.size() << " range(s))" << endl;
|
Chris@1332
|
856 #endif
|
Chris@1332
|
857 continue;
|
Chris@1332
|
858 }
|
Chris@1332
|
859
|
Chris@1367
|
860 double rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
|
Chris@1332
|
861
|
Chris@1333
|
862 if (mergingChannels && ranges.size() > 1) {
|
Chris@1332
|
863
|
Chris@1333
|
864 const auto &other = ranges[1];
|
Chris@1333
|
865
|
Chris@1333
|
866 if (in_range_for(other, i0)) {
|
Chris@1332
|
867
|
Chris@1332
|
868 range.setMax(fabsf(range.max()));
|
Chris@1333
|
869 range.setMin(-fabsf(other[i0].max()));
|
Chris@1332
|
870 range.setAbsmean
|
Chris@1333
|
871 ((range.absmean() + other[i0].absmean()) / 2);
|
Chris@1332
|
872
|
Chris@1333
|
873 if (i1 > i0 && in_range_for(other, i1)) {
|
Chris@1332
|
874 // let's not concern ourselves about the mean
|
Chris@1333
|
875 range.setMin(std::min(range.min(),
|
Chris@1333
|
876 -fabsf(other[i1].max())));
|
Chris@1332
|
877 }
|
Chris@1332
|
878 }
|
Chris@1332
|
879
|
Chris@1333
|
880 } else if (mixingChannels && ranges.size() > 1) {
|
Chris@1332
|
881
|
Chris@1333
|
882 const auto &other = ranges[1];
|
Chris@1333
|
883
|
Chris@1333
|
884 if (in_range_for(other, i0)) {
|
Chris@1332
|
885
|
Chris@1333
|
886 range.setMax((range.max() + other[i0].max()) / 2);
|
Chris@1333
|
887 range.setMin((range.min() + other[i0].min()) / 2);
|
Chris@1333
|
888 range.setAbsmean((range.absmean() + other[i0].absmean()) / 2);
|
Chris@1332
|
889 }
|
Chris@1332
|
890 }
|
Chris@1332
|
891
|
Chris@1332
|
892 switch (m_scale) {
|
Chris@1332
|
893
|
Chris@1332
|
894 case LinearScale:
|
Chris@1367
|
895 rangeBottom = range.min() * gain * m;
|
Chris@1367
|
896 rangeTop = range.max() * gain * m;
|
Chris@1367
|
897 meanBottom = range.absmean() * gain * (-m);
|
Chris@1367
|
898 meanTop = range.absmean() * gain * m;
|
Chris@1332
|
899 break;
|
Chris@1332
|
900
|
Chris@1332
|
901 case dBScale:
|
Chris@1332
|
902 if (!mergingChannels) {
|
Chris@1367
|
903 double db0 = dBscale(range.min() * gain, m);
|
Chris@1367
|
904 double db1 = dBscale(range.max() * gain, m);
|
Chris@1367
|
905 rangeTop = std::max(db0, db1);
|
Chris@1367
|
906 meanTop = std::min(db0, db1);
|
Chris@1332
|
907 if (mixingChannels) rangeBottom = meanTop;
|
Chris@1332
|
908 else rangeBottom = dBscale(range.absmean() * gain, m);
|
Chris@1367
|
909 meanBottom = rangeBottom;
|
Chris@1332
|
910 } else {
|
Chris@1367
|
911 rangeBottom = -dBscale(range.min() * gain, m);
|
Chris@1367
|
912 rangeTop = dBscale(range.max() * gain, m);
|
Chris@1367
|
913 meanBottom = -dBscale(range.absmean() * gain, m);
|
Chris@1367
|
914 meanTop = dBscale(range.absmean() * gain, m);
|
Chris@1332
|
915 }
|
Chris@1332
|
916 break;
|
Chris@1332
|
917
|
Chris@1332
|
918 case MeterScale:
|
Chris@1332
|
919 if (!mergingChannels) {
|
Chris@1367
|
920 double r0 = fabs(AudioLevel::multiplier_to_preview
|
Chris@1367
|
921 (range.min() * gain, m));
|
Chris@1367
|
922 double r1 = fabs(AudioLevel::multiplier_to_preview
|
Chris@1367
|
923 (range.max() * gain, m));
|
Chris@1367
|
924 rangeTop = std::max(r0, r1);
|
Chris@1367
|
925 meanTop = std::min(r0, r1);
|
Chris@1332
|
926 if (mixingChannels) rangeBottom = meanTop;
|
Chris@1367
|
927 else rangeBottom = AudioLevel::multiplier_to_preview
|
Chris@1367
|
928 (range.absmean() * gain, m);
|
Chris@1332
|
929 meanBottom = rangeBottom;
|
Chris@1332
|
930 } else {
|
Chris@1367
|
931 rangeBottom = -AudioLevel::multiplier_to_preview
|
Chris@1367
|
932 (range.min() * gain, m);
|
Chris@1367
|
933 rangeTop = AudioLevel::multiplier_to_preview
|
Chris@1367
|
934 (range.max() * gain, m);
|
Chris@1367
|
935 meanBottom = -AudioLevel::multiplier_to_preview
|
Chris@1367
|
936 (range.absmean() * gain, m);
|
Chris@1367
|
937 meanTop = AudioLevel::multiplier_to_preview
|
Chris@1367
|
938 (range.absmean() * gain, m);
|
Chris@1332
|
939 }
|
Chris@1332
|
940 break;
|
Chris@1332
|
941 }
|
Chris@1332
|
942
|
Chris@1367
|
943 rangeBottom = my - rangeBottom;
|
Chris@1367
|
944 rangeTop = my - rangeTop;
|
Chris@1367
|
945 meanBottom = my - meanBottom;
|
Chris@1367
|
946 meanTop = my - meanTop;
|
Chris@1332
|
947
|
Chris@1332
|
948 bool clipped = false;
|
Chris@1332
|
949
|
Chris@1332
|
950 if (rangeTop < my - m) { rangeTop = my - m; }
|
Chris@1332
|
951 if (rangeTop > my + m) { rangeTop = my + m; }
|
Chris@1332
|
952 if (rangeBottom < my - m) { rangeBottom = my - m; }
|
Chris@1332
|
953 if (rangeBottom > my + m) { rangeBottom = my + m; }
|
Chris@1332
|
954
|
Chris@1367
|
955 if (range.max() <= -1.0 || range.max() >= 1.0) {
|
Chris@1367
|
956 clipped = true;
|
Chris@1367
|
957 }
|
Chris@1332
|
958
|
Chris@1367
|
959 bool drawMean = m_showMeans;
|
Chris@1332
|
960
|
Chris@1367
|
961 meanTop = meanTop - 0.5;
|
Chris@1367
|
962 meanBottom = meanBottom + 0.5;
|
Chris@1367
|
963
|
Chris@1367
|
964 if (meanTop <= rangeTop + 1.0) {
|
Chris@1367
|
965 meanTop = rangeTop + 1.0;
|
Chris@1332
|
966 }
|
Chris@1367
|
967 if (meanBottom >= rangeBottom - 1.0 && m_scale == LinearScale) {
|
Chris@1367
|
968 meanBottom = rangeBottom - 1.0;
|
Chris@1332
|
969 }
|
Chris@1367
|
970 if (meanTop > meanBottom - 1.0) {
|
Chris@1367
|
971 drawMean = false;
|
Chris@1332
|
972 }
|
Chris@1332
|
973
|
Chris@1338
|
974 #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL
|
Chris@1338
|
975 SVCERR << "range " << rangeBottom << " -> " << rangeTop << ", means " << meanBottom << " -> " << meanTop << ", raw range " << range.min() << " -> " << range.max() << endl;
|
Chris@1332
|
976 #endif
|
Chris@1332
|
977
|
Chris@1367
|
978 double rangeMiddle = (rangeTop + rangeBottom) / 2.0;
|
Chris@1367
|
979 bool trivialRange = (fabs(rangeTop - rangeBottom) < 1.0);
|
Chris@1367
|
980 double px = x + 0.5;
|
Chris@1367
|
981
|
Chris@1367
|
982 if (showIndividualSample) {
|
Chris@1367
|
983 individualSamplePoints.push_back(QPointF(px, rangeTop));
|
Chris@1367
|
984 if (!trivialRange) {
|
Chris@1367
|
985 // common e.g. in "butterfly" merging mode
|
Chris@1367
|
986 individualSamplePoints.push_back(QPointF(px, rangeBottom));
|
Chris@1332
|
987 }
|
Chris@1332
|
988 }
|
Chris@1332
|
989
|
Chris@1367
|
990 if (firstPoint) {
|
Chris@1367
|
991 waveformPath = QPainterPath(QPointF(px, rangeMiddle));
|
Chris@1367
|
992 firstPoint = false;
|
Chris@1367
|
993 } else {
|
Chris@1367
|
994 waveformPath.lineTo(QPointF(px, rangeMiddle));
|
Chris@1367
|
995 }
|
Chris@1367
|
996
|
Chris@1367
|
997 if (!trivialRange) {
|
Chris@1367
|
998 waveformPath.lineTo(QPointF(px, rangeTop));
|
Chris@1367
|
999 waveformPath.lineTo(QPointF(px, rangeBottom));
|
Chris@1367
|
1000 waveformPath.lineTo(QPointF(px, rangeMiddle));
|
Chris@1367
|
1001 }
|
Chris@1367
|
1002
|
Chris@1332
|
1003 if (drawMean) {
|
Chris@1367
|
1004 meanPath.moveTo(QPointF(px, meanBottom));
|
Chris@1367
|
1005 meanPath.lineTo(QPointF(px, meanTop));
|
Chris@1332
|
1006 }
|
Chris@1367
|
1007
|
Chris@1367
|
1008 if (clipped) {
|
Chris@1367
|
1009 if (trivialRange) {
|
Chris@1367
|
1010 clipPath.moveTo(QPointF(px, rangeMiddle));
|
Chris@1367
|
1011 clipPath.lineTo(QPointF(px+1, rangeMiddle));
|
Chris@1367
|
1012 } else {
|
Chris@1367
|
1013 clipPath.moveTo(QPointF(px, rangeBottom));
|
Chris@1367
|
1014 clipPath.lineTo(QPointF(px, rangeTop));
|
Chris@1367
|
1015 }
|
Chris@1367
|
1016 }
|
Chris@1367
|
1017 }
|
Chris@1367
|
1018
|
Chris@1367
|
1019 double penWidth = 1.0;
|
Chris@1367
|
1020
|
Chris@1367
|
1021 if (m_model->isReady()) {
|
Chris@1367
|
1022 paint->setPen(QPen(baseColour, penWidth));
|
Chris@1367
|
1023 } else {
|
Chris@1367
|
1024 paint->setPen(QPen(midColour, penWidth));
|
Chris@1367
|
1025 }
|
Chris@1367
|
1026 paint->drawPath(waveformPath);
|
Chris@1367
|
1027
|
Chris@1367
|
1028 if (!clipPath.isEmpty()) {
|
Chris@1367
|
1029 paint->save();
|
Chris@1367
|
1030 paint->setPen(QPen(ColourDatabase::getInstance()->
|
Chris@1367
|
1031 getContrastingColour(m_colour), penWidth));
|
Chris@1367
|
1032 paint->drawPath(clipPath);
|
Chris@1367
|
1033 paint->restore();
|
Chris@1367
|
1034 }
|
Chris@1367
|
1035
|
Chris@1367
|
1036 if (!meanPath.isEmpty()) {
|
Chris@1367
|
1037 paint->save();
|
Chris@1367
|
1038 paint->setPen(QPen(midColour, penWidth));
|
Chris@1367
|
1039 paint->drawPath(meanPath);
|
Chris@1367
|
1040 paint->restore();
|
Chris@1367
|
1041 }
|
Chris@1367
|
1042
|
Chris@1367
|
1043 if (!individualSamplePoints.empty()) {
|
Chris@1371
|
1044 double sz = ViewManager::scalePixelSize(2.0);
|
Chris@1371
|
1045 if (v->getZoomLevel().zone == ZoomLevel::PixelsPerFrame) {
|
Chris@1371
|
1046 if (v->getZoomLevel().level < 10) {
|
Chris@1371
|
1047 sz = 1.5;
|
Chris@1371
|
1048 }
|
Chris@1371
|
1049 }
|
Chris@1367
|
1050 paint->save();
|
Chris@1367
|
1051 paint->setPen(QPen(baseColour, penWidth));
|
Chris@1367
|
1052 for (QPointF p: individualSamplePoints) {
|
Chris@1367
|
1053 paint->drawRect(QRectF(p.x() - sz/2, p.y() - sz/2, sz, sz));
|
Chris@1367
|
1054 }
|
Chris@1367
|
1055 paint->restore();
|
Chris@1332
|
1056 }
|
Chris@1332
|
1057 }
|
Chris@1332
|
1058
|
Chris@1335
|
1059 void
|
Chris@1335
|
1060 WaveformLayer::paintChannelScaleGuides(LayerGeometryProvider *v,
|
Chris@1335
|
1061 QPainter *paint,
|
Chris@1335
|
1062 QRect rect,
|
Chris@1335
|
1063 int ch) const
|
Chris@1335
|
1064 {
|
Chris@1335
|
1065 int x0 = rect.left();
|
Chris@1335
|
1066 int x1 = rect.right();
|
Chris@1335
|
1067
|
Chris@1335
|
1068 int n = 10;
|
Chris@1335
|
1069 int py = -1;
|
Chris@1335
|
1070
|
Chris@1335
|
1071 double gain = m_effectiveGains[ch];
|
Chris@1335
|
1072
|
Chris@1335
|
1073 if (v->hasLightBackground() &&
|
Chris@1335
|
1074 v->getViewManager() &&
|
Chris@1335
|
1075 v->getViewManager()->shouldShowScaleGuides()) {
|
Chris@1335
|
1076
|
Chris@1335
|
1077 paint->setPen(QColor(240, 240, 240));
|
Chris@1335
|
1078
|
Chris@1335
|
1079 for (int i = 1; i < n; ++i) {
|
Chris@1335
|
1080
|
Chris@1335
|
1081 double val = 0.0, nval = 0.0;
|
Chris@1335
|
1082
|
Chris@1335
|
1083 switch (m_scale) {
|
Chris@1335
|
1084
|
Chris@1335
|
1085 case LinearScale:
|
Chris@1335
|
1086 val = (i * gain) / n;
|
Chris@1335
|
1087 if (i > 0) nval = -val;
|
Chris@1335
|
1088 break;
|
Chris@1335
|
1089
|
Chris@1335
|
1090 case MeterScale:
|
Chris@1335
|
1091 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
|
Chris@1335
|
1092 break;
|
Chris@1335
|
1093
|
Chris@1335
|
1094 case dBScale:
|
Chris@1335
|
1095 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
|
Chris@1335
|
1096 break;
|
Chris@1335
|
1097 }
|
Chris@1335
|
1098
|
Chris@1335
|
1099 if (val < -1.0 || val > 1.0) continue;
|
Chris@1335
|
1100
|
Chris@1335
|
1101 int y = getYForValue(v, val, ch);
|
Chris@1335
|
1102
|
Chris@1335
|
1103 if (py >= 0 && abs(y - py) < 10) continue;
|
Chris@1335
|
1104 else py = y;
|
Chris@1335
|
1105
|
Chris@1335
|
1106 int ny = y;
|
Chris@1335
|
1107 if (nval != 0.0) {
|
Chris@1335
|
1108 ny = getYForValue(v, nval, ch);
|
Chris@1335
|
1109 }
|
Chris@1335
|
1110
|
Chris@1335
|
1111 paint->drawLine(x0, y, x1, y);
|
Chris@1335
|
1112 if (ny != y) {
|
Chris@1335
|
1113 paint->drawLine(x0, ny, x1, ny);
|
Chris@1335
|
1114 }
|
Chris@1335
|
1115 }
|
Chris@1335
|
1116 }
|
Chris@1335
|
1117 }
|
Chris@1335
|
1118
|
Chris@25
|
1119 QString
|
Chris@918
|
1120 WaveformLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
|
Chris@25
|
1121 {
|
Chris@25
|
1122 int x = pos.x();
|
Chris@25
|
1123
|
Chris@25
|
1124 if (!m_model || !m_model->isOK()) return "";
|
Chris@25
|
1125
|
Chris@1325
|
1126 ZoomLevel zoomLevel = v->getZoomLevel();
|
Chris@25
|
1127
|
Chris@1325
|
1128 int desiredBlockSize = 1;
|
Chris@1325
|
1129 if (zoomLevel.zone == ZoomLevel::FramesPerPixel) {
|
Chris@1325
|
1130 desiredBlockSize = zoomLevel.level;
|
Chris@1325
|
1131 }
|
Chris@1325
|
1132
|
Chris@1325
|
1133 int blockSize = m_model->getSummaryBlockSize(desiredBlockSize);
|
Chris@365
|
1134
|
Chris@908
|
1135 sv_frame_t f0, f1;
|
Chris@1325
|
1136 if (!getSourceFramesForX(v, x, blockSize, f0, f1)) return "";
|
Chris@25
|
1137
|
Chris@25
|
1138 QString text;
|
Chris@25
|
1139
|
Chris@25
|
1140 RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate());
|
Chris@25
|
1141 RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate());
|
Chris@25
|
1142
|
Chris@25
|
1143 if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) {
|
Chris@1266
|
1144 text += tr("Time:\t%1 - %2")
|
Chris@1266
|
1145 .arg(rt0.toText(true).c_str())
|
Chris@1266
|
1146 .arg(rt1.toText(true).c_str());
|
Chris@25
|
1147 } else {
|
Chris@1266
|
1148 text += tr("Time:\t%1")
|
Chris@1266
|
1149 .arg(rt0.toText(true).c_str());
|
Chris@25
|
1150 }
|
Chris@25
|
1151
|
Chris@805
|
1152 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
1153 bool mergingChannels = false, mixingChannels = false;
|
Chris@25
|
1154
|
Chris@67
|
1155 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
1156 mergingChannels, mixingChannels);
|
Chris@25
|
1157 if (channels == 0) return "";
|
Chris@25
|
1158
|
Chris@805
|
1159 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@25
|
1160
|
Chris@1266
|
1161 RangeSummarisableTimeValueModel::RangeBlock ranges;
|
Chris@302
|
1162 m_model->getSummaries(ch, f0, f1 - f0, ranges, blockSize);
|
Chris@25
|
1163
|
Chris@1266
|
1164 if (ranges.empty()) continue;
|
Chris@1266
|
1165
|
Chris@1266
|
1166 RangeSummarisableTimeValueModel::Range range = ranges[0];
|
Chris@1266
|
1167
|
Chris@1266
|
1168 QString label = tr("Level:");
|
Chris@1266
|
1169 if (minChannel != maxChannel) {
|
Chris@1266
|
1170 if (ch == 0) label = tr("Left:");
|
Chris@1266
|
1171 else if (ch == 1) label = tr("Right:");
|
Chris@1266
|
1172 else label = tr("Channel %1").arg(ch + 1);
|
Chris@1266
|
1173 }
|
Chris@25
|
1174
|
Chris@76
|
1175 bool singleValue = false;
|
Chris@908
|
1176 double min, max;
|
Chris@76
|
1177
|
Chris@386
|
1178 if (fabs(range.min()) < 0.01) {
|
Chris@386
|
1179 min = range.min();
|
Chris@386
|
1180 max = range.max();
|
Chris@76
|
1181 singleValue = (min == max);
|
Chris@76
|
1182 } else {
|
Chris@908
|
1183 int imin = int(lrint(range.min() * 10000));
|
Chris@908
|
1184 int imax = int(lrint(range.max() * 10000));
|
Chris@76
|
1185 singleValue = (imin == imax);
|
Chris@908
|
1186 min = double(imin)/10000;
|
Chris@908
|
1187 max = double(imax)/10000;
|
Chris@76
|
1188 }
|
Chris@76
|
1189
|
Chris@1266
|
1190 int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min()),
|
Chris@1266
|
1191 fabsf(range.max())))
|
Chris@1266
|
1192 * 100);
|
Chris@25
|
1193
|
Chris@1266
|
1194 if (!singleValue) {
|
Chris@1266
|
1195 text += tr("\n%1\t%2 - %3 (%4 dB peak)")
|
Chris@1266
|
1196 .arg(label).arg(min).arg(max).arg(double(db)/100);
|
Chris@1266
|
1197 } else {
|
Chris@1266
|
1198 text += tr("\n%1\t%2 (%3 dB peak)")
|
Chris@1266
|
1199 .arg(label).arg(min).arg(double(db)/100);
|
Chris@1266
|
1200 }
|
Chris@25
|
1201 }
|
Chris@25
|
1202
|
Chris@25
|
1203 return text;
|
Chris@25
|
1204 }
|
Chris@25
|
1205
|
Chris@0
|
1206 int
|
Chris@918
|
1207 WaveformLayer::getYForValue(const LayerGeometryProvider *v, double value, int channel) const
|
Chris@68
|
1208 {
|
Chris@805
|
1209 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@274
|
1210 bool mergingChannels = false, mixingChannels = false;
|
Chris@274
|
1211
|
Chris@274
|
1212 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@274
|
1213 mergingChannels, mixingChannels);
|
Chris@853
|
1214 if (channels == 0) return 0;
|
Chris@68
|
1215 if (maxChannel < minChannel || channel < minChannel) return 0;
|
Chris@68
|
1216
|
Chris@918
|
1217 int h = v->getPaintHeight();
|
Chris@68
|
1218 int m = (h / channels) / 2;
|
Chris@1266
|
1219
|
Chris@68
|
1220 if ((m_scale == dBScale || m_scale == MeterScale) &&
|
Chris@68
|
1221 m_channelMode != MergeChannels) {
|
Chris@68
|
1222 m = (h / channels);
|
Chris@68
|
1223 }
|
Chris@68
|
1224
|
Chris@274
|
1225 int my = m + (((channel - minChannel) * h) / channels);
|
Chris@274
|
1226
|
Chris@68
|
1227 int vy = 0;
|
Chris@68
|
1228
|
Chris@274
|
1229 switch (m_scale) {
|
Chris@68
|
1230
|
Chris@68
|
1231 case LinearScale:
|
Chris@68
|
1232 vy = int(m * value);
|
Chris@68
|
1233 break;
|
Chris@68
|
1234
|
Chris@68
|
1235 case MeterScale:
|
Chris@68
|
1236 vy = AudioLevel::multiplier_to_preview(value, m);
|
Chris@68
|
1237 break;
|
Chris@68
|
1238
|
Chris@68
|
1239 case dBScale:
|
Chris@1367
|
1240 vy = int(dBscale(value, m));
|
Chris@68
|
1241 break;
|
Chris@68
|
1242 }
|
Chris@68
|
1243
|
Chris@1338
|
1244 // SVCERR << "mergingChannels= " << mergingChannels << ", channel = " << channel << ", value = " << value << ", vy = " << vy << endl;
|
Chris@324
|
1245
|
Chris@68
|
1246 return my - vy;
|
Chris@68
|
1247 }
|
Chris@68
|
1248
|
Chris@908
|
1249 double
|
Chris@918
|
1250 WaveformLayer::getValueForY(const LayerGeometryProvider *v, int y, int &channel) const
|
Chris@261
|
1251 {
|
Chris@805
|
1252 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@274
|
1253 bool mergingChannels = false, mixingChannels = false;
|
Chris@274
|
1254
|
Chris@274
|
1255 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@274
|
1256 mergingChannels, mixingChannels);
|
Chris@853
|
1257 if (channels == 0) return 0;
|
Chris@261
|
1258 if (maxChannel < minChannel) return 0;
|
Chris@261
|
1259
|
Chris@918
|
1260 int h = v->getPaintHeight();
|
Chris@261
|
1261 int m = (h / channels) / 2;
|
Chris@261
|
1262
|
Chris@261
|
1263 if ((m_scale == dBScale || m_scale == MeterScale) &&
|
Chris@261
|
1264 m_channelMode != MergeChannels) {
|
Chris@261
|
1265 m = (h / channels);
|
Chris@261
|
1266 }
|
Chris@274
|
1267
|
Chris@274
|
1268 channel = (y * channels) / h + minChannel;
|
Chris@261
|
1269
|
Chris@261
|
1270 int my = m + (((channel - minChannel) * h) / channels);
|
Chris@261
|
1271
|
Chris@262
|
1272 int vy = my - y;
|
Chris@908
|
1273 double value = 0;
|
Chris@908
|
1274 double thresh = -50.f;
|
Chris@261
|
1275
|
Chris@274
|
1276 switch (m_scale) {
|
Chris@261
|
1277
|
Chris@261
|
1278 case LinearScale:
|
Chris@908
|
1279 value = double(vy) / m;
|
Chris@261
|
1280 break;
|
Chris@261
|
1281
|
Chris@261
|
1282 case MeterScale:
|
Chris@261
|
1283 value = AudioLevel::preview_to_multiplier(vy, m);
|
Chris@261
|
1284 break;
|
Chris@261
|
1285
|
Chris@261
|
1286 case dBScale:
|
Chris@908
|
1287 value = (-thresh * double(vy)) / m + thresh;
|
Chris@274
|
1288 value = AudioLevel::dB_to_multiplier(value);
|
Chris@261
|
1289 break;
|
Chris@261
|
1290 }
|
Chris@261
|
1291
|
Chris@274
|
1292 return value / m_gain;
|
Chris@261
|
1293 }
|
Chris@261
|
1294
|
Chris@261
|
1295 bool
|
Chris@918
|
1296 WaveformLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
|
Chris@908
|
1297 double &value, QString &unit) const
|
Chris@261
|
1298 {
|
Chris@805
|
1299 int channel;
|
Chris@261
|
1300
|
Chris@274
|
1301 value = getValueForY(v, y, channel);
|
Chris@261
|
1302
|
Chris@274
|
1303 if (m_scale == dBScale || m_scale == MeterScale) {
|
Chris@261
|
1304
|
Chris@908
|
1305 double thresh = -50.f;
|
Chris@274
|
1306
|
Chris@908
|
1307 if (value > 0.0) {
|
Chris@908
|
1308 value = 10.0 * log10(value);
|
Chris@274
|
1309 if (value < thresh) value = thresh;
|
Chris@274
|
1310 } else value = thresh;
|
Chris@274
|
1311
|
Chris@274
|
1312 unit = "dBV";
|
Chris@274
|
1313
|
Chris@274
|
1314 } else {
|
Chris@274
|
1315 unit = "V";
|
Chris@274
|
1316 }
|
Chris@274
|
1317
|
Chris@274
|
1318 return true;
|
Chris@274
|
1319 }
|
Chris@274
|
1320
|
Chris@274
|
1321 bool
|
Chris@918
|
1322 WaveformLayer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
|
Chris@908
|
1323 double &diff, QString &unit) const
|
Chris@274
|
1324 {
|
Chris@805
|
1325 int c0, c1;
|
Chris@908
|
1326 double v0 = getValueForY(v, y0, c0);
|
Chris@908
|
1327 double v1 = getValueForY(v, y1, c1);
|
Chris@274
|
1328
|
Chris@274
|
1329 if (c0 != c1) {
|
Chris@274
|
1330 // different channels, not comparable
|
Chris@908
|
1331 diff = 0.0;
|
Chris@274
|
1332 unit = "";
|
Chris@274
|
1333 return false;
|
Chris@274
|
1334 }
|
Chris@274
|
1335
|
Chris@274
|
1336 if (m_scale == dBScale || m_scale == MeterScale) {
|
Chris@274
|
1337
|
Chris@908
|
1338 double thresh = -50.0;
|
Chris@274
|
1339
|
Chris@274
|
1340 if (v1 == v0) diff = thresh;
|
Chris@274
|
1341 else {
|
Chris@274
|
1342 if (v1 > v0) diff = v0 / v1;
|
Chris@274
|
1343 else diff = v1 / v0;
|
Chris@274
|
1344
|
Chris@908
|
1345 diff = 10.0 * log10(diff);
|
Chris@274
|
1346 if (diff < thresh) diff = thresh;
|
Chris@274
|
1347 }
|
Chris@274
|
1348
|
Chris@274
|
1349 unit = "dBV";
|
Chris@274
|
1350
|
Chris@274
|
1351 } else {
|
Chris@908
|
1352 diff = fabs(v1 - v0);
|
Chris@274
|
1353 unit = "V";
|
Chris@274
|
1354 }
|
Chris@274
|
1355
|
Chris@261
|
1356 return true;
|
Chris@261
|
1357 }
|
Chris@261
|
1358
|
Chris@68
|
1359 int
|
Chris@918
|
1360 WaveformLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const
|
Chris@0
|
1361 {
|
Chris@0
|
1362 if (m_scale == LinearScale) {
|
Chris@1266
|
1363 return paint.fontMetrics().width("0.0") + 13;
|
Chris@0
|
1364 } else {
|
Chris@1266
|
1365 return std::max(paint.fontMetrics().width(tr("0dB")),
|
Chris@1266
|
1366 paint.fontMetrics().width(Strings::minus_infinity)) + 13;
|
Chris@0
|
1367 }
|
Chris@0
|
1368 }
|
Chris@0
|
1369
|
Chris@0
|
1370 void
|
Chris@918
|
1371 WaveformLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const
|
Chris@0
|
1372 {
|
Chris@0
|
1373 if (!m_model || !m_model->isOK()) {
|
Chris@1266
|
1374 return;
|
Chris@0
|
1375 }
|
Chris@0
|
1376
|
Chris@805
|
1377 int channels = 0, minChannel = 0, maxChannel = 0;
|
Chris@67
|
1378 bool mergingChannels = false, mixingChannels = false;
|
Chris@0
|
1379
|
Chris@67
|
1380 channels = getChannelArrangement(minChannel, maxChannel,
|
Chris@67
|
1381 mergingChannels, mixingChannels);
|
Chris@0
|
1382 if (channels == 0) return;
|
Chris@0
|
1383
|
Chris@0
|
1384 int h = rect.height(), w = rect.width();
|
Chris@0
|
1385 int textHeight = paint.fontMetrics().height();
|
Chris@0
|
1386 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
|
Chris@0
|
1387
|
Chris@908
|
1388 double gain = m_gain;
|
Chris@67
|
1389
|
Chris@805
|
1390 for (int ch = minChannel; ch <= maxChannel; ++ch) {
|
Chris@0
|
1391
|
Chris@1266
|
1392 int lastLabelledY = -1;
|
Chris@0
|
1393
|
Chris@805
|
1394 if (ch < (int)m_effectiveGains.size()) gain = m_effectiveGains[ch];
|
Chris@67
|
1395
|
Chris@68
|
1396 int n = 10;
|
Chris@0
|
1397
|
Chris@1266
|
1398 for (int i = 0; i <= n; ++i) {
|
Chris@68
|
1399
|
Chris@908
|
1400 double val = 0.0, nval = 0.0;
|
Chris@1266
|
1401 QString text = "";
|
Chris@0
|
1402
|
Chris@68
|
1403 switch (m_scale) {
|
Chris@68
|
1404
|
Chris@68
|
1405 case LinearScale:
|
Chris@68
|
1406 val = (i * gain) / n;
|
Chris@1266
|
1407 text = QString("%1").arg(double(i) / n);
|
Chris@1266
|
1408 if (i == 0) text = "0.0";
|
Chris@68
|
1409 else {
|
Chris@68
|
1410 nval = -val;
|
Chris@68
|
1411 if (i == n) text = "1.0";
|
Chris@68
|
1412 }
|
Chris@68
|
1413 break;
|
Chris@0
|
1414
|
Chris@68
|
1415 case MeterScale:
|
Chris@68
|
1416 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
|
Chris@1266
|
1417 text = QString("%1").arg(meterdbs[i]);
|
Chris@1266
|
1418 if (i == n) text = tr("0dB");
|
Chris@1266
|
1419 if (i == 0) {
|
Chris@1147
|
1420 text = Strings::minus_infinity;
|
Chris@68
|
1421 val = 0.0;
|
Chris@1266
|
1422 }
|
Chris@68
|
1423 break;
|
Chris@0
|
1424
|
Chris@68
|
1425 case dBScale:
|
Chris@68
|
1426 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
|
Chris@1266
|
1427 text = QString("%1").arg(-(10*n) + i * 10);
|
Chris@1266
|
1428 if (i == n) text = tr("0dB");
|
Chris@1266
|
1429 if (i == 0) {
|
Chris@1147
|
1430 text = Strings::minus_infinity;
|
Chris@68
|
1431 val = 0.0;
|
Chris@1266
|
1432 }
|
Chris@68
|
1433 break;
|
Chris@68
|
1434 }
|
Chris@0
|
1435
|
Chris@68
|
1436 if (val < -1.0 || val > 1.0) continue;
|
Chris@0
|
1437
|
Chris@274
|
1438 int y = getYForValue(v, val, ch);
|
Chris@0
|
1439
|
Chris@68
|
1440 int ny = y;
|
Chris@68
|
1441 if (nval != 0.0) {
|
Chris@274
|
1442 ny = getYForValue(v, nval, ch);
|
Chris@68
|
1443 }
|
Chris@0
|
1444
|
Chris@68
|
1445 bool spaceForLabel = (i == 0 ||
|
Chris@68
|
1446 abs(y - lastLabelledY) >= textHeight - 1);
|
Chris@0
|
1447
|
Chris@68
|
1448 if (spaceForLabel) {
|
Chris@0
|
1449
|
Chris@68
|
1450 int tx = 3;
|
Chris@68
|
1451 if (m_scale != LinearScale) {
|
Chris@68
|
1452 tx = w - 10 - paint.fontMetrics().width(text);
|
Chris@68
|
1453 }
|
Chris@68
|
1454
|
Chris@68
|
1455 int ty = y;
|
Chris@68
|
1456 if (ty < paint.fontMetrics().ascent()) {
|
Chris@68
|
1457 ty = paint.fontMetrics().ascent();
|
Chris@68
|
1458 } else if (ty > h - paint.fontMetrics().descent()) {
|
Chris@68
|
1459 ty = h - paint.fontMetrics().descent();
|
Chris@68
|
1460 } else {
|
Chris@68
|
1461 ty += toff;
|
Chris@68
|
1462 }
|
Chris@68
|
1463 paint.drawText(tx, ty, text);
|
Chris@0
|
1464
|
Chris@68
|
1465 lastLabelledY = ty - toff;
|
Chris@67
|
1466
|
Chris@68
|
1467 if (ny != y) {
|
Chris@68
|
1468 ty = ny;
|
Chris@68
|
1469 if (ty < paint.fontMetrics().ascent()) {
|
Chris@68
|
1470 ty = paint.fontMetrics().ascent();
|
Chris@68
|
1471 } else if (ty > h - paint.fontMetrics().descent()) {
|
Chris@68
|
1472 ty = h - paint.fontMetrics().descent();
|
Chris@68
|
1473 } else {
|
Chris@68
|
1474 ty += toff;
|
Chris@68
|
1475 }
|
Chris@68
|
1476 paint.drawText(tx, ty, text);
|
Chris@68
|
1477 }
|
Chris@68
|
1478
|
Chris@68
|
1479 paint.drawLine(w - 7, y, w, y);
|
Chris@68
|
1480 if (ny != y) paint.drawLine(w - 7, ny, w, ny);
|
Chris@68
|
1481
|
Chris@68
|
1482 } else {
|
Chris@68
|
1483
|
Chris@68
|
1484 paint.drawLine(w - 4, y, w, y);
|
Chris@68
|
1485 if (ny != y) paint.drawLine(w - 4, ny, w, ny);
|
Chris@68
|
1486 }
|
Chris@1266
|
1487 }
|
Chris@0
|
1488 }
|
Chris@0
|
1489 }
|
Chris@0
|
1490
|
Chris@316
|
1491 void
|
Chris@316
|
1492 WaveformLayer::toXml(QTextStream &stream,
|
Chris@316
|
1493 QString indent, QString extraAttributes) const
|
Chris@6
|
1494 {
|
Chris@6
|
1495 QString s;
|
Chris@6
|
1496
|
Chris@285
|
1497 QString colourName, colourSpec, darkbg;
|
Chris@285
|
1498 ColourDatabase::getInstance()->getStringValues
|
Chris@285
|
1499 (m_colour, colourName, colourSpec, darkbg);
|
Chris@285
|
1500
|
Chris@6
|
1501 s += QString("gain=\"%1\" "
|
Chris@1266
|
1502 "showMeans=\"%2\" "
|
Chris@1266
|
1503 "greyscale=\"%3\" "
|
Chris@1266
|
1504 "channelMode=\"%4\" "
|
Chris@1266
|
1505 "channel=\"%5\" "
|
Chris@287
|
1506 "scale=\"%6\" "
|
Chris@709
|
1507 "middleLineHeight=\"%7\" "
|
Chris@1266
|
1508 "aggressive=\"%8\" "
|
Chris@709
|
1509 "autoNormalize=\"%9\"")
|
Chris@1266
|
1510 .arg(m_gain)
|
Chris@1266
|
1511 .arg(m_showMeans)
|
Chris@1367
|
1512 .arg(true) // Option removed, but effectively always on, so
|
Chris@1367
|
1513 // retained in the session file for compatibility
|
Chris@1266
|
1514 .arg(m_channelMode)
|
Chris@1266
|
1515 .arg(m_channel)
|
Chris@1266
|
1516 .arg(m_scale)
|
Chris@709
|
1517 .arg(m_middleLineHeight)
|
Chris@1266
|
1518 .arg(m_aggressive)
|
Chris@67
|
1519 .arg(m_autoNormalize);
|
Chris@6
|
1520
|
Chris@316
|
1521 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
|
Chris@6
|
1522 }
|
Chris@6
|
1523
|
Chris@11
|
1524 void
|
Chris@11
|
1525 WaveformLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
1526 {
|
Chris@11
|
1527 bool ok = false;
|
Chris@11
|
1528
|
Chris@287
|
1529 SingleColourLayer::setProperties(attributes);
|
Chris@287
|
1530
|
Chris@11
|
1531 float gain = attributes.value("gain").toFloat(&ok);
|
Chris@11
|
1532 if (ok) setGain(gain);
|
Chris@11
|
1533
|
Chris@11
|
1534 bool showMeans = (attributes.value("showMeans") == "1" ||
|
Chris@1266
|
1535 attributes.value("showMeans") == "true");
|
Chris@11
|
1536 setShowMeans(showMeans);
|
Chris@11
|
1537
|
Chris@11
|
1538 ChannelMode channelMode = (ChannelMode)
|
Chris@1266
|
1539 attributes.value("channelMode").toInt(&ok);
|
Chris@11
|
1540 if (ok) setChannelMode(channelMode);
|
Chris@11
|
1541
|
Chris@11
|
1542 int channel = attributes.value("channel").toInt(&ok);
|
Chris@11
|
1543 if (ok) setChannel(channel);
|
Chris@11
|
1544
|
Chris@709
|
1545 Scale scale = (Scale)attributes.value("scale").toInt(&ok);
|
Chris@11
|
1546 if (ok) setScale(scale);
|
Chris@11
|
1547
|
Chris@709
|
1548 float middleLineHeight = attributes.value("middleLineHeight").toFloat(&ok);
|
Chris@709
|
1549 if (ok) setMiddleLineHeight(middleLineHeight);
|
Chris@709
|
1550
|
Chris@11
|
1551 bool aggressive = (attributes.value("aggressive") == "1" ||
|
Chris@1266
|
1552 attributes.value("aggressive") == "true");
|
Chris@1367
|
1553 setAggressiveCacheing(aggressive);
|
Chris@67
|
1554
|
Chris@67
|
1555 bool autoNormalize = (attributes.value("autoNormalize") == "1" ||
|
Chris@67
|
1556 attributes.value("autoNormalize") == "true");
|
Chris@67
|
1557 setAutoNormalize(autoNormalize);
|
Chris@11
|
1558 }
|
Chris@11
|
1559
|
Chris@133
|
1560 int
|
Chris@133
|
1561 WaveformLayer::getVerticalZoomSteps(int &defaultStep) const
|
Chris@133
|
1562 {
|
Chris@133
|
1563 defaultStep = 50;
|
Chris@133
|
1564 return 100;
|
Chris@133
|
1565 }
|
Chris@0
|
1566
|
Chris@133
|
1567 int
|
Chris@133
|
1568 WaveformLayer::getCurrentVerticalZoomStep() const
|
Chris@133
|
1569 {
|
Chris@908
|
1570 int val = int(lrint(log10(m_gain) * 20.0) + 50);
|
Chris@133
|
1571 if (val < 0) val = 0;
|
Chris@133
|
1572 if (val > 100) val = 100;
|
Chris@133
|
1573 return val;
|
Chris@133
|
1574 }
|
Chris@133
|
1575
|
Chris@133
|
1576 void
|
Chris@133
|
1577 WaveformLayer::setVerticalZoomStep(int step)
|
Chris@133
|
1578 {
|
Chris@908
|
1579 setGain(powf(10, float(step - 50) / 20.f));
|
Chris@133
|
1580 }
|
Chris@133
|
1581
|