Skip to content

Commit

Permalink
Merge pull request #87006 from AThousandShips/frame_fix
Browse files Browse the repository at this point in the history
Fix member names of `AudioFrame` to match extension
  • Loading branch information
akien-mga committed Feb 13, 2024
2 parents 5ae4faf + d8b29ef commit eb77418
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 137 deletions.
110 changes: 64 additions & 46 deletions core/math/audio_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,105 +51,123 @@ static const float AUDIO_PEAK_OFFSET = 0.0000000001f;
static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear_to_db(AUDIO_PEAK_OFFSET)

struct AudioFrame {
//left and right samples
float l = 0.f, r = 0.f;

_ALWAYS_INLINE_ const float &operator[](int idx) const { return idx == 0 ? l : r; }
_ALWAYS_INLINE_ float &operator[](int idx) { return idx == 0 ? l : r; }
// Left and right samples.
union {
struct {
float left;
float right;
};
#ifndef DISABLE_DEPRECATED
struct {
float l;
float r;
};
#endif
float levels[2] = { 0.0 };
};

_ALWAYS_INLINE_ const float &operator[](int p_idx) const {
DEV_ASSERT((unsigned int)p_idx < 2);
return levels[p_idx];
}
_ALWAYS_INLINE_ float &operator[](int p_idx) {
DEV_ASSERT((unsigned int)p_idx < 2);
return levels[p_idx];
}

_ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(l + p_frame.l, r + p_frame.r); }
_ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(l - p_frame.l, r - p_frame.r); }
_ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(l * p_frame.l, r * p_frame.r); }
_ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(l / p_frame.l, r / p_frame.r); }
_ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(left + p_frame.left, right + p_frame.right); }
_ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(left - p_frame.left, right - p_frame.right); }
_ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(left * p_frame.left, right * p_frame.right); }
_ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(left / p_frame.left, right / p_frame.right); }

_ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(l + p_sample, r + p_sample); }
_ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(l - p_sample, r - p_sample); }
_ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(l * p_sample, r * p_sample); }
_ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(l / p_sample, r / p_sample); }
_ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(left + p_sample, right + p_sample); }
_ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(left - p_sample, right - p_sample); }
_ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(left * p_sample, right * p_sample); }
_ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(left / p_sample, right / p_sample); }

_ALWAYS_INLINE_ void operator+=(const AudioFrame &p_frame) {
l += p_frame.l;
r += p_frame.r;
left += p_frame.left;
right += p_frame.right;
}
_ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) {
l -= p_frame.l;
r -= p_frame.r;
left -= p_frame.left;
right -= p_frame.right;
}
_ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) {
l *= p_frame.l;
r *= p_frame.r;
left *= p_frame.left;
right *= p_frame.right;
}
_ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) {
l /= p_frame.l;
r /= p_frame.r;
left /= p_frame.left;
right /= p_frame.right;
}

_ALWAYS_INLINE_ void operator+=(float p_sample) {
l += p_sample;
r += p_sample;
left += p_sample;
right += p_sample;
}
_ALWAYS_INLINE_ void operator-=(float p_sample) {
l -= p_sample;
r -= p_sample;
left -= p_sample;
right -= p_sample;
}
_ALWAYS_INLINE_ void operator*=(float p_sample) {
l *= p_sample;
r *= p_sample;
left *= p_sample;
right *= p_sample;
}
_ALWAYS_INLINE_ void operator/=(float p_sample) {
l /= p_sample;
r /= p_sample;
left /= p_sample;
right /= p_sample;
}

_ALWAYS_INLINE_ void undenormalize() {
l = ::undenormalize(l);
r = ::undenormalize(r);
left = ::undenormalize(left);
right = ::undenormalize(right);
}

_FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const {
AudioFrame res = *this;

res.l += (p_t * (p_b.l - l));
res.r += (p_t * (p_b.r - r));
res.left += (p_t * (p_b.left - left));
res.right += (p_t * (p_b.right - right));

return res;
}

