volume-control.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "volume-control.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include "mute-checkbox.hpp"
  4. #include "slider-absoluteset-style.hpp"
  5. #include <util/platform.h>
  6. #include <QHBoxLayout>
  7. #include <QVBoxLayout>
  8. #include <QSlider>
  9. #include <QLabel>
  10. #include <QPainter>
  11. #include <QTimer>
  12. #include <string>
  13. #include <math.h>
  14. using namespace std;
  15. void VolControl::OBSVolumeChanged(void *data, calldata_t *calldata)
  16. {
  17. Q_UNUSED(calldata);
  18. VolControl *volControl = static_cast<VolControl*>(data);
  19. QMetaObject::invokeMethod(volControl, "VolumeChanged");
  20. }
  21. void VolControl::OBSVolumeLevel(void *data, calldata_t *calldata)
  22. {
  23. VolControl *volControl = static_cast<VolControl*>(data);
  24. float peak = calldata_float(calldata, "level");
  25. float mag = calldata_float(calldata, "magnitude");
  26. float peakHold = calldata_float(calldata, "peak");
  27. QMetaObject::invokeMethod(volControl, "VolumeLevel",
  28. Q_ARG(float, mag),
  29. Q_ARG(float, peak),
  30. Q_ARG(float, peakHold));
  31. }
  32. void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
  33. {
  34. VolControl *volControl = static_cast<VolControl*>(data);
  35. bool muted = calldata_bool(calldata, "muted");
  36. QMetaObject::invokeMethod(volControl, "VolumeMuted",
  37. Q_ARG(bool, muted));
  38. }
  39. void VolControl::VolumeChanged()
  40. {
  41. slider->blockSignals(true);
  42. slider->setValue((int) (obs_fader_get_deflection(obs_fader) * 100.0f));
  43. slider->blockSignals(false);
  44. updateText();
  45. }
  46. void VolControl::VolumeLevel(float mag, float peak, float peakHold)
  47. {
  48. if (obs_source_muted(source) || !obs_source_enabled(source)) {
  49. mag = 0.0f;
  50. peak = 0.0f;
  51. peakHold = 0.0f;
  52. }
  53. volMeter->setLevels(mag, peak, peakHold);
  54. }
  55. void VolControl::VolumeMuted(bool muted)
  56. {
  57. if (mute->isChecked() != muted)
  58. mute->setChecked(muted);
  59. }
  60. void VolControl::SetMuted(bool checked)
  61. {
  62. obs_source_set_muted(source, checked);
  63. }
  64. void VolControl::SliderChanged(int vol)
  65. {
  66. obs_fader_set_deflection(obs_fader, float(vol) * 0.01f);
  67. updateText();
  68. }
  69. void VolControl::updateText()
  70. {
  71. volLabel->setText(QString::number(obs_fader_get_db(obs_fader), 'f', 1)
  72. .append(" dB"));
  73. }
  74. QString VolControl::GetName() const
  75. {
  76. return nameLabel->text();
  77. }
  78. void VolControl::SetName(const QString &newName)
  79. {
  80. nameLabel->setText(newName);
  81. }
  82. VolControl::VolControl(OBSSource source_)
  83. : source (source_),
  84. levelTotal (0.0f),
  85. levelCount (0.0f),
  86. obs_fader (obs_fader_create(OBS_FADER_CUBIC)),
  87. obs_volmeter (obs_volmeter_create(OBS_FADER_LOG))
  88. {
  89. QHBoxLayout *volLayout = new QHBoxLayout();
  90. QVBoxLayout *mainLayout = new QVBoxLayout();
  91. QHBoxLayout *textLayout = new QHBoxLayout();
  92. nameLabel = new QLabel();
  93. volLabel = new QLabel();
  94. volMeter = new VolumeMeter();
  95. mute = new MuteCheckBox();
  96. slider = new QSlider(Qt::Horizontal);
  97. QFont font = nameLabel->font();
  98. font.setPointSize(font.pointSize()-1);
  99. nameLabel->setText(obs_source_get_name(source));
  100. nameLabel->setFont(font);
  101. volLabel->setFont(font);
  102. slider->setMinimum(0);
  103. slider->setMaximum(100);
  104. // slider->setMaximumHeight(13);
  105. textLayout->setContentsMargins(0, 0, 0, 0);
  106. textLayout->addWidget(nameLabel);
  107. textLayout->addWidget(volLabel);
  108. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  109. textLayout->setAlignment(volLabel, Qt::AlignRight);
  110. mute->setChecked(obs_source_muted(source));
  111. volLayout->addWidget(slider);
  112. volLayout->addWidget(mute);
  113. volLayout->setSpacing(5);
  114. mainLayout->setContentsMargins(4, 4, 4, 4);
  115. mainLayout->setSpacing(2);
  116. mainLayout->addItem(textLayout);
  117. mainLayout->addWidget(volMeter);
  118. mainLayout->addItem(volLayout);
  119. setLayout(mainLayout);
  120. signal_handler_connect(obs_fader_get_signal_handler(obs_fader),
  121. "volume_changed", OBSVolumeChanged, this);
  122. signal_handler_connect(obs_volmeter_get_signal_handler(obs_volmeter),
  123. "levels_updated", OBSVolumeLevel, this);
  124. signal_handler_connect(obs_source_get_signal_handler(source),
  125. "mute", OBSVolumeMuted, this);
  126. QWidget::connect(slider, SIGNAL(valueChanged(int)),
  127. this, SLOT(SliderChanged(int)));
  128. QWidget::connect(mute, SIGNAL(clicked(bool)),
  129. this, SLOT(SetMuted(bool)));
  130. obs_fader_attach_source(obs_fader, source);
  131. obs_volmeter_attach_source(obs_volmeter, source);
  132. slider->setStyle(new SliderAbsoluteSetStyle(slider->style()));
  133. /* Call volume changed once to init the slider position and label */
  134. VolumeChanged();
  135. }
  136. VolControl::~VolControl()
  137. {
  138. signal_handler_disconnect(obs_fader_get_signal_handler(obs_fader),
  139. "volume_changed", OBSVolumeChanged, this);
  140. signal_handler_disconnect(obs_volmeter_get_signal_handler(obs_volmeter),
  141. "levels_updated", OBSVolumeLevel, this);
  142. signal_handler_disconnect(obs_source_get_signal_handler(source),
  143. "mute", OBSVolumeMuted, this);
  144. obs_fader_destroy(obs_fader);
  145. obs_volmeter_destroy(obs_volmeter);
  146. }
  147. QColor VolumeMeter::getBkColor() const
  148. {
  149. return bkColor;
  150. }
  151. void VolumeMeter::setBkColor(QColor c)
  152. {
  153. bkColor = c;
  154. }
  155. QColor VolumeMeter::getMagColor() const
  156. {
  157. return magColor;
  158. }
  159. void VolumeMeter::setMagColor(QColor c)
  160. {
  161. magColor = c;
  162. }
  163. QColor VolumeMeter::getPeakColor() const
  164. {
  165. return peakColor;
  166. }
  167. void VolumeMeter::setPeakColor(QColor c)
  168. {
  169. peakColor = c;
  170. }
  171. QColor VolumeMeter::getPeakHoldColor() const
  172. {
  173. return peakHoldColor;
  174. }
  175. void VolumeMeter::setPeakHoldColor(QColor c)
  176. {
  177. peakHoldColor = c;
  178. }
  179. VolumeMeter::VolumeMeter(QWidget *parent)
  180. : QWidget(parent)
  181. {
  182. setMinimumSize(1, 3);
  183. //Default meter color settings, they only show if there is no stylesheet, do not remove.
  184. bkColor.setRgb(0xDD, 0xDD, 0xDD);
  185. magColor.setRgb(0x20, 0x7D, 0x17);
  186. peakColor.setRgb(0x3E, 0xF1, 0x2B);
  187. peakHoldColor.setRgb(0x00, 0x00, 0x00);
  188. resetTimer = new QTimer(this);
  189. connect(resetTimer, SIGNAL(timeout()), this, SLOT(resetState()));
  190. resetState();
  191. }
  192. void VolumeMeter::resetState(void)
  193. {
  194. setLevels(0.0f, 0.0f, 0.0f);
  195. if (resetTimer->isActive())
  196. resetTimer->stop();
  197. }
  198. void VolumeMeter::setLevels(float nmag, float npeak, float npeakHold)
  199. {
  200. mag = nmag;
  201. peak = npeak;
  202. peakHold = npeakHold;
  203. update();
  204. if (resetTimer->isActive())
  205. resetTimer->stop();
  206. resetTimer->start(1000);
  207. }
  208. void VolumeMeter::paintEvent(QPaintEvent *event)
  209. {
  210. UNUSED_PARAMETER(event);
  211. QPainter painter(this);
  212. QLinearGradient gradient;
  213. int width = size().width();
  214. int height = size().height();
  215. int scaledMag = int((float)width * mag);
  216. int scaledPeak = int((float)width * peak);
  217. int scaledPeakHold = int((float)width * peakHold);
  218. gradient.setStart(qreal(scaledMag), 0);
  219. gradient.setFinalStop(qreal(scaledPeak), 0);
  220. gradient.setColorAt(0, magColor);
  221. gradient.setColorAt(1, peakColor);
  222. // RMS
  223. painter.fillRect(0, 0,
  224. scaledMag, height,
  225. magColor);
  226. // RMS - Peak gradient
  227. painter.fillRect(scaledMag, 0,
  228. scaledPeak - scaledMag + 1, height,
  229. QBrush(gradient));
  230. // Background
  231. painter.fillRect(scaledPeak, 0,
  232. width - scaledPeak, height,
  233. bkColor);
  234. // Peak hold
  235. if (peakHold == 1.0f)
  236. scaledPeakHold--;
  237. painter.setPen(peakHoldColor);
  238. painter.drawLine(scaledPeakHold, 0,
  239. scaledPeakHold, height);
  240. }