audio_stream_resampled.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* audio_stream_resampled.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_stream_resampled.h"
  30. #include "globals.h"
  31. int AudioStreamResampled::get_channel_count() const {
  32. if (!rb)
  33. return 0;
  34. return channels;
  35. }
  36. template<int C>
  37. void AudioStreamResampled::_resample(int32_t *p_dest,int p_todo,int32_t p_increment) {
  38. for (int i=0;i<p_todo;i++) {
  39. offset = (offset + p_increment)&(((1<<(rb_bits+MIX_FRAC_BITS))-1));
  40. uint32_t pos = offset >> MIX_FRAC_BITS;
  41. uint32_t frac = offset & MIX_FRAC_MASK;
  42. #ifndef FAST_AUDIO
  43. ERR_FAIL_COND(pos>=rb_len);
  44. #endif
  45. uint32_t pos_next = (pos+1)&rb_mask;
  46. //printf("rb pos %i\n",pos);
  47. // since this is a template with a known compile time value (C), conditionals go away when compiling.
  48. if (C==1) {
  49. int32_t v0 = rb[pos];
  50. int32_t v0n=rb[pos_next];
  51. #ifndef FAST_AUDIO
  52. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  53. #endif
  54. v0<<=16;
  55. p_dest[i]=v0;
  56. }
  57. if (C==2) {
  58. int32_t v0 = rb[(pos<<1)+0];
  59. int32_t v1 = rb[(pos<<1)+1];
  60. int32_t v0n=rb[(pos_next<<1)+0];
  61. int32_t v1n=rb[(pos_next<<1)+1];
  62. #ifndef FAST_AUDIO
  63. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  64. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  65. #endif
  66. v0<<=16;
  67. v1<<=16;
  68. p_dest[(i<<1)+0]=v0;
  69. p_dest[(i<<1)+1]=v1;
  70. }
  71. if (C==4) {
  72. int32_t v0 = rb[(pos<<2)+0];
  73. int32_t v1 = rb[(pos<<2)+1];
  74. int32_t v2 = rb[(pos<<2)+2];
  75. int32_t v3 = rb[(pos<<2)+3];
  76. int32_t v0n = rb[(pos_next<<2)+0];
  77. int32_t v1n=rb[(pos_next<<2)+1];
  78. int32_t v2n=rb[(pos_next<<2)+2];
  79. int32_t v3n=rb[(pos_next<<2)+3];
  80. #ifndef FAST_AUDIO
  81. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  82. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  83. v2+=(v2n-v2)*(int32_t)frac >> MIX_FRAC_BITS;
  84. v3+=(v3n-v3)*(int32_t)frac >> MIX_FRAC_BITS;
  85. #endif
  86. v0<<=16;
  87. v1<<=16;
  88. v2<<=16;
  89. v3<<=16;
  90. p_dest[(i<<2)+0]=v0;
  91. p_dest[(i<<2)+1]=v1;
  92. p_dest[(i<<2)+2]=v2;
  93. p_dest[(i<<2)+3]=v3;
  94. }
  95. if (C==6) {
  96. int32_t v0 = rb[(pos*6)+0];
  97. int32_t v1 = rb[(pos*6)+1];
  98. int32_t v2 = rb[(pos*6)+2];
  99. int32_t v3 = rb[(pos*6)+3];
  100. int32_t v4 = rb[(pos*6)+4];
  101. int32_t v5 = rb[(pos*6)+5];
  102. int32_t v0n = rb[(pos_next*6)+0];
  103. int32_t v1n=rb[(pos_next*6)+1];
  104. int32_t v2n=rb[(pos_next*6)+2];
  105. int32_t v3n=rb[(pos_next*6)+3];
  106. int32_t v4n=rb[(pos_next*6)+4];
  107. int32_t v5n=rb[(pos_next*6)+5];
  108. #ifndef FAST_AUDIO
  109. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  110. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  111. v2+=(v2n-v2)*(int32_t)frac >> MIX_FRAC_BITS;
  112. v3+=(v3n-v3)*(int32_t)frac >> MIX_FRAC_BITS;
  113. v4+=(v4n-v4)*(int32_t)frac >> MIX_FRAC_BITS;
  114. v5+=(v5n-v5)*(int32_t)frac >> MIX_FRAC_BITS;
  115. #endif
  116. v0<<=16;
  117. v1<<=16;
  118. v2<<=16;
  119. v3<<=16;
  120. v4<<=16;
  121. v5<<=16;
  122. p_dest[(i*6)+0]=v0;
  123. p_dest[(i*6)+1]=v1;
  124. p_dest[(i*6)+2]=v2;
  125. p_dest[(i*6)+3]=v3;
  126. p_dest[(i*6)+4]=v4;
  127. p_dest[(i*6)+5]=v5;
  128. }
  129. }
  130. rb_read_pos=offset>>MIX_FRAC_BITS;
  131. }
  132. bool AudioStreamResampled::mix(int32_t *p_dest, int p_frames) {
  133. if (!rb || !_can_mix())
  134. return false;
  135. int write_pos_cache=rb_write_pos;
  136. int32_t increment=(mix_rate*MIX_FRAC_LEN)/get_mix_rate();
  137. int rb_todo;
  138. if (write_pos_cache==rb_read_pos) {
  139. return false; //out of buffer
  140. } else if (rb_read_pos<write_pos_cache) {
  141. rb_todo=write_pos_cache-rb_read_pos-1;
  142. } else {
  143. rb_todo=(rb_len-rb_read_pos)+write_pos_cache-1;
  144. }
  145. int todo = MIN( ((int64_t(rb_todo)<<MIX_FRAC_BITS)/increment)+1, p_frames );
  146. #if 0
  147. if (int(mix_rate)==get_mix_rate()) {
  148. if (channels==6) {
  149. for(int i=0;i<p_frames;i++) {
  150. int from = ((rb_read_pos+i)&rb_mask)*6;
  151. int to = i*6;
  152. p_dest[from+0]=int32_t(rb[to+0])<<16;
  153. p_dest[from+1]=int32_t(rb[to+1])<<16;
  154. p_dest[from+2]=int32_t(rb[to+2])<<16;
  155. p_dest[from+3]=int32_t(rb[to+3])<<16;
  156. p_dest[from+4]=int32_t(rb[to+4])<<16;
  157. p_dest[from+5]=int32_t(rb[to+5])<<16;
  158. }
  159. } else {
  160. int len=p_frames*channels;
  161. int from=rb_read_pos*channels;
  162. int mask=0;
  163. switch(channels) {
  164. case 1: mask=rb_len-1; break;
  165. case 2: mask=(rb_len*2)-1; break;
  166. case 4: mask=(rb_len*4)-1; break;
  167. }
  168. for(int i=0;i<len;i++) {
  169. p_dest[i]=int32_t(rb[(from+i)&mask])<<16;
  170. }
  171. }
  172. rb_read_pos = (rb_read_pos+p_frames)&rb_mask;
  173. } else
  174. #endif
  175. {
  176. switch(channels) {
  177. case 1: _resample<1>(p_dest,todo,increment); break;
  178. case 2: _resample<2>(p_dest,todo,increment); break;
  179. case 4: _resample<4>(p_dest,todo,increment); break;
  180. case 6: _resample<6>(p_dest,todo,increment); break;
  181. }
  182. }
  183. return true;
  184. }
  185. Error AudioStreamResampled::_setup(int p_channels,int p_mix_rate,int p_minbuff_needed) {
  186. ERR_FAIL_COND_V(p_channels!=1 && p_channels!=2 && p_channels!=4 && p_channels!=6,ERR_INVALID_PARAMETER);
  187. float buffering_sec = int(GLOBAL_DEF("audio/stream_buffering_ms",500))/1000.0;
  188. int desired_rb_bits =nearest_shift(MAX(buffering_sec*p_mix_rate,p_minbuff_needed));
  189. bool recreate=!rb;
  190. if (rb && (uint32_t(desired_rb_bits)!=rb_bits || channels!=uint32_t(p_channels))) {
  191. //recreate
  192. memdelete_arr(rb);
  193. memdelete_arr(read_buf);
  194. recreate=true;
  195. }
  196. if (recreate) {
  197. channels=p_channels;
  198. rb_bits=desired_rb_bits;
  199. rb_len=(1<<rb_bits);
  200. rb_mask=rb_len-1;
  201. rb = memnew_arr( int16_t, rb_len * p_channels );
  202. read_buf = memnew_arr( int16_t, rb_len * p_channels );
  203. }
  204. mix_rate=p_mix_rate;
  205. offset=0;
  206. rb_read_pos=0;
  207. rb_write_pos=0;
  208. //avoid maybe strange noises upon load
  209. for (int i=0;i<(rb_len*channels);i++) {
  210. rb[i]=0;
  211. read_buf[i]=0;
  212. }
  213. return OK;
  214. }
  215. void AudioStreamResampled::_clear() {
  216. if (!rb)
  217. return;
  218. AudioServer::get_singleton()->lock();
  219. //should be stopped at this point but just in case
  220. if (rb) {
  221. memdelete_arr(rb);
  222. memdelete_arr(read_buf);
  223. }
  224. rb=NULL;
  225. offset=0;
  226. rb_read_pos=0;
  227. rb_write_pos=0;
  228. read_buf=NULL;
  229. AudioServer::get_singleton()->unlock();
  230. }
  231. AudioStreamResampled::AudioStreamResampled() {
  232. rb=NULL;
  233. offset=0;
  234. read_buf=NULL;
  235. }
  236. AudioStreamResampled::~AudioStreamResampled() {
  237. if (rb) {
  238. memdelete_arr(rb);
  239. memdelete_arr(read_buf);
  240. }
  241. }