2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_rb_resampler.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2015-09-09 23:50:52 +02:00
|
|
|
#include "audio_rb_resampler.h"
|
2017-09-14 22:45:02 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2017-09-14 22:45:02 +02:00
|
|
|
#include "servers/audio_server.h"
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
int AudioRBResampler::get_channel_count() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!rb) {
|
2015-09-09 23:50:52 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:37:17 +01:00
|
|
|
// Linear interpolation based sample rate conversion (low quality)
|
2017-09-14 22:45:02 +02:00
|
|
|
// Note that AudioStreamPlaybackResampled::mix has better algorithm,
|
2021-12-06 17:24:45 +01:00
|
|
|
// but it wasn't obvious to integrate that with VideoStreamPlayer
|
2017-03-05 16:44:50 +01:00
|
|
|
template <int C>
|
2017-09-14 22:45:02 +02:00
|
|
|
uint32_t AudioRBResampler::_resample(AudioFrame *p_dest, int p_todo, int32_t p_increment) {
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t read = offset & MIX_FRAC_MASK;
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_todo; i++) {
|
|
|
|
offset = (offset + p_increment) & (((1 << (rb_bits + MIX_FRAC_BITS)) - 1));
|
|
|
|
read += p_increment;
|
2015-09-09 23:50:52 +02:00
|
|
|
uint32_t pos = offset >> MIX_FRAC_BITS;
|
2017-09-14 22:45:02 +02:00
|
|
|
float frac = float(offset & MIX_FRAC_MASK) / float(MIX_FRAC_LEN);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(pos >= rb_len, 0);
|
|
|
|
uint32_t pos_next = (pos + 1) & rb_mask;
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
// since this is a template with a known compile time value (C), conditionals go away when compiling.
|
2022-09-29 08:18:07 +02:00
|
|
|
if constexpr (C == 1) {
|
2017-09-14 22:45:02 +02:00
|
|
|
float v0 = rb[pos];
|
|
|
|
float v0n = rb[pos_next];
|
|
|
|
v0 += (v0n - v0) * frac;
|
|
|
|
p_dest[i] = AudioFrame(v0, v0);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
2017-09-14 22:45:02 +02:00
|
|
|
|
2022-09-29 08:18:07 +02:00
|
|
|
if constexpr (C == 2) {
|
2017-09-14 22:45:02 +02:00
|
|
|
float v0 = rb[(pos << 1) + 0];
|
|
|
|
float v1 = rb[(pos << 1) + 1];
|
|
|
|
float v0n = rb[(pos_next << 1) + 0];
|
|
|
|
float v1n = rb[(pos_next << 1) + 1];
|
|
|
|
|
|
|
|
v0 += (v0n - v0) * frac;
|
|
|
|
v1 += (v1n - v1) * frac;
|
|
|
|
p_dest[i] = AudioFrame(v0, v1);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 02:57:31 +01:00
|
|
|
// This will probably never be used, but added anyway
|
2022-09-29 08:18:07 +02:00
|
|
|
if constexpr (C == 4) {
|
2017-09-14 22:45:02 +02:00
|
|
|
float v0 = rb[(pos << 2) + 0];
|
|
|
|
float v1 = rb[(pos << 2) + 1];
|
|
|
|
float v0n = rb[(pos_next << 2) + 0];
|
|
|
|
float v1n = rb[(pos_next << 2) + 1];
|
|
|
|
v0 += (v0n - v0) * frac;
|
|
|
|
v1 += (v1n - v1) * frac;
|
|
|
|
p_dest[i] = AudioFrame(v0, v1);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 08:18:07 +02:00
|
|
|
if constexpr (C == 6) {
|
2017-09-14 22:45:02 +02:00
|
|
|
float v0 = rb[(pos * 6) + 0];
|
|
|
|
float v1 = rb[(pos * 6) + 1];
|
|
|
|
float v0n = rb[(pos_next * 6) + 0];
|
|
|
|
float v1n = rb[(pos_next * 6) + 1];
|
|
|
|
|
2018-11-22 02:57:31 +01:00
|
|
|
v0 += (v0n - v0) * frac;
|
|
|
|
v1 += (v1n - v1) * frac;
|
2017-09-14 22:45:02 +02:00
|
|
|
p_dest[i] = AudioFrame(v0, v1);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
return read >> MIX_FRAC_BITS; //rb_read_pos = offset >> MIX_FRAC_BITS;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
bool AudioRBResampler::mix(AudioFrame *p_dest, int p_frames) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!rb) {
|
2015-09-09 23:50:52 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
|
2017-09-14 22:45:02 +02:00
|
|
|
int read_space = get_reader_space();
|
|
|
|
int target_todo = MIN(get_num_of_ready_frames(), p_frames);
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
{
|
2017-09-14 22:45:02 +02:00
|
|
|
int src_read = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (channels) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case 1:
|
|
|
|
src_read = _resample<1>(p_dest, target_todo, increment);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
src_read = _resample<2>(p_dest, target_todo, increment);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
src_read = _resample<4>(p_dest, target_todo, increment);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
src_read = _resample<6>(p_dest, target_todo, increment);
|
|
|
|
break;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (src_read > read_space) {
|
2017-09-14 22:45:02 +02:00
|
|
|
src_read = read_space;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2021-02-10 19:22:13 +01:00
|
|
|
rb_read_pos.set((rb_read_pos.get() + src_read) & rb_mask);
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
// Create fadeout effect for the end of stream (note that it can be because of slow writer)
|
|
|
|
if (p_frames - target_todo > 0) {
|
|
|
|
for (int i = 0; i < target_todo; i++) {
|
|
|
|
p_dest[i] = p_dest[i] * float(target_todo - i) / float(target_todo);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
// Fill zeros (silence) for the rest of frames
|
2018-09-26 17:38:02 +02:00
|
|
|
for (int i = target_todo; i < p_frames; i++) {
|
2017-09-14 22:45:02 +02:00
|
|
|
p_dest[i] = AudioFrame(0, 0);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
int AudioRBResampler::get_num_of_ready_frames() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_ready()) {
|
2017-09-14 22:45:02 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-14 22:45:02 +02:00
|
|
|
int32_t increment = (src_mix_rate * MIX_FRAC_LEN) / target_mix_rate;
|
|
|
|
int read_space = get_reader_space();
|
|
|
|
return (int64_t(read_space) << MIX_FRAC_BITS) / increment;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) {
|
|
|
|
ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6, ERR_INVALID_PARAMETER);
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int desired_rb_bits = nearest_shift(MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed));
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool recreate = !rb;
|
2015-09-09 23:50:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) {
|
2015-09-09 23:50:52 +02:00
|
|
|
memdelete_arr(rb);
|
|
|
|
memdelete_arr(read_buf);
|
2017-03-05 16:44:50 +01:00
|
|
|
recreate = true;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (recreate) {
|
2017-03-05 16:44:50 +01:00
|
|
|
channels = p_channels;
|
|
|
|
rb_bits = desired_rb_bits;
|
|
|
|
rb_len = (1 << rb_bits);
|
|
|
|
rb_mask = rb_len - 1;
|
2021-09-08 06:25:35 +02:00
|
|
|
const size_t array_size = rb_len * (size_t)p_channels;
|
|
|
|
rb = memnew_arr(float, array_size);
|
|
|
|
read_buf = memnew_arr(float, array_size);
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
src_mix_rate = p_src_mix_rate;
|
|
|
|
target_mix_rate = p_target_mix_rate;
|
|
|
|
offset = 0;
|
2021-02-10 19:22:13 +01:00
|
|
|
rb_read_pos.set(0);
|
|
|
|
rb_write_pos.set(0);
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
//avoid maybe strange noises upon load
|
2017-08-31 23:30:35 +02:00
|
|
|
for (unsigned int i = 0; i < (rb_len * channels); i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
rb[i] = 0;
|
|
|
|
read_buf[i] = 0;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioRBResampler::clear() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!rb) {
|
2015-09-09 23:50:52 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-09-09 23:50:52 +02:00
|
|
|
|
|
|
|
//should be stopped at this point but just in case
|
2019-04-08 11:03:37 +02:00
|
|
|
memdelete_arr(rb);
|
|
|
|
memdelete_arr(read_buf);
|
2020-04-02 01:20:12 +02:00
|
|
|
rb = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
offset = 0;
|
2021-02-10 19:22:13 +01:00
|
|
|
rb_read_pos.set(0);
|
|
|
|
rb_write_pos.set(0);
|
2020-04-02 01:20:12 +02:00
|
|
|
read_buf = nullptr;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioRBResampler::AudioRBResampler() {
|
2020-04-02 01:20:12 +02:00
|
|
|
rb = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
offset = 0;
|
2020-04-02 01:20:12 +02:00
|
|
|
read_buf = nullptr;
|
2021-02-10 19:22:13 +01:00
|
|
|
rb_read_pos.set(0);
|
|
|
|
rb_write_pos.set(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
|
|
|
rb_bits = 0;
|
|
|
|
rb_len = 0;
|
|
|
|
rb_mask = 0;
|
|
|
|
read_buff_len = 0;
|
|
|
|
channels = 0;
|
|
|
|
src_mix_rate = 0;
|
|
|
|
target_mix_rate = 0;
|
2015-09-09 23:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioRBResampler::~AudioRBResampler() {
|
|
|
|
if (rb) {
|
|
|
|
memdelete_arr(rb);
|
|
|
|
memdelete_arr(read_buf);
|
|
|
|
}
|
|
|
|
}
|