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