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