volume-control.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #pragma once
  2. #include <obs.hpp>
  3. #include <QWidget>
  4. #include <QPaintEvent>
  5. #include <QTimer>
  6. #include <QMutex>
  7. #include <QList>
  8. #include <QMenu>
  9. class QPushButton;
  10. class VolumeMeterTimer;
  11. class VolumeMeter : public QWidget {
  12. Q_OBJECT
  13. Q_PROPERTY(QColor backgroundNominalColor READ getBackgroundNominalColor
  14. WRITE setBackgroundNominalColor DESIGNABLE true)
  15. Q_PROPERTY(QColor backgroundWarningColor READ getBackgroundWarningColor
  16. WRITE setBackgroundWarningColor DESIGNABLE true)
  17. Q_PROPERTY(QColor backgroundErrorColor READ getBackgroundErrorColor
  18. WRITE setBackgroundErrorColor DESIGNABLE true)
  19. Q_PROPERTY(QColor foregroundNominalColor READ getForegroundNominalColor
  20. WRITE setForegroundNominalColor DESIGNABLE true)
  21. Q_PROPERTY(QColor foregroundWarningColor READ getForegroundWarningColor
  22. WRITE setForegroundWarningColor DESIGNABLE true)
  23. Q_PROPERTY(QColor foregroundErrorColor READ getForegroundErrorColor
  24. WRITE setForegroundErrorColor DESIGNABLE true)
  25. Q_PROPERTY(QColor backgroundNominalColorDisabled READ
  26. getBackgroundNominalColorDisabled WRITE
  27. setBackgroundNominalColorDisabled
  28. DESIGNABLE true)
  29. Q_PROPERTY(QColor backgroundWarningColorDisabled READ
  30. getBackgroundWarningColorDisabled WRITE
  31. setBackgroundWarningColorDisabled
  32. DESIGNABLE true)
  33. Q_PROPERTY(
  34. QColor backgroundErrorColorDisabled READ
  35. getBackgroundErrorColorDisabled WRITE
  36. setBackgroundErrorColorDisabled DESIGNABLE true)
  37. Q_PROPERTY(QColor foregroundNominalColorDisabled READ
  38. getForegroundNominalColorDisabled WRITE
  39. setForegroundNominalColorDisabled
  40. DESIGNABLE true)
  41. Q_PROPERTY(QColor foregroundWarningColorDisabled READ
  42. getForegroundWarningColorDisabled WRITE
  43. setForegroundWarningColorDisabled
  44. DESIGNABLE true)
  45. Q_PROPERTY(
  46. QColor foregroundErrorColorDisabled READ
  47. getForegroundErrorColorDisabled WRITE
  48. setForegroundErrorColorDisabled DESIGNABLE true)
  49. Q_PROPERTY(QColor clipColor READ getClipColor WRITE setClipColor
  50. DESIGNABLE true)
  51. Q_PROPERTY(QColor magnitudeColor READ getMagnitudeColor WRITE
  52. setMagnitudeColor DESIGNABLE true)
  53. Q_PROPERTY(QColor majorTickColor READ getMajorTickColor WRITE
  54. setMajorTickColor DESIGNABLE true)
  55. Q_PROPERTY(QColor minorTickColor READ getMinorTickColor WRITE
  56. setMinorTickColor DESIGNABLE true)
  57. Q_PROPERTY(int meterThickness READ getMeterThickness WRITE
  58. setMeterThickness DESIGNABLE true)
  59. Q_PROPERTY(qreal meterFontScaling READ getMeterFontScaling WRITE
  60. setMeterFontScaling DESIGNABLE true)
  61. // Levels are denoted in dBFS.
  62. Q_PROPERTY(qreal minimumLevel READ getMinimumLevel WRITE setMinimumLevel
  63. DESIGNABLE true)
  64. Q_PROPERTY(qreal warningLevel READ getWarningLevel WRITE setWarningLevel
  65. DESIGNABLE true)
  66. Q_PROPERTY(qreal errorLevel READ getErrorLevel WRITE setErrorLevel
  67. DESIGNABLE true)
  68. Q_PROPERTY(qreal clipLevel READ getClipLevel WRITE setClipLevel
  69. DESIGNABLE true)
  70. Q_PROPERTY(qreal minimumInputLevel READ getMinimumInputLevel WRITE
  71. setMinimumInputLevel DESIGNABLE true)
  72. // Rates are denoted in dB/second.
  73. Q_PROPERTY(qreal peakDecayRate READ getPeakDecayRate WRITE
  74. setPeakDecayRate DESIGNABLE true)
  75. // Time in seconds for the VU meter to integrate over.
  76. Q_PROPERTY(
  77. qreal magnitudeIntegrationTime READ getMagnitudeIntegrationTime
  78. WRITE setMagnitudeIntegrationTime DESIGNABLE true)
  79. // Duration is denoted in seconds.
  80. Q_PROPERTY(qreal peakHoldDuration READ getPeakHoldDuration WRITE
  81. setPeakHoldDuration DESIGNABLE true)
  82. Q_PROPERTY(qreal inputPeakHoldDuration READ getInputPeakHoldDuration
  83. WRITE setInputPeakHoldDuration DESIGNABLE true)
  84. friend class VolControl;
  85. private:
  86. obs_volmeter_t *obs_volmeter;
  87. static std::weak_ptr<VolumeMeterTimer> updateTimer;
  88. std::shared_ptr<VolumeMeterTimer> updateTimerRef;
  89. inline void resetLevels();
  90. inline void doLayout();
  91. inline bool detectIdle(uint64_t ts);
  92. inline void calculateBallistics(uint64_t ts,
  93. qreal timeSinceLastRedraw = 0.0);
  94. inline void calculateBallisticsForChannel(int channelNr, uint64_t ts,
  95. qreal timeSinceLastRedraw);
  96. inline int convertToInt(float number);
  97. void paintInputMeter(QPainter &painter, int x, int y, int width,
  98. int height, float peakHold);
  99. void paintHMeter(QPainter &painter, int x, int y, int width, int height,
  100. float magnitude, float peak, float peakHold);
  101. void paintHTicks(QPainter &painter, int x, int y, int width);
  102. void paintVMeter(QPainter &painter, int x, int y, int width, int height,
  103. float magnitude, float peak, float peakHold);
  104. void paintVTicks(QPainter &painter, int x, int y, int height);
  105. QMutex dataMutex;
  106. bool recalculateLayout = true;
  107. uint64_t currentLastUpdateTime = 0;
  108. float currentMagnitude[MAX_AUDIO_CHANNELS];
  109. float currentPeak[MAX_AUDIO_CHANNELS];
  110. float currentInputPeak[MAX_AUDIO_CHANNELS];
  111. int displayNrAudioChannels = 0;
  112. float displayMagnitude[MAX_AUDIO_CHANNELS];
  113. float displayPeak[MAX_AUDIO_CHANNELS];
  114. float displayPeakHold[MAX_AUDIO_CHANNELS];
  115. uint64_t displayPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  116. float displayInputPeakHold[MAX_AUDIO_CHANNELS];
  117. uint64_t displayInputPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  118. QFont tickFont;
  119. QColor backgroundNominalColor;
  120. QColor backgroundWarningColor;
  121. QColor backgroundErrorColor;
  122. QColor foregroundNominalColor;
  123. QColor foregroundWarningColor;
  124. QColor foregroundErrorColor;
  125. QColor backgroundNominalColorDisabled;
  126. QColor backgroundWarningColorDisabled;
  127. QColor backgroundErrorColorDisabled;
  128. QColor foregroundNominalColorDisabled;
  129. QColor foregroundWarningColorDisabled;
  130. QColor foregroundErrorColorDisabled;
  131. QColor clipColor;
  132. QColor magnitudeColor;
  133. QColor majorTickColor;
  134. QColor minorTickColor;
  135. int meterThickness;
  136. qreal meterFontScaling;
  137. qreal minimumLevel;
  138. qreal warningLevel;
  139. qreal errorLevel;
  140. qreal clipLevel;
  141. qreal minimumInputLevel;
  142. qreal peakDecayRate;
  143. qreal magnitudeIntegrationTime;
  144. qreal peakHoldDuration;
  145. qreal inputPeakHoldDuration;
  146. QColor p_backgroundNominalColor;
  147. QColor p_backgroundWarningColor;
  148. QColor p_backgroundErrorColor;
  149. QColor p_foregroundNominalColor;
  150. QColor p_foregroundWarningColor;
  151. QColor p_foregroundErrorColor;
  152. uint64_t lastRedrawTime = 0;
  153. int channels = 0;
  154. bool clipping = false;
  155. bool vertical;
  156. bool muted = false;
  157. public:
  158. explicit VolumeMeter(QWidget *parent = nullptr,
  159. obs_volmeter_t *obs_volmeter = nullptr,
  160. bool vertical = false);
  161. ~VolumeMeter();
  162. void setLevels(const float magnitude[MAX_AUDIO_CHANNELS],
  163. const float peak[MAX_AUDIO_CHANNELS],
  164. const float inputPeak[MAX_AUDIO_CHANNELS]);
  165. QRect getBarRect() const;
  166. bool needLayoutChange();
  167. QColor getBackgroundNominalColor() const;
  168. void setBackgroundNominalColor(QColor c);
  169. QColor getBackgroundWarningColor() const;
  170. void setBackgroundWarningColor(QColor c);
  171. QColor getBackgroundErrorColor() const;
  172. void setBackgroundErrorColor(QColor c);
  173. QColor getForegroundNominalColor() const;
  174. void setForegroundNominalColor(QColor c);
  175. QColor getForegroundWarningColor() const;
  176. void setForegroundWarningColor(QColor c);
  177. QColor getForegroundErrorColor() const;
  178. void setForegroundErrorColor(QColor c);
  179. QColor getBackgroundNominalColorDisabled() const;
  180. void setBackgroundNominalColorDisabled(QColor c);
  181. QColor getBackgroundWarningColorDisabled() const;
  182. void setBackgroundWarningColorDisabled(QColor c);
  183. QColor getBackgroundErrorColorDisabled() const;
  184. void setBackgroundErrorColorDisabled(QColor c);
  185. QColor getForegroundNominalColorDisabled() const;
  186. void setForegroundNominalColorDisabled(QColor c);
  187. QColor getForegroundWarningColorDisabled() const;
  188. void setForegroundWarningColorDisabled(QColor c);
  189. QColor getForegroundErrorColorDisabled() const;
  190. void setForegroundErrorColorDisabled(QColor c);
  191. QColor getClipColor() const;
  192. void setClipColor(QColor c);
  193. QColor getMagnitudeColor() const;
  194. void setMagnitudeColor(QColor c);
  195. QColor getMajorTickColor() const;
  196. void setMajorTickColor(QColor c);
  197. QColor getMinorTickColor() const;
  198. void setMinorTickColor(QColor c);
  199. int getMeterThickness() const;
  200. void setMeterThickness(int v);
  201. qreal getMeterFontScaling() const;
  202. void setMeterFontScaling(qreal v);
  203. qreal getMinimumLevel() const;
  204. void setMinimumLevel(qreal v);
  205. qreal getWarningLevel() const;
  206. void setWarningLevel(qreal v);
  207. qreal getErrorLevel() const;
  208. void setErrorLevel(qreal v);
  209. qreal getClipLevel() const;
  210. void setClipLevel(qreal v);
  211. qreal getMinimumInputLevel() const;
  212. void setMinimumInputLevel(qreal v);
  213. qreal getPeakDecayRate() const;
  214. void setPeakDecayRate(qreal v);
  215. qreal getMagnitudeIntegrationTime() const;
  216. void setMagnitudeIntegrationTime(qreal v);
  217. qreal getPeakHoldDuration() const;
  218. void setPeakHoldDuration(qreal v);
  219. qreal getInputPeakHoldDuration() const;
  220. void setInputPeakHoldDuration(qreal v);
  221. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  222. virtual void mousePressEvent(QMouseEvent *event) override;
  223. virtual void wheelEvent(QWheelEvent *event) override;
  224. protected:
  225. void paintEvent(QPaintEvent *event) override;
  226. void changeEvent(QEvent *e) override;
  227. };
  228. class VolumeMeterTimer : public QTimer {
  229. Q_OBJECT
  230. public:
  231. inline VolumeMeterTimer() : QTimer() {}
  232. void AddVolControl(VolumeMeter *meter);
  233. void RemoveVolControl(VolumeMeter *meter);
  234. protected:
  235. void timerEvent(QTimerEvent *event) override;
  236. QList<VolumeMeter *> volumeMeters;
  237. };
  238. class QLabel;
  239. class QSlider;
  240. class MuteCheckBox;
  241. class VolControl : public QWidget {
  242. Q_OBJECT
  243. private:
  244. OBSSource source;
  245. QLabel *nameLabel;
  246. QLabel *volLabel;
  247. VolumeMeter *volMeter;
  248. QSlider *slider;
  249. MuteCheckBox *mute;
  250. QPushButton *config = nullptr;
  251. float levelTotal;
  252. float levelCount;
  253. OBSFader obs_fader;
  254. OBSVolMeter obs_volmeter;
  255. bool vertical;
  256. QMenu *contextMenu;
  257. static void OBSVolumeChanged(void *param, float db);
  258. static void OBSVolumeLevel(void *data,
  259. const float magnitude[MAX_AUDIO_CHANNELS],
  260. const float peak[MAX_AUDIO_CHANNELS],
  261. const float inputPeak[MAX_AUDIO_CHANNELS]);
  262. static void OBSVolumeMuted(void *data, calldata_t *calldata);
  263. static void OBSMixersOrMonitoringChanged(void *data, calldata_t *);
  264. void EmitConfigClicked();
  265. private slots:
  266. void VolumeChanged();
  267. void VolumeMuted(bool muted);
  268. void MixersOrMonitoringChanged();
  269. void SetMuted(bool checked);
  270. void SliderChanged(int vol);
  271. void updateText();
  272. signals:
  273. void ConfigClicked();
  274. public:
  275. explicit VolControl(OBSSource source, bool showConfig = false,
  276. bool vertical = false);
  277. ~VolControl();
  278. inline obs_source_t *GetSource() const { return source; }
  279. QString GetName() const;
  280. void SetName(const QString &newName);
  281. void SetMeterDecayRate(qreal q);
  282. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  283. void EnableSlider(bool enable);
  284. inline void SetContextMenu(QMenu *cm) { contextMenu = cm; }
  285. void refreshColors();
  286. };