-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudiolevelmeter.h
66 lines (56 loc) · 2.06 KB
/
audiolevelmeter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef AUDIOLEVELMONITOR_H
#define AUDIOLEVELMONITOR_H
#include <QObject>
#include <QAudioBuffer>
/**
* An audio level meter.
* Reads data from a QAudioBuffer and updates audio level readings.
* Also implements audio a heuristic trigger signal based on the average level of the audio.
*/
class AudioLevelMeter : public QObject
{
Q_OBJECT
public:
typedef enum { LOW, HIGH } ThresholdState;
AudioLevelMeter(QObject *parent = nullptr);
~AudioLevelMeter();
void init();
QAudioFormat format() const { return m_audioFormat; }
bool setFormat(const QAudioFormat &format);
void setDampening(qreal d) { m_alphaExp = qMax(0.0, qMin(1.0, d)); }
qreal getDampening() const { return m_alphaExp; }
qreal getLowThreshold() const { return m_lowLevelThreshold; }
qreal setLowThreshold(qreal l);
qreal getHighThreshold() const { return m_highLevelThreshold; }
qreal setHighThreshold(qreal h);
QPair<qreal,qreal> thresholds() const { return QPair<qreal,qreal>(m_lowLevelThreshold, m_highLevelThreshold); }
bool isValid() { return m_validAudioFormat; }
quint32 maxAmplitude() const;
QList<qreal> avgAmplitude() const;
QList<qreal> expAmplitude() const;
QList<qreal> peakAmplitude() const;
qreal channelAvgAmplitude() const;
qreal channelExpAmplitude() const;
qreal channelPeakAmplitude() const;
ThresholdState thresholdState() const { return m_thresholdState; }
private slots:
int read(const QAudioBuffer &buffer);
private:
void updateThresholLevel();
QAudioFormat m_audioFormat;
bool m_validAudioFormat = false;
qint32 m_channelCount = 0;
qreal m_alphaExp = 0.990;
quint32 m_maxAmplitude = 0;
QList<qreal> m_peakAmplitude;
QList<qreal> m_avgAmplitude;
QList<qreal> m_expAmplitude;
ThresholdState m_thresholdState = LOW;
qreal m_lowLevelThreshold = 0.2;
qreal m_highLevelThreshold = 0.4;
signals:
void levelChange(AudioLevelMeter::ThresholdState level);
void update(int read);
};
QDebug operator<<(QDebug dbg, AudioLevelMeter &s);
#endif // AUDIOLEVELMONITOR_H