VolumeMeter.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include <obs.hpp>
  3. #include <QMutex>
  4. #include <QPixmap>
  5. #include <QWidget>
  6. #define FADER_PRECISION 4096.0
  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 magnitudeColor READ getMagnitudeColor WRITE setMagnitudeColor DESIGNABLE true)
  34. Q_PROPERTY(QColor majorTickColor READ getMajorTickColor WRITE setMajorTickColor DESIGNABLE true)
  35. Q_PROPERTY(QColor minorTickColor READ getMinorTickColor WRITE setMinorTickColor DESIGNABLE true)
  36. friend class VolumeControl;
  37. private:
  38. OBSWeakSource weakSource;
  39. OBSVolMeter obsVolumeMeter;
  40. static QPointer<QTimer> updateTimer;
  41. static void obsVolMeterChanged(void *data, const float magnitude[MAX_AUDIO_CHANNELS],
  42. const float peak[MAX_AUDIO_CHANNELS], const float inputPeak[MAX_AUDIO_CHANNELS]);
  43. OBSSignal destroyedSignal;
  44. static void obsSourceDestroyed(void *data, calldata_t *);
  45. inline void resetLevels();
  46. inline void doLayout();
  47. inline bool detectIdle(uint64_t ts);
  48. inline void calculateBallistics(uint64_t ts, qreal timeSinceLastRedraw = 0.0);
  49. inline void calculateBallisticsForChannel(int channelNr, uint64_t ts, qreal timeSinceLastRedraw);
  50. inline int convertToInt(float number);
  51. QColor getPeakColor(float peakHold);
  52. void paintHTicks(QPainter &painter, int x, int y, int width);
  53. void paintVTicks(QPainter &painter, int x, int y, int height);
  54. QMutex dataMutex;
  55. bool recalculateLayout{true};
  56. uint64_t currentLastUpdateTime{0};
  57. float currentMagnitude[MAX_AUDIO_CHANNELS];
  58. float currentPeak[MAX_AUDIO_CHANNELS];
  59. float currentInputPeak[MAX_AUDIO_CHANNELS];
  60. int displayNrAudioChannels{0};
  61. float displayMagnitude[MAX_AUDIO_CHANNELS];
  62. float displayPeak[MAX_AUDIO_CHANNELS];
  63. float displayPeakHold[MAX_AUDIO_CHANNELS];
  64. uint64_t displayPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  65. float displayInputPeakHold[MAX_AUDIO_CHANNELS];
  66. uint64_t displayInputPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  67. QPixmap backgroundCache;
  68. void updateBackgroundCache();
  69. QFont tickFont;
  70. QColor backgroundNominalColor;
  71. QColor backgroundWarningColor;
  72. QColor backgroundErrorColor;
  73. QColor foregroundNominalColor;
  74. QColor foregroundWarningColor;
  75. QColor foregroundErrorColor;
  76. QColor backgroundNominalColorDisabled;
  77. QColor backgroundWarningColorDisabled;
  78. QColor backgroundErrorColorDisabled;
  79. QColor foregroundNominalColorDisabled;
  80. QColor foregroundWarningColorDisabled;
  81. QColor foregroundErrorColorDisabled;
  82. QColor clipColor;
  83. QColor magnitudeColor;
  84. QColor majorTickColor;
  85. QColor minorTickColor;
  86. int meterThickness;
  87. qreal meterFontScaling;
  88. qreal minimumLevel;
  89. qreal warningLevel;
  90. qreal errorLevel;
  91. qreal clipLevel;
  92. qreal minimumInputLevel;
  93. qreal peakDecayRate;
  94. qreal magnitudeIntegrationTime;
  95. qreal peakHoldDuration;
  96. qreal inputPeakHoldDuration;
  97. QColor p_backgroundNominalColor;
  98. QColor p_backgroundWarningColor;
  99. QColor p_backgroundErrorColor;
  100. QColor p_foregroundNominalColor;
  101. QColor p_foregroundWarningColor;
  102. QColor p_foregroundErrorColor;
  103. uint64_t lastRedrawTime{0};
  104. int channels{0};
  105. bool clipping{false};
  106. bool vertical{false};
  107. bool hidden{false};
  108. bool muted{false};
  109. bool useDisabledColors{false};
  110. public:
  111. explicit VolumeMeter(QWidget *parent = nullptr, obs_source_t *source = nullptr);
  112. ~VolumeMeter();
  113. void setLevels(const float magnitude[MAX_AUDIO_CHANNELS], const float peak[MAX_AUDIO_CHANNELS],
  114. const float inputPeak[MAX_AUDIO_CHANNELS]);
  115. QRect getBarRect() const;
  116. bool needLayoutChange();
  117. void setVertical(bool vertical = true);
  118. void setUseDisabledColors(bool enable);
  119. void setMuted(bool mute);
  120. void refreshColors();
  121. QColor getBackgroundNominalColor() const;
  122. void setBackgroundNominalColor(QColor c);
  123. QColor getBackgroundWarningColor() const;
  124. void setBackgroundWarningColor(QColor c);
  125. QColor getBackgroundErrorColor() const;
  126. void setBackgroundErrorColor(QColor c);
  127. QColor getForegroundNominalColor() const;
  128. void setForegroundNominalColor(QColor c);
  129. QColor getForegroundWarningColor() const;
  130. void setForegroundWarningColor(QColor c);
  131. QColor getForegroundErrorColor() const;
  132. void setForegroundErrorColor(QColor c);
  133. QColor getBackgroundNominalColorDisabled() const;
  134. void setBackgroundNominalColorDisabled(QColor c);
  135. QColor getBackgroundWarningColorDisabled() const;
  136. void setBackgroundWarningColorDisabled(QColor c);
  137. QColor getBackgroundErrorColorDisabled() const;
  138. void setBackgroundErrorColorDisabled(QColor c);
  139. QColor getForegroundNominalColorDisabled() const;
  140. void setForegroundNominalColorDisabled(QColor c);
  141. QColor getForegroundWarningColorDisabled() const;
  142. void setForegroundWarningColorDisabled(QColor c);
  143. QColor getForegroundErrorColorDisabled() const;
  144. void setForegroundErrorColorDisabled(QColor c);
  145. QColor getMagnitudeColor() const;
  146. void setMagnitudeColor(QColor c);
  147. QColor getMajorTickColor() const;
  148. void setMajorTickColor(QColor c);
  149. QColor getMinorTickColor() const;
  150. void setMinorTickColor(QColor c);
  151. qreal getWarningLevel() const;
  152. void setWarningLevel(qreal v);
  153. qreal getErrorLevel() const;
  154. void setErrorLevel(qreal v);
  155. void setPeakDecayRate(qreal v);
  156. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  157. virtual void mousePressEvent(QMouseEvent *event) override;
  158. virtual void wheelEvent(QWheelEvent *event) override;
  159. protected:
  160. void paintEvent(QPaintEvent *event) override;
  161. void changeEvent(QEvent *e) override;
  162. private slots:
  163. void handleSourceDestroyed() { deleteLater(); }
  164. };