1
0

VolumeMeter.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #pragma once
  2. #include <obs.hpp>
  3. #include <QMutex>
  4. #include <QWidget>
  5. #define FADER_PRECISION 4096.0
  6. class VolumeMeterTimer;
  7. class VolumeMeter : public QWidget {
  8. Q_OBJECT
  9. Q_PROPERTY(QColor backgroundNominalColor READ getBackgroundNominalColor WRITE setBackgroundNominalColor
  10. DESIGNABLE true)
  11. Q_PROPERTY(QColor backgroundWarningColor READ getBackgroundWarningColor WRITE setBackgroundWarningColor
  12. DESIGNABLE true)
  13. Q_PROPERTY(
  14. QColor backgroundErrorColor READ getBackgroundErrorColor WRITE setBackgroundErrorColor DESIGNABLE true)
  15. Q_PROPERTY(QColor foregroundNominalColor READ getForegroundNominalColor WRITE setForegroundNominalColor
  16. DESIGNABLE true)
  17. Q_PROPERTY(QColor foregroundWarningColor READ getForegroundWarningColor WRITE setForegroundWarningColor
  18. DESIGNABLE true)
  19. Q_PROPERTY(
  20. QColor foregroundErrorColor READ getForegroundErrorColor WRITE setForegroundErrorColor DESIGNABLE true)
  21. Q_PROPERTY(QColor backgroundNominalColorDisabled READ getBackgroundNominalColorDisabled WRITE
  22. setBackgroundNominalColorDisabled DESIGNABLE true)
  23. Q_PROPERTY(QColor backgroundWarningColorDisabled READ getBackgroundWarningColorDisabled WRITE
  24. setBackgroundWarningColorDisabled DESIGNABLE true)
  25. Q_PROPERTY(QColor backgroundErrorColorDisabled READ getBackgroundErrorColorDisabled WRITE
  26. setBackgroundErrorColorDisabled DESIGNABLE true)
  27. Q_PROPERTY(QColor foregroundNominalColorDisabled READ getForegroundNominalColorDisabled WRITE
  28. setForegroundNominalColorDisabled DESIGNABLE true)
  29. Q_PROPERTY(QColor foregroundWarningColorDisabled READ getForegroundWarningColorDisabled WRITE
  30. setForegroundWarningColorDisabled DESIGNABLE true)
  31. Q_PROPERTY(QColor foregroundErrorColorDisabled READ getForegroundErrorColorDisabled WRITE
  32. setForegroundErrorColorDisabled DESIGNABLE true)
  33. Q_PROPERTY(QColor clipColor READ getClipColor WRITE setClipColor DESIGNABLE true)
  34. Q_PROPERTY(QColor magnitudeColor READ getMagnitudeColor WRITE setMagnitudeColor DESIGNABLE true)
  35. Q_PROPERTY(QColor majorTickColor READ getMajorTickColor WRITE setMajorTickColor DESIGNABLE true)
  36. Q_PROPERTY(QColor minorTickColor READ getMinorTickColor WRITE setMinorTickColor DESIGNABLE true)
  37. Q_PROPERTY(int meterThickness READ getMeterThickness WRITE setMeterThickness DESIGNABLE true)
  38. Q_PROPERTY(qreal meterFontScaling READ getMeterFontScaling WRITE setMeterFontScaling DESIGNABLE true)
  39. // Levels are denoted in dBFS.
  40. Q_PROPERTY(qreal minimumLevel READ getMinimumLevel WRITE setMinimumLevel DESIGNABLE true)
  41. Q_PROPERTY(qreal warningLevel READ getWarningLevel WRITE setWarningLevel DESIGNABLE true)
  42. Q_PROPERTY(qreal errorLevel READ getErrorLevel WRITE setErrorLevel DESIGNABLE true)
  43. Q_PROPERTY(qreal clipLevel READ getClipLevel WRITE setClipLevel DESIGNABLE true)
  44. Q_PROPERTY(qreal minimumInputLevel READ getMinimumInputLevel WRITE setMinimumInputLevel DESIGNABLE true)
  45. // Rates are denoted in dB/second.
  46. Q_PROPERTY(qreal peakDecayRate READ getPeakDecayRate WRITE setPeakDecayRate DESIGNABLE true)
  47. // Time in seconds for the VU meter to integrate over.
  48. Q_PROPERTY(qreal magnitudeIntegrationTime READ getMagnitudeIntegrationTime WRITE setMagnitudeIntegrationTime
  49. DESIGNABLE true)
  50. // Duration is denoted in seconds.
  51. Q_PROPERTY(qreal peakHoldDuration READ getPeakHoldDuration WRITE setPeakHoldDuration DESIGNABLE true)
  52. Q_PROPERTY(qreal inputPeakHoldDuration READ getInputPeakHoldDuration WRITE setInputPeakHoldDuration
  53. DESIGNABLE true)
  54. friend class VolControl;
  55. private:
  56. obs_volmeter_t *obs_volmeter;
  57. static std::weak_ptr<VolumeMeterTimer> updateTimer;
  58. std::shared_ptr<VolumeMeterTimer> updateTimerRef;
  59. inline void resetLevels();
  60. inline void doLayout();
  61. inline bool detectIdle(uint64_t ts);
  62. inline void calculateBallistics(uint64_t ts, qreal timeSinceLastRedraw = 0.0);
  63. inline void calculateBallisticsForChannel(int channelNr, uint64_t ts, qreal timeSinceLastRedraw);
  64. inline int convertToInt(float number);
  65. void paintInputMeter(QPainter &painter, int x, int y, int width, int height, float peakHold);
  66. void paintHMeter(QPainter &painter, int x, int y, int width, int height, float magnitude, float peak,
  67. float peakHold);
  68. void paintHTicks(QPainter &painter, int x, int y, int width);
  69. void paintVMeter(QPainter &painter, int x, int y, int width, int height, float magnitude, float peak,
  70. float peakHold);
  71. void paintVTicks(QPainter &painter, int x, int y, int height);
  72. QMutex dataMutex;
  73. bool recalculateLayout = true;
  74. uint64_t currentLastUpdateTime = 0;
  75. float currentMagnitude[MAX_AUDIO_CHANNELS];
  76. float currentPeak[MAX_AUDIO_CHANNELS];
  77. float currentInputPeak[MAX_AUDIO_CHANNELS];
  78. int displayNrAudioChannels = 0;
  79. float displayMagnitude[MAX_AUDIO_CHANNELS];
  80. float displayPeak[MAX_AUDIO_CHANNELS];
  81. float displayPeakHold[MAX_AUDIO_CHANNELS];
  82. uint64_t displayPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  83. float displayInputPeakHold[MAX_AUDIO_CHANNELS];
  84. uint64_t displayInputPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  85. QFont tickFont;
  86. QColor backgroundNominalColor;
  87. QColor backgroundWarningColor;
  88. QColor backgroundErrorColor;
  89. QColor foregroundNominalColor;
  90. QColor foregroundWarningColor;
  91. QColor foregroundErrorColor;
  92. QColor backgroundNominalColorDisabled;
  93. QColor backgroundWarningColorDisabled;
  94. QColor backgroundErrorColorDisabled;
  95. QColor foregroundNominalColorDisabled;
  96. QColor foregroundWarningColorDisabled;
  97. QColor foregroundErrorColorDisabled;
  98. QColor clipColor;
  99. QColor magnitudeColor;
  100. QColor majorTickColor;
  101. QColor minorTickColor;
  102. int meterThickness;
  103. qreal meterFontScaling;
  104. qreal minimumLevel;
  105. qreal warningLevel;
  106. qreal errorLevel;
  107. qreal clipLevel;
  108. qreal minimumInputLevel;
  109. qreal peakDecayRate;
  110. qreal magnitudeIntegrationTime;
  111. qreal peakHoldDuration;
  112. qreal inputPeakHoldDuration;
  113. QColor p_backgroundNominalColor;
  114. QColor p_backgroundWarningColor;
  115. QColor p_backgroundErrorColor;
  116. QColor p_foregroundNominalColor;
  117. QColor p_foregroundWarningColor;
  118. QColor p_foregroundErrorColor;
  119. uint64_t lastRedrawTime = 0;
  120. int channels = 0;
  121. bool clipping = false;
  122. bool vertical;
  123. bool muted = false;
  124. public:
  125. explicit VolumeMeter(QWidget *parent = nullptr, obs_volmeter_t *obs_volmeter = nullptr, bool vertical = false);
  126. ~VolumeMeter();
  127. void setLevels(const float magnitude[MAX_AUDIO_CHANNELS], const float peak[MAX_AUDIO_CHANNELS],
  128. const float inputPeak[MAX_AUDIO_CHANNELS]);
  129. QRect getBarRect() const;
  130. bool needLayoutChange();
  131. QColor getBackgroundNominalColor() const;
  132. void setBackgroundNominalColor(QColor c);
  133. QColor getBackgroundWarningColor() const;
  134. void setBackgroundWarningColor(QColor c);
  135. QColor getBackgroundErrorColor() const;
  136. void setBackgroundErrorColor(QColor c);
  137. QColor getForegroundNominalColor() const;
  138. void setForegroundNominalColor(QColor c);
  139. QColor getForegroundWarningColor() const;
  140. void setForegroundWarningColor(QColor c);
  141. QColor getForegroundErrorColor() const;
  142. void setForegroundErrorColor(QColor c);
  143. QColor getBackgroundNominalColorDisabled() const;
  144. void setBackgroundNominalColorDisabled(QColor c);
  145. QColor getBackgroundWarningColorDisabled() const;
  146. void setBackgroundWarningColorDisabled(QColor c);
  147. QColor getBackgroundErrorColorDisabled() const;
  148. void setBackgroundErrorColorDisabled(QColor c);
  149. QColor getForegroundNominalColorDisabled() const;
  150. void setForegroundNominalColorDisabled(QColor c);
  151. QColor getForegroundWarningColorDisabled() const;
  152. void setForegroundWarningColorDisabled(QColor c);
  153. QColor getForegroundErrorColorDisabled() const;
  154. void setForegroundErrorColorDisabled(QColor c);
  155. QColor getClipColor() const;
  156. void setClipColor(QColor c);
  157. QColor getMagnitudeColor() const;
  158. void setMagnitudeColor(QColor c);
  159. QColor getMajorTickColor() const;
  160. void setMajorTickColor(QColor c);
  161. QColor getMinorTickColor() const;
  162. void setMinorTickColor(QColor c);
  163. int getMeterThickness() const;
  164. void setMeterThickness(int v);
  165. qreal getMeterFontScaling() const;
  166. void setMeterFontScaling(qreal v);
  167. qreal getMinimumLevel() const;
  168. void setMinimumLevel(qreal v);
  169. qreal getWarningLevel() const;
  170. void setWarningLevel(qreal v);
  171. qreal getErrorLevel() const;
  172. void setErrorLevel(qreal v);
  173. qreal getClipLevel() const;
  174. void setClipLevel(qreal v);
  175. qreal getMinimumInputLevel() const;
  176. void setMinimumInputLevel(qreal v);
  177. qreal getPeakDecayRate() const;
  178. void setPeakDecayRate(qreal v);
  179. qreal getMagnitudeIntegrationTime() const;
  180. void setMagnitudeIntegrationTime(qreal v);
  181. qreal getPeakHoldDuration() const;
  182. void setPeakHoldDuration(qreal v);
  183. qreal getInputPeakHoldDuration() const;
  184. void setInputPeakHoldDuration(qreal v);
  185. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  186. virtual void mousePressEvent(QMouseEvent *event) override;
  187. virtual void wheelEvent(QWheelEvent *event) override;
  188. protected:
  189. void paintEvent(QPaintEvent *event) override;
  190. void changeEvent(QEvent *e) override;
  191. };