volume-control.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #include "volume-control.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include "obs-app.hpp"
  4. #include "mute-checkbox.hpp"
  5. #include "slider-absoluteset-style.hpp"
  6. #include <obs-audio-controls.h>
  7. #include <util/platform.h>
  8. #include <util/threading.h>
  9. #include <QFontDatabase>
  10. #include <QHBoxLayout>
  11. #include <QVBoxLayout>
  12. #include <QPushButton>
  13. #include <QVariant>
  14. #include <QSlider>
  15. #include <QLabel>
  16. #include <QPainter>
  17. #include <QTimer>
  18. #include <string>
  19. #include <math.h>
  20. using namespace std;
  21. #define CLAMP(x, min, max) ((x) < min ? min : ((x) > max ? max : (x)))
  22. QWeakPointer<VolumeMeterTimer> VolumeMeter::updateTimer;
  23. void VolControl::OBSVolumeChanged(void *data, float db)
  24. {
  25. Q_UNUSED(db);
  26. VolControl *volControl = static_cast<VolControl*>(data);
  27. QMetaObject::invokeMethod(volControl, "VolumeChanged");
  28. }
  29. void VolControl::OBSVolumeLevel(void *data,
  30. const float magnitude[MAX_AUDIO_CHANNELS],
  31. const float peak[MAX_AUDIO_CHANNELS],
  32. const float inputPeak[MAX_AUDIO_CHANNELS])
  33. {
  34. VolControl *volControl = static_cast<VolControl*>(data);
  35. volControl->volMeter->setLevels(magnitude, peak, inputPeak);
  36. }
  37. void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
  38. {
  39. VolControl *volControl = static_cast<VolControl*>(data);
  40. bool muted = calldata_bool(calldata, "muted");
  41. QMetaObject::invokeMethod(volControl, "VolumeMuted",
  42. Q_ARG(bool, muted));
  43. }
  44. void VolControl::VolumeChanged()
  45. {
  46. slider->blockSignals(true);
  47. slider->setValue((int) (obs_fader_get_deflection(obs_fader) * 100.0f));
  48. slider->blockSignals(false);
  49. updateText();
  50. }
  51. void VolControl::VolumeMuted(bool muted)
  52. {
  53. if (mute->isChecked() != muted)
  54. mute->setChecked(muted);
  55. }
  56. void VolControl::SetMuted(bool checked)
  57. {
  58. obs_source_set_muted(source, checked);
  59. }
  60. void VolControl::SliderChanged(int vol)
  61. {
  62. obs_fader_set_deflection(obs_fader, float(vol) * 0.01f);
  63. updateText();
  64. }
  65. void VolControl::updateText()
  66. {
  67. QString db = QString::number(obs_fader_get_db(obs_fader), 'f', 1)
  68. .append(" dB");
  69. volLabel->setText(db);
  70. bool muted = obs_source_muted(source);
  71. const char *accTextLookup = muted
  72. ? "VolControl.SliderMuted"
  73. : "VolControl.SliderUnmuted";
  74. QString sourceName = obs_source_get_name(source);
  75. QString accText = QTStr(accTextLookup).arg(sourceName, db);
  76. slider->setAccessibleName(accText);
  77. }
  78. QString VolControl::GetName() const
  79. {
  80. return nameLabel->text();
  81. }
  82. void VolControl::SetName(const QString &newName)
  83. {
  84. nameLabel->setText(newName);
  85. }
  86. void VolControl::EmitConfigClicked()
  87. {
  88. emit ConfigClicked();
  89. }
  90. VolControl::VolControl(OBSSource source_, bool showConfig)
  91. : source (source_),
  92. levelTotal (0.0f),
  93. levelCount (0.0f),
  94. obs_fader (obs_fader_create(OBS_FADER_CUBIC)),
  95. obs_volmeter (obs_volmeter_create(OBS_FADER_LOG))
  96. {
  97. QHBoxLayout *volLayout = new QHBoxLayout();
  98. QVBoxLayout *mainLayout = new QVBoxLayout();
  99. QHBoxLayout *textLayout = new QHBoxLayout();
  100. QHBoxLayout *botLayout = new QHBoxLayout();
  101. nameLabel = new QLabel();
  102. volLabel = new QLabel();
  103. volMeter = new VolumeMeter(0, obs_volmeter);
  104. mute = new MuteCheckBox();
  105. slider = new QSlider(Qt::Horizontal);
  106. QFont font = nameLabel->font();
  107. font.setPointSize(font.pointSize()-1);
  108. QString sourceName = obs_source_get_name(source);
  109. nameLabel->setText(sourceName);
  110. nameLabel->setFont(font);
  111. volLabel->setFont(font);
  112. slider->setMinimum(0);
  113. slider->setMaximum(100);
  114. // slider->setMaximumHeight(13);
  115. textLayout->setContentsMargins(0, 0, 0, 0);
  116. textLayout->addWidget(nameLabel);
  117. textLayout->addWidget(volLabel);
  118. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  119. textLayout->setAlignment(volLabel, Qt::AlignRight);
  120. bool muted = obs_source_muted(source);
  121. mute->setChecked(muted);
  122. mute->setAccessibleName(
  123. QTStr("VolControl.Mute").arg(sourceName));
  124. volLayout->addWidget(slider);
  125. volLayout->addWidget(mute);
  126. volLayout->setSpacing(5);
  127. botLayout->setContentsMargins(0, 0, 0, 0);
  128. botLayout->setSpacing(0);
  129. botLayout->addLayout(volLayout);
  130. if (showConfig) {
  131. config = new QPushButton(this);
  132. config->setProperty("themeID", "configIconSmall");
  133. config->setFlat(true);
  134. config->setSizePolicy(QSizePolicy::Maximum,
  135. QSizePolicy::Maximum);
  136. config->setMaximumSize(22, 22);
  137. config->setAutoDefault(false);
  138. config->setAccessibleName(QTStr("VolControl.Properties")
  139. .arg(sourceName));
  140. connect(config, &QAbstractButton::clicked,
  141. this, &VolControl::EmitConfigClicked);
  142. botLayout->addWidget(config);
  143. }
  144. mainLayout->setContentsMargins(4, 4, 4, 4);
  145. mainLayout->setSpacing(2);
  146. mainLayout->addItem(textLayout);
  147. mainLayout->addWidget(volMeter);
  148. mainLayout->addItem(botLayout);
  149. setLayout(mainLayout);
  150. obs_fader_add_callback(obs_fader, OBSVolumeChanged, this);
  151. obs_volmeter_add_callback(obs_volmeter, OBSVolumeLevel, this);
  152. signal_handler_connect(obs_source_get_signal_handler(source),
  153. "mute", OBSVolumeMuted, this);
  154. QWidget::connect(slider, SIGNAL(valueChanged(int)),
  155. this, SLOT(SliderChanged(int)));
  156. QWidget::connect(mute, SIGNAL(clicked(bool)),
  157. this, SLOT(SetMuted(bool)));
  158. obs_fader_attach_source(obs_fader, source);
  159. obs_volmeter_attach_source(obs_volmeter, source);
  160. slider->setStyle(new SliderAbsoluteSetStyle(slider->style()));
  161. /* Call volume changed once to init the slider position and label */
  162. VolumeChanged();
  163. }
  164. VolControl::~VolControl()
  165. {
  166. obs_fader_remove_callback(obs_fader, OBSVolumeChanged, this);
  167. obs_volmeter_remove_callback(obs_volmeter, OBSVolumeLevel, this);
  168. signal_handler_disconnect(obs_source_get_signal_handler(source),
  169. "mute", OBSVolumeMuted, this);
  170. obs_fader_destroy(obs_fader);
  171. obs_volmeter_destroy(obs_volmeter);
  172. }
  173. QColor VolumeMeter::getBackgroundNominalColor() const
  174. {
  175. return backgroundNominalColor;
  176. }
  177. void VolumeMeter::setBackgroundNominalColor(QColor c)
  178. {
  179. backgroundNominalColor = c;
  180. }
  181. QColor VolumeMeter::getBackgroundWarningColor() const
  182. {
  183. return backgroundWarningColor;
  184. }
  185. void VolumeMeter::setBackgroundWarningColor(QColor c)
  186. {
  187. backgroundWarningColor = c;
  188. }
  189. QColor VolumeMeter::getBackgroundErrorColor() const
  190. {
  191. return backgroundErrorColor;
  192. }
  193. void VolumeMeter::setBackgroundErrorColor(QColor c)
  194. {
  195. backgroundErrorColor = c;
  196. }
  197. QColor VolumeMeter::getForegroundNominalColor() const
  198. {
  199. return foregroundNominalColor;
  200. }
  201. void VolumeMeter::setForegroundNominalColor(QColor c)
  202. {
  203. foregroundNominalColor = c;
  204. }
  205. QColor VolumeMeter::getForegroundWarningColor() const
  206. {
  207. return foregroundWarningColor;
  208. }
  209. void VolumeMeter::setForegroundWarningColor(QColor c)
  210. {
  211. foregroundWarningColor = c;
  212. }
  213. QColor VolumeMeter::getForegroundErrorColor() const
  214. {
  215. return foregroundErrorColor;
  216. }
  217. void VolumeMeter::setForegroundErrorColor(QColor c)
  218. {
  219. foregroundErrorColor = c;
  220. }
  221. QColor VolumeMeter::getClipColor() const
  222. {
  223. return clipColor;
  224. }
  225. void VolumeMeter::setClipColor(QColor c)
  226. {
  227. clipColor = c;
  228. }
  229. QColor VolumeMeter::getMagnitudeColor() const
  230. {
  231. return magnitudeColor;
  232. }
  233. void VolumeMeter::setMagnitudeColor(QColor c)
  234. {
  235. magnitudeColor = c;
  236. }
  237. QColor VolumeMeter::getMajorTickColor() const
  238. {
  239. return majorTickColor;
  240. }
  241. void VolumeMeter::setMajorTickColor(QColor c)
  242. {
  243. majorTickColor = c;
  244. }
  245. QColor VolumeMeter::getMinorTickColor() const
  246. {
  247. return minorTickColor;
  248. }
  249. void VolumeMeter::setMinorTickColor(QColor c)
  250. {
  251. minorTickColor = c;
  252. }
  253. qreal VolumeMeter::getMinimumLevel() const
  254. {
  255. return minimumLevel;
  256. }
  257. void VolumeMeter::setMinimumLevel(qreal v)
  258. {
  259. minimumLevel = v;
  260. }
  261. qreal VolumeMeter::getWarningLevel() const
  262. {
  263. return warningLevel;
  264. }
  265. void VolumeMeter::setWarningLevel(qreal v)
  266. {
  267. warningLevel = v;
  268. }
  269. qreal VolumeMeter::getErrorLevel() const
  270. {
  271. return errorLevel;
  272. }
  273. void VolumeMeter::setErrorLevel(qreal v)
  274. {
  275. errorLevel = v;
  276. }
  277. qreal VolumeMeter::getClipLevel() const
  278. {
  279. return clipLevel;
  280. }
  281. void VolumeMeter::setClipLevel(qreal v)
  282. {
  283. clipLevel = v;
  284. }
  285. qreal VolumeMeter::getMinimumInputLevel() const
  286. {
  287. return minimumInputLevel;
  288. }
  289. void VolumeMeter::setMinimumInputLevel(qreal v)
  290. {
  291. minimumInputLevel = v;
  292. }
  293. qreal VolumeMeter::getPeakDecayRate() const
  294. {
  295. return peakDecayRate;
  296. }
  297. void VolumeMeter::setPeakDecayRate(qreal v)
  298. {
  299. peakDecayRate = v;
  300. }
  301. qreal VolumeMeter::getMagnitudeIntegrationTime() const
  302. {
  303. return magnitudeIntegrationTime;
  304. }
  305. void VolumeMeter::setMagnitudeIntegrationTime(qreal v)
  306. {
  307. magnitudeIntegrationTime = v;
  308. }
  309. qreal VolumeMeter::getPeakHoldDuration() const
  310. {
  311. return peakHoldDuration;
  312. }
  313. void VolumeMeter::setPeakHoldDuration(qreal v)
  314. {
  315. peakHoldDuration = v;
  316. }
  317. qreal VolumeMeter::getInputPeakHoldDuration() const
  318. {
  319. return inputPeakHoldDuration;
  320. }
  321. void VolumeMeter::setInputPeakHoldDuration(qreal v)
  322. {
  323. inputPeakHoldDuration = v;
  324. }
  325. VolumeMeter::VolumeMeter(QWidget *parent, obs_volmeter_t *obs_volmeter)
  326. : QWidget(parent), obs_volmeter(obs_volmeter)
  327. {
  328. // Use a font that can be rendered small.
  329. tickFont = QFont("Arial");
  330. tickFont.setPixelSize(7);
  331. // Default meter color settings, they only show if
  332. // there is no stylesheet, do not remove.
  333. backgroundNominalColor.setRgb(0x26, 0x7f, 0x26); // Dark green
  334. backgroundWarningColor.setRgb(0x7f, 0x7f, 0x26); // Dark yellow
  335. backgroundErrorColor.setRgb(0x7f, 0x26, 0x26); // Dark red
  336. foregroundNominalColor.setRgb(0x4c, 0xff, 0x4c); // Bright green
  337. foregroundWarningColor.setRgb(0xff, 0xff, 0x4c); // Bright yellow
  338. foregroundErrorColor.setRgb(0xff, 0x4c, 0x4c); // Bright red
  339. clipColor.setRgb(0xff, 0xff, 0xff); // Bright white
  340. magnitudeColor.setRgb(0x00, 0x00, 0x00); // Black
  341. majorTickColor.setRgb(0xff, 0xff, 0xff); // Black
  342. minorTickColor.setRgb(0xcc, 0xcc, 0xcc); // Black
  343. minimumLevel = -60.0; // -60 dB
  344. warningLevel = -20.0; // -20 dB
  345. errorLevel = -9.0; // -9 dB
  346. clipLevel = -0.5; // -0.5 dB
  347. minimumInputLevel = -50.0; // -50 dB
  348. peakDecayRate = 11.7; // 20 dB / 1.7 sec
  349. magnitudeIntegrationTime = 0.3; // 99% in 300 ms
  350. peakHoldDuration = 20.0; // 20 seconds
  351. inputPeakHoldDuration = 1.0; // 1 second
  352. handleChannelCofigurationChange();
  353. updateTimerRef = updateTimer.toStrongRef();
  354. if (!updateTimerRef) {
  355. updateTimerRef = QSharedPointer<VolumeMeterTimer>::create();
  356. updateTimerRef->start(34);
  357. updateTimer = updateTimerRef;
  358. }
  359. updateTimerRef->AddVolControl(this);
  360. }
  361. VolumeMeter::~VolumeMeter()
  362. {
  363. updateTimerRef->RemoveVolControl(this);
  364. }
  365. void VolumeMeter::setLevels(
  366. const float magnitude[MAX_AUDIO_CHANNELS],
  367. const float peak[MAX_AUDIO_CHANNELS],
  368. const float inputPeak[MAX_AUDIO_CHANNELS])
  369. {
  370. uint64_t ts = os_gettime_ns();
  371. QMutexLocker locker(&dataMutex);
  372. currentLastUpdateTime = ts;
  373. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  374. currentMagnitude[channelNr] = magnitude[channelNr];
  375. currentPeak[channelNr] = peak[channelNr];
  376. currentInputPeak[channelNr] = inputPeak[channelNr];
  377. }
  378. // In case there are more updates then redraws we must make sure
  379. // that the ballistics of peak and hold are recalculated.
  380. locker.unlock();
  381. calculateBallistics(ts);
  382. }
  383. inline void VolumeMeter::resetLevels()
  384. {
  385. currentLastUpdateTime = 0;
  386. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  387. currentMagnitude[channelNr] = -M_INFINITE;
  388. currentPeak[channelNr] = -M_INFINITE;
  389. currentInputPeak[channelNr] = -M_INFINITE;
  390. displayMagnitude[channelNr] = -M_INFINITE;
  391. displayPeak[channelNr] = -M_INFINITE;
  392. displayPeakHold[channelNr] = -M_INFINITE;
  393. displayPeakHoldLastUpdateTime[channelNr] = 0;
  394. displayInputPeakHold[channelNr] = -M_INFINITE;
  395. displayInputPeakHoldLastUpdateTime[channelNr] = 0;
  396. }
  397. }
  398. inline void VolumeMeter::handleChannelCofigurationChange()
  399. {
  400. QMutexLocker locker(&dataMutex);
  401. int currentNrAudioChannels = obs_volmeter_get_nr_channels(obs_volmeter);
  402. if (displayNrAudioChannels != currentNrAudioChannels) {
  403. displayNrAudioChannels = currentNrAudioChannels;
  404. // Make room for 3 pixels high meter, with one pixel between
  405. // each. Then 9 pixels below it for ticks and numbers.
  406. setMinimumSize(130, displayNrAudioChannels * 4 + 8);
  407. resetLevels();
  408. }
  409. }
  410. inline bool VolumeMeter::detectIdle(uint64_t ts)
  411. {
  412. float timeSinceLastUpdate = (ts - currentLastUpdateTime) * 0.000000001;
  413. if (timeSinceLastUpdate > 0.5) {
  414. resetLevels();
  415. return true;
  416. } else {
  417. return false;
  418. }
  419. }
  420. inline void VolumeMeter::calculateBallisticsForChannel(int channelNr,
  421. uint64_t ts, qreal timeSinceLastRedraw)
  422. {
  423. if (currentPeak[channelNr] >= displayPeak[channelNr] ||
  424. isnan(displayPeak[channelNr])) {
  425. // Attack of peak is immediate.
  426. displayPeak[channelNr] = currentPeak[channelNr];
  427. } else {
  428. // Decay of peak is 20 dB / 1.7 seconds.
  429. qreal decay = peakDecayRate * timeSinceLastRedraw;
  430. displayPeak[channelNr] = CLAMP(displayPeak[channelNr] - decay,
  431. currentPeak[channelNr], 0);
  432. }
  433. if (currentPeak[channelNr] >= displayPeakHold[channelNr] ||
  434. !isfinite(displayPeakHold[channelNr])) {
  435. // Attack of peak-hold is immediate, but keep track
  436. // when it was last updated.
  437. displayPeakHold[channelNr] = currentPeak[channelNr];
  438. displayPeakHoldLastUpdateTime[channelNr] = ts;
  439. } else {
  440. // The peak and hold falls back to peak
  441. // after 20 seconds.
  442. qreal timeSinceLastPeak = (uint64_t)(ts -
  443. displayPeakHoldLastUpdateTime[channelNr]) * 0.000000001;
  444. if (timeSinceLastPeak > peakHoldDuration) {
  445. displayPeakHold[channelNr] = currentPeak[channelNr];
  446. displayPeakHoldLastUpdateTime[channelNr] = ts;
  447. }
  448. }
  449. if (currentInputPeak[channelNr] >= displayInputPeakHold[channelNr] ||
  450. !isfinite(displayInputPeakHold[channelNr])) {
  451. // Attack of peak-hold is immediate, but keep track
  452. // when it was last updated.
  453. displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
  454. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  455. } else {
  456. // The peak and hold falls back to peak after 1 second.
  457. qreal timeSinceLastPeak = (uint64_t)(ts -
  458. displayInputPeakHoldLastUpdateTime[channelNr]) *
  459. 0.000000001;
  460. if (timeSinceLastPeak > inputPeakHoldDuration) {
  461. displayInputPeakHold[channelNr] =
  462. currentInputPeak[channelNr];
  463. displayInputPeakHoldLastUpdateTime[channelNr] =
  464. ts;
  465. }
  466. }
  467. if (!isfinite(displayMagnitude[channelNr])) {
  468. // The statements in the else-leg do not work with
  469. // NaN and infinite displayMagnitude.
  470. displayMagnitude[channelNr] =
  471. currentMagnitude[channelNr];
  472. } else {
  473. // A VU meter will integrate to the new value to 99% in 300 ms.
  474. // The calculation here is very simplified and is more accurate
  475. // with higher frame-rate.
  476. qreal attack = (currentMagnitude[channelNr] -
  477. displayMagnitude[channelNr]) *
  478. (timeSinceLastRedraw /
  479. magnitudeIntegrationTime) * 0.99;
  480. displayMagnitude[channelNr] = CLAMP(
  481. displayMagnitude[channelNr] + attack,
  482. minimumLevel, 0);
  483. }
  484. }
  485. inline void VolumeMeter::calculateBallistics(uint64_t ts,
  486. qreal timeSinceLastRedraw)
  487. {
  488. QMutexLocker locker(&dataMutex);
  489. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  490. calculateBallisticsForChannel(channelNr, ts,
  491. timeSinceLastRedraw);
  492. }
  493. }
  494. void VolumeMeter::paintInputMeter(QPainter &painter, int x, int y,
  495. int width, int height, float peakHold)
  496. {
  497. QMutexLocker locker(&dataMutex);
  498. if (peakHold < minimumInputLevel) {
  499. painter.fillRect(x, y, width, height, backgroundNominalColor);
  500. } else if (peakHold < warningLevel) {
  501. painter.fillRect(x, y, width, height, foregroundNominalColor);
  502. } else if (peakHold < errorLevel) {
  503. painter.fillRect(x, y, width, height, foregroundWarningColor);
  504. } else if (peakHold <= clipLevel) {
  505. painter.fillRect(x, y, width, height, foregroundErrorColor);
  506. } else {
  507. painter.fillRect(x, y, width, height, clipColor);
  508. }
  509. }
  510. void VolumeMeter::paintTicks(QPainter &painter, int x, int y,
  511. int width, int height)
  512. {
  513. qreal scale = width / minimumLevel;
  514. painter.setFont(tickFont);
  515. painter.setPen(majorTickColor);
  516. // Draw major tick lines and numeric indicators.
  517. for (int i = 0; i >= minimumLevel; i-= 5) {
  518. int position = x + width - (i * scale) - 1;
  519. QString str = QString::number(i);
  520. if (i == 0 || i == -5) {
  521. painter.drawText(position - 3, height, str);
  522. } else {
  523. painter.drawText(position - 5, height, str);
  524. }
  525. painter.drawLine(position, y, position, y + 2);
  526. }
  527. // Draw minor tick lines.
  528. painter.setPen(minorTickColor);
  529. for (int i = 0; i >= minimumLevel; i--) {
  530. int position = x + width - (i * scale) - 1;
  531. if (i % 5 != 0) {
  532. painter.drawLine(position, y, position, y + 1);
  533. }
  534. }
  535. }
  536. void VolumeMeter::paintMeter(QPainter &painter, int x, int y,
  537. int width, int height, float magnitude, float peak, float peakHold)
  538. {
  539. qreal scale = width / minimumLevel;
  540. QMutexLocker locker(&dataMutex);
  541. int minimumPosition = x + 0;
  542. int maximumPosition = x + width;
  543. int magnitudePosition = x + width - (magnitude * scale);
  544. int peakPosition = x + width - (peak * scale);
  545. int peakHoldPosition = x + width - (peakHold * scale);
  546. int warningPosition = x + width - (warningLevel * scale);
  547. int errorPosition = x + width - (errorLevel * scale);
  548. int nominalLength = warningPosition - minimumPosition;
  549. int warningLength = errorPosition - warningPosition;
  550. int errorLength = maximumPosition - errorPosition;
  551. locker.unlock();
  552. if (peakPosition < minimumPosition) {
  553. painter.fillRect(
  554. minimumPosition, y,
  555. nominalLength, height,
  556. backgroundNominalColor);
  557. painter.fillRect(
  558. warningPosition, y,
  559. warningLength, height,
  560. backgroundWarningColor);
  561. painter.fillRect(
  562. errorPosition, y,
  563. errorLength, height,
  564. backgroundErrorColor);
  565. } else if (peakPosition < warningPosition) {
  566. painter.fillRect(
  567. minimumPosition, y,
  568. peakPosition - minimumPosition, height,
  569. foregroundNominalColor);
  570. painter.fillRect(
  571. peakPosition, y,
  572. warningPosition - peakPosition, height,
  573. backgroundNominalColor);
  574. painter.fillRect(
  575. warningPosition, y,
  576. warningLength, height,
  577. backgroundWarningColor);
  578. painter.fillRect(errorPosition, y,
  579. errorLength, height,
  580. backgroundErrorColor);
  581. } else if (peakPosition < errorPosition) {
  582. painter.fillRect(
  583. minimumPosition, y,
  584. nominalLength, height,
  585. foregroundNominalColor);
  586. painter.fillRect(
  587. warningPosition, y,
  588. peakPosition - warningPosition, height,
  589. foregroundWarningColor);
  590. painter.fillRect(
  591. peakPosition, y,
  592. errorPosition - peakPosition, height,
  593. backgroundWarningColor);
  594. painter.fillRect(
  595. errorPosition, y,
  596. errorLength, height,
  597. backgroundErrorColor);
  598. } else if (peakPosition < maximumPosition) {
  599. painter.fillRect(
  600. minimumPosition, y,
  601. nominalLength, height,
  602. foregroundNominalColor);
  603. painter.fillRect(
  604. warningPosition, y,
  605. warningLength, height,
  606. foregroundWarningColor);
  607. painter.fillRect(
  608. errorPosition, y,
  609. peakPosition - errorPosition, height,
  610. foregroundErrorColor);
  611. painter.fillRect(
  612. peakPosition, y,
  613. maximumPosition - peakPosition, height,
  614. backgroundErrorColor);
  615. } else {
  616. painter.fillRect(
  617. minimumPosition, y,
  618. nominalLength, height,
  619. foregroundNominalColor);
  620. painter.fillRect(
  621. warningPosition, y,
  622. warningLength, height,
  623. foregroundWarningColor);
  624. painter.fillRect(
  625. errorPosition, y,
  626. errorLength, height,
  627. foregroundErrorColor);
  628. }
  629. if (peakHoldPosition - 3 < minimumPosition) {
  630. // Peak-hold below minimum, no drawing.
  631. } else if (peakHoldPosition < warningPosition) {
  632. painter.fillRect(
  633. peakHoldPosition - 3, y,
  634. 3, height,
  635. foregroundNominalColor);
  636. } else if (peakHoldPosition < errorPosition) {
  637. painter.fillRect(
  638. peakHoldPosition - 3, y,
  639. 3, height,
  640. foregroundWarningColor);
  641. } else {
  642. painter.fillRect(
  643. peakHoldPosition - 3, y,
  644. 3, height,
  645. foregroundErrorColor);
  646. }
  647. if (magnitudePosition - 3 < minimumPosition) {
  648. // Magnitude below minimum, no drawing.
  649. } else if (magnitudePosition < warningPosition) {
  650. painter.fillRect(
  651. magnitudePosition - 3, y,
  652. 3, height,
  653. magnitudeColor);
  654. } else if (magnitudePosition < errorPosition) {
  655. painter.fillRect(
  656. magnitudePosition - 3, y,
  657. 3, height,
  658. magnitudeColor);
  659. } else {
  660. painter.fillRect(
  661. magnitudePosition - 3, y,
  662. 3, height,
  663. magnitudeColor);
  664. }
  665. }
  666. void VolumeMeter::paintEvent(QPaintEvent *event)
  667. {
  668. UNUSED_PARAMETER(event);
  669. uint64_t ts = os_gettime_ns();
  670. qreal timeSinceLastRedraw = (ts - lastRedrawTime) * 0.000000001;
  671. int width = size().width();
  672. int height = size().height();
  673. handleChannelCofigurationChange();
  674. calculateBallistics(ts, timeSinceLastRedraw);
  675. bool idle = detectIdle(ts);
  676. // Draw the ticks in a off-screen buffer when the widget changes size.
  677. QSize tickPaintCacheSize = QSize(width, 9);
  678. if (tickPaintCache == NULL ||
  679. tickPaintCache->size() != tickPaintCacheSize) {
  680. delete tickPaintCache;
  681. tickPaintCache = new QPixmap(tickPaintCacheSize);
  682. QColor clearColor(0, 0, 0, 0);
  683. tickPaintCache->fill(clearColor);
  684. QPainter tickPainter(tickPaintCache);
  685. paintTicks(tickPainter, 6, 0, tickPaintCacheSize.width() - 6,
  686. tickPaintCacheSize.height());
  687. tickPainter.end();
  688. }
  689. // Actual painting of the widget starts here.
  690. QPainter painter(this);
  691. painter.drawPixmap(0, height - 9, *tickPaintCache);
  692. for (int channelNr = 0; channelNr < displayNrAudioChannels;
  693. channelNr++) {
  694. paintMeter(painter,
  695. 5, channelNr * 4, width - 5, 3,
  696. displayMagnitude[channelNr], displayPeak[channelNr],
  697. displayPeakHold[channelNr]);
  698. if (!idle) {
  699. // By not drawing the input meter boxes the user can
  700. // see that the audio stream has been stopped, without
  701. // having too much visual impact.
  702. paintInputMeter(painter,
  703. 0, channelNr * 4, 3, 3,
  704. displayInputPeakHold[channelNr]);
  705. }
  706. }
  707. lastRedrawTime = ts;
  708. }
  709. void VolumeMeterTimer::AddVolControl(VolumeMeter *meter)
  710. {
  711. volumeMeters.push_back(meter);
  712. }
  713. void VolumeMeterTimer::RemoveVolControl(VolumeMeter *meter)
  714. {
  715. volumeMeters.removeOne(meter);
  716. }
  717. void VolumeMeterTimer::timerEvent(QTimerEvent*)
  718. {
  719. for (VolumeMeter *meter : volumeMeters)
  720. meter->update();
  721. }