diff --git a/scripts/loaders/sound_loader.gd b/scripts/loaders/sound_loader.gd index b3db022..49679b8 100644 --- a/scripts/loaders/sound_loader.gd +++ b/scripts/loaders/sound_loader.gd @@ -52,7 +52,7 @@ func decode_brr(data: PoolByteArray): var filter_designation := (data[0] >> 2) & 0x03 var loop := bool(data[0] & 0x02) var end := bool(data[0] & 0x01) - var samples := [] + var samples := PoolIntArray() for i in range(1, 9): var b := data[i] samples.append(process_sample(b >> 4, exponent)) @@ -60,8 +60,12 @@ func decode_brr(data: PoolByteArray): return [samples, loop, end, filter_designation] -func clamp_short(i: int): - return min(max(i, -0x8000), 0x7FFF) +func clamp_short(i: int) -> int: + if i < -0x8000: + return -0x8000 + if i > 0x7FFF: + return 0x7FFF + return i func make_sample(rom: File, size: int, sample_rate: int) -> AudioStreamSample: var audio := AudioStreamSample.new() @@ -72,9 +76,11 @@ func make_sample(rom: File, size: int, sample_rate: int) -> AudioStreamSample: if (size % 9) != 0: print_debug('Oh no! An instrument sample has an invalid size of %d! at $%06X' % [size, rom.get_position()-2]) return audio + var num_packets := size/9 - var samples = [0, 0] # Two zero samples for filter purposes, strip them from the actual output - for pkt in (size / 9): + var samples = PoolIntArray([0, 0]) # Start with two zero samples for filter purposes, strip them from the actual output + var i := 2 + for pkt in num_packets: var data = decode_brr(rom.get_buffer(9)) samples.append_array(data[0]) #var loop = data[1] @@ -82,20 +88,21 @@ func make_sample(rom: File, size: int, sample_rate: int) -> AudioStreamSample: var filter = data[3] match filter: 1: - for i in range(samples.size()-8, samples.size()): - samples[i] = clamp_short(samples[i] + (samples[i-1]*15)/16) + for j in range(samples.size()-16, samples.size()): + samples[j] = clamp_short(samples[j] + (samples[j-1]*15)/16) 2: - for i in range(samples.size()-8, samples.size()): - samples[i] = clamp_short(samples[i] + (samples[i-1]*61)/32 - (samples[i-2]*15)/16) + for j in range(samples.size()-16, samples.size()): + samples[j] = clamp_short(samples[j] + (samples[j-1]*61)/32 - (samples[j-2]*15)/16) 3: - for i in range(samples.size()-8, samples.size()): - samples[i] = clamp_short(samples[i] + (samples[i-1]*115)/64 - (samples[i-2]*13)/16) + for j in range(samples.size()-16, samples.size()): + samples[j] = clamp_short(samples[j] + (samples[j-1]*115)/64 - (samples[j-2]*13)/16) if end: # print('End flag on packet') break + # Convert int array to byte array var audio_data = PoolByteArray() - for i in range(2, samples.size()): - var b = samples[i] + for j in range(2, samples.size()): + var b = samples[j] audio_data.append(b & 0xFF) audio_data.append(b >> 8) audio.data = audio_data