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