volume-control.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "volume-control.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include <util/platform.h>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QSlider>
  7. #include <QLabel>
  8. #include <string>
  9. #include <math.h>
  10. using namespace std;
  11. #define VOL_MIN -96.0f
  12. #define VOL_MAX 0.0f
  13. #define VOL_MIN_LOG -2.0086001717619175
  14. #define VOL_MAX_LOG -0.77815125038364363
  15. #define UPDATE_INTERVAL_MS 50
  16. static inline float DBToLog(float db)
  17. {
  18. return -log10f(0.0f - (db - 6.0f));
  19. }
  20. static inline float DBToLinear(float db_full)
  21. {
  22. float db = fmaxf(fminf(db_full, VOL_MAX), VOL_MIN);
  23. return (DBToLog(db) - VOL_MIN_LOG) / (VOL_MAX_LOG - VOL_MIN_LOG);
  24. }
  25. void VolControl::OBSVolumeChanged(void *data, calldata_t calldata)
  26. {
  27. VolControl *volControl = static_cast<VolControl*>(data);
  28. int vol = (int)(calldata_float(calldata, "volume") * 100.0f + 0.5f);
  29. QMetaObject::invokeMethod(volControl, "VolumeChanged", Q_ARG(int, vol));
  30. }
  31. void VolControl::OBSVolumeLevel(void *data, calldata_t calldata)
  32. {
  33. VolControl *volControl = static_cast<VolControl*>(data);
  34. float level = calldata_float(calldata, "level");
  35. float mag = calldata_float(calldata, "magnitude");
  36. /*
  37. * TODO: an actual volume control that can process level, mag, peak.
  38. *
  39. * for the time being, just average level and magnitude.
  40. */
  41. float result = (level + mag) * 0.5f;
  42. QMetaObject::invokeMethod(volControl, "VolumeLevel",
  43. Q_ARG(float, result));
  44. }
  45. void VolControl::VolumeChanged(int vol)
  46. {
  47. signalChanged = false;
  48. slider->setValue(vol);
  49. signalChanged = true;
  50. }
  51. void VolControl::VolumeLevel(float level)
  52. {
  53. uint64_t curMeterTime = os_gettime_ns() / 1000000;
  54. levelTotal += level;
  55. levelCount += 1.0f;
  56. /* only update after a certain amount of time */
  57. if ((curMeterTime - lastMeterTime) > UPDATE_INTERVAL_MS) {
  58. lastMeterTime = curMeterTime;
  59. float finalLevel = levelTotal / levelCount;
  60. volMeter->setValue(int(DBToLinear(finalLevel) * 10000.0f));
  61. levelTotal = 0.0f;
  62. levelCount = 0.0f;
  63. }
  64. }
  65. void VolControl::SliderChanged(int vol)
  66. {
  67. if (signalChanged) {
  68. signal_handler_disconnect(obs_source_signalhandler(source),
  69. "volume", OBSVolumeChanged, this);
  70. obs_source_setvolume(source, float(vol)*0.01f);
  71. signal_handler_connect(obs_source_signalhandler(source),
  72. "volume", OBSVolumeChanged, this);
  73. }
  74. volLabel->setText(QString::number(vol));
  75. }
  76. VolControl::VolControl(OBSSource source_)
  77. : source (source_),
  78. signalChanged (true),
  79. lastMeterTime (0),
  80. levelTotal (0.0f),
  81. levelCount (0.0f)
  82. {
  83. QVBoxLayout *mainLayout = new QVBoxLayout();
  84. QHBoxLayout *textLayout = new QHBoxLayout();
  85. int vol = int(obs_source_getvolume(source) * 100.0f);
  86. nameLabel = new QLabel();
  87. volLabel = new QLabel();
  88. volMeter = new QProgressBar();
  89. slider = new QSlider(Qt::Horizontal);
  90. QFont font = nameLabel->font();
  91. font.setPointSize(font.pointSize()-1);
  92. nameLabel->setText(obs_source_getname(source));
  93. nameLabel->setFont(font);
  94. volLabel->setText(QString::number(vol));
  95. volLabel->setFont(font);
  96. slider->setMinimum(0);
  97. slider->setMaximum(100);
  98. slider->setValue(vol);
  99. // slider->setMaximumHeight(13);
  100. volMeter->setMaximumHeight(1);
  101. volMeter->setMinimum(0);
  102. volMeter->setMaximum(10000);
  103. volMeter->setTextVisible(false);
  104. /* [Danni] Temporary color. */
  105. QString testColor = "QProgressBar "
  106. "{border: 0px} "
  107. "QProgressBar::chunk "
  108. "{width: 1px; background-color: #AA0000;}";
  109. volMeter->setStyleSheet(testColor);
  110. textLayout->setContentsMargins(0, 0, 0, 0);
  111. textLayout->addWidget(nameLabel);
  112. textLayout->addWidget(volLabel);
  113. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  114. textLayout->setAlignment(volLabel, Qt::AlignRight);
  115. mainLayout->setContentsMargins(4, 4, 4, 4);
  116. mainLayout->setSpacing(2);
  117. mainLayout->addItem(textLayout);
  118. mainLayout->addWidget(volMeter);
  119. mainLayout->addWidget(slider);
  120. setLayout(mainLayout);
  121. signal_handler_connect(obs_source_signalhandler(source),
  122. "volume", OBSVolumeChanged, this);
  123. signal_handler_connect(obs_source_signalhandler(source),
  124. "volume_level", OBSVolumeLevel, this);
  125. QWidget::connect(slider, SIGNAL(valueChanged(int)),
  126. this, SLOT(SliderChanged(int)));
  127. }
  128. VolControl::~VolControl()
  129. {
  130. signal_handler_disconnect(obs_source_signalhandler(source),
  131. "volume", OBSVolumeChanged, this);
  132. signal_handler_disconnect(obs_source_signalhandler(source),
  133. "volume_level", OBSVolumeLevel, this);
  134. }