diff options
Diffstat (limited to 'core/math/audio_frame.h')
-rw-r--r-- | core/math/audio_frame.h | 110 |
1 files changed, 64 insertions, 46 deletions
diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h index d26336e9a2..e205126cdf 100644 --- a/core/math/audio_frame.h +++ b/core/math/audio_frame.h @@ -51,105 +51,123 @@ static const float AUDIO_PEAK_OFFSET = 0.0000000001f; static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear_to_db(AUDIO_PEAK_OFFSET) struct AudioFrame { - //left and right samples - float l = 0.f, r = 0.f; - - _ALWAYS_INLINE_ const float &operator[](int idx) const { return idx == 0 ? l : r; } - _ALWAYS_INLINE_ float &operator[](int idx) { return idx == 0 ? l : r; } + // Left and right samples. + union { + struct { + float left; + float right; + }; +#ifndef DISABLE_DEPRECATED + struct { + float l; + float r; + }; +#endif + float levels[2] = { 0.0 }; + }; + + _ALWAYS_INLINE_ const float &operator[](int p_idx) const { + DEV_ASSERT((unsigned int)p_idx < 2); + return levels[p_idx]; + } + _ALWAYS_INLINE_ float &operator[](int p_idx) { + DEV_ASSERT((unsigned int)p_idx < 2); + return levels[p_idx]; + } - _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(l + p_frame.l, r + p_frame.r); } - _ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(l - p_frame.l, r - p_frame.r); } - _ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(l * p_frame.l, r * p_frame.r); } - _ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(l / p_frame.l, r / p_frame.r); } + _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(left + p_frame.left, right + p_frame.right); } + _ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(left - p_frame.left, right - p_frame.right); } + _ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(left * p_frame.left, right * p_frame.right); } + _ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(left / p_frame.left, right / p_frame.right); } - _ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(l + p_sample, r + p_sample); } - _ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(l - p_sample, r - p_sample); } - _ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(l * p_sample, r * p_sample); } - _ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(l / p_sample, r / p_sample); } + _ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(left + p_sample, right + p_sample); } + _ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(left - p_sample, right - p_sample); } + _ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(left * p_sample, right * p_sample); } + _ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(left / p_sample, right / p_sample); } _ALWAYS_INLINE_ void operator+=(const AudioFrame &p_frame) { - l += p_frame.l; - r += p_frame.r; + left += p_frame.left; + right += p_frame.right; } _ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) { - l -= p_frame.l; - r -= p_frame.r; + left -= p_frame.left; + right -= p_frame.right; } _ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) { - l *= p_frame.l; - r *= p_frame.r; + left *= p_frame.left; + right *= p_frame.right; } _ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) { - l /= p_frame.l; - r /= p_frame.r; + left /= p_frame.left; + right /= p_frame.right; } _ALWAYS_INLINE_ void operator+=(float p_sample) { - l += p_sample; - r += p_sample; + left += p_sample; + right += p_sample; } _ALWAYS_INLINE_ void operator-=(float p_sample) { - l -= p_sample; - r -= p_sample; + left -= p_sample; + right -= p_sample; } _ALWAYS_INLINE_ void operator*=(float p_sample) { - l *= p_sample; - r *= p_sample; + left *= p_sample; + right *= p_sample; } _ALWAYS_INLINE_ void operator/=(float p_sample) { - l /= p_sample; - r /= p_sample; + left /= p_sample; + right /= p_sample; } _ALWAYS_INLINE_ void undenormalize() { - l = ::undenormalize(l); - r = ::undenormalize(r); + left = ::undenormalize(left); + right = ::undenormalize(right); } _FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const { AudioFrame res = *this; - res.l += (p_t * (p_b.l - l)); - res.r += (p_t * (p_b.r - r)); + res.left += (p_t * (p_b.left - left)); + res.right += (p_t * (p_b.right - right)); return res; } - _ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) { - l = p_l; - r = p_r; + _ALWAYS_INLINE_ AudioFrame(float p_left, float p_right) { + left = p_left; + right = p_right; } _ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) { - l = p_frame.l; - r = p_frame.r; + left = p_frame.left; + right = p_frame.right; } _ALWAYS_INLINE_ void operator=(const AudioFrame &p_frame) { - l = p_frame.l; - r = p_frame.r; + left = p_frame.left; + right = p_frame.right; } _ALWAYS_INLINE_ operator Vector2() const { - return Vector2(l, r); + return Vector2(left, right); } _ALWAYS_INLINE_ AudioFrame(const Vector2 &p_v2) { - l = p_v2.x; - r = p_v2.y; + left = p_v2.x; + right = p_v2.y; } _ALWAYS_INLINE_ AudioFrame() {} }; _ALWAYS_INLINE_ AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) { - return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar); + return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); } _ALWAYS_INLINE_ AudioFrame operator*(int32_t p_scalar, const AudioFrame &p_frame) { - return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar); + return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); } _ALWAYS_INLINE_ AudioFrame operator*(int64_t p_scalar, const AudioFrame &p_frame) { - return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar); + return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar); } #endif // AUDIO_FRAME_H |