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