volume-control.hpp 11 KB

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