audio_rb_resampler.h 5.3 KB

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