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