audio_stream_resampled.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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-2016 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. #if 0
  32. int AudioStreamResampled::get_channel_count() const {
  33. if (!rb)
  34. return 0;
  35. return channels;
  36. }
  37. template<int C>
  38. uint32_t AudioStreamResampled::_resample(int32_t *p_dest,int p_todo,int32_t p_increment) {
  39. uint32_t read=offset&MIX_FRAC_MASK;
  40. for (int i=0;i<p_todo;i++) {
  41. offset = (offset + p_increment)&(((1<<(rb_bits+MIX_FRAC_BITS))-1));
  42. read+=p_increment;
  43. uint32_t pos = offset >> MIX_FRAC_BITS;
  44. uint32_t frac = offset & MIX_FRAC_MASK;
  45. #ifndef FAST_AUDIO
  46. ERR_FAIL_COND_V(pos>=rb_len,0);
  47. #endif
  48. uint32_t pos_next = (pos+1)&rb_mask;
  49. //printf("rb pos %i\n",pos);
  50. // since this is a template with a known compile time value (C), conditionals go away when compiling.
  51. if (C==1) {
  52. int32_t v0 = rb[pos];
  53. int32_t v0n=rb[pos_next];
  54. #ifndef FAST_AUDIO
  55. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  56. #endif
  57. v0<<=16;
  58. p_dest[i]=v0;
  59. }
  60. if (C==2) {
  61. int32_t v0 = rb[(pos<<1)+0];
  62. int32_t v1 = rb[(pos<<1)+1];
  63. int32_t v0n=rb[(pos_next<<1)+0];
  64. int32_t v1n=rb[(pos_next<<1)+1];
  65. #ifndef FAST_AUDIO
  66. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  67. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  68. #endif
  69. v0<<=16;
  70. v1<<=16;
  71. p_dest[(i<<1)+0]=v0;
  72. p_dest[(i<<1)+1]=v1;
  73. }
  74. if (C==4) {
  75. int32_t v0 = rb[(pos<<2)+0];
  76. int32_t v1 = rb[(pos<<2)+1];
  77. int32_t v2 = rb[(pos<<2)+2];
  78. int32_t v3 = rb[(pos<<2)+3];
  79. int32_t v0n = rb[(pos_next<<2)+0];
  80. int32_t v1n=rb[(pos_next<<2)+1];
  81. int32_t v2n=rb[(pos_next<<2)+2];
  82. int32_t v3n=rb[(pos_next<<2)+3];
  83. #ifndef FAST_AUDIO
  84. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  85. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  86. v2+=(v2n-v2)*(int32_t)frac >> MIX_FRAC_BITS;
  87. v3+=(v3n-v3)*(int32_t)frac >> MIX_FRAC_BITS;
  88. #endif
  89. v0<<=16;
  90. v1<<=16;
  91. v2<<=16;
  92. v3<<=16;
  93. p_dest[(i<<2)+0]=v0;
  94. p_dest[(i<<2)+1]=v1;
  95. p_dest[(i<<2)+2]=v2;
  96. p_dest[(i<<2)+3]=v3;
  97. }
  98. if (C==6) {
  99. int32_t v0 = rb[(pos*6)+0];
  100. int32_t v1 = rb[(pos*6)+1];
  101. int32_t v2 = rb[(pos*6)+2];
  102. int32_t v3 = rb[(pos*6)+3];
  103. int32_t v4 = rb[(pos*6)+4];
  104. int32_t v5 = rb[(pos*6)+5];
  105. int32_t v0n = rb[(pos_next*6)+0];
  106. int32_t v1n=rb[(pos_next*6)+1];
  107. int32_t v2n=rb[(pos_next*6)+2];
  108. int32_t v3n=rb[(pos_next*6)+3];
  109. int32_t v4n=rb[(pos_next*6)+4];
  110. int32_t v5n=rb[(pos_next*6)+5];
  111. #ifndef FAST_AUDIO
  112. v0+=(v0n-v0)*(int32_t)frac >> MIX_FRAC_BITS;
  113. v1+=(v1n-v1)*(int32_t)frac >> MIX_FRAC_BITS;
  114. v2+=(v2n-v2)*(int32_t)frac >> MIX_FRAC_BITS;
  115. v3+=(v3n-v3)*(int32_t)frac >> MIX_FRAC_BITS;
  116. v4+=(v4n-v4)*(int32_t)frac >> MIX_FRAC_BITS;
  117. v5+=(v5n-v5)*(int32_t)frac >> MIX_FRAC_BITS;
  118. #endif
  119. v0<<=16;
  120. v1<<=16;
  121. v2<<=16;
  122. v3<<=16;
  123. v4<<=16;
  124. v5<<=16;
  125. p_dest[(i*6)+0]=v0;
  126. p_dest[(i*6)+1]=v1;
  127. p_dest[(i*6)+2]=v2;
  128. p_dest[(i*6)+3]=v3;
  129. p_dest[(i*6)+4]=v4;
  130. p_dest[(i*6)+5]=v5;
  131. }
  132. }
  133. return read>>MIX_FRAC_BITS;//rb_read_pos=offset>>MIX_FRAC_BITS;
  134. }
  135. bool AudioStreamResampled::mix(int32_t *p_dest, int p_frames) {
  136. if (!rb || !_can_mix())
  137. return false;
  138. int write_pos_cache=rb_write_pos;
  139. int32_t increment=(mix_rate*MIX_FRAC_LEN)/get_mix_rate();
  140. int rb_todo;
  141. if (write_pos_cache==rb_read_pos) {
  142. return false; //out of buffer
  143. } else if (rb_read_pos<write_pos_cache) {
  144. rb_todo=write_pos_cache-rb_read_pos; //-1?
  145. } else {
  146. rb_todo=(rb_len-rb_read_pos)+write_pos_cache; //-1?
  147. }
  148. int todo = MIN( ((int64_t(rb_todo)<<MIX_FRAC_BITS)/increment)+1, p_frames );
  149. #if 0
  150. if (int(mix_rate)==get_mix_rate()) {
  151. if (channels==6) {
  152. for(int i=0;i<p_frames;i++) {
  153. int from = ((rb_read_pos+i)&rb_mask)*6;
  154. int to = i*6;
  155. p_dest[from+0]=int32_t(rb[to+0])<<16;
  156. p_dest[from+1]=int32_t(rb[to+1])<<16;
  157. p_dest[from+2]=int32_t(rb[to+2])<<16;
  158. p_dest[from+3]=int32_t(rb[to+3])<<16;
  159. p_dest[from+4]=int32_t(rb[to+4])<<16;
  160. p_dest[from+5]=int32_t(rb[to+5])<<16;
  161. }
  162. } else {
  163. int len=p_frames*channels;
  164. int from=rb_read_pos*channels;
  165. int mask=0;
  166. switch(channels) {
  167. case 1: mask=rb_len-1; break;
  168. case 2: mask=(rb_len*2)-1; break;
  169. case 4: mask=(rb_len*4)-1; break;
  170. }
  171. for(int i=0;i<len;i++) {
  172. p_dest[i]=int32_t(rb[(from+i)&mask])<<16;
  173. }
  174. }
  175. rb_read_pos = (rb_read_pos+p_frames)&rb_mask;
  176. } else
  177. #endif
  178. {
  179. uint32_t read=0;
  180. switch(channels) {
  181. case 1: read=_resample<1>(p_dest,todo,increment); break;
  182. case 2: read=_resample<2>(p_dest,todo,increment); break;
  183. case 4: read=_resample<4>(p_dest,todo,increment); break;
  184. case 6: read=_resample<6>(p_dest,todo,increment); break;
  185. }
  186. #if 1
  187. //end of stream, fadeout
  188. int remaining = p_frames-todo;
  189. if (remaining && todo>0) {
  190. //print_line("fadeout");
  191. for(int c=0;c<channels;c++) {
  192. for(int i=0;i<todo;i++) {
  193. int32_t samp = p_dest[i*channels+c]>>8;
  194. uint32_t mul = (todo-i) * 256 /todo;
  195. //print_line("mul: "+itos(i)+" "+itos(mul));
  196. p_dest[i*channels+c]=samp*mul;
  197. }
  198. }
  199. }
  200. #else
  201. int remaining = p_frames-todo;
  202. if (remaining && todo>0) {
  203. for(int c=0;c<channels;c++) {
  204. int32_t from = p_dest[(todo-1)*channels+c]>>8;
  205. for(int i=0;i<remaining;i++) {
  206. uint32_t mul = (remaining-i) * 256 /remaining;
  207. p_dest[(todo+i)*channels+c]=from*mul;
  208. }
  209. }
  210. }
  211. #endif
  212. //zero out what remains there to avoid glitches
  213. for(int i=todo*channels;i<int(p_frames)*channels;i++) {
  214. p_dest[i]=0;
  215. }
  216. if (read>rb_todo)
  217. read=rb_todo;
  218. rb_read_pos = (rb_read_pos+read)&rb_mask;
  219. }
  220. return true;
  221. }
  222. Error AudioStreamResampled::_setup(int p_channels,int p_mix_rate,int p_minbuff_needed) {
  223. ERR_FAIL_COND_V(p_channels!=1 && p_channels!=2 && p_channels!=4 && p_channels!=6,ERR_INVALID_PARAMETER);
  224. float buffering_sec = int(GLOBAL_DEF("audio/stream_buffering_ms",500))/1000.0;
  225. int desired_rb_bits =nearest_shift(MAX(buffering_sec*p_mix_rate,p_minbuff_needed));
  226. bool recreate=!rb;
  227. if (rb && (uint32_t(desired_rb_bits)!=rb_bits || channels!=uint32_t(p_channels))) {
  228. //recreate
  229. memdelete_arr(rb);
  230. memdelete_arr(read_buf);
  231. recreate=true;
  232. }
  233. if (recreate) {
  234. channels=p_channels;
  235. rb_bits=desired_rb_bits;
  236. rb_len=(1<<rb_bits);
  237. rb_mask=rb_len-1;
  238. rb = memnew_arr( int16_t, rb_len * p_channels );
  239. read_buf = memnew_arr( int16_t, rb_len * p_channels );
  240. }
  241. mix_rate=p_mix_rate;
  242. offset=0;
  243. rb_read_pos=0;
  244. rb_write_pos=0;
  245. //avoid maybe strange noises upon load
  246. for (int i=0;i<(rb_len*channels);i++) {
  247. rb[i]=0;
  248. read_buf[i]=0;
  249. }
  250. return OK;
  251. }
  252. void AudioStreamResampled::_clear() {
  253. if (!rb)
  254. return;
  255. AudioServer::get_singleton()->lock();
  256. //should be stopped at this point but just in case
  257. if (rb) {
  258. memdelete_arr(rb);
  259. memdelete_arr(read_buf);
  260. }
  261. rb=NULL;
  262. offset=0;
  263. rb_read_pos=0;
  264. rb_write_pos=0;
  265. read_buf=NULL;
  266. AudioServer::get_singleton()->unlock();
  267. }
  268. AudioStreamResampled::AudioStreamResampled() {
  269. rb=NULL;
  270. offset=0;
  271. read_buf=NULL;
  272. rb_read_pos=0;
  273. rb_write_pos=0;
  274. rb_bits=0;
  275. rb_len=0;
  276. rb_mask=0;
  277. read_buff_len=0;
  278. channels=0;
  279. mix_rate=0;
  280. }
  281. AudioStreamResampled::~AudioStreamResampled() {
  282. if (rb) {
  283. memdelete_arr(rb);
  284. memdelete_arr(read_buf);
  285. }
  286. }
  287. #endif