2023-07-27 19:58:29 +09:30
|
|
|
extends Node2D
|
2023-12-08 21:11:47 +10:30
|
|
|
#warning-ignore-all:return_value_discarded
|
2023-08-23 17:26:16 +09:30
|
|
|
const MusicPlayer := preload('res://scripts/MusicPlayer.gd')
|
|
|
|
var MusicLoader := preload('res://scripts/loaders/snes/music_ff5.gd').new()
|
2023-07-27 19:58:29 +09:30
|
|
|
var inst_buttons = []
|
|
|
|
var sfx_buttons = []
|
|
|
|
|
|
|
|
func _create_sfx_buttons():
|
|
|
|
var disable_btn := !SoundLoader.has_loaded_audio_samples
|
|
|
|
for i in SoundLoader.INST_NUM:
|
|
|
|
var btn = Button.new()
|
2023-08-23 19:38:59 +09:30
|
|
|
btn.text = 'Inst #%02X' % i
|
2023-07-27 19:58:29 +09:30
|
|
|
btn.align = Button.ALIGN_CENTER
|
2023-08-23 19:38:59 +09:30
|
|
|
btn.set_position(Vector2((i%7)*50, (i/7)*24))
|
|
|
|
btn.rect_min_size.x = 48
|
2023-07-27 19:58:29 +09:30
|
|
|
add_child(btn)
|
|
|
|
btn.connect('pressed', SoundLoader, 'play_sample', [i])
|
|
|
|
inst_buttons.append(btn)
|
|
|
|
btn.disabled = disable_btn
|
|
|
|
for i in SoundLoader.SFX_NUM:
|
|
|
|
var btn = Button.new()
|
|
|
|
btn.text = 'SFX #%02X' % i
|
|
|
|
btn.align = Button.ALIGN_CENTER
|
2023-08-23 17:26:16 +09:30
|
|
|
btn.set_position(Vector2((i%4)*50, 130 + (i/4)*24))
|
2023-08-23 19:38:59 +09:30
|
|
|
btn.rect_min_size.x = 48
|
2023-07-27 19:58:29 +09:30
|
|
|
add_child(btn)
|
|
|
|
btn.connect('pressed', SoundLoader, 'play_sfx', [i])
|
|
|
|
sfx_buttons.append(btn)
|
|
|
|
btn.disabled = disable_btn
|
|
|
|
if disable_btn:
|
|
|
|
SoundLoader.connect('audio_samples_loaded', self, '_enable_sfx_buttons')
|
|
|
|
SoundLoader.connect('audio_inst_sample_loaded', self, '_enable_inst_button')
|
|
|
|
SoundLoader.connect('audio_sfx_sample_loaded', self, '_enable_sfx_button')
|
|
|
|
|
|
|
|
func _enable_sfx_buttons():
|
|
|
|
for btn in sfx_buttons:
|
|
|
|
btn.disabled = false
|
|
|
|
for btn in inst_buttons:
|
|
|
|
btn.disabled = false
|
|
|
|
|
|
|
|
func _enable_sfx_button(id: int):
|
|
|
|
# NB: This assumes sequential loading (may change, probably won't)
|
|
|
|
for i in id+1:
|
|
|
|
sfx_buttons[i].disabled = false
|
|
|
|
|
|
|
|
func _enable_inst_button(id: int):
|
|
|
|
# NB: This assumes sequential loading (may change, probably won't)
|
|
|
|
for i in id+1:
|
|
|
|
inst_buttons[i].disabled = false
|
|
|
|
|
2023-08-23 17:26:16 +09:30
|
|
|
const NUM_CHANNELS := 8
|
|
|
|
var music_player = null
|
|
|
|
var inst_sample_map := {}
|
|
|
|
var bgm_tracksets := {}
|
|
|
|
|
|
|
|
func evaluate_bgm(id: int):
|
|
|
|
var buffer: StreamPeerBuffer = RomLoader.snes_buffer.duplicate()
|
|
|
|
var bgm_song_ptr: int = RomLoader.snes_data.bgm_song_pointers[id] & 0x3FFFFF
|
|
|
|
var bank_offset: int = bgm_song_ptr & 0x3F0000
|
|
|
|
buffer.seek(bgm_song_ptr)
|
|
|
|
var length := buffer.get_u16()
|
|
|
|
var rom_address_base := buffer.get_u16()
|
|
|
|
var track_ptrs := PoolIntArray()
|
|
|
|
for i in NUM_CHANNELS:
|
|
|
|
var track_ptr := buffer.get_u16() + bank_offset
|
|
|
|
if track_ptr < bgm_song_ptr:
|
|
|
|
track_ptr += 0x010000 # next bank
|
|
|
|
track_ptrs.append(track_ptr)
|
|
|
|
var end_ptr := buffer.get_u16() + bank_offset
|
|
|
|
if end_ptr < bgm_song_ptr:
|
|
|
|
end_ptr += 0x010000 # next bank
|
|
|
|
var tracks := []
|
|
|
|
for i in NUM_CHANNELS:
|
|
|
|
var track_ptr := track_ptrs[i]
|
|
|
|
# var channel: MusicChannel = self.channels[i]
|
|
|
|
# print('Unrolling BGM track %02d:%02d at 0x%06X:0x%06X:0x%06X' % [id, i, bgm_song_ptr, track_ptr, end_ptr])
|
|
|
|
tracks.append(MusicLoader.unroll_track(buffer.duplicate(), bgm_song_ptr, track_ptr, end_ptr, '%02d:%02d'%[id, i]))
|
|
|
|
bgm_tracksets[id] = tracks
|
|
|
|
|
|
|
|
func play_bgm(id: int) -> void:
|
|
|
|
var inst_indices = RomLoader.snes_data.bgm_instrument_indices[id]
|
|
|
|
for i in 16:
|
|
|
|
var inst_idx: int = inst_indices[i]-1
|
|
|
|
if inst_idx < 0:
|
|
|
|
self.inst_sample_map[i + 0x20] = null
|
|
|
|
else:
|
|
|
|
self.inst_sample_map[i + 0x20] = SoundLoader.instrument_samples[inst_idx]
|
|
|
|
if self.music_player:
|
|
|
|
remove_child(music_player)
|
|
|
|
self.music_player = MusicPlayer.new(bgm_tracksets[id], self.inst_sample_map)
|
|
|
|
add_child(self.music_player)
|
|
|
|
self.music_player.is_playing = true
|
|
|
|
print('Playing BGM%02d' % id)
|
|
|
|
|
|
|
|
func _play_bgm() -> void:
|
|
|
|
self.play_bgm($sb_bgm.value)
|
|
|
|
|
|
|
|
func _create_bgm_playback() -> void:
|
|
|
|
$sb_bgm.max_value = SoundLoader.BGM_NUM
|
|
|
|
$btn_bgm.connect('pressed', self, '_play_bgm')
|
|
|
|
for i in SoundLoader.SFX_NUM:
|
|
|
|
self.inst_sample_map[i] = SoundLoader.sfx_samples[i]
|
|
|
|
for i in SoundLoader.BGM_NUM:
|
|
|
|
evaluate_bgm(i)
|
|
|
|
|
2023-12-05 23:18:27 +10:30
|
|
|
func _stop_all() -> void:
|
|
|
|
if self.music_player:
|
|
|
|
self.music_player.queue_free()
|
|
|
|
self.music_player = null
|
|
|
|
SoundLoader.player.stop()
|
2023-08-23 17:26:16 +09:30
|
|
|
|
2023-07-27 19:58:29 +09:30
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready() -> void:
|
2023-08-23 17:26:16 +09:30
|
|
|
self._create_sfx_buttons()
|
|
|
|
self._create_bgm_playback()
|
2023-12-05 23:18:27 +10:30
|
|
|
$btn_stop.connect('pressed', self, '_stop_all')
|
2023-08-23 17:26:16 +09:30
|
|
|
for i in len(RomLoader.snes_data.bgm_song_pointers):
|
|
|
|
var pointer = RomLoader.snes_data.bgm_song_pointers[i]
|
|
|
|
print('BGM 0x%02X (%02d) at 0x%06X' % [i, i, pointer])
|