audio_stream_resampled.h 5.0 KB

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