audio_frame.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* audio_frame.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 AUDIOFRAME_H
  31. #define AUDIOFRAME_H
  32. #include "core/math/vector2.h"
  33. #include "core/typedefs.h"
  34. static inline float undenormalise(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. struct AudioFrame {
  45. //left and right samples
  46. float l, r;
  47. _ALWAYS_INLINE_ const float &operator[](int idx) const { return idx == 0 ? l : r; }
  48. _ALWAYS_INLINE_ float &operator[](int idx) { return idx == 0 ? l : r; }
  49. _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(l + p_frame.l, r + p_frame.r); }
  50. _ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(l - p_frame.l, r - p_frame.r); }
  51. _ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(l * p_frame.l, r * p_frame.r); }
  52. _ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(l / p_frame.l, r / p_frame.r); }
  53. _ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(l + p_sample, r + p_sample); }
  54. _ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(l - p_sample, r - p_sample); }
  55. _ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(l * p_sample, r * p_sample); }
  56. _ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(l / p_sample, r / p_sample); }
  57. _ALWAYS_INLINE_ void operator+=(const AudioFrame &p_frame) {
  58. l += p_frame.l;
  59. r += p_frame.r;
  60. }
  61. _ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) {
  62. l -= p_frame.l;
  63. r -= p_frame.r;
  64. }
  65. _ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) {
  66. l *= p_frame.l;
  67. r *= p_frame.r;
  68. }
  69. _ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) {
  70. l /= p_frame.l;
  71. r /= p_frame.r;
  72. }
  73. _ALWAYS_INLINE_ void operator+=(float p_sample) {
  74. l += p_sample;
  75. r += p_sample;
  76. }
  77. _ALWAYS_INLINE_ void operator-=(float p_sample) {
  78. l -= p_sample;
  79. r -= p_sample;
  80. }
  81. _ALWAYS_INLINE_ void operator*=(float p_sample) {
  82. l *= p_sample;
  83. r *= p_sample;
  84. }
  85. _ALWAYS_INLINE_ void operator/=(float p_sample) {
  86. l /= p_sample;
  87. r /= p_sample;
  88. }
  89. _ALWAYS_INLINE_ void undenormalise() {
  90. l = ::undenormalise(l);
  91. r = ::undenormalise(r);
  92. }
  93. _FORCE_INLINE_ AudioFrame linear_interpolate(const AudioFrame &p_b, float p_t) const {
  94. AudioFrame res = *this;
  95. res.l += (p_t * (p_b.l - l));
  96. res.r += (p_t * (p_b.r - r));
  97. return res;
  98. }
  99. _ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) {
  100. l = p_l;
  101. r = p_r;
  102. }
  103. _ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
  104. l = p_frame.l;
  105. r = p_frame.r;
  106. }
  107. _ALWAYS_INLINE_ AudioFrame operator=(const AudioFrame &p_frame) {
  108. l = p_frame.l;
  109. r = p_frame.r;
  110. return *this;
  111. }
  112. _ALWAYS_INLINE_ operator Vector2() const {
  113. return Vector2(l, r);
  114. }
  115. _ALWAYS_INLINE_ AudioFrame(const Vector2 &p_v2) {
  116. l = p_v2.x;
  117. r = p_v2.y;
  118. }
  119. _ALWAYS_INLINE_ AudioFrame() {}
  120. };
  121. #endif