| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/audio_frame.h" |
| 34 |
#include "core/templates/safe_refcount.h" |
| 35 |
#include "core/typedefs.h" |
| 36 |
|
| 37 |
struct AudioRBResampler { |
| 38 |
uint32_t rb_bits; |
| 39 |
uint32_t rb_len; |
| 40 |
uint32_t rb_mask; |
| 41 |
uint32_t read_buff_len; |
| 42 |
uint32_t channels; |
| 43 |
uint32_t src_mix_rate; |
| 44 |
uint32_t target_mix_rate; |
| 45 |
double playback_speed = 1.0; |
| 46 |
|
| 47 |
SafeNumeric<int> rb_read_pos; |
| 48 |
SafeNumeric<int> rb_write_pos; |
| 49 |
|
| 50 |
int32_t offset; |
| 51 |
enum { |
| 52 |
MIX_FRAC_BITS = 13, |
| 53 |
MIX_FRAC_LEN = (1 << MIX_FRAC_BITS), |
| 54 |
MIX_FRAC_MASK = MIX_FRAC_LEN - 1, |
| 55 |
}; |
| 56 |
|
| 57 |
float *read_buf = nullptr; |
| 58 |
float *rb = nullptr; |
| 59 |
|
| 60 |
template <int C> |
| 61 |
uint32_t _resample(AudioFrame *p_dest, int p_todo, int32_t p_increment); |
| 62 |
|
| 63 |
public: |
| 64 |
_FORCE_INLINE_ void flush() { |
| 65 |
rb_read_pos.set(0); |
| 66 |
rb_write_pos.set(0); |
| 67 |
offset = 0; |
| 68 |
} |
| 69 |
|
| 70 |
_FORCE_INLINE_ bool is_ready() const { |
| 71 |
return rb != nullptr; |
| 72 |
} |
| 73 |
|
| 74 |
_FORCE_INLINE_ int get_total() const { |
| 75 |
return rb_len - 1; |
| 76 |
} |
| 77 |
|
| 78 |
_FORCE_INLINE_ int get_writer_space() const { |
| 79 |
int space, r, w; |
| 80 |
|
| 81 |
r = rb_read_pos.get(); |
| 82 |
w = rb_write_pos.get(); |
| 83 |
|
| 84 |
if (r == w) { |
| 85 |
space = rb_len - 1; |
| 86 |
} else if (w < r) { |
| 87 |
space = r - w - 1; |
| 88 |
} else { |
| 89 |
space = (rb_len - w) + (r - 1); |
| 90 |
} |
| 91 |
|
| 92 |
return space; |
| 93 |
} |
| 94 |
|
| 95 |
_FORCE_INLINE_ int get_reader_space() const { |
| 96 |
int space, r, w; |
| 97 |
|
| 98 |
r = rb_read_pos.get(); |
| 99 |
w = rb_write_pos.get(); |
| 100 |
|
| 101 |
if (r == w) { |
| 102 |
space = 0; |
| 103 |
} else if (w < r) { |
| 104 |
space = rb_len - r + w; |
| 105 |
} else { |
| 106 |
space = w - r; |
| 107 |
} |
| 108 |
|
| 109 |
return space; |
| 110 |
} |
| 111 |
|
| 112 |
_FORCE_INLINE_ bool has_data() const { |
| 113 |
return rb && rb_read_pos.get() != rb_write_pos.get(); |
| 114 |
} |
| 115 |
|
| 116 |
_FORCE_INLINE_ float *get_write_buffer() { return read_buf; } |
| 117 |
_FORCE_INLINE_ void write(uint32_t p_frames) { |
| 118 |
ERR_FAIL_COND(p_frames >= rb_len); |
| 119 |
|
| 120 |
int wp = rb_write_pos.get(); |
| 121 |
|
| 122 |
switch (channels) { |
| 123 |
case 1: { |
| 124 |
for (uint32_t i = 0; i < p_frames; i++) { |
| 125 |
rb[wp] = read_buf[i]; |
| 126 |
wp = (wp + 1) & rb_mask; |
| 127 |
} |
| 128 |
} break; |
| 129 |
case 2: { |
| 130 |
for (uint32_t i = 0; i < p_frames; i++) { |
| 131 |
rb[(wp << 1) + 0] = read_buf[(i << 1) + 0]; |
| 132 |
rb[(wp << 1) + 1] = read_buf[(i << 1) + 1]; |
| 133 |
wp = (wp + 1) & rb_mask; |
| 134 |
} |
| 135 |
} break; |
| 136 |
case 4: { |
| 137 |
for (uint32_t i = 0; i < p_frames; i++) { |
| 138 |
rb[(wp << 2) + 0] = read_buf[(i << 2) + 0]; |
| 139 |
rb[(wp << 2) + 1] = read_buf[(i << 2) + 1]; |
| 140 |
rb[(wp << 2) + 2] = read_buf[(i << 2) + 2]; |
| 141 |
rb[(wp << 2) + 3] = read_buf[(i << 2) + 3]; |
| 142 |
wp = (wp + 1) & rb_mask; |
| 143 |
} |
| 144 |
} break; |
| 145 |
case 6: { |
| 146 |
for (uint32_t i = 0; i < p_frames; i++) { |
| 147 |
rb[(wp * 6) + 0] = read_buf[(i * 6) + 0]; |
| 148 |
rb[(wp * 6) + 1] = read_buf[(i * 6) + 1]; |
| 149 |
rb[(wp * 6) + 2] = read_buf[(i * 6) + 2]; |
| 150 |
rb[(wp * 6) + 3] = read_buf[(i * 6) + 3]; |
| 151 |
rb[(wp * 6) + 4] = read_buf[(i * 6) + 4]; |
| 152 |
rb[(wp * 6) + 5] = read_buf[(i * 6) + 5]; |
| 153 |
wp = (wp + 1) & rb_mask; |
| 154 |
} |
| 155 |
} break; |
| 156 |
case 8: { |
| 157 |
for (uint32_t i = 0; i < p_frames; i++) { |
| 158 |
rb[(wp << 3) + 0] = read_buf[(i << 3) + 0]; |
| 159 |
rb[(wp << 3) + 1] = read_buf[(i << 3) + 1]; |
| 160 |
rb[(wp << 3) + 2] = read_buf[(i << 3) + 2]; |
| 161 |
rb[(wp << 3) + 3] = read_buf[(i << 3) + 3]; |
| 162 |
rb[(wp << 3) + 4] = read_buf[(i << 3) + 4]; |
| 163 |
rb[(wp << 3) + 5] = read_buf[(i << 3) + 5]; |
| 164 |
rb[(wp << 3) + 6] = read_buf[(i << 3) + 6]; |
| 165 |
rb[(wp << 3) + 7] = read_buf[(i << 3) + 7]; |
| 166 |
wp = (wp + 1) & rb_mask; |
| 167 |
} |
| 168 |
} break; |
| 169 |
} |
| 170 |
|
| 171 |
rb_write_pos.set(wp); |
| 172 |
} |
| 173 |
|
| 174 |
int get_channel_count() const; |
| 175 |
|
| 176 |
Error setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed = -1); |
| 177 |
void clear(); |
| 178 |
bool mix(AudioFrame *p_dest, int p_frames); |
| 179 |
int get_num_of_ready_frames(); |
| 180 |
void set_playback_speed(double p_playback_speed); |
| 181 |
double get_playback_speed() const; |
| 182 |
|
| 183 |
AudioRBResampler(); |
| 184 |
~AudioRBResampler(); |
| 185 |
}; |