_ALWAYS_INLINE_ AudioFrame(float p_l, float p_r) {
l = p_l;
r = p_r;
_ALWAYS_INLINE_ AudioFrame(float p_left, float p_right) {
left = p_left;
right = p_right;
}
_ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
l = p_frame.l;
r = p_frame.r;
left = p_frame.left;
right = p_frame.right;
}

_ALWAYS_INLINE_ void operator=(const AudioFrame &p_frame) {
l = p_frame.l;
r = p_frame.r;
left = p_frame.left;
right = p_frame.right;
}

_ALWAYS_INLINE_ operator Vector2() const {
return Vector2(l, r);
return Vector2(left, right);
}

_ALWAYS_INLINE_ AudioFrame(const Vector2 &p_v2) {
l = p_v2.x;
r = p_v2.y;
left = p_v2.x;
right = p_v2.y;
}
_ALWAYS_INLINE_ AudioFrame() {}
};

_ALWAYS_INLINE_ AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) {
return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar);
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
}

_ALWAYS_INLINE_ AudioFrame operator*(int32_t p_scalar, const AudioFrame &p_frame) {
return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar);
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
}

_ALWAYS_INLINE_ AudioFrame operator*(int64_t p_scalar, const AudioFrame &p_frame) {
return AudioFrame(p_frame.l * p_scalar, p_frame.r * p_scalar);
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
}

#endif // AUDIO_FRAME_H
8 changes: 4 additions & 4 deletions editor/audio_stream_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ void AudioStreamPreviewGenerator::_preview_thread(void *p_preview) {
}

for (int j = from; j < to; j++) {
max = MAX(max, mix_chunk[j].l);
max = MAX(max, mix_chunk[j].r);
max = MAX(max, mix_chunk[j].left);
max = MAX(max, mix_chunk[j].right);

min = MIN(min, mix_chunk[j].l);
min = MIN(min, mix_chunk[j].r);
min = MIN(min, mix_chunk[j].left);
min = MIN(min, mix_chunk[j].right);
}

uint8_t pfrom = CLAMP((min * 0.5 + 0.5) * 255, 0, 255);
Expand Down
8 changes: 4 additions & 4 deletions editor/plugins/editor_preview_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,11 @@ Ref<Texture2D> EditorAudioStreamPreviewPlugin::generate(const Ref<Resource> &p_f
}

for (int j = from; j < to; j++) {
max = MAX(max, frames[j].l);
max = MAX(max, frames[j].r);
max = MAX(max, frames[j].left);
max = MAX(max, frames[j].right);

min = MIN(min, frames[j].l);
min = MIN(min, frames[j].r);
min = MIN(min, frames[j].left);
min = MIN(min, frames[j].right);
}

