Path: blob/master/servers/audio/audio_rb_resampler.cpp
10277 views
/**************************************************************************/1/* audio_rb_resampler.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */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/**************************************************************************/2930#include "audio_rb_resampler.h"3132#include "core/math/audio_frame.h"33#include "core/os/memory.h"3435int AudioRBResampler::get_channel_count() const {36if (!rb) {37return 0;38}3940return channels;41}4243// Linear interpolation based sample rate conversion (low quality)44// Note that AudioStreamPlaybackResampled::mix has better algorithm,45// but it wasn't obvious to integrate that with VideoStreamPlayer46template <int C>47uint32_t AudioRBResampler::_resample(AudioFrame *p_dest, int p_todo, int32_t p_increment) {48uint32_t read = offset & MIX_FRAC_MASK;4950for (int i = 0; i < p_todo; i++) {51offset = (offset + p_increment) & (((1 << (rb_bits + MIX_FRAC_BITS)) - 1));52read += p_increment;53uint32_t pos = offset >> MIX_FRAC_BITS;54float frac = float(offset & MIX_FRAC_MASK) / float(MIX_FRAC_LEN);55ERR_FAIL_COND_V(pos >= rb_len, 0);56uint32_t pos_next = (pos + 1) & rb_mask;5758// since this is a template with a known compile time value (C), conditionals go away when compiling.59if constexpr (C == 1) {60float v0 = rb[pos];61float v0n = rb[pos_next];62v0 += (v0n - v0) * frac;63p_dest[i] = AudioFrame(v0, v0);64}6566if constexpr (C == 2) {67float v0 = rb[(pos << 1) + 0];68float v1 = rb[(pos << 1) + 1];69float v0n = rb[(pos_next << 1) + 0];70float v1n = rb[(pos_next << 1) + 1];7172v0 += (v0n - v0) * frac;73v1 += (v1n - v1) * frac;74p_dest[i] = AudioFrame(v0, v1);75}7677// Downmix to stereo. Apply -3dB to center, and sides, -6dB to rear.7879// four channels - channel order: front left, front right, rear left, rear right80if constexpr (C == 4) {81float v0 = rb[(pos << 2) + 0] + rb[(pos << 2) + 2] / 2;82float v1 = rb[(pos << 2) + 1] + rb[(pos << 2) + 3] / 2;83float v0n = rb[(pos_next << 2) + 0] + rb[(pos_next << 2) + 2] / 2;84float v1n = rb[(pos_next << 2) + 1] + rb[(pos_next << 2) + 3] / 2;85v0 += (v0n - v0) * frac;86v1 += (v1n - v1) * frac;87p_dest[i] = AudioFrame(v0, v1);88}8990// six channels - channel order: front left, center, front right, rear left, rear right, LFE91if constexpr (C == 6) {92float v0 = rb[(pos * 6) + 0] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 3] / 2;93float v1 = rb[(pos * 6) + 2] + rb[(pos * 6) + 1] / Math::SQRT2 + rb[(pos * 6) + 4] / 2;94float v0n = rb[(pos_next * 6) + 0] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 3] / 2;95float v1n = rb[(pos_next * 6) + 2] + rb[(pos_next * 6) + 1] / Math::SQRT2 + rb[(pos_next * 6) + 4] / 2;96v0 += (v0n - v0) * frac;97v1 += (v1n - v1) * frac;98p_dest[i] = AudioFrame(v0, v1);99}100101// eight channels - channel order: front left, center, front right, side left, side right, rear left, rear102// right, LFE103if constexpr (C == 8) {104float v0 = rb[(pos << 3) + 0] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 3] / Math::SQRT2 + rb[(pos << 3) + 5] / 2;105float v1 = rb[(pos << 3) + 2] + rb[(pos << 3) + 1] / Math::SQRT2 + rb[(pos << 3) + 4] / Math::SQRT2 + rb[(pos << 3) + 6] / 2;106float 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;107float 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;108v0 += (v0n - v0) * frac;109v1 += (v1n - v1) * frac;110p_dest[i] = AudioFrame(v0, v1);111}112}113114return read >> MIX_FRAC_BITS; //rb_read_pos = offset >> MIX_FRAC_BITS;115}116117bool AudioRBResampler::mix(AudioFrame *p_dest, int p_frames) {118if (!rb) {119return false;120}121122int32_t increment = (src_mix_rate * MIX_FRAC_LEN * playback_speed) / target_mix_rate;123int read_space = get_reader_space();124int target_todo = MIN(get_num_of_ready_frames(), p_frames);125126{127int src_read = 0;128switch (channels) {129case 1:130src_read = _resample<1>(p_dest, target_todo, increment);131break;132case 2:133src_read = _resample<2>(p_dest, target_todo, increment);134break;135case 4:136src_read = _resample<4>(p_dest, target_todo, increment);137break;138case 6:139src_read = _resample<6>(p_dest, target_todo, increment);140break;141case 8:142src_read = _resample<8>(p_dest, target_todo, increment);143break;144}145146if (src_read > read_space) {147src_read = read_space;148}149150rb_read_pos.set((rb_read_pos.get() + src_read) & rb_mask);151152// Create fadeout effect for the end of stream (note that it can be because of slow writer)153if (p_frames - target_todo > 0) {154for (int i = 0; i < target_todo; i++) {155p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo);156}157}158159// Fill zeros (silence) for the rest of frames160for (int i = target_todo; i < p_frames; i++) {161p_dest[i] = AudioFrame(0, 0);162}163}164165return true;166}167168int AudioRBResampler::get_num_of_ready_frames() {169if (!is_ready()) {170return 0;171}172int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;173int read_space = get_reader_space();174return (int64_t(read_space) << MIX_FRAC_BITS) / increment;175}176177Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) {178ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6 && p_channels != 8, ERR_INVALID_PARAMETER);179180int desired_rb_bits = nearest_shift((uint32_t)MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed));181182bool recreate = !rb;183184if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) {185memdelete_arr(rb);186memdelete_arr(read_buf);187recreate = true;188}189190if (recreate) {191channels = p_channels;192rb_bits = desired_rb_bits;193rb_len = (1 << rb_bits);194rb_mask = rb_len - 1;195const size_t array_size = rb_len * (size_t)p_channels;196rb = memnew_arr(float, array_size);197read_buf = memnew_arr(float, array_size);198}199200src_mix_rate = p_src_mix_rate;201target_mix_rate = p_target_mix_rate;202offset = 0;203rb_read_pos.set(0);204rb_write_pos.set(0);205206//avoid maybe strange noises upon load207for (unsigned int i = 0; i < (rb_len * channels); i++) {208rb[i] = 0;209read_buf[i] = 0;210}211212return OK;213}214215void AudioRBResampler::clear() {216if (!rb) {217return;218}219220//should be stopped at this point but just in case221memdelete_arr(rb);222memdelete_arr(read_buf);223rb = nullptr;224offset = 0;225rb_read_pos.set(0);226rb_write_pos.set(0);227read_buf = nullptr;228}229230void AudioRBResampler::set_playback_speed(double p_playback_speed) {231playback_speed = p_playback_speed;232}233234double AudioRBResampler::get_playback_speed() const {235return playback_speed;236}237238AudioRBResampler::AudioRBResampler() {239rb = nullptr;240offset = 0;241read_buf = nullptr;242rb_read_pos.set(0);243rb_write_pos.set(0);244245rb_bits = 0;246rb_len = 0;247rb_mask = 0;248read_buff_len = 0;249channels = 0;250src_mix_rate = 0;251target_mix_rate = 0;252}253254AudioRBResampler::~AudioRBResampler() {255if (rb) {256memdelete_arr(rb);257memdelete_arr(read_buf);258}259}260261262