volume-control.cpp 7.5 KB

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