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