volume-control.cpp 7.1 KB

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