| 1 | /**************************************************************************/ |
| 2 | /* audio_rb_resampler.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "audio_rb_resampler.h" |
| 32 | |
| 33 | #include "core/math/audio_frame.h" |
| 34 | #include "core/os/memory.h" |
| 35 | |
| 36 | int AudioRBResampler::get_channel_count() const { |
| 37 | if (!rb) { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | return channels; |
| 42 | } |
| 43 | |
| 44 | // Linear interpolation based sample rate conversion (low quality) |
| 45 | // Note that AudioStreamPlaybackResampled::mix has better algorithm, |
| 46 | // but it wasn't obvious to integrate that with VideoStreamPlayer |
| 47 | template <int C> |
| 48 | uint32_t AudioRBResampler::_resample(AudioFrame *p_dest, int p_todo, int32_t p_increment) { |
| 49 | uint32_t read = offset & MIX_FRAC_MASK; |
| 50 | |
| 51 | for (int i = 0; i < p_todo; i++) { |
| 52 | offset = (offset + p_increment) & (((1 << (rb_bits + MIX_FRAC_BITS)) - 1)); |
| 53 | read += p_increment; |
| 54 | uint32_t pos = offset >> MIX_FRAC_BITS; |
| 55 | float frac = float(offset & MIX_FRAC_MASK) / float(MIX_FRAC_LEN); |
| 56 | ERR_FAIL_COND_V(pos >= rb_len, 0); |
| 57 | uint32_t pos_next = (pos + 1) & rb_mask; |
| 58 | |
| 59 | // since this is a template with a known compile time value (C), conditionals go away when compiling. |
| 60 | if constexpr (C == 1) { |
| 61 | float v0 = rb[pos]; |
| 62 | float v0n = rb[pos_next]; |
| 63 | v0 += (v0n - v0) * frac; |
| 64 | p_dest[i] = AudioFrame(v0, v0); |
| 65 | } |
| 66 | |
| 67 | if constexpr (C == 2) { |
| 68 | float v0 = rb[(pos << 1) + 0]; |
| 69 | float v1 = rb[(pos << 1) + 1]; |
| 70 | float v0n = rb[(pos_next << 1) + 0]; |
| 71 | float v1n = rb[(pos_next << 1) + 1]; |
| 72 | |
| 73 | v0 += (v0n - v0) * frac; |
| 74 | v1 += (v1n - v1) * frac; |
| 75 | p_dest[i] = AudioFrame(v0, v1); |
| 76 | } |
| 77 | |
| 78 | // Downmix to stereo. Apply -3dB to center, and sides, -6dB to rear. |
| 79 | |
| 80 | // four channels - channel order: front left, front right, rear left, rear right |
| 81 | if constexpr (C == 4) { |
| 82 | float v0 = rb[(pos << 2) + 0] + rb[(pos << 2) + 2] / 2; |
| 83 | float v1 = rb[(pos << 2) + 1] + rb[(pos << 2) + 3] / 2; |
| 84 | float v0n = rb[(pos_next << 2) + 0] + rb[(pos_next << 2) + 2] / 2; |
| 85 | float v1n = rb[(pos_next << 2) + 1] + rb[(pos_next << 2) + 3] / 2; |
| 86 | v0 += (v0n - v0) * frac; |
| 87 | v1 += (v1n - v1) * frac; |
| 88 | p_dest[i] = AudioFrame(v0, v1); |
| 89 | } |
| 90 | |
| 91 | // six channels - channel order: front left, center, front right, rear left, rear right, LFE |
| 92 | if constexpr (C == 6) { |
| 93 | float v0 = rb[(pos * 6) + 0] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 3] / 2; |
| 94 | float v1 = rb[(pos * 6) + 2] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 4] / 2; |
| 95 | float v0n = rb[(pos_next * 6) + 0] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 3] / 2; |
| 96 | float v1n = rb[(pos_next * 6) + 2] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 4] / 2; |
| 97 | v0 += (v0n - v0) * frac; |
| 98 | v1 += (v1n - v1) * frac; |
| 99 | p_dest[i] = AudioFrame(v0, v1); |
| 100 | } |
| 101 | |
| 102 | // eight channels - channel order: front left, center, front right, side left, side right, rear left, rear |
| 103 | // right, LFE |
| 104 | if constexpr (C == 8) { |
| 105 | float v0 = rb[(pos << 3) + 0] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 3] / Math::SQRT2 + rb[(pos << 3) + 5] / 2; |
| 106 | float v1 = rb[(pos << 3) + 2] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 4] / Math::SQRT2 + rb[(pos << 3) + 6] / 2; |
| 107 | float v0n = rb[(pos_next << 3) + 0] + rb[(pos_next << 3) + 1] / Math::SQRT2 + rb[(pos_next << 3) + 3] / Math::SQRT2 + rb[(pos_next << 3) + 5] / 2; |
| 108 | float v1n = rb[(pos_next << 3) + 2] + rb[(pos_next << 3) + 1] / Math::SQRT2 + rb[(pos_next << 3) + 4] / Math::SQRT2 + rb[(pos_next << 3) + 6] / 2; |
| 109 | v0 += (v0n - v0) * frac; |
| 110 | v1 += (v1n - v1) * frac; |
| 111 | p_dest[i] = AudioFrame(v0, v1); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return read >> MIX_FRAC_BITS; //rb_read_pos = offset >> MIX_FRAC_BITS; |
| 116 | } |
| 117 | |
| 118 | bool AudioRBResampler::mix(AudioFrame *p_dest, int p_frames) { |
| 119 | if (!rb) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | int32_t increment = (src_mix_rate * MIX_FRAC_LEN * playback_speed) / target_mix_rate; |
| 124 | int read_space = get_reader_space(); |
| 125 | int target_todo = MIN(get_num_of_ready_frames(), p_frames); |
| 126 | |
| 127 | { |
| 128 | int src_read = 0; |
| 129 | switch (channels) { |
| 130 | case 1: |
| 131 | src_read = _resample<1>(p_dest, target_todo, increment); |
| 132 | break; |
| 133 | case 2: |
| 134 | src_read = _resample<2>(p_dest, target_todo, increment); |
| 135 | break; |
| 136 | case 4: |
| 137 | src_read = _resample<4>(p_dest, target_todo, increment); |
| 138 | break; |
| 139 | case 6: |
| 140 | src_read = _resample<6>(p_dest, target_todo, increment); |
| 141 | break; |
| 142 | case 8: |
| 143 | src_read = _resample<8>(p_dest, target_todo, increment); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | if (src_read > read_space) { |
| 148 | src_read = read_space; |
| 149 | } |
| 150 | |
| 151 | rb_read_pos.set((rb_read_pos.get() + src_read) & rb_mask); |
| 152 | |
| 153 | // Create fadeout effect for the end of stream (note that it can be because of slow writer) |
| 154 | if (p_frames - target_todo > 0) { |
| 155 | for (int i = 0; i < target_todo; i++) { |
| 156 | p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Fill zeros (silence) for the rest of frames |
| 161 | for (int i = target_todo; i < p_frames; i++) { |
| 162 | p_dest[i] = AudioFrame(0, 0); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | int AudioRBResampler::get_num_of_ready_frames() { |
| 170 | if (!is_ready()) { |
| 171 | return 0; |
| 172 | } |
| 173 | int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate; |
| 174 | int read_space = get_reader_space(); |
| 175 | return (int64_t(read_space) << MIX_FRAC_BITS) / increment; |
| 176 | } |
| 177 | |
| 178 | Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) { |
| 179 | ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6 && p_channels != 8, ERR_INVALID_PARAMETER); |
| 180 | |
| 181 | int desired_rb_bits = nearest_shift((uint32_t)MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed)); |
| 182 | |
| 183 | bool recreate = !rb; |
| 184 | |
| 185 | if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) { |
| 186 | memdelete_arr(rb); |
| 187 | memdelete_arr(read_buf); |
| 188 | recreate = true; |
| 189 | } |
| 190 | |
| 191 | if (recreate) { |
| 192 | channels = p_channels; |
| 193 | rb_bits = desired_rb_bits; |
| 194 | rb_len = (1 << rb_bits); |
| 195 | rb_mask = rb_len - 1; |
| 196 | const size_t array_size = rb_len * (size_t)p_channels; |
| 197 | rb = memnew_arr(float, array_size); |
| 198 | read_buf = memnew_arr(float, array_size); |
| 199 | } |
| 200 | |
| 201 | src_mix_rate = p_src_mix_rate; |
| 202 | target_mix_rate = p_target_mix_rate; |
| 203 | offset = 0; |
| 204 | rb_read_pos.set(0); |
| 205 | rb_write_pos.set(0); |
| 206 | |
| 207 | //avoid maybe strange noises upon load |
| 208 | for (unsigned int i = 0; i < (rb_len * channels); i++) { |
| 209 | rb[i] = 0; |
| 210 | read_buf[i] = 0; |
| 211 | } |
| 212 | |
| 213 | return OK; |
| 214 | } |
| 215 | |
| 216 | void AudioRBResampler::clear() { |
| 217 | if (!rb) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | //should be stopped at this point but just in case |
| 222 | memdelete_arr(rb); |
| 223 | memdelete_arr(read_buf); |
| 224 | rb = nullptr; |
| 225 | offset = 0; |
| 226 | rb_read_pos.set(0); |
| 227 | rb_write_pos.set(0); |
| 228 | read_buf = nullptr; |
| 229 | } |
| 230 | |
| 231 | void AudioRBResampler::set_playback_speed(double p_playback_speed) { |
| 232 | playback_speed = p_playback_speed; |
| 233 | } |
| 234 | |
| 235 | double AudioRBResampler::get_playback_speed() const { |
| 236 | return playback_speed; |
| 237 | } |
| 238 | |
| 239 | AudioRBResampler::AudioRBResampler() { |
| 240 | rb = nullptr; |
| 241 | offset = 0; |
| 242 | read_buf = nullptr; |
| 243 | rb_read_pos.set(0); |
| 244 | rb_write_pos.set(0); |
| 245 | |
| 246 | rb_bits = 0; |
| 247 | rb_len = 0; |
| 248 | rb_mask = 0; |
| 249 | read_buff_len = 0; |
| 250 | channels = 0; |
| 251 | src_mix_rate = 0; |
| 252 | target_mix_rate = 0; |
| 253 | } |
| 254 | |
| 255 | AudioRBResampler::~AudioRBResampler() { |
| 256 | if (rb) { |
| 257 | memdelete_arr(rb); |
| 258 | memdelete_arr(read_buf); |
| 259 | } |
| 260 | } |