volume-control.cpp 7.2 KB

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