| 1 | /* |
| 2 | Copyright (C) 2005-2019 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifndef VUMETER_H |
| 19 | #define VUMETER_H |
| 20 | |
| 21 | #include <QTime> |
| 22 | #include <QWidget> |
| 23 | #include <QtCore> |
| 24 | #include <math.h> |
| 25 | |
| 26 | // helper functions |
| 27 | const float LEVEL_MIN = 1.f / (1 << 20); // minimal positive sample for 20 bit resolution |
| 28 | inline float levelToDB(float level) |
| 29 | { |
| 30 | if (level < LEVEL_MIN) |
| 31 | level = LEVEL_MIN; // prevent from div by 0 |
| 32 | return (25.0f) * log(level); |
| 33 | } |
| 34 | |
| 35 | inline float DBToLevel(float db) |
| 36 | { |
| 37 | return exp(db / (log(2.f) / 6.f)); |
| 38 | } |
| 39 | |
| 40 | class VUMeter : public QWidget { |
| 41 | Q_OBJECT |
| 42 | Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation) |
| 43 | public: |
| 44 | VUMeter(QWidget* parent = nullptr); |
| 45 | ~VUMeter(); |
| 46 | |
| 47 | void setValueLeft(double); |
| 48 | void setValueRight(double); |
| 49 | void setPercentage(double); |
| 50 | void checkPeakTime(); |
| 51 | void setOrientation(Qt::Orientation); |
| 52 | Qt::Orientation orientation() const; |
| 53 | void setLinesPerSegment(int); |
| 54 | void setSpacesBetweenSegments(int); |
| 55 | void setSegmentsPerPeak(int); |
| 56 | void setMargin(int); |
| 57 | QColor LevelColorNormal; |
| 58 | QColor LevelColorHigh; |
| 59 | QColor LevelColorOff; |
| 60 | QColor BackgroundColor; |
| 61 | void reset(); |
| 62 | |
| 63 | protected: |
| 64 | void paintEvent(QPaintEvent*); |
| 65 | void resizeEvent(QResizeEvent*); |
| 66 | |
| 67 | private: |
| 68 | struct VUMeterPrivate* p; |
| 69 | void drawMeter(); |
| 70 | }; |
| 71 | |
| 72 | #endif |