ChocolateBird/test/audio_system.gd

251 lines
8.9 KiB
GDScript3
Raw Normal View History

2023-07-27 19:58:29 +09:30
extends Node2D
#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()
2024-07-10 22:13:58 +09:30
onready var bgm_titles := Common.load_glyph_table('res://data/5/bgm_titles.txt')
onready var audio_renderer := $'%audio_renderer'
onready var audio_player := $audio_player
var prerendered_bgms := {}
var prerendered_bgm_start_and_end_loops := {}
2023-07-27 19:58:29 +09:30
var inst_buttons = []
var sfx_buttons = []
2024-07-10 22:13:58 +09:30
var initialized_instrument_texture := false
var queued_bgm_playback := ''
const NUM_CHANNELS := 8
var music_player = null
var inst_sample_map := {}
var bgm_tracksets := {}
2023-07-27 19:58:29 +09:30
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
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
2024-07-10 22:13:58 +09:30
func play_bgm(id: int, live: bool) -> void:
self._stop_all()
if live:
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:
2024-07-10 22:13:58 +09:30
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(bgm_tracksets[id], self.inst_sample_map)
add_child(self.music_player)
self.music_player.is_playing = true
else:
# Play prerendered
var bgm_key = 'BGM%02d'%id
if bgm_key in self.prerendered_bgms:
self.audio_player.stream = self.prerendered_bgms[bgm_key]
self.audio_player.play()
else:
self.queue_prerender_bgm(id)
self.queued_bgm_playback = bgm_key
2023-08-23 17:26:16 +09:30
print('Playing BGM%02d' % id)
2024-07-10 22:13:58 +09:30
func _play_bgm_live() -> void:
self.play_bgm($sb_bgm.value, true)
func _play_bgm_prerendered() -> void:
self.play_bgm($sb_bgm.value, false)
2023-08-23 17:26:16 +09:30
func _create_bgm_playback() -> void:
$sb_bgm.max_value = SoundLoader.BGM_NUM
2024-07-10 22:13:58 +09:30
$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')
2023-08-23 17:26:16 +09:30
for i in SoundLoader.SFX_NUM:
self.inst_sample_map[i] = SoundLoader.sfx_samples[i]
for i in SoundLoader.BGM_NUM:
evaluate_bgm(i)
2024-07-10 22:13:58 +09:30
func _stop_all() -> void:
if self.music_player:
self.music_player.queue_free()
self.music_player = null
SoundLoader.player.stop()
2024-07-10 22:13:58 +09:30
self.audio_player.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 = ''
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()
$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]
2024-07-10 22:13:58 +09:30
# print('BGM 0x%02X (%02d) at 0x%06X' % [i, i, pointer])
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 initialize_instrument_texture() -> void:
get_ms(true)
SoundLoader.samples_to_texture()
audio_renderer.material.set_shader_param('instrument_samples', SoundLoader.samples_tex)
audio_renderer.material.set_shader_param('instrument_samples_size', SoundLoader.samples_tex.get_size())
self.initialized_instrument_texture = true
print('@%dms - Initialized instrument samples texture' % get_ms())
func queue_prerender_bgm(bgm_id: int) -> void:
if not self.initialized_instrument_texture:
self.initialize_instrument_texture()
var mp = MusicPlayer.new(bgm_tracksets[bgm_id], self.inst_sample_map)
var data_and_target_time_and_loops = mp.render_channels(0, 540, RomLoader.snes_data.bgm_instrument_indices[bgm_id])
var data = data_and_target_time_and_loops[0]
var target_time = data_and_target_time_and_loops[1]
var target_samples = target_time * 32000
var target_rows = ceil(target_samples/4096.0)
var bgm_key := 'BGM%02d'%bgm_id
audio_renderer.push_bytes(data, target_rows, bgm_key)
self.prerendered_bgm_start_and_end_loops[bgm_key] = data_and_target_time_and_loops[2]
func render_all_bgm(bgms_to_render: int = 64) -> void:
self.initialize_instrument_texture()
for bgm_id in bgms_to_render:
self.queue_prerender_bgm(bgm_id)
if bgm_id % 10 == 9 and bgm_id < bgms_to_render-1:
print('@%dms - Processed %d/%d bgm tracks' % [get_ms(), bgm_id+1, bgms_to_render])
print('@%dms - Processed %d bgm tracks and sent to gpu for rendering' % [get_ms(), bgms_to_render])
const save_prerendered_audio := false
func _get_prerendered_audio():
audio_renderer.get_result()
2024-07-14 23:34:23 +09:30
while audio_renderer.result_queue:
var result = audio_renderer.result_queue.pop_front()
var desc = result[0]
var rendered_audio := AudioStreamSample.new()
rendered_audio.data = result[1]
rendered_audio.stereo = true
rendered_audio.mix_rate = 32000
rendered_audio.format = AudioStreamSample.FORMAT_16_BITS
if prerendered_bgm_start_and_end_loops[desc][0] >= 0:
rendered_audio.loop_begin = int(round(prerendered_bgm_start_and_end_loops[desc][0]))
rendered_audio.loop_end = int(round(prerendered_bgm_start_and_end_loops[desc][1]))
rendered_audio.loop_mode = AudioStreamSample.LOOP_FORWARD
self.prerendered_bgms[desc] = rendered_audio
if save_prerendered_audio:
var error = rendered_audio.save_to_wav('output/rendered_%s.wav'%desc)
print('@%dms - Saved render of %s (error code %s)' % [get_ms(), desc, globals.ERROR_CODE_STRINGS[error]])
else:
print('@%dms - Rendered %s without saving' % [get_ms(), desc])
if self.queued_bgm_playback == desc:
self.audio_player.stream = rendered_audio
self.audio_player.play()
self.queued_bgm_playback = ''
2024-07-10 22:13:58 +09:30
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
func _process(_delta):
update()
func _draw() -> void:
if audio_renderer.waiting_for_viewport:
self._get_prerendered_audio()