Debian Codegraph @

core/math/audio_frame.h

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_frame.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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 // original: return (v.i & 0x7f800000) == 0 ? 0.0f : f;
45 // version from Tim Blechmann:
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; // linear_to_db(AUDIO_PEAK_OFFSET)
51
52 struct AudioFrame {
53 // Left and right samples.
54 union {
55 // NOLINTBEGIN(modernize-use-default-member-init)
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 // NOLINTEND(modernize-use-default-member-init)
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 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
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 // NOLINTEND(cppcoreguidelines-pro-type-member-init)
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 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
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 // NOLINTEND(cppcoreguidelines-pro-type-member-init)
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 {};