audio_rb_resampler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* audio_rb_resampler.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 AUDIO_RB_RESAMPLER_H
  31. #define AUDIO_RB_RESAMPLER_H
  32. #include "core/os/memory.h"
  33. #include "core/safe_refcount.h"
  34. #include "core/typedefs.h"
  35. #include "servers/audio_server.h"
  36. struct AudioRBResampler {
  37. uint32_t rb_bits;
  38. uint32_t rb_len;
  39. uint32_t rb_mask;
  40. uint32_t read_buff_len;
  41. uint32_t channels;
  42. uint32_t src_mix_rate;
  43. uint32_t target_mix_rate;
  44. SafeNumeric<int> rb_read_pos;
  45. SafeNumeric<int> rb_write_pos;
  46. int32_t offset; //contains the fractional remainder of the resampler
  47. enum {
  48. MIX_FRAC_BITS = 13,
  49. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  50. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  51. };
  52. float *read_buf;
  53. float *rb;
  54. template <int C>
  55. uint32_t _resample(AudioFrame *p_dest, int p_todo, int32_t p_increment);
  56. public:
  57. _FORCE_INLINE_ void flush() {
  58. rb_read_pos.set(0);
  59. rb_write_pos.set(0);
  60. offset = 0;
  61. }
  62. _FORCE_INLINE_ bool is_ready() const {
  63. return rb != nullptr;
  64. }
  65. _FORCE_INLINE_ int get_total() const {
  66. return rb_len - 1;
  67. }
  68. _FORCE_INLINE_ int get_writer_space() const {
  69. int space, r, w;
  70. r = rb_read_pos.get();
  71. w = rb_write_pos.get();
  72. if (r == w) {
  73. space = rb_len - 1;
  74. } else if (w < r) {
  75. space = r - w - 1;
  76. } else {
  77. space = (rb_len - r) + w - 1;
  78. }
  79. return space;
  80. }
  81. _FORCE_INLINE_ int get_reader_space() const {
  82. int space, r, w;
  83. r = rb_read_pos.get();
  84. w = rb_write_pos.get();
  85. if (r == w) {
  86. space = 0;
  87. } else if (w < r) {
  88. space = rb_len - r + w;
  89. } else {
  90. space = w - r;
  91. }
  92. return space;
  93. }
  94. _FORCE_INLINE_ bool has_data() const {
  95. return rb && rb_read_pos.get() != rb_write_pos.get();
  96. }
  97. _FORCE_INLINE_ float *get_write_buffer() { return read_buf; }
  98. _FORCE_INLINE_ void write(uint32_t p_frames) {
  99. ERR_FAIL_COND(p_frames >= rb_len);
  100. int wp = rb_write_pos.get();
  101. switch (channels) {
  102. case 1: {
  103. for (uint32_t i = 0; i < p_frames; i++) {
  104. rb[wp] = read_buf[i];
  105. wp = (wp + 1) & rb_mask;
  106. }
  107. } break;
  108. case 2: {
  109. for (uint32_t i = 0; i < p_frames; i++) {
  110. rb[(wp << 1) + 0] = read_buf[(i << 1) + 0];
  111. rb[(wp << 1) + 1] = read_buf[(i << 1) + 1];
  112. wp = (wp + 1) & rb_mask;
  113. }
  114. } break;
  115. case 4: {
  116. for (uint32_t i = 0; i < p_frames; i++) {
  117. rb[(wp << 2) + 0] = read_buf[(i << 2) + 0];
  118. rb[(wp << 2) + 1] = read_buf[(i << 2) + 1];
  119. rb[(wp << 2) + 2] = read_buf[(i << 2) + 2];
  120. rb[(wp << 2) + 3] = read_buf[(i << 2) + 3];
  121. wp = (wp + 1) & rb_mask;
  122. }
  123. } break;
  124. case 6: {
  125. for (uint32_t i = 0; i < p_frames; i++) {
  126. rb[(wp * 6) + 0] = read_buf[(i * 6) + 0];
  127. rb[(wp * 6) + 1] = read_buf[(i * 6) + 1];
  128. rb[(wp * 6) + 2] = read_buf[(i * 6) + 2];
  129. rb[(wp * 6) + 3] = read_buf[(i * 6) + 3];
  130. rb[(wp * 6) + 4] = read_buf[(i * 6) + 4];
  131. rb[(wp * 6) + 5] = read_buf[(i * 6) + 5];
  132. wp = (wp + 1) & rb_mask;
  133. }
  134. } break;
  135. }
  136. rb_write_pos.set(wp);
  137. }
  138. int get_channel_count() const;
  139. Error setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed = -1);
  140. void clear();
  141. bool mix(AudioFrame *p_dest, int p_frames);
  142. int get_num_of_ready_frames();
  143. AudioRBResampler();
  144. ~AudioRBResampler();
  145. };
  146. #endif // AUDIO_RB_RESAMPLER_H