audio_rb_resampler.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. // Downmix to stereo. Apply -3dB to center, and sides, -6dB to rear.
  69. // four channels - channel order: front left, front right, rear left, rear right
  70. if constexpr (C == 4) {
  71. float v0 = rb[(pos << 2) + 0] + rb[(pos << 2) + 2] / 2;
  72. float v1 = rb[(pos << 2) + 1] + rb[(pos << 2) + 3] / 2;
  73. float v0n = rb[(pos_next << 2) + 0] + rb[(pos_next << 2) + 2] / 2;
  74. float v1n = rb[(pos_next << 2) + 1] + rb[(pos_next << 2) + 3] / 2;
  75. v0 += (v0n - v0) * frac;
  76. v1 += (v1n - v1) * frac;
  77. p_dest[i] = AudioFrame(v0, v1);
  78. }
  79. // six channels - channel order: front left, center, front right, rear left, rear right, LFE
  80. if constexpr (C == 6) {
  81. float v0 = rb[(pos * 6) + 0] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 3] / 2;
  82. float v1 = rb[(pos * 6) + 2] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 4] / 2;
  83. float v0n = rb[(pos_next * 6) + 0] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 3] / 2;
  84. float v1n = rb[(pos_next * 6) + 2] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 4] / 2;
  85. v0 += (v0n - v0) * frac;
  86. v1 += (v1n - v1) * frac;
  87. p_dest[i] = AudioFrame(v0, v1);
  88. }
  89. // eight channels - channel order: front left, center, front right, side left, side right, rear left, rear
  90. // right, LFE
  91. if constexpr (C == 8) {
  92. float v0 = rb[(pos << 3) + 0] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 3] / Math::SQRT2 + rb[(pos << 3) + 5] / 2;
  93. float v1 = rb[(pos << 3) + 2] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 4] / Math::SQRT2 + rb[(pos << 3) + 6] / 2;
  94. float v0n = rb[(pos_next << 3) + 0] + rb[(pos_next << 3) + 1] / Math::SQRT2 + rb[(pos_next << 3) + 3] / Math::SQRT2 + rb[(pos_next << 3) + 5] / 2;
  95. float v1n = rb[(pos_next << 3) + 2] + rb[(pos_next << 3) + 1] / Math::SQRT2 + rb[(pos_next << 3) + 4] / Math::SQRT2 + rb[(pos_next << 3) + 6] / 2;
  96. v0 += (v0n - v0) * frac;
  97. v1 += (v1n - v1) * frac;
  98. p_dest[i] = AudioFrame(v0, v1);
  99. }
  100. }
  101. return read >> MIX_FRAC_BITS; //rb_read_pos = offset >> MIX_FRAC_BITS;
  102. }
  103. bool AudioRBResampler::mix(AudioFrame *p_dest, int p_frames) {
  104. if (!rb) {
  105. return false;
  106. }
  107. int32_t increment = (src_mix_rate * MIX_FRAC_LEN * playback_speed) / target_mix_rate;
  108. int read_space = get_reader_space();
  109. int target_todo = MIN(get_num_of_ready_frames(), p_frames);
  110. {
  111. int src_read = 0;
  112. switch (channels) {
  113. case 1:
  114. src_read = _resample<1>(p_dest, target_todo, increment);
  115. break;
  116. case 2:
  117. src_read = _resample<2>(p_dest, target_todo, increment);
  118. break;
  119. case 4:
  120. src_read = _resample<4>(p_dest, target_todo, increment);
  121. break;
  122. case 6:
  123. src_read = _resample<6>(p_dest, target_todo, increment);
  124. break;
  125. case 8:
  126. src_read = _resample<8>(p_dest, target_todo, increment);
  127. break;
  128. }
  129. if (src_read > read_space) {
  130. src_read = read_space;
  131. }
  132. rb_read_pos.set((rb_read_pos.get() + src_read) & rb_mask);
  133. // Create fadeout effect for the end of stream (note that it can be because of slow writer)
  134. if (p_frames - target_todo > 0) {
  135. for (int i = 0; i < target_todo; i++) {
  136. p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo);
  137. }
  138. }
  139. // Fill zeros (silence) for the rest of frames
  140. for (int i = target_todo; i < p_frames; i++) {
  141. p_dest[i] = AudioFrame(0, 0);
  142. }
  143. }
  144. return true;
  145. }
  146. int AudioRBResampler::get_num_of_ready_frames() {
  147. if (!is_ready()) {
  148. return 0;
  149. }
  150. int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
  151. int read_space = get_reader_space();
  152. return (int64_t(read_space) << MIX_FRAC_BITS) / increment;
  153. }
  154. Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) {
  155. ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6 && p_channels != 8, ERR_INVALID_PARAMETER);
  156. int desired_rb_bits = nearest_shift(MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed));
  157. bool recreate = !rb;
  158. if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) {
  159. memdelete_arr(rb);
  160. memdelete_arr(read_buf);
  161. recreate = true;
  162. }
  163. if (recreate) {
  164. channels = p_channels;
  165. rb_bits = desired_rb_bits;
  166. rb_len = (1 << rb_bits);
  167. rb_mask = rb_len - 1;
  168. const size_t array_size = rb_len * (size_t)p_channels;
  169. rb = memnew_arr(float, array_size);
  170. read_buf = memnew_arr(float, array_size);
  171. }
  172. src_mix_rate = p_src_mix_rate;
  173. target_mix_rate = p_target_mix_rate;
  174. offset = 0;
  175. rb_read_pos.set(0);
  176. rb_write_pos.set(0);
  177. //avoid maybe strange noises upon load
  178. for (unsigned int i = 0; i < (rb_len * channels); i++) {
  179. rb[i] = 0;
  180. read_buf[i] = 0;
  181. }
  182. return OK;
  183. }
  184. void AudioRBResampler::clear() {
  185. if (!rb) {
  186. return;
  187. }
  188. //should be stopped at this point but just in case
  189. memdelete_arr(rb);
  190. memdelete_arr(read_buf);
  191. rb = nullptr;
  192. offset = 0;
  193. rb_read_pos.set(0);
  194. rb_write_pos.set(0);
  195. read_buf = nullptr;
  196. }
  197. void AudioRBResampler::set_playback_speed(double p_playback_speed) {
  198. playback_speed = p_playback_speed;
  199. }
  200. double AudioRBResampler::get_playback_speed() const {
  201. return playback_speed;
  202. }
  203. AudioRBResampler::AudioRBResampler() {
  204. rb = nullptr;
  205. offset = 0;
  206. read_buf = nullptr;
  207. rb_read_pos.set(0);
  208. rb_write_pos.set(0);
  209. rb_bits = 0;
  210. rb_len = 0;
  211. rb_mask = 0;
  212. read_buff_len = 0;
  213. channels = 0;
  214. src_mix_rate = 0;
  215. target_mix_rate = 0;
  216. }
  217. AudioRBResampler::~AudioRBResampler() {
  218. if (rb) {
  219. memdelete_arr(rb);
  220. memdelete_arr(read_buf);
  221. }
  222. }