audio_rb_resampler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**************************************************************************/
  2. /* audio_rb_resampler.cpp */
  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. #include "audio_rb_resampler.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/os/os.h"
  33. #include "servers/audio_server.h"
  34. int AudioRBResampler::get_channel_count() const {
  35. if (!rb) {
  36. return 0;
  37. }
  38. return channels;
  39. }
  40. // Linear interpolation based sample rate conversion (low quality)
  41. // Note that AudioStreamPlaybackResampled::mix has better algorithm,
  42. // but it wasn't obvious to integrate that with VideoStreamPlayer
  43. template <int C>
  44. uint32_t AudioRBResampler::_resample(AudioFrame *p_dest, int p_todo, int32_t p_increment) {
  45. uint32_t read = offset & MIX_FRAC_MASK;
  46. for (int i = 0; i < p_todo; i++) {
  47. offset = (offset + p_increment) & (((1 << (rb_bits + MIX_FRAC_BITS)) - 1));
  48. read += p_increment;
  49. uint32_t pos = offset >> MIX_FRAC_BITS;
  50. float frac = float(offset & MIX_FRAC_MASK) / float(MIX_FRAC_LEN);
  51. ERR_FAIL_COND_V(pos >= rb_len, 0);
  52. uint32_t pos_next = (pos + 1) & rb_mask;
  53. // since this is a template with a known compile time value (C), conditionals go away when compiling.
  54. if constexpr (C == 1) {
  55. float v0 = rb[pos];
  56. float v0n = rb[pos_next];
  57. v0 += (v0n - v0) * frac;
  58. p_dest[i] = AudioFrame(v0, v0);
  59. }
  60. if constexpr (C == 2) {
  61. float v0 = rb[(pos << 1) + 0];
  62. float v1 = rb[(pos << 1) + 1];
  63. float v0n = rb[(pos_next << 1) + 0];
  64. float v1n = rb[(pos_next << 1) + 1];
  65. v0 += (v0n - v0) * frac;
  66. v1 += (v1n - v1) * frac;
  67. p_dest[i] = AudioFrame(v0, v1);
  68. }
  69. // This will probably never be used, but added anyway
  70. if constexpr (C == 4) {
  71. float v0 = rb[(pos << 2) + 0];
  72. float v1 = rb[(pos << 2) + 1];
  73. float v0n = rb[(pos_next << 2) + 0];
  74. float v1n = rb[(pos_next << 2) + 1];
  75. v0 += (v0n - v0) * frac;
  76. v1 += (v1n - v1) * frac;
  77. p_dest[i] = AudioFrame(v0, v1);
  78. }
  79. if constexpr (C == 6) {
  80. float v0 = rb[(pos * 6) + 0];
  81. float v1 = rb[(pos * 6) + 1];
  82. float v0n = rb[(pos_next * 6) + 0];
  83. float v1n = rb[(pos_next * 6) + 1];
  84. v0 += (v0n - v0) * frac;
  85. v1 += (v1n - v1) * frac;
  86. p_dest[i] = AudioFrame(v0, v1);
  87. }
  88. }
  89. return read >> MIX_FRAC_BITS; //rb_read_pos = offset >> MIX_FRAC_BITS;
  90. }
  91. bool AudioRBResampler::mix(AudioFrame *p_dest, int p_frames) {
  92. if (!rb) {
  93. return false;
  94. }
  95. int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
  96. int read_space = get_reader_space();
  97. int target_todo = MIN(get_num_of_ready_frames(), p_frames);
  98. {
  99. int src_read = 0;
  100. switch (channels) {
  101. case 1:
  102. src_read = _resample<1>(p_dest, target_todo, increment);
  103. break;
  104. case 2:
  105. src_read = _resample<2>(p_dest, target_todo, increment);
  106. break;
  107. case 4:
  108. src_read = _resample<4>(p_dest, target_todo, increment);
  109. break;
  110. case 6:
  111. src_read = _resample<6>(p_dest, target_todo, increment);
  112. break;
  113. }
  114. if (src_read > read_space) {
  115. src_read = read_space;
  116. }
  117. rb_read_pos.set((rb_read_pos.get() + src_read) & rb_mask);
  118. // Create fadeout effect for the end of stream (note that it can be because of slow writer)
  119. if (p_frames - target_todo > 0) {
  120. for (int i = 0; i < target_todo; i++) {
  121. p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo);
  122. }
  123. }
  124. // Fill zeros (silence) for the rest of frames
  125. for (int i = target_todo; i < p_frames; i++) {
  126. p_dest[i] = AudioFrame(0, 0);
  127. }
  128. }
  129. return true;
  130. }
  131. int AudioRBResampler::get_num_of_ready_frames() {
  132. if (!is_ready()) {
  133. return 0;
  134. }
  135. int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
  136. int read_space = get_reader_space();
  137. return (int64_t(read_space) << MIX_FRAC_BITS) / increment;
  138. }
  139. Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) {
  140. ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6, ERR_INVALID_PARAMETER);
  141. int desired_rb_bits = nearest_shift(MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed));
  142. bool recreate = !rb;
  143. if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) {
  144. memdelete_arr(rb);
  145. memdelete_arr(read_buf);
  146. recreate = true;
  147. }
  148. if (recreate) {
  149. channels = p_channels;
  150. rb_bits = desired_rb_bits;
  151. rb_len = (1 << rb_bits);
  152. rb_mask = rb_len - 1;
  153. const size_t array_size = rb_len * (size_t)p_channels;
  154. rb = memnew_arr(float, array_size);
  155. read_buf = memnew_arr(float, array_size);
  156. }
  157. src_mix_rate = p_src_mix_rate;
  158. target_mix_rate = p_target_mix_rate;
  159. offset = 0;
  160. rb_read_pos.set(0);
  161. rb_write_pos.set(0);
  162. //avoid maybe strange noises upon load
  163. for (unsigned int i = 0; i < (rb_len * channels); i++) {
  164. rb[i] = 0;
  165. read_buf[i] = 0;
  166. }
  167. return OK;
  168. }
  169. void AudioRBResampler::clear() {
  170. if (!rb) {
  171. return;
  172. }
  173. //should be stopped at this point but just in case
  174. memdelete_arr(rb);
  175. memdelete_arr(read_buf);
  176. rb = nullptr;
  177. offset = 0;
  178. rb_read_pos.set(0);
  179. rb_write_pos.set(0);
  180. read_buf = nullptr;
  181. }
  182. AudioRBResampler::AudioRBResampler() {
  183. rb = nullptr;
  184. offset = 0;
  185. read_buf = nullptr;
  186. rb_read_pos.set(0);
  187. rb_write_pos.set(0);
  188. rb_bits = 0;
  189. rb_len = 0;
  190. rb_mask = 0;
  191. read_buff_len = 0;
  192. channels = 0;
  193. src_mix_rate = 0;
  194. target_mix_rate = 0;
  195. }
  196. AudioRBResampler::~AudioRBResampler() {
  197. if (rb) {
  198. memdelete_arr(rb);
  199. memdelete_arr(read_buf);
  200. }
  201. }