]> git.sesse.net Git - nageru/blob - nonlinear_fader.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / nonlinear_fader.cpp
1 #include "nonlinear_fader.h"
2
3 #include <assert.h>
4 #include <math.h>
5 #include <QPainter>
6 #include <QPoint>
7 #include <QRect>
8 #include <QStyle>
9 #include <QStyleOption>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13
14 #include "piecewise_interpolator.h"
15
16 class QPaintEvent;
17 class QWidget;
18
19 using namespace std;
20
21 namespace {
22
23 PiecewiseInterpolator interpolator({
24         // The main area is from +6 to -12 dB (18 dB), and we use half the slider range for it.
25         // Adjust slightly so that the MIDI controller value of 106 becomes exactly 0.0 dB
26         // (cf. map_controller_to_float()); otherwise, we'd miss ever so slightly, which is
27         // really frustrating.
28         { 6.0, 1.0 },
29         { -12.0, 1.0 - (1.0 - 106.5/127.0) * 3.0 },  // About 0.492.
30
31         // -12 to -21 is half the range (9 dB). Halve.
32         { -21.0, 0.325 },
33
34         // -21 to -30 (9 dB) gets the same range as the previous one.
35         { -30.0, 0.25 },
36
37         // -30 to -48 (18 dB) gets half of half.
38         { -48.0, 0.125 },
39
40         // -48 to -84 (36 dB) gets half of half of half.
41         { -84.0, 0.0 },
42 });
43
44 }  // namespace
45
46 NonLinearFader::NonLinearFader(QWidget *parent)
47         : QSlider(parent)
48 {
49         update_slider_position();
50 }
51
52 void NonLinearFader::setDbValue(double db)
53 {
54         db_value = db;
55         update_slider_position();
56         emit dbValueChanged(db);
57 }
58
59 void NonLinearFader::paintEvent(QPaintEvent *event)
60 {
61         QStyleOptionSlider opt;
62         this->initStyleOption(&opt);
63         QRect gr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
64         QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
65
66         // FIXME: Where does the slider_length / 2 come from? I can't really find it
67         // in the Qt code, but it seems to match up with reality.
68         int slider_length = sr.height();
69         int slider_max = gr.top() + (slider_length / 2);
70         int slider_min = gr.bottom() + (slider_length / 2) - slider_length + 1;
71
72         QPainter p(this);
73
74         // Draw some ticks every 6 dB.
75         // FIXME: Find a way to make the slider wider, so that we have more space for tickmarks
76         // and some dB numbering.
77         int x_margin = 5;
78         p.setPen(Qt::darkGray);
79         for (int db = -84; db <= 6; db += 6) {
80                 int y = slider_min + lrint(interpolator.db_to_fraction(db) * (slider_max - slider_min));
81                 p.drawLine(QPoint(0, y), QPoint(gr.left() - x_margin, y));
82                 p.drawLine(QPoint(gr.right() + x_margin, y), QPoint(width() - 1, y));
83         }
84
85         QSlider::paintEvent(event);
86 }
87
88 void NonLinearFader::sliderChange(SliderChange change)
89 {
90         QSlider::sliderChange(change);
91         if (change == QAbstractSlider::SliderValueChange && !inhibit_updates) {
92                 if (value() == 0) {
93                         db_value = -HUGE_VAL;
94                 } else {
95                         double frac = double(value() - minimum()) / (maximum() - minimum());
96                         db_value = interpolator.fraction_to_db(frac);
97                 }
98                 emit dbValueChanged(db_value);
99         }
100 }
101
102 void NonLinearFader::update_slider_position()
103 {
104         inhibit_updates = true;
105         double val = interpolator.db_to_fraction(db_value) * (maximum() - minimum()) + minimum();
106         setValue(lrint(val));
107         inhibit_updates = false;
108 }