Skip to content

Commit 7b5c53d

Browse files
committed
Use 64-bit offset/loop points in AudioStreamWAV
1 parent 8b4b93a commit 7b5c53d

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

scene/resources/audio_stream_wav.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void AudioStreamPlaybackWAV::decode_samples(const Depth *p_src, AudioFrame *p_ds
174174
}
175175

176176
} else if (is_qoa) {
177-
uint32_t new_data_ofs = 8 + pos / QOA_FRAME_LEN * p_qoa->frame_len;
177+
uint64_t new_data_ofs = 8 + pos / QOA_FRAME_LEN * p_qoa->frame_len;
178178

179179
if (p_qoa->data_ofs != new_data_ofs) {
180180
p_qoa->data_ofs = new_data_ofs;
@@ -222,7 +222,7 @@ int AudioStreamPlaybackWAV::_mix_internal(AudioFrame *p_buffer, int p_frames) {
222222
return 0;
223223
}
224224

225-
uint32_t len = base->data_bytes;
225+
uint64_t len = base->data_bytes;
226226
switch (base->format) {
227227
case AudioStreamWAV::FORMAT_8_BITS:
228228
len /= 1;
@@ -327,7 +327,7 @@ int AudioStreamPlaybackWAV::_mix_internal(AudioFrame *p_buffer, int p_frames) {
327327
}
328328
} else {
329329
/* no loop, check for end of sample */
330-
if (offset >= len) {
330+
if ((uint64_t)offset >= len) {
331331
active = false;
332332
break;
333333
}
@@ -486,7 +486,7 @@ Dictionary AudioStreamWAV::get_tags() const {
486486
}
487487

488488
double AudioStreamWAV::get_length() const {
489-
int len = data_bytes;
489+
uint64_t len = data_bytes;
490490
switch (format) {
491491
case AudioStreamWAV::FORMAT_8_BITS:
492492
len /= 1;
@@ -587,14 +587,14 @@ Error AudioStreamWAV::save_to_wav(const String &p_path) {
587587
const uint8_t *read_data = data.ptr();
588588
switch (format) {
589589
case AudioStreamWAV::FORMAT_8_BITS:
590-
for (unsigned int i = 0; i < data_bytes; i++) {
590+
for (uint64_t i = 0; i < data_bytes; i++) {
591591
uint8_t data_point = (read_data[i] + 128);
592592
file->store_8(data_point);
593593
}
594594
break;
595595
case AudioStreamWAV::FORMAT_16_BITS:
596596
case AudioStreamWAV::FORMAT_QOA:
597-
for (unsigned int i = 0; i < data_bytes / 2; i++) {
597+
for (uint64_t i = 0; i < data_bytes / 2; i++) {
598598
uint16_t data_point = decode_uint16(&read_data[i * 2]);
599599
file->store_16(data_point);
600600
}
@@ -616,8 +616,8 @@ Ref<AudioStreamPlayback> AudioStreamWAV::instantiate_playback() {
616616
uint32_t ffp = qoa_decode_header(data.ptr(), data_bytes, &sample->qoa.desc);
617617
ERR_FAIL_COND_V(ffp != 8, Ref<AudioStreamPlaybackWAV>());
618618
sample->qoa.frame_len = qoa_max_frame_size(&sample->qoa.desc);
619-
int samples_len = sample->qoa.desc.samples > QOA_FRAME_LEN ? QOA_FRAME_LEN : (sample->qoa.desc.samples + 1);
620-
int dec_len = sample->qoa.desc.channels * samples_len;
619+
uint32_t samples_len = MIN(sample->qoa.desc.samples + 1, (uint32_t)QOA_FRAME_LEN);
620+
uint32_t dec_len = sample->qoa.desc.channels * samples_len;
621621
sample->qoa.dec.resize(dec_len);
622622
}
623623

@@ -697,17 +697,17 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
697697
// We parse the WAV loop points only with "Detect From WAV" (0).
698698
int import_loop_mode = p_options["edit/loop_mode"];
699699

700-
int format_bits = 0;
701-
int format_channels = 0;
700+
uint16_t format_bits = 0;
701+
uint16_t format_channels = 0;
702702

703703
AudioStreamWAV::LoopMode loop_mode = AudioStreamWAV::LOOP_DISABLED;
704704
uint16_t compression_code = 1;
705705
bool format_found = false;
706706
bool data_found = false;
707-
int format_freq = 0;
708-
int loop_begin = 0;
709-
int loop_end = 0;
710-
int frames = 0;
707+
uint32_t format_freq = 0;
708+
int64_t loop_begin = 0;
709+
int64_t loop_end = 0;
710+
int64_t frames = 0;
711711

712712
Vector<float> data;
713713

@@ -785,23 +785,23 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
785785
print_line("bits: "+itos(format_bits));
786786
*/
787787

788-
data.resize(frames * format_channels);
788+
ERR_FAIL_COND_V(data.resize(frames * format_channels) != OK, Ref<AudioStreamWAV>());
789789

790790
if (compression_code == 1) {
791791
if (format_bits == 8) {
792-
for (int i = 0; i < frames * format_channels; i++) {
792+
for (int64_t i = 0; i < frames * format_channels; i++) {
793793
// 8 bit samples are UNSIGNED
794794

795795
data.write[i] = int8_t(file->get_8() - 128) / 128.f;
796796
}
797797
} else if (format_bits == 16) {
798-
for (int i = 0; i < frames * format_channels; i++) {
798+
for (int64_t i = 0; i < frames * format_channels; i++) {
799799
//16 bit SIGNED
800800

801801
data.write[i] = int16_t(file->get_16()) / 32768.f;
802802
}
803803
} else {
804-
for (int i = 0; i < frames * format_channels; i++) {
804+
for (int64_t i = 0; i < frames * format_channels; i++) {
805805
//16+ bits samples are SIGNED
806806
// if sample is > 16 bits, just read extra bytes
807807

@@ -816,13 +816,13 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
816816
}
817817
} else if (compression_code == 3) {
818818
if (format_bits == 32) {
819-
for (int i = 0; i < frames * format_channels; i++) {
819+
for (int64_t i = 0; i < frames * format_channels; i++) {
820820
//32 bit IEEE Float
821821

822822
data.write[i] = file->get_float();
823823
}
824824
} else if (format_bits == 64) {
825-
for (int i = 0; i < frames * format_channels; i++) {
825+
for (int64_t i = 0; i < frames * format_channels; i++) {
826826
//64 bit IEEE Float
827827

828828
data.write[i] = file->get_double();
@@ -855,7 +855,7 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
855855
// only read 0x00 (loop forward), 0x01 (loop ping-pong) and 0x02 (loop backward)
856856
// Skip anything else because it's not supported, reserved for future uses or sampler specific
857857
// from https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl (loop type values table)
858-
int loop_type = file->get_32();
858+
uint32_t loop_type = file->get_32();
859859
if (loop_type == 0x00 || loop_type == 0x01 || loop_type == 0x02) {
860860
if (loop_type == 0x00) {
861861
loop_mode = AudioStreamWAV::LOOP_FORWARD;
@@ -890,7 +890,7 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
890890
}
891891

892892
Vector<char> text;
893-
text.resize(text_size);
893+
ERR_FAIL_COND_V(text.resize(text_size) != OK, Ref<AudioStreamWAV>());
894894
file->get_buffer((uint8_t *)&text[0], text_size);
895895

896896
// Skip padding byte if text_size is odd
@@ -918,7 +918,7 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
918918
// STEP 2, APPLY CONVERSIONS
919919

920920
bool is16 = format_bits != 8;
921-
int rate = format_freq;
921+
uint32_t rate = format_freq;
922922

923923
/*
924924
print_line("Input Sample: ");
@@ -934,18 +934,18 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
934934
//apply frequency limit
935935

936936
bool limit_rate = p_options["force/max_rate"];
937-
int limit_rate_hz = p_options["force/max_rate_hz"];
937+
uint32_t limit_rate_hz = p_options["force/max_rate_hz"];
938938
if (limit_rate && rate > limit_rate_hz && rate > 0 && frames > 0) {
939939
// resample!
940-
int new_data_frames = (int)(frames * (float)limit_rate_hz / (float)rate);
940+
int64_t new_data_frames = (int64_t)(frames * (float)limit_rate_hz / (float)rate);
941941

942942
Vector<float> new_data;
943-
new_data.resize(new_data_frames * format_channels);
943+
ERR_FAIL_COND_V(new_data.resize(new_data_frames * format_channels) != OK, Ref<AudioStreamWAV>());
944944
for (int c = 0; c < format_channels; c++) {
945945
float frac = 0.0;
946-
int ipos = 0;
946+
int64_t ipos = 0;
947947

948-
for (int i = 0; i < new_data_frames; i++) {
948+
for (int64_t i = 0; i < new_data_frames; i++) {
949949
// Cubic interpolation should be enough.
950950

951951
float y0 = data[MAX(0, ipos - 1) * format_channels + c];
@@ -959,15 +959,15 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
959959
// in order to avoid 32bit floating point precision errors
960960

961961
frac += (float)rate / (float)limit_rate_hz;
962-
int tpos = (int)Math::floor(frac);
962+
int64_t tpos = (int64_t)Math::floor(frac);
963963
ipos += tpos;
964964
frac -= tpos;
965965
}
966966
}
967967

968968
if (loop_mode) {
969-
loop_begin = (int)(loop_begin * (float)new_data_frames / (float)frames);
970-
loop_end = (int)(loop_end * (float)new_data_frames / (float)frames);
969+
loop_begin = (int64_t)(loop_begin * (float)new_data_frames / (float)frames);
970+
loop_end = (int64_t)(loop_end * (float)new_data_frames / (float)frames);
971971
}
972972

973973
data = new_data;
@@ -997,14 +997,14 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
997997
bool trim = p_options["edit/trim"];
998998

999999
if (trim && (loop_mode == AudioStreamWAV::LOOP_DISABLED) && format_channels > 0) {
1000-
int first = 0;
1001-
int last = (frames / format_channels) - 1;
1000+
int64_t first = 0;
1001+
int64_t last = (frames / format_channels) - 1;
10021002
bool found = false;
10031003
float limit = Math::db_to_linear(TRIM_DB_LIMIT);
10041004

1005-
for (int i = 0; i < data.size() / format_channels; i++) {
1005+
for (int64_t i = 0; i < data.size() / format_channels; i++) {
10061006
float amp_channel_sum = 0.0;
1007-
for (int j = 0; j < format_channels; j++) {
1007+
for (uint16_t j = 0; j < format_channels; j++) {
10081008
amp_channel_sum += Math::abs(data[(i * format_channels) + j]);
10091009
}
10101010

@@ -1022,15 +1022,15 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
10221022

10231023
if (first < last) {
10241024
Vector<float> new_data;
1025-
new_data.resize((last - first) * format_channels);
1026-
for (int i = first; i < last; i++) {
1025+
ERR_FAIL_COND_V(new_data.resize((last - first) * format_channels) != OK, Ref<AudioStreamWAV>());
1026+
for (int64_t i = first; i < last; i++) {
10271027
float fade_out_mult = 1.0;
10281028

10291029
if (last - i < TRIM_FADE_OUT_FRAMES) {
10301030
fade_out_mult = ((float)(last - i - 1) / (float)TRIM_FADE_OUT_FRAMES);
10311031
}
10321032

1033-
for (int j = 0; j < format_channels; j++) {
1033+
for (uint16_t j = 0; j < format_channels; j++) {
10341034
new_data.write[((i - first) * format_channels) + j] = data[(i * format_channels) + j] * fade_out_mult;
10351035
}
10361036
}
@@ -1058,8 +1058,8 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
10581058

10591059
if (force_mono && format_channels == 2) {
10601060
Vector<float> new_data;
1061-
new_data.resize(data.size() / 2);
1062-
for (int i = 0; i < frames; i++) {
1061+
ERR_FAIL_COND_V(new_data.resize(data.size() / 2) != OK, Ref<AudioStreamWAV>());
1062+
for (int64_t i = 0; i < frames; i++) {
10631063
new_data.write[i] = (data[i * 2 + 0] + data[i * 2 + 1]) / 2.0;
10641064
}
10651065

@@ -1084,11 +1084,11 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
10841084
Vector<float> left;
10851085
Vector<float> right;
10861086

1087-
int tframes = data.size() / 2;
1088-
left.resize(tframes);
1089-
right.resize(tframes);
1087+
int64_t tframes = data.size() / 2;
1088+
ERR_FAIL_COND_V(left.resize(tframes) != OK, Ref<AudioStreamWAV>());
1089+
ERR_FAIL_COND_V(right.resize(tframes) != OK, Ref<AudioStreamWAV>());
10901090

1091-
for (int i = 0; i < tframes; i++) {
1091+
for (int64_t i = 0; i < tframes; i++) {
10921092
left.write[i] = data[i * 2 + 0];
10931093
right.write[i] = data[i * 2 + 1];
10941094
}
@@ -1100,7 +1100,7 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
11001100
_compress_ima_adpcm(right, bright);
11011101

11021102
int dl = bleft.size();
1103-
dst_data.resize(dl * 2);
1103+
ERR_FAIL_COND_V(dst_data.resize(dl * 2) != OK, Ref<AudioStreamWAV>());
11041104

11051105
uint8_t *w = dst_data.ptrw();
11061106
const uint8_t *rl = bleft.ptr();
@@ -1123,7 +1123,7 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
11231123
_compress_qoa(data, dst_data, &desc);
11241124
} else {
11251125
dst_format = is16 ? AudioStreamWAV::FORMAT_16_BITS : AudioStreamWAV::FORMAT_8_BITS;
1126-
dst_data.resize(data.size() * (is16 ? 2 : 1));
1126+
ERR_FAIL_COND_V(dst_data.resize(data.size() * (is16 ? 2 : 1)) != OK, Ref<AudioStreamWAV>());
11271127
{
11281128
uint8_t *w = dst_data.ptrw();
11291129

scene/resources/audio_stream_wav.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class AudioStreamPlaybackWAV : public AudioStreamPlaybackResampled {
5252

5353
struct QOA_State {
5454
qoa_desc desc = {};
55-
uint32_t data_ofs = 0;
55+
uint64_t data_ofs = 0;
5656
uint32_t frame_len = 0;
5757
TightLocalVector<int16_t> dec;
5858
uint32_t dec_len = 0;
@@ -118,10 +118,10 @@ class AudioStreamWAV : public AudioStream {
118118
Format format = FORMAT_8_BITS;
119119
LoopMode loop_mode = LOOP_DISABLED;
120120
bool stereo = false;
121-
int loop_begin = 0;
122-
int loop_end = 0;
123-
int mix_rate = 44100;
124-
TightLocalVector<uint8_t> data;
121+
int64_t loop_begin = 0;
122+
int64_t loop_end = 0;
123+
uint32_t mix_rate = 44100;
124+
TightLocalVector<uint8_t, uint64_t> data;
125125
uint32_t data_bytes = 0;
126126

127127
Dictionary tags;

0 commit comments

Comments
 (0)