ChocolateBird/test/audio_system.gd

179 lines
5.9 KiB
GDScript

extends Node2D
#warning-ignore-all:return_value_discarded
signal exit
const MusicPlayer := preload('res://scripts/MusicPlayer.gd')
const MusicRenderer := preload('res://scripts/MusicRenderer.gd')
var MusicLoader := preload('res://scripts/loaders/snes/music_ff5.gd').new()
onready var bgm_titles := Common.load_glyph_table('res://data/5/bgm_titles.txt')
onready var audio_renderer: Node = SoundLoader.audio_renderer
var inst_buttons = []
var sfx_buttons = []
const NUM_CHANNELS := 8
var music_player = null
var inst_sample_map := {}
func _create_sfx_buttons():
var disable_btn := !SoundLoader.has_loaded_audio_samples
var btn_width := 48
var btn_height := 22
var btns_per_row := 8
var inst_pos_offset: Vector2 = $inst_buttons.rect_position
for i in SoundLoader.INST_NUM:
var btn = Button.new()
btn.text = 'Inst #%02d' % i
btn.align = Button.ALIGN_CENTER
btn.set_position(Vector2((i%btns_per_row)*btn_width, (i/btns_per_row)*btn_height) + inst_pos_offset)
btn.rect_min_size.x = btn_width
add_child(btn)
btn.connect('pressed', SoundLoader, 'play_sample', [i])
inst_buttons.append(btn)
btn.disabled = disable_btn
btns_per_row = int($sfx_buttons.rect_size.x/btn_width)
var sfx_pos_offset: Vector2 = $sfx_buttons.rect_position
for i in SoundLoader.SFX_NUM:
var btn = Button.new()
btn.text = 'SFX #%02d' % i
btn.align = Button.ALIGN_CENTER
btn.set_position(Vector2((i%btns_per_row)*btn_width, (i/btns_per_row)*btn_height) + sfx_pos_offset)
btn.rect_min_size.x = btn_width
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
func play_bgm(id: int, live: bool) -> void:
self._stop_all()
if live:
print('@%dms - Playing BGM%02d' % [get_ms(), id])
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_HACK_EXTENDED_LOOPS[inst_idx]
if self.music_player:
remove_child(music_player)
self.music_player = MusicPlayer.new(MusicManager.bgm_tracksets[id], self.inst_sample_map)
add_child(self.music_player)
self.music_player.is_playing = true
else:
# Play prerendered
var tempo_thou = $slider_tempo.value * 10
var key = 'BGM%02d-%04d' % [id, tempo_thou]
MusicManager.play_bgm(key)
func _play_bgm_live() -> void:
self.play_bgm($sb_bgm.value, true)
func _play_bgm_prerendered() -> void:
self.play_bgm($sb_bgm.value, false)
func _create_bgm_playback() -> void:
$sb_bgm.max_value = SoundLoader.BGM_NUM
$sb_bgm.connect('value_changed', self, '_update_bgm_label')
self._update_bgm_label()
$btn_bgm_live.connect('pressed', self, '_play_bgm_live')
$btn_bgm_prerendered.connect('pressed', self, '_play_bgm_prerendered')
$btn_render.connect('pressed', self, 'render_all_bgm')
for i in SoundLoader.SFX_NUM:
self.inst_sample_map[i] = SoundLoader.sfx_samples[i]
func _stop_all() -> void:
if self.music_player:
self.music_player.queue_free()
self.music_player = null
SoundLoader.player.stop()
MusicManager.stop()
func _update_bgm_label(id = 0) -> void:
if id < len(bgm_titles):
$lbl_bgm_title.text = bgm_titles[id]
else:
$lbl_bgm_title.text = ''
func _update_tempo(tempo := 100) -> void:
$lbl_tempo.text = 'Tempo scale: %d' % tempo
var tempo_thou = tempo * 10
MusicManager.update_tempo(tempo_thou)
func _reset_tempo() -> void:
self._update_tempo()
$slider_tempo.value = 100
func _exit() -> void:
self.emit_signal('exit')
func _ready() -> void:
self._create_sfx_buttons()
self._create_bgm_playback()
$btn_stop.connect('pressed', self, '_stop_all')
$slider_tempo.connect('value_changed', self, '_update_tempo')
$btn_reset_tempo.connect('pressed', self, '_reset_tempo')
$btn_exit.connect('pressed', self, '_exit')
var load_start_tick: int
func get_ms(restart: bool = false) -> int:
if restart or not self.load_start_tick:
self.load_start_tick = Time.get_ticks_msec()
return 0
return Time.get_ticks_msec() - self.load_start_tick
func save_bgm_disassembly(bgm_id: int, filename: String = '') -> void:
if not filename:
filename = 'output/BGM%02d disassembly.txt' % bgm_id
var disassembly: PoolStringArray = MusicRenderer.disassemble_bgm(MusicManager.bgm_tracksets[bgm_id], RomLoader.snes_data.bgm_instrument_indices[bgm_id])
var file := File.new()
file.open(filename, File.WRITE)
for line in disassembly:
file.store_line(line)
file.close()
func get_shader_test_pattern() -> PoolByteArray:
var midi_events_bytes := StreamPeerBuffer.new()
var midi_events_bytes2 := StreamPeerBuffer.new()
var midi_events_bytes3 := StreamPeerBuffer.new()
var midi_events_bytes4 := StreamPeerBuffer.new()
for i in 2048:
var t = i * 2.0
midi_events_bytes.put_32(t*32000) # t_start
midi_events_bytes2.put_32((t+1.75)*32000) # t_end
midi_events_bytes3.put_u8(i%35) # instrument
midi_events_bytes3.put_u8(71) # pitch_idx
# midi_events_bytes.put_float((35 + (i%40))) # pitch_idx
midi_events_bytes3.put_u8(255) # velocity
midi_events_bytes3.put_u8(0) # pan
# midi_events_bytes3.put_u8(i%256) # pan
midi_events_bytes4.put_32(0) # ADSR
return midi_events_bytes.data_array + midi_events_bytes2.data_array + midi_events_bytes3.data_array + midi_events_bytes4.data_array