Skip to content

Commit

Permalink
Deprecate IMA ADPCM
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeJayLSP committed Dec 7, 2024
1 parent aa8d9b8 commit f68095e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
25 changes: 22 additions & 3 deletions scene/resources/audio_stream_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const float TRIM_DB_LIMIT = -50;
const int TRIM_FADE_OUT_FRAMES = 500;

void AudioStreamPlaybackWAV::start(double p_from_pos) {
#ifndef DISABLE_DEPRECATED
if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
//no seeking in IMA_ADPCM
for (int i = 0; i < 2; i++) {
Expand All @@ -51,8 +52,11 @@ void AudioStreamPlaybackWAV::start(double p_from_pos) {

offset = 0;
} else {
#endif
seek(p_from_pos);
#ifndef DISABLE_DEPRECATED
}
#endif

sign = 1;
active = true;
Expand All @@ -75,9 +79,11 @@ double AudioStreamPlaybackWAV::get_playback_position() const {
}

void AudioStreamPlaybackWAV::seek(double p_time) {
#ifndef DISABLE_DEPRECATED
if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
return; //no seeking in ima-adpcm
}
#endif

double max = base->get_length();
if (p_time < 0) {
Expand All @@ -101,6 +107,7 @@ void AudioStreamPlaybackWAV::do_resample(const Depth *p_src, AudioFrame *p_dst,
pos <<= 1;
}

#ifndef DISABLE_DEPRECATED
if (is_ima_adpcm) {
int64_t sample_pos = pos + p_ima_adpcm[0].window_ofs;

Expand Down Expand Up @@ -176,6 +183,7 @@ void AudioStreamPlaybackWAV::do_resample(const Depth *p_src, AudioFrame *p_dst,
}

} else {
#endif // DISABLE_DEPRECATED
if (is_qoa) {
if (pos != p_qoa->cache_pos) { // Prevents triple decoding on lower mix rates.
for (int i = 0; i < 2; i++) {
Expand Down Expand Up @@ -252,7 +260,9 @@ void AudioStreamPlaybackWAV::do_resample(const Depth *p_src, AudioFrame *p_dst,
if (is_stereo) {
final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
}
#ifndef DISABLE_DEPRECATED
}
#endif

if (!is_stereo) {
final_r = final; //copy to right channel if stereo
Expand Down Expand Up @@ -327,13 +337,15 @@ int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_
const uint8_t *data = base->data.ptr() + AudioStreamWAV::DATA_PAD;
AudioFrame *dst_buff = p_buffer;

#ifndef DISABLE_DEPRECATED
if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
if (loop_format != AudioStreamWAV::LOOP_DISABLED) {
ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
loop_format = AudioStreamWAV::LOOP_FORWARD;
}
}
#endif

while (todo > 0) {
int64_t limit = 0;
Expand Down Expand Up @@ -374,7 +386,7 @@ int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_
sign *= -1;
} else {
/* go to loop-begin */

#ifndef DISABLE_DEPRECATED
if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
for (int i = 0; i < 2; i++) {
ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
Expand All @@ -383,8 +395,11 @@ int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_
}
offset = loop_begin_fp;
} else {
#endif
offset = loop_begin_fp + (offset - loop_end_fp);
#ifndef DISABLE_DEPRECATED
}
#endif
}
} else {
/* no loop, check for end of sample */
Expand Down Expand Up @@ -412,7 +427,7 @@ int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_

todo -= target;

switch (base->format) {
switch (format) {
case AudioStreamWAV::FORMAT_8_BITS: {
if (is_stereo) {
do_resample<int8_t, true, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
Expand Down Expand Up @@ -1146,6 +1161,8 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_fi
AudioStreamWAV::Format dst_format;

if (compression == 1) {
#ifndef DISABLE_DEPRECATED
WARN_DEPRECATED_MSG("IMA ADPCM compression is deprecated. Consider using Quite OK Audio instead.");
dst_format = AudioStreamWAV::FORMAT_IMA_ADPCM;
if (format_channels == 1) {
_compress_ima_adpcm(data, dst_data);
Expand Down Expand Up @@ -1181,7 +1198,9 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_fi
w[i * 2 + 1] = rr[i];
}
}

#else
ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "This build was compiled without deprecated features. IMA ADPCM is not available.");
#endif
} else if (compression == 2) {
dst_format = AudioStreamWAV::FORMAT_QOA;

Expand Down
2 changes: 2 additions & 0 deletions scene/resources/audio_stream_wav.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class AudioStreamWAV : public AudioStream {
}
virtual Ref<AudioSample> generate_sample() const override;

#ifndef DISABLE_DEPRECATED
static void _compress_ima_adpcm(const Vector<float> &p_data, Vector<uint8_t> &r_dst_data) {
static const int16_t _ima_adpcm_step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
Expand Down Expand Up @@ -269,6 +270,7 @@ class AudioStreamWAV : public AudioStream {
}
}
}
#endif

static void _compress_qoa(const Vector<float> &p_data, Vector<uint8_t> &dst_data, qoa_desc *p_desc) {
uint32_t frames_len = (p_desc->samples + QOA_FRAME_LEN - 1) / QOA_FRAME_LEN * (QOA_LMS_LEN * 4 * p_desc->channels + 8);
Expand Down
2 changes: 2 additions & 0 deletions servers/audio/effects/audio_effect_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ Ref<AudioStreamWAV> AudioEffectRecord::get_recording() const {
int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
encode_uint16(v, &w[i * 2]);
}
#ifndef DISABLE_DEPRECATED
} else if (dst_format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
//byte interleave
Vector<float> left;
Expand Down Expand Up @@ -250,6 +251,7 @@ Ref<AudioStreamWAV> AudioEffectRecord::get_recording() const {
w[i * 2 + 0] = rl[i];
w[i * 2 + 1] = rr[i];
}
#endif
} else if (dst_format == AudioStreamWAV::FORMAT_QOA) {
qoa_desc desc = {};
desc.samples = current_instance->recording_data.size() / 2;
Expand Down

0 comments on commit f68095e

Please sign in to comment.