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