volume-control.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 bkColor READ getBkColor WRITE setBkColor DESIGNABLE true)
  14. Q_PROPERTY(QColor magColor READ getMagColor WRITE setMagColor DESIGNABLE true)
  15. Q_PROPERTY(QColor peakColor READ getPeakColor WRITE setPeakColor DESIGNABLE true)
  16. Q_PROPERTY(QColor peakHoldColor READ getPeakHoldColor WRITE setPeakHoldColor DESIGNABLE true)
  17. private:
  18. static QWeakPointer<VolumeMeterTimer> updateTimer;
  19. QSharedPointer<VolumeMeterTimer> updateTimerRef;
  20. float curMag = 0.0f, curPeak = 0.0f, curPeakHold = 0.0f;
  21. inline void calcLevels();
  22. QMutex dataMutex;
  23. float mag = 0.0f, peak = 0.0f, peakHold = 0.0f;
  24. float multiple = 0.0f;
  25. uint64_t lastUpdateTime = 0;
  26. QColor bkColor, magColor, peakColor, peakHoldColor;
  27. public:
  28. explicit VolumeMeter(QWidget *parent = 0);
  29. ~VolumeMeter();
  30. void setLevels(float nmag, float npeak, float npeakHold);
  31. QColor getBkColor() const;
  32. void setBkColor(QColor c);
  33. QColor getMagColor() const;
  34. void setMagColor(QColor c);
  35. QColor getPeakColor() const;
  36. void setPeakColor(QColor c);
  37. QColor getPeakHoldColor() const;
  38. void setPeakHoldColor(QColor c);
  39. protected:
  40. void paintEvent(QPaintEvent *event);
  41. };
  42. class VolumeMeterTimer : public QTimer {
  43. Q_OBJECT
  44. public:
  45. inline VolumeMeterTimer() : QTimer() {}
  46. void AddVolControl(VolumeMeter *meter);
  47. void RemoveVolControl(VolumeMeter *meter);
  48. protected:
  49. virtual void timerEvent(QTimerEvent *event) override;
  50. QList<VolumeMeter*> volumeMeters;
  51. };
  52. class QLabel;
  53. class QSlider;
  54. class MuteCheckBox;
  55. class VolControl : public QWidget {
  56. Q_OBJECT
  57. private:
  58. OBSSource source;
  59. QLabel *nameLabel;
  60. QLabel *volLabel;
  61. VolumeMeter *volMeter;
  62. QSlider *slider;
  63. MuteCheckBox *mute;
  64. QPushButton *config = nullptr;
  65. float levelTotal;
  66. float levelCount;
  67. obs_fader_t *obs_fader;
  68. obs_volmeter_t *obs_volmeter;
  69. static void OBSVolumeChanged(void *param, float db);
  70. static void OBSVolumeLevel(void *data, float level, float mag,
  71. float peak, float muted);
  72. static void OBSVolumeMuted(void *data, calldata_t *calldata);
  73. void EmitConfigClicked();
  74. private slots:
  75. void VolumeChanged();
  76. void VolumeMuted(bool muted);
  77. void VolumeLevel(float mag, float peak, float peakHold, bool muted);
  78. void SetMuted(bool checked);
  79. void SliderChanged(int vol);
  80. void updateText();
  81. signals:
  82. void ConfigClicked();
  83. public:
  84. VolControl(OBSSource source, bool showConfig = false);
  85. ~VolControl();
  86. inline obs_source_t *GetSource() const {return source;}
  87. QString GetName() const;
  88. void SetName(const QString &newName);
  89. };