diff --git a/scripts/loaders/SoundLoader.gd b/scripts/loaders/SoundLoader.gd index 40f4a0c..2d3269d 100644 --- a/scripts/loaders/SoundLoader.gd +++ b/scripts/loaders/SoundLoader.gd @@ -48,20 +48,6 @@ func process_sample(mantissa: int, exponent: int) -> int: var unsigned = (mantissa << exponent) & 0xFFFF return unsigned16_to_signed(unsigned) -func decode_brr(data: PoolByteArray): - # Decodes a single 9byte BRR packet - var exponent := data[0] >> 4 - var filter_designation := (data[0] >> 2) & 0x03 - var loop := bool(data[0] & 0x02) - var end := bool(data[0] & 0x01) - var samples := PoolIntArray() - for i in range(1, 9): - var b := data[i] - samples.append(process_sample(b >> 4, exponent)) - samples.append(process_sample(b & 0x0F, exponent)) - return [samples, loop, end, filter_designation] - - func clamp_short(i: int) -> int: if i < -0x8000: return -0x8000 @@ -83,20 +69,27 @@ func make_sample(rom: File, size: int, sample_rate: int) -> AudioStreamSample: 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] - var end = data[2] - var filter = data[3] + # Decode a single 9byte BRR packet + var header_byte := rom.get_8() + var exponent := header_byte >> 4 + var filter := (header_byte >> 2) & 0x03 + var loop := bool(header_byte & 0x02) + var end := bool(header_byte & 0x01) + for sample in 8: + var b := rom.get_8() + samples.append(process_sample(b >> 4, exponent)) + samples.append(process_sample(b & 0x0F, exponent)) + # Apply filter + var l := len(samples) match filter: 1: - for j in range(samples.size()-16, samples.size()): + for j in range(l-16, l): samples[j] = clamp_short(samples[j] + (samples[j-1]*15)/16) 2: - for j in range(samples.size()-16, samples.size()): + for j in range(l-16, l): samples[j] = clamp_short(samples[j] + (samples[j-1]*61)/32 - (samples[j-2]*15)/16) 3: - for j in range(samples.size()-16, samples.size()): + for j in range(l-16, l): samples[j] = clamp_short(samples[j] + (samples[j-1]*115)/64 - (samples[j-2]*13)/16) if end: # print('End flag on packet') @@ -202,12 +195,12 @@ func load_bgms(rom: File): var player := AudioStreamPlayer.new() # Make one for each channel, later func play_sample(id: int): - print('Playing sample #%02X' % id) + print('Playing inst sample #%02X' % id) player.stream = instrument_samples[id] player.play() func play_sfx(id: int): - print('Playing sample #%02X' % id) + print('Playing sfx sample #%02X' % id) player.stream = sfx_samples[id] player.play() diff --git a/test/audio_system.gd b/test/audio_system.gd index 04feba4..b8cb8a9 100644 --- a/test/audio_system.gd +++ b/test/audio_system.gd @@ -7,9 +7,10 @@ func _create_sfx_buttons(): var disable_btn := !SoundLoader.has_loaded_audio_samples for i in SoundLoader.INST_NUM: var btn = Button.new() - btn.text = 'Play #%02X' % i + btn.text = 'Inst #%02X' % i btn.align = Button.ALIGN_CENTER - btn.set_position(Vector2((i%8)*72, (i/8)*24)) + btn.set_position(Vector2((i%7)*50, (i/7)*24)) + btn.rect_min_size.x = 48 add_child(btn) btn.connect('pressed', SoundLoader, 'play_sample', [i]) inst_buttons.append(btn) @@ -18,7 +19,8 @@ func _create_sfx_buttons(): var btn = Button.new() btn.text = 'SFX #%02X' % i btn.align = Button.ALIGN_CENTER - btn.set_position(Vector2((i%8)*72, 128 + (i/8)*24)) + btn.set_position(Vector2((i%4)*50, 156 + (i/4)*24)) + btn.rect_min_size.x = 48 add_child(btn) btn.connect('pressed', SoundLoader, 'play_sfx', [i]) sfx_buttons.append(btn)