extends Node const MAX_15B = 1 << 15 const MAX_16B = 1 << 16 func unsigned16_to_signed(unsigned): return (unsigned + MAX_15B) % MAX_16B - MAX_15B func process_sample(mantissa: int, exponent: int) -> int: # For filter arithmetic the samples need to be in signed form. # Sign-extend if mantissa >= 8: mantissa |= 0xFFF0 exponent = min(exponent, 12) 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 := [] 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): return min(max(i, -0x8000), 0x7FFF) func make_sample(rom: File, 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 var samples = [0, 0] # Two zero samples for filter purposes, strip them from the actual output for pkt in (size / 9): 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] match filter: 1: for i in range(samples.size()-8, samples.size()): samples[i] = clamp_short(samples[i] + (samples[i-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) 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) if end: break var audio_data = PoolByteArray() for i in range(2, samples.size()): var b = samples[i] audio_data.push_back(b & 0xFF) audio_data.push_back(b >> 8) audio.data = audio_data return audio const INST_NUM := 35 const INST_BRR_LOOKUP := 0x043C6F const INST_SR := 0x043CD8 const INST_LOOP := 0x043D1E const INST_ADSR := 0x043D64 const SFX_NUM := 8 const SFX_BRR_START := 0x041E3F const SFX_ADSR := 0x041F71 const SFX_SR := 0x041F83 var instrument_samples = [] var sfx_samples = [] func get_inst_sample_data(rom: File, id: int) -> AudioStreamSample: rom.seek(INST_SR + (id*2)) var sample_rate := (rom.get_16() * 32000)/4096 var lookup_offset := INST_BRR_LOOKUP + (id*3) rom.seek(lookup_offset) var brr_offset := rom.get_16() + ((rom.get_8() & 0x3F) << 16) rom.seek(brr_offset) var sample := make_sample(rom, 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 func load_sfx_samples_data(rom: File): var sample_rates = [] rom.seek(SFX_SR) for i in SFX_NUM: sample_rates.append((rom.get_16() * 32000)/4096) rom.seek(SFX_BRR_START) for i in SFX_NUM: print('Loading sfx sample #%X with BRR data offset $%06X' % [i, rom.get_position()]) sfx_samples.append(make_sample(rom, sample_rates[i])) 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): for i in INST_NUM: instrument_samples.append(get_inst_sample_data(rom, i)) load_sfx_samples_data(rom) var player := AudioStreamPlayer.new() # Make one for each channel, later func play_sample(id: int): print('Playing sample #%02X' % id) player.stream = instrument_samples[id] player.play() func play_sfx(id: int): print('Playing sample #%02X' % id) player.stream = sfx_samples[id] player.play() func _ready() -> void: add_child(player) # Called every frame. 'delta' is the elapsed time since the previous frame. #func _process(delta: float) -> void: # pass