audio_frame.h 6.2 KB

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