volume-control.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 clipColor READ getClipColor WRITE setClipColor
  27. DESIGNABLE true)
  28. Q_PROPERTY(QColor magnitudeColor READ getMagnitudeColor WRITE
  29. setMagnitudeColor DESIGNABLE true)
  30. Q_PROPERTY(QColor majorTickColor READ getMajorTickColor WRITE
  31. setMajorTickColor DESIGNABLE true)
  32. Q_PROPERTY(QColor minorTickColor READ getMinorTickColor WRITE
  33. setMinorTickColor DESIGNABLE true)
  34. // Levels are denoted in dBFS.
  35. Q_PROPERTY(qreal minimumLevel READ getMinimumLevel WRITE setMinimumLevel
  36. DESIGNABLE true)
  37. Q_PROPERTY(qreal warningLevel READ getWarningLevel WRITE setWarningLevel
  38. DESIGNABLE true)
  39. Q_PROPERTY(qreal errorLevel READ getErrorLevel WRITE setErrorLevel
  40. DESIGNABLE true)
  41. Q_PROPERTY(qreal clipLevel READ getClipLevel WRITE setClipLevel
  42. DESIGNABLE true)
  43. Q_PROPERTY(qreal minimumInputLevel READ getMinimumInputLevel WRITE
  44. setMinimumInputLevel DESIGNABLE true)
  45. // Rates are denoted in dB/second.
  46. Q_PROPERTY(qreal peakDecayRate READ getPeakDecayRate WRITE
  47. setPeakDecayRate DESIGNABLE true)
  48. // Time in seconds for the VU meter to integrate over.
  49. Q_PROPERTY(
  50. qreal magnitudeIntegrationTime READ getMagnitudeIntegrationTime
  51. WRITE setMagnitudeIntegrationTime DESIGNABLE true)
  52. // Duration is denoted in seconds.
  53. Q_PROPERTY(qreal peakHoldDuration READ getPeakHoldDuration WRITE
  54. setPeakHoldDuration DESIGNABLE true)
  55. Q_PROPERTY(qreal inputPeakHoldDuration READ getInputPeakHoldDuration
  56. WRITE setInputPeakHoldDuration DESIGNABLE true)
  57. friend class VolControl;
  58. private slots:
  59. void ClipEnding();
  60. private:
  61. obs_volmeter_t *obs_volmeter;
  62. static QWeakPointer<VolumeMeterTimer> updateTimer;
  63. QSharedPointer<VolumeMeterTimer> updateTimerRef;
  64. inline void resetLevels();
  65. inline void handleChannelCofigurationChange();
  66. inline bool detectIdle(uint64_t ts);
  67. inline void calculateBallistics(uint64_t ts,
  68. qreal timeSinceLastRedraw = 0.0);
  69. inline void calculateBallisticsForChannel(int channelNr, uint64_t ts,
  70. qreal timeSinceLastRedraw);
  71. void paintInputMeter(QPainter &painter, int x, int y, int width,
  72. int height, float peakHold);
  73. void paintHMeter(QPainter &painter, int x, int y, int width, int height,
  74. float magnitude, float peak, float peakHold);
  75. void paintHTicks(QPainter &painter, int x, int y, int width,
  76. int height);
  77. void paintVMeter(QPainter &painter, int x, int y, int width, int height,
  78. float magnitude, float peak, float peakHold);
  79. void paintVTicks(QPainter &painter, int x, int y, int height);
  80. QColor ConvertToGrayscale(const QColor &color) const;
  81. QMutex dataMutex;
  82. uint64_t currentLastUpdateTime = 0;
  83. float currentMagnitude[MAX_AUDIO_CHANNELS];
  84. float currentPeak[MAX_AUDIO_CHANNELS];
  85. float currentInputPeak[MAX_AUDIO_CHANNELS];
  86. QPixmap *tickPaintCache = nullptr;
  87. int displayNrAudioChannels = 0;
  88. float displayMagnitude[MAX_AUDIO_CHANNELS];
  89. float displayPeak[MAX_AUDIO_CHANNELS];
  90. float displayPeakHold[MAX_AUDIO_CHANNELS];
  91. uint64_t displayPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  92. float displayInputPeakHold[MAX_AUDIO_CHANNELS];
  93. uint64_t displayInputPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  94. QFont tickFont;
  95. QColor backgroundNominalColor;
  96. QColor backgroundWarningColor;
  97. QColor backgroundErrorColor;
  98. QColor foregroundNominalColor;
  99. QColor foregroundWarningColor;
  100. QColor foregroundErrorColor;
  101. QColor clipColor;
  102. QColor magnitudeColor;
  103. QColor majorTickColor;
  104. QColor minorTickColor;
  105. qreal minimumLevel;
  106. qreal warningLevel;
  107. qreal errorLevel;
  108. qreal clipLevel;
  109. qreal minimumInputLevel;
  110. qreal peakDecayRate;
  111. qreal magnitudeIntegrationTime;
  112. qreal peakHoldDuration;
  113. qreal inputPeakHoldDuration;
  114. uint64_t lastRedrawTime = 0;
  115. int channels = 0;
  116. bool clipping = false;
  117. bool vertical;
  118. bool muted = false;
  119. public:
  120. explicit VolumeMeter(QWidget *parent = nullptr,
  121. obs_volmeter_t *obs_volmeter = nullptr,
  122. bool vertical = false);
  123. ~VolumeMeter();
  124. void setLevels(const float magnitude[MAX_AUDIO_CHANNELS],
  125. const float peak[MAX_AUDIO_CHANNELS],
  126. const float inputPeak[MAX_AUDIO_CHANNELS]);
  127. QColor getBackgroundNominalColor() const;
  128. void setBackgroundNominalColor(QColor c);
  129. QColor getBackgroundWarningColor() const;
  130. void setBackgroundWarningColor(QColor c);
  131. QColor getBackgroundErrorColor() const;
  132. void setBackgroundErrorColor(QColor c);
  133. QColor getForegroundNominalColor() const;
  134. void setForegroundNominalColor(QColor c);
  135. QColor getForegroundWarningColor() const;
  136. void setForegroundWarningColor(QColor c);
  137. QColor getForegroundErrorColor() const;
  138. void setForegroundErrorColor(QColor c);
  139. QColor getClipColor() const;
  140. void setClipColor(QColor c);
  141. QColor getMagnitudeColor() const;
  142. void setMagnitudeColor(QColor c);
  143. QColor getMajorTickColor() const;
  144. void setMajorTickColor(QColor c);
  145. QColor getMinorTickColor() const;
  146. void setMinorTickColor(QColor c);
  147. qreal getMinimumLevel() const;
  148. void setMinimumLevel(qreal v);
  149. qreal getWarningLevel() const;
  150. void setWarningLevel(qreal v);
  151. qreal getErrorLevel() const;
  152. void setErrorLevel(qreal v);
  153. qreal getClipLevel() const;
  154. void setClipLevel(qreal v);
  155. qreal getMinimumInputLevel() const;
  156. void setMinimumInputLevel(qreal v);
  157. qreal getPeakDecayRate() const;
  158. void setPeakDecayRate(qreal v);
  159. qreal getMagnitudeIntegrationTime() const;
  160. void setMagnitudeIntegrationTime(qreal v);
  161. qreal getPeakHoldDuration() const;
  162. void setPeakHoldDuration(qreal v);
  163. qreal getInputPeakHoldDuration() const;
  164. void setInputPeakHoldDuration(qreal v);
  165. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  166. virtual void mousePressEvent(QMouseEvent *event) override;
  167. virtual void wheelEvent(QWheelEvent *event) override;
  168. protected:
  169. void paintEvent(QPaintEvent *event) override;
  170. };
  171. class VolumeMeterTimer : public QTimer {
  172. Q_OBJECT
  173. public:
  174. inline VolumeMeterTimer() : QTimer() {}
  175. void AddVolControl(VolumeMeter *meter);
  176. void RemoveVolControl(VolumeMeter *meter);
  177. protected:
  178. void timerEvent(QTimerEvent *event) override;
  179. QList<VolumeMeter *> volumeMeters;
  180. };
  181. class QLabel;
  182. class QSlider;
  183. class MuteCheckBox;
  184. class VolControl : public QWidget {
  185. Q_OBJECT
  186. private:
  187. OBSSource source;
  188. QLabel *nameLabel;
  189. QLabel *volLabel;
  190. VolumeMeter *volMeter;
  191. QSlider *slider;
  192. MuteCheckBox *mute;
  193. QPushButton *config = nullptr;
  194. float levelTotal;
  195. float levelCount;
  196. obs_fader_t *obs_fader;
  197. obs_volmeter_t *obs_volmeter;
  198. bool vertical;
  199. QMenu *contextMenu;
  200. static void OBSVolumeChanged(void *param, float db);
  201. static void OBSVolumeLevel(void *data,
  202. const float magnitude[MAX_AUDIO_CHANNELS],
  203. const float peak[MAX_AUDIO_CHANNELS],
  204. const float inputPeak[MAX_AUDIO_CHANNELS]);
  205. static void OBSVolumeMuted(void *data, calldata_t *calldata);
  206. void EmitConfigClicked();
  207. private slots:
  208. void VolumeChanged();
  209. void VolumeMuted(bool muted);
  210. void SetMuted(bool checked);
  211. void SliderChanged(int vol);
  212. void updateText();
  213. signals:
  214. void ConfigClicked();
  215. public:
  216. explicit VolControl(OBSSource source, bool showConfig = false,
  217. bool vertical = false);
  218. ~VolControl();
  219. inline obs_source_t *GetSource() const { return source; }
  220. QString GetName() const;
  221. void SetName(const QString &newName);
  222. void SetMeterDecayRate(qreal q);
  223. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  224. void EnableSlider(bool enable);
  225. inline void SetContextMenu(QMenu *cm) { contextMenu = cm; }
  226. };