Chris@923
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@923
|
2
|
Chris@923
|
3 /*
|
Chris@923
|
4 Sonic Visualiser
|
Chris@923
|
5 An audio file viewer and annotation editor.
|
Chris@923
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@923
|
7
|
Chris@923
|
8 This program is free software; you can redistribute it and/or
|
Chris@923
|
9 modify it under the terms of the GNU General Public License as
|
Chris@923
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@923
|
11 License, or (at your option) any later version. See the file
|
Chris@923
|
12 COPYING included with this distribution for more information.
|
Chris@923
|
13 */
|
Chris@923
|
14
|
Chris@923
|
15 #include "LevelPanWidget.h"
|
Chris@923
|
16
|
Chris@923
|
17 #include <QPainter>
|
Chris@923
|
18 #include <QMouseEvent>
|
Chris@923
|
19 #include <QWheelEvent>
|
Chris@923
|
20
|
Chris@923
|
21 #include "layer/ColourMapper.h"
|
Chris@925
|
22 #include "base/AudioLevel.h"
|
Chris@923
|
23
|
Chris@1176
|
24 #include "WidgetScale.h"
|
Chris@1176
|
25
|
Chris@923
|
26 #include <iostream>
|
Chris@926
|
27 #include <cmath>
|
Chris@940
|
28 #include <cassert>
|
Chris@923
|
29
|
Chris@923
|
30 using std::cerr;
|
Chris@923
|
31 using std::endl;
|
Chris@923
|
32
|
Chris@940
|
33 static const int maxLevel = 4; // min is 0, may be mute or not depending on m_includeMute
|
Chris@923
|
34 static const int maxPan = 2; // range is -maxPan to maxPan
|
Chris@923
|
35
|
Chris@923
|
36 LevelPanWidget::LevelPanWidget(QWidget *parent) :
|
Chris@923
|
37 QWidget(parent),
|
Chris@923
|
38 m_level(maxLevel),
|
Chris@923
|
39 m_pan(0),
|
Chris@1177
|
40 m_monitorLeft(-1),
|
Chris@1177
|
41 m_monitorRight(-1),
|
Chris@940
|
42 m_editable(true),
|
Chris@940
|
43 m_includeMute(true)
|
Chris@923
|
44 {
|
Chris@923
|
45 }
|
Chris@923
|
46
|
Chris@923
|
47 LevelPanWidget::~LevelPanWidget()
|
Chris@923
|
48 {
|
Chris@923
|
49 }
|
Chris@923
|
50
|
Chris@929
|
51 QSize
|
Chris@929
|
52 LevelPanWidget::sizeHint() const
|
Chris@929
|
53 {
|
Chris@1176
|
54 return WidgetScale::scaleQSize(QSize(40, 40));
|
Chris@929
|
55 }
|
Chris@929
|
56
|
Chris@940
|
57 static int
|
Chris@940
|
58 db_to_level(double db)
|
Chris@940
|
59 {
|
Chris@940
|
60 // Only if !m_includeMute, otherwise AudioLevel is used.
|
Chris@940
|
61 // Levels are: +6 0 -6 -12 -20
|
Chris@940
|
62 assert(maxLevel == 4);
|
Chris@940
|
63 if (db > 3.) return 4;
|
Chris@940
|
64 else if (db > -3.) return 3;
|
Chris@940
|
65 else if (db > -9.) return 2;
|
Chris@940
|
66 else if (db > -16.) return 1;
|
Chris@940
|
67 else return 0;
|
Chris@940
|
68 }
|
Chris@940
|
69
|
Chris@940
|
70 static double
|
Chris@940
|
71 level_to_db(int level)
|
Chris@940
|
72 {
|
Chris@940
|
73 // Only if !m_includeMute, otherwise AudioLevel is used.
|
Chris@940
|
74 // Levels are: +6 0 -6 -12 -20
|
Chris@940
|
75 assert(maxLevel == 4);
|
Chris@940
|
76 if (level >= 4) return 6.;
|
Chris@940
|
77 else if (level == 3) return 0.;
|
Chris@940
|
78 else if (level == 2) return -6.;
|
Chris@940
|
79 else if (level == 1) return -12.;
|
Chris@940
|
80 else return -20.;
|
Chris@940
|
81 }
|
Chris@940
|
82
|
Chris@1177
|
83 int
|
Chris@1177
|
84 LevelPanWidget::audioLevelToLevel(float audioLevel, bool withMute)
|
Chris@1177
|
85 {
|
Chris@1177
|
86 int level;
|
Chris@1177
|
87 if (withMute) {
|
Chris@1177
|
88 level = AudioLevel::multiplier_to_fader
|
Chris@1177
|
89 (audioLevel, maxLevel, AudioLevel::ShortFader);
|
Chris@1177
|
90 } else {
|
Chris@1177
|
91 level = db_to_level(AudioLevel::multiplier_to_dB(audioLevel));
|
Chris@1177
|
92 }
|
Chris@1177
|
93 if (level < 0) level = 0;
|
Chris@1177
|
94 if (level > maxLevel) level = maxLevel;
|
Chris@1177
|
95 return level;
|
Chris@1177
|
96 }
|
Chris@1177
|
97
|
Chris@1177
|
98 float
|
Chris@1177
|
99 LevelPanWidget::levelToAudioLevel(int level, bool withMute)
|
Chris@1177
|
100 {
|
Chris@1177
|
101 if (withMute) {
|
Chris@1177
|
102 return float(AudioLevel::fader_to_multiplier
|
Chris@1177
|
103 (level, maxLevel, AudioLevel::ShortFader));
|
Chris@1177
|
104 } else {
|
Chris@1177
|
105 return float(AudioLevel::dB_to_multiplier(level_to_db(level)));
|
Chris@1177
|
106 }
|
Chris@1177
|
107 }
|
Chris@1177
|
108
|
Chris@923
|
109 void
|
Chris@925
|
110 LevelPanWidget::setLevel(float flevel)
|
Chris@923
|
111 {
|
Chris@1177
|
112 int level = audioLevelToLevel(flevel, m_includeMute);
|
Chris@925
|
113 if (level != m_level) {
|
Chris@925
|
114 m_level = level;
|
Chris@925
|
115 float convertsTo = getLevel();
|
Chris@925
|
116 if (fabsf(convertsTo - flevel) > 1e-5) {
|
Chris@925
|
117 emitLevelChanged();
|
Chris@925
|
118 }
|
Chris@925
|
119 update();
|
Chris@925
|
120 }
|
Chris@923
|
121 }
|
Chris@923
|
122
|
Chris@940
|
123 float
|
Chris@940
|
124 LevelPanWidget::getLevel() const
|
Chris@940
|
125 {
|
Chris@1177
|
126 return levelToAudioLevel(m_level, m_includeMute);
|
Chris@1177
|
127 }
|
Chris@1177
|
128
|
Chris@1177
|
129 int
|
Chris@1177
|
130 LevelPanWidget::audioPanToPan(float audioPan)
|
Chris@1177
|
131 {
|
Chris@1177
|
132 int pan = int(round(audioPan * maxPan));
|
Chris@1177
|
133 if (pan < -maxPan) pan = -maxPan;
|
Chris@1177
|
134 if (pan > maxPan) pan = maxPan;
|
Chris@1177
|
135 return pan;
|
Chris@1177
|
136 }
|
Chris@1177
|
137
|
Chris@1177
|
138 float
|
Chris@1177
|
139 LevelPanWidget::panToAudioPan(int pan)
|
Chris@1177
|
140 {
|
Chris@1177
|
141 return float(pan) / float(maxPan);
|
Chris@1177
|
142 }
|
Chris@1177
|
143
|
Chris@1177
|
144 void
|
Chris@1177
|
145 LevelPanWidget::setPan(float fpan)
|
Chris@1177
|
146 {
|
Chris@1177
|
147 int pan = audioPanToPan(fpan);
|
Chris@1177
|
148 if (pan != m_pan) {
|
Chris@1177
|
149 m_pan = pan;
|
Chris@1177
|
150 update();
|
Chris@940
|
151 }
|
Chris@940
|
152 }
|
Chris@940
|
153
|
Chris@1177
|
154 float
|
Chris@1177
|
155 LevelPanWidget::getPan() const
|
Chris@1177
|
156 {
|
Chris@1177
|
157 return panToAudioPan(m_pan);
|
Chris@1177
|
158 }
|
Chris@1177
|
159
|
Chris@923
|
160 void
|
Chris@1177
|
161 LevelPanWidget::setMonitoringLevels(float left, float right)
|
Chris@923
|
162 {
|
Chris@1177
|
163 m_monitorLeft = left;
|
Chris@1177
|
164 m_monitorRight = right;
|
Chris@923
|
165 update();
|
Chris@923
|
166 }
|
Chris@923
|
167
|
Chris@940
|
168 bool
|
Chris@940
|
169 LevelPanWidget::isEditable() const
|
Chris@940
|
170 {
|
Chris@940
|
171 return m_editable;
|
Chris@940
|
172 }
|
Chris@940
|
173
|
Chris@940
|
174 bool
|
Chris@940
|
175 LevelPanWidget::includesMute() const
|
Chris@940
|
176 {
|
Chris@940
|
177 return m_includeMute;
|
Chris@940
|
178 }
|
Chris@940
|
179
|
Chris@923
|
180 void
|
Chris@923
|
181 LevelPanWidget::setEditable(bool editable)
|
Chris@923
|
182 {
|
Chris@923
|
183 m_editable = editable;
|
Chris@923
|
184 update();
|
Chris@923
|
185 }
|
Chris@923
|
186
|
Chris@940
|
187 void
|
Chris@940
|
188 LevelPanWidget::setIncludeMute(bool include)
|
Chris@923
|
189 {
|
Chris@940
|
190 m_includeMute = include;
|
Chris@940
|
191 emitLevelChanged();
|
Chris@940
|
192 update();
|
Chris@923
|
193 }
|
Chris@923
|
194
|
Chris@923
|
195 void
|
Chris@923
|
196 LevelPanWidget::emitLevelChanged()
|
Chris@923
|
197 {
|
Chris@923
|
198 cerr << "emitting levelChanged(" << getLevel() << ")" << endl;
|
Chris@923
|
199 emit levelChanged(getLevel());
|
Chris@923
|
200 }
|
Chris@923
|
201
|
Chris@923
|
202 void
|
Chris@923
|
203 LevelPanWidget::emitPanChanged()
|
Chris@923
|
204 {
|
Chris@923
|
205 cerr << "emitting panChanged(" << getPan() << ")" << endl;
|
Chris@923
|
206 emit panChanged(getPan());
|
Chris@923
|
207 }
|
Chris@923
|
208
|
Chris@923
|
209 void
|
Chris@923
|
210 LevelPanWidget::mousePressEvent(QMouseEvent *e)
|
Chris@923
|
211 {
|
Chris@923
|
212 mouseMoveEvent(e);
|
Chris@923
|
213 }
|
Chris@923
|
214
|
Chris@923
|
215 void
|
Chris@923
|
216 LevelPanWidget::mouseMoveEvent(QMouseEvent *e)
|
Chris@923
|
217 {
|
Chris@923
|
218 if (!m_editable) return;
|
Chris@923
|
219
|
Chris@923
|
220 int level, pan;
|
Chris@929
|
221 toCell(rect(), e->pos(), level, pan);
|
Chris@923
|
222 if (level == m_level && pan == m_pan) {
|
Chris@923
|
223 return;
|
Chris@923
|
224 }
|
Chris@923
|
225 if (level != m_level) {
|
Chris@923
|
226 m_level = level;
|
Chris@923
|
227 emitLevelChanged();
|
Chris@923
|
228 }
|
Chris@923
|
229 if (pan != m_pan) {
|
Chris@923
|
230 m_pan = pan;
|
Chris@923
|
231 emitPanChanged();
|
Chris@923
|
232 }
|
Chris@923
|
233 update();
|
Chris@923
|
234 }
|
Chris@923
|
235
|
Chris@923
|
236 void
|
Chris@923
|
237 LevelPanWidget::mouseReleaseEvent(QMouseEvent *e)
|
Chris@923
|
238 {
|
Chris@923
|
239 mouseMoveEvent(e);
|
Chris@923
|
240 }
|
Chris@923
|
241
|
Chris@923
|
242 void
|
Chris@923
|
243 LevelPanWidget::wheelEvent(QWheelEvent *e)
|
Chris@923
|
244 {
|
Chris@923
|
245 if (e->modifiers() & Qt::ControlModifier) {
|
Chris@923
|
246 if (e->delta() > 0) {
|
Chris@923
|
247 if (m_pan < maxPan) {
|
Chris@923
|
248 ++m_pan;
|
Chris@923
|
249 emitPanChanged();
|
Chris@923
|
250 update();
|
Chris@923
|
251 }
|
Chris@923
|
252 } else {
|
Chris@923
|
253 if (m_pan > -maxPan) {
|
Chris@923
|
254 --m_pan;
|
Chris@923
|
255 emitPanChanged();
|
Chris@923
|
256 update();
|
Chris@923
|
257 }
|
Chris@923
|
258 }
|
Chris@923
|
259 } else {
|
Chris@923
|
260 if (e->delta() > 0) {
|
Chris@923
|
261 if (m_level < maxLevel) {
|
Chris@923
|
262 ++m_level;
|
Chris@923
|
263 emitLevelChanged();
|
Chris@923
|
264 update();
|
Chris@923
|
265 }
|
Chris@923
|
266 } else {
|
Chris@923
|
267 if (m_level > 0) {
|
Chris@923
|
268 --m_level;
|
Chris@923
|
269 emitLevelChanged();
|
Chris@923
|
270 update();
|
Chris@923
|
271 }
|
Chris@923
|
272 }
|
Chris@923
|
273 }
|
Chris@923
|
274 }
|
Chris@923
|
275
|
Chris@923
|
276 void
|
Chris@929
|
277 LevelPanWidget::toCell(QRectF rect, QPointF loc, int &level, int &pan) const
|
Chris@923
|
278 {
|
Chris@929
|
279 double w = rect.width(), h = rect.height();
|
Chris@929
|
280
|
Chris@923
|
281 int npan = maxPan * 2 + 1;
|
Chris@924
|
282 int nlevel = maxLevel + 1;
|
Chris@929
|
283
|
Chris@924
|
284 double wcell = w / npan, hcell = h / nlevel;
|
Chris@929
|
285
|
Chris@932
|
286 level = int((h - (loc.y() - rect.y())) / hcell);
|
Chris@924
|
287 if (level < 0) level = 0;
|
Chris@923
|
288 if (level > maxLevel) level = maxLevel;
|
Chris@929
|
289
|
Chris@932
|
290 pan = int((loc.x() - rect.x()) / wcell) - maxPan;
|
Chris@923
|
291 if (pan < -maxPan) pan = -maxPan;
|
Chris@923
|
292 if (pan > maxPan) pan = maxPan;
|
Chris@923
|
293 }
|
Chris@923
|
294
|
Chris@923
|
295 QSizeF
|
Chris@929
|
296 LevelPanWidget::cellSize(QRectF rect) const
|
Chris@923
|
297 {
|
Chris@929
|
298 double w = rect.width(), h = rect.height();
|
Chris@923
|
299 int npan = maxPan * 2 + 1;
|
Chris@924
|
300 int nlevel = maxLevel + 1;
|
Chris@924
|
301 double wcell = w / npan, hcell = h / nlevel;
|
Chris@923
|
302 return QSizeF(wcell, hcell);
|
Chris@923
|
303 }
|
Chris@923
|
304
|
Chris@923
|
305 QPointF
|
Chris@929
|
306 LevelPanWidget::cellCentre(QRectF rect, int level, int pan) const
|
Chris@923
|
307 {
|
Chris@929
|
308 QSizeF cs = cellSize(rect);
|
Chris@932
|
309 return QPointF(rect.x() + cs.width() * (pan + maxPan) + cs.width() / 2.,
|
Chris@932
|
310 rect.y() + rect.height() - cs.height() * (level + 1) + cs.height() / 2.);
|
Chris@923
|
311 }
|
Chris@923
|
312
|
Chris@923
|
313 QSizeF
|
Chris@929
|
314 LevelPanWidget::cellLightSize(QRectF rect) const
|
Chris@923
|
315 {
|
Chris@923
|
316 double extent = 3. / 4.;
|
Chris@929
|
317 QSizeF cs = cellSize(rect);
|
Chris@923
|
318 double m = std::min(cs.width(), cs.height());
|
Chris@923
|
319 return QSizeF(m * extent, m * extent);
|
Chris@923
|
320 }
|
Chris@923
|
321
|
Chris@923
|
322 QRectF
|
Chris@929
|
323 LevelPanWidget::cellLightRect(QRectF rect, int level, int pan) const
|
Chris@923
|
324 {
|
Chris@929
|
325 QSizeF cls = cellLightSize(rect);
|
Chris@929
|
326 QPointF cc = cellCentre(rect, level, pan);
|
Chris@923
|
327 return QRectF(cc.x() - cls.width() / 2.,
|
Chris@923
|
328 cc.y() - cls.height() / 2.,
|
Chris@923
|
329 cls.width(),
|
Chris@923
|
330 cls.height());
|
Chris@923
|
331 }
|
Chris@923
|
332
|
Chris@923
|
333 double
|
Chris@929
|
334 LevelPanWidget::thinLineWidth(QRectF rect) const
|
Chris@923
|
335 {
|
Chris@929
|
336 double tw = ceil(rect.width() / (maxPan * 2. * 10.));
|
Chris@929
|
337 double th = ceil(rect.height() / (maxLevel * 10.));
|
Chris@923
|
338 return std::min(th, tw);
|
Chris@923
|
339 }
|
Chris@923
|
340
|
Chris@941
|
341 static QColor
|
Chris@941
|
342 level_to_colour(int level)
|
Chris@941
|
343 {
|
Chris@941
|
344 assert(maxLevel == 4);
|
Chris@941
|
345 if (level == 0) return Qt::black;
|
Chris@941
|
346 else if (level == 1) return QColor(80, 0, 0);
|
Chris@941
|
347 else if (level == 2) return QColor(160, 0, 0);
|
Chris@941
|
348 else if (level == 3) return QColor(255, 0, 0);
|
Chris@941
|
349 else return QColor(255, 255, 0);
|
Chris@941
|
350 }
|
Chris@941
|
351
|
Chris@923
|
352 void
|
Chris@929
|
353 LevelPanWidget::renderTo(QPaintDevice *dev, QRectF rect, bool asIfEditable) const
|
Chris@923
|
354 {
|
Chris@929
|
355 QPainter paint(dev);
|
Chris@923
|
356
|
Chris@923
|
357 paint.setRenderHint(QPainter::Antialiasing, true);
|
Chris@923
|
358
|
Chris@923
|
359 QPen pen;
|
Chris@923
|
360
|
Chris@929
|
361 double thin = thinLineWidth(rect);
|
Chris@938
|
362
|
Chris@923
|
363 pen.setColor(QColor(127, 127, 127, 127));
|
Chris@929
|
364 pen.setWidthF(cellLightSize(rect).width() + thin);
|
Chris@923
|
365 pen.setCapStyle(Qt::RoundCap);
|
Chris@923
|
366 paint.setPen(pen);
|
Chris@1177
|
367 paint.setBrush(Qt::NoBrush);
|
Chris@923
|
368
|
Chris@923
|
369 for (int pan = -maxPan; pan <= maxPan; ++pan) {
|
Chris@929
|
370 paint.drawLine(cellCentre(rect, 0, pan), cellCentre(rect, maxLevel, pan));
|
Chris@924
|
371 }
|
Chris@924
|
372
|
Chris@1177
|
373 if (m_monitorLeft > 0.f || m_monitorRight > 0.f) {
|
Chris@1177
|
374 paint.setPen(Qt::NoPen);
|
Chris@1177
|
375 for (int pan = -maxPan; pan <= maxPan; ++pan) {
|
Chris@1177
|
376 float audioPan = panToAudioPan(pan);
|
Chris@1177
|
377 float audioLevel;
|
Chris@1177
|
378 if (audioPan < 0.f) {
|
Chris@1177
|
379 audioLevel = m_monitorLeft + m_monitorRight * (1.f + audioPan);
|
Chris@1177
|
380 } else {
|
Chris@1177
|
381 audioLevel = m_monitorRight + m_monitorLeft * (1.f - audioPan);
|
Chris@1177
|
382 }
|
Chris@1177
|
383 int levelHere = audioLevelToLevel(audioLevel, false);
|
Chris@1177
|
384 for (int level = 0; level <= levelHere; ++level) {
|
Chris@1177
|
385 paint.setBrush(level_to_colour(level));
|
Chris@1177
|
386 QRectF clr = cellLightRect(rect, level, pan);
|
Chris@1177
|
387 paint.drawEllipse(clr);
|
Chris@1177
|
388 }
|
Chris@1177
|
389 }
|
Chris@1177
|
390 paint.setPen(pen);
|
Chris@1177
|
391 paint.setBrush(Qt::NoBrush);
|
Chris@1177
|
392 }
|
Chris@1177
|
393
|
Chris@924
|
394 if (isEnabled()) {
|
Chris@924
|
395 pen.setColor(Qt::black);
|
Chris@924
|
396 } else {
|
Chris@924
|
397 pen.setColor(Qt::darkGray);
|
Chris@923
|
398 }
|
Chris@929
|
399
|
Chris@940
|
400 if (!asIfEditable && m_includeMute && m_level == 0) {
|
Chris@929
|
401 pen.setWidthF(thin * 2);
|
Chris@929
|
402 pen.setCapStyle(Qt::RoundCap);
|
Chris@929
|
403 paint.setPen(pen);
|
Chris@930
|
404 paint.drawLine(cellCentre(rect, 0, -maxPan),
|
Chris@930
|
405 cellCentre(rect, maxLevel, maxPan));
|
Chris@930
|
406 paint.drawLine(cellCentre(rect, maxLevel, -maxPan),
|
Chris@930
|
407 cellCentre(rect, 0, maxPan));
|
Chris@929
|
408 return;
|
Chris@929
|
409 }
|
Chris@923
|
410
|
Chris@923
|
411 pen.setWidthF(thin);
|
Chris@923
|
412 pen.setCapStyle(Qt::FlatCap);
|
Chris@923
|
413 paint.setPen(pen);
|
Chris@923
|
414
|
Chris@924
|
415 for (int level = 0; level <= m_level; ++level) {
|
Chris@924
|
416 if (isEnabled()) {
|
Chris@941
|
417 paint.setBrush(level_to_colour(level));
|
Chris@924
|
418 }
|
Chris@929
|
419 QRectF clr = cellLightRect(rect, level, m_pan);
|
Chris@940
|
420 if (m_includeMute && m_level == 0) {
|
Chris@924
|
421 paint.drawLine(clr.topLeft(), clr.bottomRight());
|
Chris@924
|
422 paint.drawLine(clr.bottomLeft(), clr.topRight());
|
Chris@924
|
423 } else {
|
Chris@924
|
424 paint.drawEllipse(clr);
|
Chris@924
|
425 }
|
Chris@923
|
426 }
|
Chris@923
|
427 }
|
Chris@923
|
428
|
Chris@929
|
429 void
|
Chris@929
|
430 LevelPanWidget::paintEvent(QPaintEvent *)
|
Chris@929
|
431 {
|
Chris@929
|
432 renderTo(this, rect(), m_editable);
|
Chris@929
|
433 }
|
Chris@923
|
434
|
Chris@1180
|
435 void
|
Chris@1180
|
436 LevelPanWidget::enterEvent(QEvent *e)
|
Chris@1180
|
437 {
|
Chris@1180
|
438 QWidget::enterEvent(e);
|
Chris@1180
|
439 emit mouseEntered();
|
Chris@1180
|
440 }
|
Chris@929
|
441
|
Chris@1180
|
442 void
|
Chris@1180
|
443 LevelPanWidget::leaveEvent(QEvent *e)
|
Chris@1180
|
444 {
|
Chris@1180
|
445 QWidget::enterEvent(e);
|
Chris@1180
|
446 emit mouseLeft();
|
Chris@1180
|
447 }
|
Chris@929
|
448
|