2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_rb_resampler.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00: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-04 23:50:27 +00:00
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
#ifndef AUDIO_RB_RESAMPLER_H
|
|
|
|
#define AUDIO_RB_RESAMPLER_H
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/typedefs.h"
|
2017-09-14 20:45:02 +00:00
|
|
|
#include "servers/audio_server.h"
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
struct AudioRBResampler {
|
|
|
|
|
|
|
|
uint32_t rb_bits;
|
|
|
|
uint32_t rb_len;
|
|
|
|
uint32_t rb_mask;
|
|
|
|
uint32_t read_buff_len;
|
|
|
|
uint32_t channels;
|
|
|
|
uint32_t src_mix_rate;
|
|
|
|
uint32_t target_mix_rate;
|
|
|
|
|
|
|
|
volatile int rb_read_pos;
|
|
|
|
volatile int rb_write_pos;
|
|
|
|
|
|
|
|
int32_t offset; //contains the fractional remainder of the resampler
|
|
|
|
enum {
|
2017-03-05 15:44:50 +00:00
|
|
|
MIX_FRAC_BITS = 13,
|
|
|
|
MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
|
|
|
|
MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
|
2015-09-09 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
float *read_buf;
|
|
|
|
float *rb;
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <int C>
|
2017-09-14 20:45:02 +00:00
|
|
|
uint32_t _resample(AudioFrame *p_dest, int p_todo, int32_t p_increment);
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ void flush() {
|
2017-03-05 15:44:50 +00:00
|
|
|
rb_read_pos = 0;
|
|
|
|
rb_write_pos = 0;
|
|
|
|
offset = 0;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ bool is_ready() const {
|
|
|
|
return rb != NULL;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int get_total() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return rb_len - 1;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
_FORCE_INLINE_ int get_writer_space() const {
|
|
|
|
int space, r, w;
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
r = rb_read_pos;
|
|
|
|
w = rb_write_pos;
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
if (r == w) {
|
|
|
|
space = rb_len - 1;
|
|
|
|
} else if (w < r) {
|
|
|
|
space = r - w - 1;
|
2015-09-09 21:50:52 +00:00
|
|
|
} else {
|
2017-09-14 20:45:02 +00:00
|
|
|
space = (rb_len - r) + w - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int get_reader_space() const {
|
|
|
|
int space, r, w;
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
r = rb_read_pos;
|
|
|
|
w = rb_write_pos;
|
|
|
|
|
|
|
|
if (r == w) {
|
|
|
|
space = 0;
|
|
|
|
} else if (w < r) {
|
|
|
|
space = rb_len - r + w;
|
|
|
|
} else {
|
|
|
|
space = w - r;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
return space;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 04:55:22 +00:00
|
|
|
_FORCE_INLINE_ bool has_data() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return rb && rb_read_pos != rb_write_pos;
|
2015-11-01 04:55:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:45:02 +00:00
|
|
|
_FORCE_INLINE_ float *get_write_buffer() { return read_buf; }
|
2015-09-09 21:50:52 +00:00
|
|
|
_FORCE_INLINE_ void write(uint32_t p_frames) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(p_frames >= rb_len);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (channels) {
|
2015-09-09 21:50:52 +00:00
|
|
|
case 1: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 0; i < p_frames; i++) {
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rb[rb_write_pos] = read_buf[i];
|
|
|
|
rb_write_pos = (rb_write_pos + 1) & rb_mask;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 0; i < p_frames; i++) {
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rb[(rb_write_pos << 1) + 0] = read_buf[(i << 1) + 0];
|
|
|
|
rb[(rb_write_pos << 1) + 1] = read_buf[(i << 1) + 1];
|
|
|
|
rb_write_pos = (rb_write_pos + 1) & rb_mask;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 4: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 0; i < p_frames; i++) {
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rb[(rb_write_pos << 2) + 0] = read_buf[(i << 2) + 0];
|
|
|
|
rb[(rb_write_pos << 2) + 1] = read_buf[(i << 2) + 1];
|
|
|
|
rb[(rb_write_pos << 2) + 2] = read_buf[(i << 2) + 2];
|
|
|
|
rb[(rb_write_pos << 2) + 3] = read_buf[(i << 2) + 3];
|
|
|
|
rb_write_pos = (rb_write_pos + 1) & rb_mask;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 6: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 0; i < p_frames; i++) {
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rb[(rb_write_pos * 6) + 0] = read_buf[(i * 6) + 0];
|
|
|
|
rb[(rb_write_pos * 6) + 1] = read_buf[(i * 6) + 1];
|
|
|
|
rb[(rb_write_pos * 6) + 2] = read_buf[(i * 6) + 2];
|
|
|
|
rb[(rb_write_pos * 6) + 3] = read_buf[(i * 6) + 3];
|
|
|
|
rb[(rb_write_pos * 6) + 4] = read_buf[(i * 6) + 4];
|
|
|
|
rb[(rb_write_pos * 6) + 5] = read_buf[(i * 6) + 5];
|
|
|
|
rb_write_pos = (rb_write_pos + 1) & rb_mask;
|
2015-09-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_channel_count() const;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed = -1);
|
2015-09-09 21:50:52 +00:00
|
|
|
void clear();
|
2017-09-14 20:45:02 +00:00
|
|
|
bool mix(AudioFrame *p_dest, int p_frames);
|
|
|
|
int get_num_of_ready_frames();
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
AudioRBResampler();
|
|
|
|
~AudioRBResampler();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AUDIO_RB_RESAMPLER_H
|