| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/vector2.h" |
| 34 |
#include "core/typedefs.h" |
| 35 |
|
| 36 |
static _FORCE_INLINE_ float undenormalize(float f) { |
| 37 |
union { |
| 38 |
uint32_t i; |
| 39 |
float f; |
| 40 |
} v; |
| 41 |
|
| 42 |
v.f = f; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : f; |
| 47 |
} |
| 48 |
|
| 49 |
static const float AUDIO_PEAK_OFFSET = 0.0000000001f; |
| 50 |
static const float AUDIO_MIN_PEAK_DB = -200.0f; |
| 51 |
|
| 52 |
struct AudioFrame { |
| 53 |
|
| 54 |
union { |
| 55 |
|
| 56 |
struct { |
| 57 |
float left; |
| 58 |
float right; |
| 59 |
}; |
| 60 |
#ifndef DISABLE_DEPRECATED |
| 61 |
struct { |
| 62 |
float l; |
| 63 |
float r; |
| 64 |
}; |
| 65 |
#endif |
| 66 |
float levels[2] = { 0.0 }; |
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
_ALWAYS_INLINE_ const float &operator[](int p_idx) const { |
| 71 |
DEV_ASSERT((unsigned int)p_idx < 2); |
| 72 |
return levels[p_idx]; |
| 73 |
} |
| 74 |
_ALWAYS_INLINE_ float &operator[](int p_idx) { |
| 75 |
DEV_ASSERT((unsigned int)p_idx < 2); |
| 76 |
return levels[p_idx]; |
| 77 |
} |
| 78 |
|
| 79 |
constexpr AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(left + p_frame.left, right + p_frame.right); } |
| 80 |
constexpr AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(left - p_frame.left, right - p_frame.right); } |
| 81 |
constexpr AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(left * p_frame.left, right * p_frame.right); } |
| 82 |
constexpr AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(left / p_frame.left, right / p_frame.right); } |
| 83 |
|
| 84 |
constexpr AudioFrame operator+(float p_sample) const { return AudioFrame(left + p_sample, right + p_sample); } |
| 85 |
constexpr AudioFrame operator-(float p_sample) const { return AudioFrame(left - p_sample, right - p_sample); } |
| 86 |
constexpr AudioFrame operator*(float p_sample) const { return AudioFrame(left * p_sample, right * p_sample); } |
| 87 |
constexpr AudioFrame operator/(float p_sample) const { return AudioFrame(left / p_sample, right / p_sample); } |
| 88 |
|
| 89 |
constexpr void operator+=(const AudioFrame &p_frame) { |
| 90 |
left += p_frame.left; |
| 91 |
right += p_frame.right; |
| 92 |
} |
| 93 |
constexpr void operator-=(const AudioFrame &p_frame) { |
| 94 |
left -= p_frame.left; |
| 95 |
right -= p_frame.right; |
| 96 |
} |
| 97 |
constexpr void operator*=(const AudioFrame &p_frame) { |
| 98 |
left *= p_frame.left; |
| 99 |
right *= p_frame.right; |
| 100 |
} |
| 101 |
constexpr void operator/=(const AudioFrame &p_frame) { |
| 102 |
left /= p_frame.left; |
| 103 |
right /= p_frame.right; |
| 104 |
} |
| 105 |
|
| 106 |
constexpr void operator+=(float p_sample) { |
| 107 |
left += p_sample; |
| 108 |
right += p_sample; |
| 109 |
} |
| 110 |
constexpr void operator-=(float p_sample) { |
| 111 |
left -= p_sample; |
| 112 |
right -= p_sample; |
| 113 |
} |
| 114 |
constexpr void operator*=(float p_sample) { |
| 115 |
left *= p_sample; |
| 116 |
right *= p_sample; |
| 117 |
} |
| 118 |
constexpr void operator/=(float p_sample) { |
| 119 |
left /= p_sample; |
| 120 |
right /= p_sample; |
| 121 |
} |
| 122 |
|
| 123 |
_ALWAYS_INLINE_ void undenormalize() { |
| 124 |
left = ::undenormalize(left); |
| 125 |
right = ::undenormalize(right); |
| 126 |
} |
| 127 |
|
| 128 |
_FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const { |
| 129 |
AudioFrame res = *this; |
| 130 |
|
| 131 |
res.left += (p_t * (p_b.left - left)); |
| 132 |
res.right += (p_t * (p_b.right - right)); |
| 133 |
|
| 134 |
return res; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
constexpr AudioFrame(float p_left, float p_right) : |
| 139 |
left(p_left), right(p_right) {} |
| 140 |
constexpr AudioFrame(const AudioFrame &p_frame) : |
| 141 |
left(p_frame.left), right(p_frame.right) {} |
| 142 |
|
| 143 |
|
| 144 |
constexpr void operator=(const AudioFrame &p_frame) { |
| 145 |
left = p_frame.left; |
| 146 |
right = p_frame.right; |
| 147 |
} |
| 148 |
|
| 149 |
constexpr operator Vector2() const { |
| 150 |
return Vector2(left, right); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
constexpr AudioFrame(const Vector2 &p_v2) : |
| 155 |
left(p_v2.x), right(p_v2.y) {} |
| 156 |
constexpr AudioFrame() : |
| 157 |
left(0), right(0) {} |
| 158 |
|
| 159 |
}; |
| 160 |
|
| 161 |
constexpr AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) { |
| 162 |
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); |
| 163 |
} |
| 164 |
|
| 165 |
constexpr AudioFrame operator*(int32_t p_scalar, const AudioFrame &p_frame) { |
| 166 |
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); |
| 167 |
} |
| 168 |
|
| 169 |
constexpr AudioFrame operator*(int64_t p_scalar, const AudioFrame &p_frame) { |
| 170 |
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); |
| 171 |
} |
| 172 |
|
| 173 |
template <> |
| 174 |
struct is_zero_constructible<AudioFrame> : std::true_type {}; |