audio_rb_resampler.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*************************************************************************/
  2. /* audio_rb_resampler.cpp */
  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. #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. 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 VideoPlayer
  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 (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 (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 (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 (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. int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
  94. int read_space = get_reader_space();
  95. int target_todo = MIN(get_num_of_ready_frames(), p_frames);
  96. {
  97. int src_read = 0;
  98. switch (channels) {
  99. case 1: src_read = _resample<1>(p_dest, target_todo, increment); break;
  100. case 2: src_read = _resample<2>(p_dest, target_todo, increment); break;
  101. case 4: src_read = _resample<4>(p_dest, target_todo, increment); break;
  102. case 6: src_read = _resample<6>(p_dest, target_todo, increment); break;
  103. }
  104. if (src_read > read_space)
  105. src_read = read_space;
  106. rb_read_pos = (rb_read_pos + src_read) & rb_mask;
  107. // Create fadeout effect for the end of stream (note that it can be because of slow writer)
  108. if (p_frames - target_todo > 0) {
  109. for (int i = 0; i < target_todo; i++) {
  110. p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo);
  111. }
  112. }
  113. // Fill zeros (silence) for the rest of frames
  114. for (int i = target_todo; i < p_frames; i++) {
  115. p_dest[i] = AudioFrame(0, 0);
  116. }
  117. }
  118. return true;
  119. }
  120. int AudioRBResampler::get_num_of_ready_frames() {
  121. if (!is_ready())
  122. return 0;
  123. int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
  124. int read_space = get_reader_space();
  125. return (int64_t(read_space) << MIX_FRAC_BITS) / increment;
  126. }
  127. Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) {
  128. ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6, ERR_INVALID_PARAMETER);
  129. int desired_rb_bits = nearest_shift(MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed));
  130. bool recreate = !rb;
  131. if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) {
  132. memdelete_arr(rb);
  133. memdelete_arr(read_buf);
  134. recreate = true;
  135. }
  136. if (recreate) {
  137. channels = p_channels;
  138. rb_bits = desired_rb_bits;
  139. rb_len = (1 << rb_bits);
  140. rb_mask = rb_len - 1;
  141. rb = memnew_arr(float, rb_len *p_channels);
  142. read_buf = memnew_arr(float, rb_len *p_channels);
  143. }
  144. src_mix_rate = p_src_mix_rate;
  145. target_mix_rate = p_target_mix_rate;
  146. offset = 0;
  147. rb_read_pos = 0;
  148. rb_write_pos = 0;
  149. //avoid maybe strange noises upon load
  150. for (unsigned int i = 0; i < (rb_len * channels); i++) {
  151. rb[i] = 0;
  152. read_buf[i] = 0;
  153. }
  154. return OK;
  155. }
  156. void AudioRBResampler::clear() {
  157. if (!rb)
  158. return;
  159. //should be stopped at this point but just in case
  160. if (rb) {
  161. memdelete_arr(rb);
  162. memdelete_arr(read_buf);
  163. }
  164. rb = NULL;
  165. offset = 0;
  166. rb_read_pos = 0;
  167. rb_write_pos = 0;
  168. read_buf = NULL;
  169. }
  170. AudioRBResampler::AudioRBResampler() {
  171. rb = NULL;
  172. offset = 0;
  173. read_buf = NULL;
  174. rb_read_pos = 0;
  175. rb_write_pos = 0;
  176. rb_bits = 0;
  177. rb_len = 0;
  178. rb_mask = 0;
  179. read_buff_len = 0;
  180. channels = 0;
  181. src_mix_rate = 0;
  182. target_mix_rate = 0;
  183. }
  184. AudioRBResampler::~AudioRBResampler() {
  185. if (rb) {
  186. memdelete_arr(rb);
  187. memdelete_arr(read_buf);
  188. }
  189. }