volume-control.cpp 6.7 KB

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