diff --git a/scripts/loaders/sound_loader.gd b/scripts/loaders/sound_loader.gd index d04027e..0b42b62 100644 --- a/scripts/loaders/sound_loader.gd +++ b/scripts/loaders/sound_loader.gd @@ -4,11 +4,12 @@ const BGM_NUM := 70 const BGM_LOOKUP := 0x043B97 const INST_NUM := 35 const INST_BRR_LOOKUP := 0x043C6F -const INST_SR := 0x043CD8 -const INST_LOOP := 0x043D1E +const INST_LOOP := 0x043CD8 +const INST_SR := 0x043D1E const INST_ADSR := 0x043D64 const SFX_NUM := 8 -const SFX_BRR_START := 0x041E3F +const SFX_BRR_SPC_TABLE := 0x041F4F + 2 # (first two bytes are the length of 0x0020 = 32 bytes = 4*8) +const SFX_BRR_START := 0x041E3F + 2 # First two bytes are the length of the block, 0x010E = 270 bytes = 16 BRR packets = 480 samples const SFX_ADSR := 0x041F71 const SFX_SR := 0x041F83 var bgm_tracks = [] @@ -51,13 +52,12 @@ func decode_brr(data: PoolByteArray): func clamp_short(i: int): return min(max(i, -0x8000), 0x7FFF) -func make_sample(rom: File, sample_rate: int) -> AudioStreamSample: +func make_sample(rom: File, size: int, sample_rate: int) -> AudioStreamSample: var audio := AudioStreamSample.new() audio.mix_rate = sample_rate audio.stereo = false audio.set_format(AudioStreamSample.FORMAT_16_BITS) - var size := rom.get_16() 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 @@ -80,6 +80,7 @@ func make_sample(rom: File, sample_rate: int) -> AudioStreamSample: 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) if end: + print('End flag on packet') break var audio_data = PoolByteArray() for i in range(2, samples.size()): @@ -97,7 +98,8 @@ func get_inst_sample_data(rom: File, id: int) -> AudioStreamSample: rom.seek(lookup_offset) var brr_offset := read_rom_address(rom) rom.seek(brr_offset) - var sample := make_sample(rom, sample_rate) + var size := rom.get_16() + var sample := make_sample(rom, size, sample_rate) #print_debug('Loaded sample instrument #%02X with lookup offset $%06X, BRR data offset $%06X and length $%04X (%f packets)' % [id, lookup_offset, brr_offset, size, size/9.0]) return sample @@ -106,18 +108,27 @@ func load_sfx_samples_data(rom: File): rom.seek(SFX_SR) for i in SFX_NUM: sample_rates.append((rom.get_16() * 32000)/4096) - rom.seek(SFX_BRR_START) + var brr_spc_addrs = [] + var brr_spc_loop_addrs = [] + rom.seek(SFX_BRR_SPC_TABLE) for i in SFX_NUM: + brr_spc_addrs.append(rom.get_16()) + brr_spc_loop_addrs.append(rom.get_16()) + var brr_spc_start = brr_spc_addrs[0] + for i in SFX_NUM: + brr_spc_addrs[i] += SFX_BRR_START - brr_spc_start + for i in SFX_NUM: + rom.seek(brr_spc_addrs[i]) print('Loading sfx sample #%X with BRR data offset $%06X' % [i, rom.get_position()]) - sfx_samples.append(make_sample(rom, sample_rates[i])) + sfx_samples.append(make_sample(rom, 900, sample_rates[i])) # Use 900 as a limit, it won't be hit, parser stops after End packet anyway print('size of %d samples' % sfx_samples[i].data.size()) # Called when the node enters the scene tree for the first time. func load_samples(rom: File): + load_sfx_samples_data(rom) for i in INST_NUM: instrument_samples.append(get_inst_sample_data(rom, i)) - load_sfx_samples_data(rom)