volume-control.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, int width,
  89. int height, float peakHold);
  90. void paintHMeter(QPainter &painter, int x, int y, int width, int height,
  91. float magnitude, float peak, float peakHold);
  92. void paintHTicks(QPainter &painter, int x, int y, int width,
  93. int height);
  94. void paintVMeter(QPainter &painter, int x, int y, int width, int height,
  95. float magnitude, float peak, float peakHold);
  96. void paintVTicks(QPainter &painter, int x, int y, int height);
  97. QMutex dataMutex;
  98. uint64_t currentLastUpdateTime = 0;
  99. float currentMagnitude[MAX_AUDIO_CHANNELS];
  100. float currentPeak[MAX_AUDIO_CHANNELS];
  101. float currentInputPeak[MAX_AUDIO_CHANNELS];
  102. QPixmap *tickPaintCache = nullptr;
  103. int displayNrAudioChannels = 0;
  104. float displayMagnitude[MAX_AUDIO_CHANNELS];
  105. float displayPeak[MAX_AUDIO_CHANNELS];
  106. float displayPeakHold[MAX_AUDIO_CHANNELS];
  107. uint64_t displayPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  108. float displayInputPeakHold[MAX_AUDIO_CHANNELS];
  109. uint64_t displayInputPeakHoldLastUpdateTime[MAX_AUDIO_CHANNELS];
  110. QFont tickFont;
  111. QColor backgroundNominalColor;
  112. QColor backgroundWarningColor;
  113. QColor backgroundErrorColor;
  114. QColor foregroundNominalColor;
  115. QColor foregroundWarningColor;
  116. QColor foregroundErrorColor;
  117. QColor clipColor;
  118. QColor magnitudeColor;
  119. QColor majorTickColor;
  120. QColor minorTickColor;
  121. qreal minimumLevel;
  122. qreal warningLevel;
  123. qreal errorLevel;
  124. qreal clipLevel;
  125. qreal minimumInputLevel;
  126. qreal peakDecayRate;
  127. qreal magnitudeIntegrationTime;
  128. qreal peakHoldDuration;
  129. qreal inputPeakHoldDuration;
  130. uint64_t lastRedrawTime = 0;
  131. bool clipping = false;
  132. bool vertical;
  133. public:
  134. explicit VolumeMeter(QWidget *parent = nullptr,
  135. obs_volmeter_t *obs_volmeter = nullptr,
  136. bool vertical = false);
  137. ~VolumeMeter();
  138. void setLevels(
  139. const float magnitude[MAX_AUDIO_CHANNELS],
  140. const float peak[MAX_AUDIO_CHANNELS],
  141. const float inputPeak[MAX_AUDIO_CHANNELS]);
  142. QColor getBackgroundNominalColor() const;
  143. void setBackgroundNominalColor(QColor c);
  144. QColor getBackgroundWarningColor() const;
  145. void setBackgroundWarningColor(QColor c);
  146. QColor getBackgroundErrorColor() const;
  147. void setBackgroundErrorColor(QColor c);
  148. QColor getForegroundNominalColor() const;
  149. void setForegroundNominalColor(QColor c);
  150. QColor getForegroundWarningColor() const;
  151. void setForegroundWarningColor(QColor c);
  152. QColor getForegroundErrorColor() const;
  153. void setForegroundErrorColor(QColor c);
  154. QColor getClipColor() const;
  155. void setClipColor(QColor c);
  156. QColor getMagnitudeColor() const;
  157. void setMagnitudeColor(QColor c);
  158. QColor getMajorTickColor() const;
  159. void setMajorTickColor(QColor c);
  160. QColor getMinorTickColor() const;
  161. void setMinorTickColor(QColor c);
  162. qreal getMinimumLevel() const;
  163. void setMinimumLevel(qreal v);
  164. qreal getWarningLevel() const;
  165. void setWarningLevel(qreal v);
  166. qreal getErrorLevel() const;
  167. void setErrorLevel(qreal v);
  168. qreal getClipLevel() const;
  169. void setClipLevel(qreal v);
  170. qreal getMinimumInputLevel() const;
  171. void setMinimumInputLevel(qreal v);
  172. qreal getPeakDecayRate() const;
  173. void setPeakDecayRate(qreal v);
  174. qreal getMagnitudeIntegrationTime() const;
  175. void setMagnitudeIntegrationTime(qreal v);
  176. qreal getPeakHoldDuration() const;
  177. void setPeakHoldDuration(qreal v);
  178. qreal getInputPeakHoldDuration() const;
  179. void setInputPeakHoldDuration(qreal v);
  180. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  181. protected:
  182. void paintEvent(QPaintEvent *event) override;
  183. };
  184. class VolumeMeterTimer : public QTimer {
  185. Q_OBJECT
  186. public:
  187. inline VolumeMeterTimer() : QTimer() {}
  188. void AddVolControl(VolumeMeter *meter);
  189. void RemoveVolControl(VolumeMeter *meter);
  190. protected:
  191. void timerEvent(QTimerEvent *event) override;
  192. QList<VolumeMeter*> volumeMeters;
  193. };
  194. class QLabel;
  195. class QSlider;
  196. class MuteCheckBox;
  197. class VolControl : public QWidget {
  198. Q_OBJECT
  199. private:
  200. OBSSource source;
  201. QLabel *nameLabel;
  202. QLabel *volLabel;
  203. VolumeMeter *volMeter;
  204. QSlider *slider;
  205. MuteCheckBox *mute;
  206. QPushButton *config = nullptr;
  207. float levelTotal;
  208. float levelCount;
  209. obs_fader_t *obs_fader;
  210. obs_volmeter_t *obs_volmeter;
  211. bool vertical;
  212. static void OBSVolumeChanged(void *param, float db);
  213. static void OBSVolumeLevel(void *data,
  214. const float magnitude[MAX_AUDIO_CHANNELS],
  215. const float peak[MAX_AUDIO_CHANNELS],
  216. const float inputPeak[MAX_AUDIO_CHANNELS]);
  217. static void OBSVolumeMuted(void *data, calldata_t *calldata);
  218. void EmitConfigClicked();
  219. private slots:
  220. void VolumeChanged();
  221. void VolumeMuted(bool muted);
  222. void SetMuted(bool checked);
  223. void SliderChanged(int vol);
  224. void updateText();
  225. signals:
  226. void ConfigClicked();
  227. public:
  228. explicit VolControl(OBSSource source, bool showConfig = false,
  229. bool vertical = false);
  230. ~VolControl();
  231. inline obs_source_t *GetSource() const {return source;}
  232. QString GetName() const;
  233. void SetName(const QString &newName);
  234. void SetMeterDecayRate(qreal q);
  235. void setPeakMeterType(enum obs_peak_meter_type peakMeterType);
  236. };