forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume-control.cpp
More file actions
251 lines (198 loc) · 5.83 KB
/
volume-control.cpp
File metadata and controls
251 lines (198 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "volume-control.hpp"
#include "qt-wrappers.hpp"
#include <util/platform.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSlider>
#include <QLabel>
#include <QPainter>
#include <QTimer>
#include <string>
#include <math.h>
using namespace std;
#define VOL_MIN -96.0f
#define VOL_MAX 0.0f
/*
VOL_MIN_LOG = DBToLog(VOL_MIN)
VOL_MAX_LOG = DBToLog(VOL_MAX)
... just in case someone wants to use a smaller scale
*/
#define VOL_MIN_LOG -2.0086001717619175
#define VOL_MAX_LOG -0.77815125038364363
#define UPDATE_INTERVAL_MS 50
static inline float DBToLog(float db)
{
return -log10f(0.0f - (db - 6.0f));
}
static inline float DBToLinear(float db_full)
{
float db = fmaxf(fminf(db_full, VOL_MAX), VOL_MIN);
return (DBToLog(db) - VOL_MIN_LOG) / (VOL_MAX_LOG - VOL_MIN_LOG);
}
void VolControl::OBSVolumeChanged(void *data, calldata_t calldata)
{
VolControl *volControl = static_cast<VolControl*>(data);
int vol = (int)(calldata_float(calldata, "volume") * 100.0f + 0.5f);
QMetaObject::invokeMethod(volControl, "VolumeChanged", Q_ARG(int, vol));
}
void VolControl::OBSVolumeLevel(void *data, calldata_t calldata)
{
VolControl *volControl = static_cast<VolControl*>(data);
float peak = calldata_float(calldata, "level");
float mag = calldata_float(calldata, "magnitude");
float peakHold = calldata_float(calldata, "peak");
QMetaObject::invokeMethod(volControl, "VolumeLevel",
Q_ARG(float, mag),
Q_ARG(float, peak),
Q_ARG(float, peakHold));
}
void VolControl::VolumeChanged(int vol)
{
signalChanged = false;
slider->setValue(vol);
signalChanged = true;
}
void VolControl::VolumeLevel(float mag, float peak, float peakHold)
{
uint64_t curMeterTime = os_gettime_ns() / 1000000;
/*
Add again peak averaging?
*/
/* only update after a certain amount of time */
if ((curMeterTime - lastMeterTime) > UPDATE_INTERVAL_MS) {
float vol = (float)slider->value() * 0.01f;
lastMeterTime = curMeterTime;
volMeter->setLevels(DBToLinear(mag) * vol,
DBToLinear(peak) * vol,
DBToLinear(peakHold) * vol);
}
}
void VolControl::SliderChanged(int vol)
{
if (signalChanged) {
signal_handler_disconnect(obs_source_signalhandler(source),
"volume", OBSVolumeChanged, this);
obs_source_setvolume(source, float(vol)*0.01f);
signal_handler_connect(obs_source_signalhandler(source),
"volume", OBSVolumeChanged, this);
}
volLabel->setText(QString::number(vol));
}
QString VolControl::GetName() const
{
return nameLabel->text();
}
void VolControl::SetName(const QString &newName)
{
nameLabel->setText(newName);
}
VolControl::VolControl(OBSSource source_)
: source (source_),
signalChanged (true),
lastMeterTime (0),
levelTotal (0.0f),
levelCount (0.0f)
{
QVBoxLayout *mainLayout = new QVBoxLayout();
QHBoxLayout *textLayout = new QHBoxLayout();
int vol = int(obs_source_getvolume(source) * 100.0f);
nameLabel = new QLabel();
volLabel = new QLabel();
volMeter = new VolumeMeter();
slider = new QSlider(Qt::Horizontal);
QFont font = nameLabel->font();
font.setPointSize(font.pointSize()-1);
nameLabel->setText(obs_source_getname(source));
nameLabel->setFont(font);
volLabel->setText(QString::number(vol));
volLabel->setFont(font);
slider->setMinimum(0);
slider->setMaximum(100);
slider->setValue(vol);
// slider->setMaximumHeight(13);
textLayout->setContentsMargins(0, 0, 0, 0);
textLayout->addWidget(nameLabel);
textLayout->addWidget(volLabel);
textLayout->setAlignment(nameLabel, Qt::AlignLeft);
textLayout->setAlignment(volLabel, Qt::AlignRight);
mainLayout->setContentsMargins(4, 4, 4, 4);
mainLayout->setSpacing(2);
mainLayout->addItem(textLayout);
mainLayout->addWidget(volMeter);
mainLayout->addWidget(slider);
setLayout(mainLayout);
signal_handler_connect(obs_source_signalhandler(source),
"volume", OBSVolumeChanged, this);
signal_handler_connect(obs_source_signalhandler(source),
"volume_level", OBSVolumeLevel, this);
QWidget::connect(slider, SIGNAL(valueChanged(int)),
this, SLOT(SliderChanged(int)));
}
VolControl::~VolControl()
{
signal_handler_disconnect(obs_source_signalhandler(source),
"volume", OBSVolumeChanged, this);
signal_handler_disconnect(obs_source_signalhandler(source),
"volume_level", OBSVolumeLevel, this);
}
VolumeMeter::VolumeMeter(QWidget *parent)
: QWidget(parent)
{
setMinimumSize(1, 3);
bkColor.setRgb(0xDD, 0xDD, 0xDD);
magColor.setRgb(0x20, 0x7D, 0x17);
peakColor.setRgb(0x3E, 0xF1, 0x2B);
peakHoldColor.setRgb(0x00, 0x00, 0x00);
resetTimer = new QTimer(this);
connect(resetTimer, SIGNAL(timeout()), this, SLOT(resetState()));
resetState();
}
void VolumeMeter::resetState(void)
{
setLevels(0.0f, 0.0f, 0.0f);
if (resetTimer->isActive())
resetTimer->stop();
}
void VolumeMeter::setLevels(float nmag, float npeak, float npeakHold)
{
mag = nmag;
peak = npeak;
peakHold = npeakHold;
update();
if (resetTimer->isActive())
resetTimer->stop();
resetTimer->start(250);
}
void VolumeMeter::paintEvent(QPaintEvent *event)
{
UNUSED_PARAMETER(event);
QPainter painter(this);
QLinearGradient gradient;
int width = size().width();
int height = size().height();
int scaledMag = int((float)width * mag);
int scaledPeak = int((float)width * peak);
int scaledPeakHold = int((float)width * peakHold);
gradient.setStart(qreal(scaledMag), 0);
gradient.setFinalStop(qreal(scaledPeak), 0);
gradient.setColorAt(0, magColor);
gradient.setColorAt(1, peakColor);
// RMS
painter.fillRect(0, 0,
scaledMag, height,
magColor);
// RMS - Peak gradient
painter.fillRect(scaledMag, 0,
scaledPeak - scaledMag + 1, height,
QBrush(gradient));
// Background
painter.fillRect(scaledPeak, 0,
width - scaledPeak, height,
bkColor);
// Peak hold
if (peakHold == 1.0f)
scaledPeakHold--;
painter.setPen(peakHoldColor);
painter.drawLine(scaledPeakHold, 0,
scaledPeakHold, height);
}