Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Optimize AudioServer::_driver_process() #63430

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,40 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {

//master master, send to output
int cs = master->channels.size();

// take away 1 from the stride as we are manually incrementing one for stereo
uintptr_t stride_minus_one = (cs * 2) - 1;

for (int k = 0; k < cs; k++) {
// destination start for data will be the same in all cases
int32_t *dest = &p_buffer[(from_buf * (cs * 2)) + (k * 2)];

if (master->channels[k].active) {
const AudioFrame *buf = master->channels[k].buffer.ptr();

for (int j = 0; j < to_copy; j++) {
float l = CLAMP(buf[from + j].l, -1.0, 1.0);
int32_t vl = l * ((1 << 20) - 1);
int32_t vl2 = (vl < 0 ? -1 : 1) * (ABS(vl) << 11);
p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = vl2;
*dest = vl2;
dest++;

float r = CLAMP(buf[from + j].r, -1.0, 1.0);
int32_t vr = r * ((1 << 20) - 1);
int32_t vr2 = (vr < 0 ? -1 : 1) * (ABS(vr) << 11);
p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = vr2;
*dest = vr2;
dest += stride_minus_one;
}

} else {
// Bizarrely, profiling indicates that detecting the common case of cs == 1
// and k == 0, and using memset is SLOWER than setting individually.
// (Perhaps it gets optimized to a faster instruction than memset).
for (int j = 0; j < to_copy; j++) {
p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = 0;
p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = 0;
*dest = 0;
dest++;
*dest = 0;
dest += stride_minus_one;
}
}
}
Expand Down