libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
audiolevel.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_PROAV
13#include <cmath>
14#include <limits>
15#include <promeki/namespace.h>
16#include <promeki/string.h>
17
18PROMEKI_NAMESPACE_BEGIN
19
20class String;
21
39class AudioLevel {
40 public:
46 static AudioLevel fromDbfs(double dbfs) { return AudioLevel(dbfs); }
47
53 static AudioLevel fromLinear(double linear) {
54 if (linear <= 0.0) return AudioLevel(-std::numeric_limits<double>::infinity());
55 return AudioLevel(20.0 * std::log10(linear));
56 }
57
59 AudioLevel() : _dbfs(-std::numeric_limits<double>::infinity()) {}
60
65 explicit AudioLevel(double dbfs) : _dbfs(dbfs) {}
66
68 double dbfs() const { return _dbfs; }
69
74 double toLinear() const {
75 if (std::isinf(_dbfs) && _dbfs < 0) return 0.0;
76 return std::pow(10.0, _dbfs / 20.0);
77 }
78
83 float toLinearFloat() const { return static_cast<float>(toLinear()); }
84
86 bool isSilence() const { return std::isinf(_dbfs) && _dbfs < 0; }
87
89 bool isClipping() const { return _dbfs > 0.0; }
90
95 String toString() const;
96
98 bool operator==(const AudioLevel &rhs) const { return _dbfs == rhs._dbfs; }
100 bool operator!=(const AudioLevel &rhs) const { return _dbfs != rhs._dbfs; }
102 bool operator<(const AudioLevel &rhs) const { return _dbfs < rhs._dbfs; }
104 bool operator>(const AudioLevel &rhs) const { return _dbfs > rhs._dbfs; }
106 bool operator<=(const AudioLevel &rhs) const { return _dbfs <= rhs._dbfs; }
108 bool operator>=(const AudioLevel &rhs) const { return _dbfs >= rhs._dbfs; }
109
110 private:
111 double _dbfs;
112};
113
114PROMEKI_NAMESPACE_END
115
116PROMEKI_FORMAT_VIA_TOSTRING(promeki::AudioLevel);
117
118#endif // PROMEKI_ENABLE_PROAV