volume-control.hpp 7.3 KB

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