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