audio_stream_resampled.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* audio_stream_resampled.h */
  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. #ifndef AUDIO_STREAM_RESAMPLED_H
  30. #define AUDIO_STREAM_RESAMPLED_H
  31. #include "scene/resources/audio_stream.h"
  32. class AudioStreamResampled : public AudioStream {
  33. OBJ_TYPE(AudioStreamResampled,AudioStream);
  34. uint32_t rb_bits;
  35. uint32_t rb_len;
  36. uint32_t rb_mask;
  37. uint32_t read_buff_len;
  38. uint32_t channels;
  39. uint32_t mix_rate;
  40. volatile int rb_read_pos;
  41. volatile int rb_write_pos;
  42. int32_t offset; //contains the fractional remainder of the resampler
  43. enum {
  44. MIX_FRAC_BITS=13,
  45. MIX_FRAC_LEN=(1<<MIX_FRAC_BITS),
  46. MIX_FRAC_MASK=MIX_FRAC_LEN-1,
  47. };
  48. int16_t *read_buf;
  49. int16_t *rb;
  50. template<int C>
  51. void _resample(int32_t *p_dest,int p_todo,int32_t p_increment);
  52. protected:
  53. _FORCE_INLINE_ int get_total() const {
  54. return rb_len;
  55. }
  56. _FORCE_INLINE_ int get_todo() const { //return amount of frames to mix
  57. int todo;
  58. int read_pos_cache=rb_read_pos;
  59. if (read_pos_cache==rb_write_pos) {
  60. todo=rb_len-1;
  61. } else if (read_pos_cache>rb_write_pos) {
  62. todo=read_pos_cache-rb_write_pos-1;
  63. } else {
  64. todo=(rb_len-rb_write_pos)+read_pos_cache-1;
  65. }
  66. return todo;
  67. }
  68. //Stream virtual funcs
  69. virtual int get_channel_count() const;
  70. virtual bool mix(int32_t *p_dest, int p_frames);
  71. _FORCE_INLINE_ void _flush() {
  72. rb_read_pos=0;
  73. rb_write_pos=0;
  74. }
  75. _FORCE_INLINE_ int16_t *get_write_buffer() { return read_buf; }
  76. _FORCE_INLINE_ void write(uint32_t p_frames) {
  77. ERR_FAIL_COND(p_frames > rb_len);
  78. switch(channels) {
  79. case 1: {
  80. for(uint32_t i=0;i<p_frames;i++) {
  81. rb[ rb_write_pos ] = read_buf[i];
  82. rb_write_pos=(rb_write_pos+1)&rb_mask;
  83. }
  84. } break;
  85. case 2: {
  86. for(uint32_t i=0;i<p_frames;i++) {
  87. rb[ (rb_write_pos<<1)+0 ] = read_buf[(i<<1)+0];
  88. rb[ (rb_write_pos<<1)+1 ] = read_buf[(i<<1)+1];
  89. rb_write_pos=(rb_write_pos+1)&rb_mask;
  90. }
  91. } break;
  92. case 4: {
  93. for(uint32_t i=0;i<p_frames;i++) {
  94. rb[ (rb_write_pos<<2)+0 ] = read_buf[(i<<2)+0];
  95. rb[ (rb_write_pos<<2)+1 ] = read_buf[(i<<2)+1];
  96. rb[ (rb_write_pos<<2)+2 ] = read_buf[(i<<2)+2];
  97. rb[ (rb_write_pos<<2)+3 ] = read_buf[(i<<2)+3];
  98. rb_write_pos=(rb_write_pos+1)&rb_mask;
  99. }
  100. } break;
  101. case 6: {
  102. for(uint32_t i=0;i<p_frames;i++) {
  103. rb[ (rb_write_pos*6)+0 ] = read_buf[(i*6)+0];
  104. rb[ (rb_write_pos*6)+1 ] = read_buf[(i*6)+1];
  105. rb[ (rb_write_pos*6)+2 ] = read_buf[(i*6)+2];
  106. rb[ (rb_write_pos*6)+3 ] = read_buf[(i*6)+3];
  107. rb[ (rb_write_pos*6)+4 ] = read_buf[(i*6)+4];
  108. rb[ (rb_write_pos*6)+5 ] = read_buf[(i*6)+5];
  109. rb_write_pos=(rb_write_pos+1)&rb_mask;
  110. }
  111. } break;
  112. }
  113. }
  114. virtual bool _can_mix() const =0;
  115. _FORCE_INLINE_ bool _is_ready() const{
  116. return rb!=NULL;
  117. }
  118. Error _setup(int p_channels,int p_mix_rate,int p_minbuff_needed=-1);
  119. void _clear();
  120. public:
  121. AudioStreamResampled();
  122. ~AudioStreamResampled();
  123. };
  124. #endif // AUDIO_STREAM_RESAMPLED_H