int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
Expand Down
8 changes: 4 additions & 4 deletions modules/vorbis/audio_stream_ogg_vorbis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ int AudioStreamPlaybackOggVorbis::_mix_frames_vorbis(AudioFrame *p_buffer, int p

if (info.channels > 1) {
for (int frame = 0; frame < frames; frame++) {
p_buffer[frame].l = pcm[0][frame];
p_buffer[frame].r = pcm[1][frame];
p_buffer[frame].left = pcm[0][frame];
p_buffer[frame].right = pcm[1][frame];
}
} else {
for (int frame = 0; frame < frames; frame++) {
p_buffer[frame].l = pcm[0][frame];
p_buffer[frame].r = pcm[0][frame];
p_buffer[frame].left = pcm[0][frame];
p_buffer[frame].right = pcm[0][frame];
}
}
vorbis_synthesis_read(&dsp_state, frames);
Expand Down
32 changes: 16 additions & 16 deletions scene/3d/audio_stream_player_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ void AudioStreamPlayer3D::_calc_output_vol(const Vector3 &source_dir, real_t tig

switch (AudioServer::get_singleton()->get_speaker_mode()) {
case AudioServer::SPEAKER_SURROUND_71:
output.write[3].l = volumes[5]; // side-left
output.write[3].r = volumes[6]; // side-right
output.write[3].left = volumes[5]; // side-left
output.write[3].right = volumes[6]; // side-right
[[fallthrough]];
case AudioServer::SPEAKER_SURROUND_51:
output.write[2].l = volumes[3]; // rear-left
output.write[2].r = volumes[4]; // rear-right
output.write[2].left = volumes[3]; // rear-left
output.write[2].right = volumes[4]; // rear-right
[[fallthrough]];
case AudioServer::SPEAKER_SURROUND_31:
output.write[1].r = 1.0; // LFE - always full power
output.write[1].l = volumes[2]; // center
output.write[1].right = 1.0; // LFE - always full power
output.write[1].left = volumes[2]; // center
[[fallthrough]];
case AudioServer::SPEAKER_MODE_STEREO:
output.write[0].r = volumes[1]; // front-right
output.write[0].l = volumes[0]; // front-left
output.write[0].right = volumes[1]; // front-right
output.write[0].left = volumes[0]; // front-left
break;
}
}
Expand Down Expand Up @@ -168,25 +168,25 @@ void AudioStreamPlayer3D::_calc_reverb_vol(Area3D *area, Vector3 listener_area_p

// Stereo pair.
float c = rev_pos.x * 0.5 + 0.5;
reverb_vol.write[0].l = 1.0 - c;
reverb_vol.write[0].r = c;
reverb_vol.write[0].left = 1.0 - c;
reverb_vol.write[0].right = c;

if (channel_count >= 3) {
// Center pair + Side pair
float xl = Vector3(-1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;
float xr = Vector3(1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;

reverb_vol.write[1].l = xl;
reverb_vol.write[1].r = xr;
reverb_vol.write[2].l = 1.0 - xr;
reverb_vol.write[2].r = 1.0 - xl;
reverb_vol.write[1].left = xl;
reverb_vol.write[1].right = xr;
reverb_vol.write[2].left = 1.0 - xr;
reverb_vol.write[2].right = 1.0 - xl;
}

if (channel_count >= 4) {
// Rear pair
// FIXME: Not sure what math should be done here
reverb_vol.write[3].l = 1.0 - c;
reverb_vol.write[3].r = c;
reverb_vol.write[3].left = 1.0 - c;
reverb_vol.write[3].right = c;
}

for (int i = 0; i < channel_count; i++) {
Expand Down
4 changes: 2 additions & 2 deletions scene/resources/audio_stream_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ void AudioStreamPlaybackWAV::do_resample(const Depth *p_src, AudioFrame *p_dst,
final_r = final; //copy to right channel if stereo
}

p_dst->l = final / 32767.0;
p_dst->r = final_r / 32767.0;
p_dst->left = final / 32767.0;
p_dst->right = final_r / 32767.0;
p_dst++;

p_offset += p_increment;
Expand Down
2 changes: 1 addition & 1 deletion servers/audio/effects/audio_effect_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PackedVector2Array AudioEffectCapture::get_buffer(int p_frames) {
streaming_data.resize(p_frames);
buffer.read(streaming_data.ptrw(), p_frames);
for (int32_t i = 0; i < p_frames; i++) {
ret.write[i] = Vector2(streaming_data[i].l, streaming_data[i].r);
ret.write[i] = Vector2(streaming_data[i].left, streaming_data[i].right);
}
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions servers/audio/effects/audio_effect_chorus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames, A
//vol modifier

AudioFrame vol_modifier = AudioFrame(base->wet, base->wet) * Math::db_to_linear(v.level);
vol_modifier.l *= CLAMP(1.0 - v.pan, 0, 1);
vol_modifier.r *= CLAMP(1.0 + v.pan, 0, 1);
vol_modifier.left *= CLAMP(1.0 - v.pan, 0, 1);
vol_modifier.right *= CLAMP(1.0 + v.pan, 0, 1);

for (int i = 0; i < p_frame_count; i++) {
/** COMPUTE WAVEFORM **/
Expand Down
6 changes: 3 additions & 3 deletions servers/audio/effects/audio_effect_compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ void AudioEffectCompressorInstance::process(const AudioFrame *p_src_frames, Audi
for (int i = 0; i < p_frame_count; i++) {
AudioFrame s = src[i];
//convert to positive
s.l = Math::abs(s.l);
s.r = Math::abs(s.r);
s.left = Math::abs(s.left);
s.right = Math::abs(s.right);

float peak = MAX(s.l, s.r);
float peak = MAX(s.left, s.right);

float overdb = 2.08136898f * Math::linear_to_db(peak / threshold);

Expand Down
8 changes: 4 additions & 4 deletions servers/audio/effects/audio_effect_delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void AudioEffectDelayInstance::_process_chunk(const AudioFrame *p_src_frames, Au

AudioFrame tap1_vol = AudioFrame(tap_1_level_f, tap_1_level_f);

tap1_vol.l *= CLAMP(1.0 - base->tap_1_pan, 0, 1);
tap1_vol.r *= CLAMP(1.0 + base->tap_1_pan, 0, 1);
tap1_vol.left *= CLAMP(1.0 - base->tap_1_pan, 0, 1);
tap1_vol.right *= CLAMP(1.0 + base->tap_1_pan, 0, 1);

AudioFrame tap2_vol = AudioFrame(tap_2_level_f, tap_2_level_f);

tap2_vol.l *= CLAMP(1.0 - base->tap_2_pan, 0, 1);
tap2_vol.r *= CLAMP(1.0 + base->tap_2_pan, 0, 1);
tap2_vol.left *= CLAMP(1.0 - base->tap_2_pan, 0, 1);
tap2_vol.right *= CLAMP(1.0 + base->tap_2_pan, 0, 1);

// feedback lowpass here
float lpf_c = expf(-Math_TAU * base->feedback_lowpass / mix_rate); // 0 .. 10khz
Expand Down
8 changes: 4 additions & 4 deletions servers/audio/effects/audio_effect_eq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *
AudioFrame dst = AudioFrame(0, 0);

for (int j = 0; j < band_count; j++) {
float l = src.l;
float r = src.r;
float l = src.left;
float r = src.right;

proc_l[j].process_one(l);
proc_r[j].process_one(r);

dst.l += l * bgain[j];
dst.r += r * bgain[j];
dst.left += l * bgain[j];
dst.right += r * bgain[j];
}

p_dst_frames[i] = dst;
Expand Down
8 changes: 4 additions & 4 deletions servers/audio/effects/audio_effect_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
template <int S>
void AudioEffectFilterInstance::_process_filter(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
for (int i = 0; i < p_frame_count; i++) {
float f = p_src_frames[i].l;
float f = p_src_frames[i].left;
filter_process[0][0].process_one(f);
if constexpr (S > 1) {
filter_process[0][1].process_one(f);
Expand All @@ -46,11 +46,11 @@ void AudioEffectFilterInstance::_process_filter(const AudioFrame *p_src_frames,
filter_process[0][3].process_one(f);
}

p_dst_frames[i].l = f;
p_dst_frames[i].left = f;
}

for (int i = 0; i < p_frame_count; i++) {
float f = p_src_frames[i].r;
float f = p_src_frames[i].right;
filter_process[1][0].process_one(f);
if constexpr (S > 1) {
filter_process[1][1].process_one(f);
Expand All @@ -62,7 +62,7 @@ void AudioEffectFilterInstance::_process_filter(const AudioFrame *p_src_frames,
filter_process[1][3].process_one(f);
}

p_dst_frames[i].r = f;
p_dst_frames[i].right = f;
}
}

Expand Down
Loading

0 comments on commit eb77418

Please sign in to comment.