]> git.sesse.net Git - nageru/blob - nonlinear_fader.cpp
Add support for controlling the audio using a MIDI controller.
[nageru] / nonlinear_fader.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <vector>
5 #include <utility>
6
7 #include <QResizeEvent>
8 #include <QPainter>
9 #include <QStyle>
10 #include <QStyleOptionSlider>
11
12 #include "nonlinear_fader.h"
13
14 using namespace std;
15
16 namespace {
17
18 vector<pair<double, double>> fader_control_points = {
19         // The main area is from +6 to -12 dB (18 dB), and we use half the slider range for it.
20         // Adjust slightly so that the MIDI controller value of 106 becomes exactly 0.0 dB
21         // (cf. map_controller_to_float()); otherwise, we'd miss ever so slightly, which is
22         // really frustrating.
23         { 6.0, 1.0 },
24         { -12.0, 1.0 - (1.0 - 106.5/127.0) * 3.0 },  // About 0.492.
25
26         // -12 to -21 is half the range (9 dB). Halve.
27         { -21.0, 0.325 },
28
29         // -21 to -30 (9 dB) gets the same range as the previous one.
30         { -30.0, 0.25 },
31
32         // -30 to -48 (18 dB) gets half of half.
33         { -48.0, 0.125 },
34
35         // -48 to -84 (36 dB) gets half of half of half.
36         { -84.0, 0.0 },
37 };
38
39 double slider_fraction_to_db(double db)
40 {
41         if (db >= fader_control_points[0].second) {
42                 return fader_control_points[0].first;
43         }
44         if (db <= fader_control_points.back().second) {
45                 return fader_control_points.back().first;
46         }
47         for (unsigned i = 1; i < fader_control_points.size(); ++i) {
48                 const double x0 = fader_control_points[i].second;
49                 const double x1 = fader_control_points[i - 1].second;
50                 const double y0 = fader_control_points[i].first;
51                 const double y1 = fader_control_points[i - 1].first;
52                 if (db >= x0 && db <= x1) {
53                         const double t = (db - x0) / (x1 - x0);
54                         return y0 + t * (y1 - y0);
55                 }
56         }
57         assert(false);
58 }
59
60 double db_to_slider_fraction(double x)
61 {
62         if (x >= fader_control_points[0].first) {
63                 return fader_control_points[0].second;
64         }
65         if (x <= fader_control_points.back().first) {
66                 return fader_control_points.back().second;
67         }
68         for (unsigned i = 1; i < fader_control_points.size(); ++i) {
69                 const double x0 = fader_control_points[i].first;
70                 const double x1 = fader_control_points[i - 1].first;
71                 const double y0 = fader_control_points[i].second;
72                 const double y1 = fader_control_points[i - 1].second;
73                 if (x >= x0 && x <= x1) {
74                         const double t = (x - x0) / (x1 - x0);
75                         return y0 + t * (y1 - y0);
76                 }
77         }
78         assert(false);
79 }
80
81 }  // namespace
82
83 NonLinearFader::NonLinearFader(QWidget *parent)
84         : QSlider(parent)
85 {
86         update_slider_position();
87 }
88
89 void NonLinearFader::setDbValue(double db)
90 {
91         db_value = db;
92         update_slider_position();
93         emit dbValueChanged(db);
94 }
95
96 void NonLinearFader::paintEvent(QPaintEvent *event)
97 {
98         QStyleOptionSlider opt;
99         this->initStyleOption(&opt);
100         QRect gr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
101         QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
102
103         // FIXME: Where does the slider_length / 2 come from? I can't really find it
104         // in the Qt code, but it seems to match up with reality.
105         int slider_length = sr.height();
106         int slider_max = gr.top() + (slider_length / 2);
107         int slider_min = gr.bottom() + (slider_length / 2) - slider_length + 1;
108
109         QPainter p(this);
110
111         // Draw some ticks every 6 dB.
112         // FIXME: Find a way to make the slider wider, so that we have more space for tickmarks
113         // and some dB numbering.
114         int x_margin = 5;
115         p.setPen(Qt::darkGray);
116         for (int db = -84; db <= 6; db += 6) {
117                 int y = slider_min + lrint(db_to_slider_fraction(db) * (slider_max - slider_min));
118                 p.drawLine(QPoint(0, y), QPoint(gr.left() - x_margin, y));
119                 p.drawLine(QPoint(gr.right() + x_margin, y), QPoint(width() - 1, y));
120         }
121
122         QSlider::paintEvent(event);
123 }
124
125 void NonLinearFader::sliderChange(SliderChange change)
126 {
127         QSlider::sliderChange(change);
128         if (change == QAbstractSlider::SliderValueChange && !inhibit_updates) {
129                 if (value() == 0) {
130                         db_value = -HUGE_VAL;
131                 } else {
132                         double frac = double(value() - minimum()) / (maximum() - minimum());
133                         db_value = slider_fraction_to_db(frac);
134                 }
135                 emit dbValueChanged(db_value);
136         }
137 }
138
139 void NonLinearFader::update_slider_position()
140 {
141         inhibit_updates = true;
142         double val = db_to_slider_fraction(db_value) * (maximum() - minimum()) + minimum();
143         setValue(lrint(val));
144         inhibit_updates = false;
145 }