audio_frame.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*************************************************************************/
  2. /* audio_frame.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "typedefs.h"
  33. static inline float undenormalise(volatile float f) {
  34. union {
  35. uint32_t i;
  36. float f;
  37. } v;
  38. v.f = f;
  39. // original: return (v.i & 0x7f800000) == 0 ? 0.0f : f;
  40. // version from Tim Blechmann:
  41. return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : f;
  42. }
  43. struct AudioFrame {
  44. //left and right samples
  45. float l, r;
  46. _ALWAYS_INLINE_ const float &operator[](int idx) const { return idx == 0 ? l : r; }
  47. _ALWAYS_INLINE_ float &operator[](int idx) { return idx == 0 ? l : r; }
  48. _ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(l + p_frame.l, r + p_frame.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+(float p_sample) const { return AudioFrame(l + p_sample, r + p_sample); }
  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_ void operator+=(const AudioFrame &p_frame) {
  57. l += p_frame.l;
  58. r += p_frame.r;
  59. }
  60. _ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) {
  61. l -= p_frame.l;
  62. r -= p_frame.r;
  63. }
  64. _ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) {
  65. l *= p_frame.l;
  66. r *= p_frame.r;
  67. }
  68. _ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) {
  69. l /= p_frame.l;
  70. r /= p_frame.r;
  71. }
  72. _ALWAYS_INLINE_ void operator+=(float p_sample) {
  73. l += p_sample;
  74. r += p_sample;
  75. }
  76. _ALWAYS_INLINE_ void operator-=(float p_sample) {
  77. l -= p_sample;
  78. r -= p_sample;
  79. }
  80. _ALWAYS_INLINE_ void operator*=(float p_sample) {
  81. l *= p_sample;
  82. r *= p_sample;
  83. }
  84. _ALWAYS_INLINE_ void operator/=(float p_sample) {
  85. l /= p_sample;
  86. r /= p_sample;
  87. }
  88. _ALWAYS_INLINE_ void undenormalise() {
  89. l = ::undenormalise(l);
  90. r = ::undenormalise(r);
  91. }
  92. _FORCE_INLINE_ AudioFrame linear_interpolate(const AudioFrame &p_b, float p_t) const {
  93. AudioFrame res = *this;
  94. res.l += (p_t * (p_b.l - l));
  95. res.r += (p_t * (p_b.r - r));
  96. return res;
  97. }
  98. _ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) {
  99. l = p_l;
  100. r = p_r;
  101. }
  102. _ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
  103. l = p_frame.l;
  104. r = p_frame.r;
  105. }
  106. _ALWAYS_INLINE_ AudioFrame() {}
  107. };
  108. #endif