DaveTheCoder If it's the parsing, rather than the file I/O, that's consuming the time, then a significant speed-up could be achieved by writing a GDExtension in C or C++, or using .NET Godot with C#.
Its not IO, file gets read instantly. Its the for loop.. it iterates every byte, but in reality i dont need to do that, because when i read byte length and process the data, i need the iterator to be at the last read byte value to continue the next loop.. and that value is dependant on the data size and is variable..
What i need is recursive something.. somehow where i can set the cursor position to the byte index where the next loop needs to start..
So far with updated method i get nice gains.. from 18s to 4s 🙂
Results:
Old meth:
---START---read_wav_decode---
file_start: res://Test_files/sfx/01 - Title Theme (Main Menu).wav
---
RIFF
size: 22557324
format: WAVE
---
fmt
f_size: 18
f_format: 1
f_num_channels: 2
f_sample_rate: 44100
f_byte_rate: 176400
f_block_align: 4
f_bits_per_sample: 16
---
bext
b_size: 602
b_data:
---
LIST
l_size: 66
l_type: INFO
---SUB---
inf_type: IART
inf_size: 11
inf_data: David Wise
---END---
---Elapsed time---: 18.6229999065399s
Improved meth
---START---read_wav_decode_new---
file_start: res://Test_files/sfx/01 - Title Theme (Main Menu).wav
---
RIFF
size: 22557324
format: WAVE
---
fmt
f_size: 18
f_format: 1
f_num_channels: 2
f_sample_rate: 44100
f_byte_rate: 176400
f_block_align: 4
f_bits_per_sample: 16
---
bext
b_size: 602
b_data:
---
LIST
l_size: 66
l_type: INFO
---SUB---
inf_type: IART
inf_size: 11
inf_data: David Wise
---END---
---Elapsed time---: 4.02900004386902s
Code:
func read_wav_decode(wav_file_path:String):
var start_time = Time.get_unix_time_from_system()
var sfx_file = FileAccess.open(wav_file_path,FileAccess.READ)
print("---START---read_wav_decode---")
print("file_start: ",wav_file_path)
while sfx_file.get_position() < sfx_file.get_length():
# Read data
var id_tag:String = sfx_file.get_buffer(4).get_string_from_ascii()
match id_tag:
"RIFF":
var r_buff:PackedByteArray = sfx_file.get_buffer(8)
var r_size:int = r_buff.slice(0,4).decode_u32(0)
var r_format:String = r_buff.slice(4,8).get_string_from_ascii()
print("---")
print(id_tag)
print("size: ",r_size)
print("format: ",r_format)
"fmt ":
var f_buff:PackedByteArray = sfx_file.get_buffer(20)
var f_size:int = f_buff.slice(0,4).decode_u32(0)
var f_format:int = f_buff.slice(4,6)[0]
var f_num_channels:int = f_buff.slice(6,8)[0]
var f_sample_rate:int = f_buff.slice(8,12).decode_u32(0)
var f_byte_rate:int = f_buff.slice(12,16).decode_u32(0)
var f_block_align:int = f_buff.slice(16,18)[0]
var f_bits_per_sample:int = f_buff.slice(18,20)[0]
print("---")
print(id_tag)
print("f_size: ",f_size)
print("f_format: ",f_format)
print("f_num_channels: ",f_num_channels)
print("f_sample_rate: ",f_sample_rate)
print("f_byte_rate: ",f_byte_rate)
print("f_block_align: ",f_block_align)
print("f_bits_per_sample: ",f_bits_per_sample)
"data":
var d_buff:PackedByteArray = sfx_file.get_buffer(4)
var d_size:int = d_buff.slice(0,4).decode_u32(0)
print("---")
print(id_tag)
print("d_size: ",d_size)
"bext":
var b_buff:PackedByteArray = sfx_file.get_buffer(4)
var b_size:int = b_buff.slice(0,4).decode_u32(0)
var b_data:String = sfx_file.get_buffer(4+b_size).get_string_from_ascii()
print("---")
print(id_tag)
print("b_size: ",b_size)
print("b_data: ",b_data)
"iXML":
var i_buff:PackedByteArray = sfx_file.get_buffer(4)
var i_size:int = i_buff.slice(0,4).decode_u32(0)
var i_data:String = sfx_file.get_buffer(4+i_size).get_string_from_ascii()
print("---")
print(id_tag)
print("i_size: ",i_size)
print("i_data: ",i_data)
"LIST":
var l_buff:PackedByteArray = sfx_file.get_buffer(8)
var l_size:int = l_buff.slice(0,4).decode_u32(0)
var l_type:String = l_buff.slice(4,8).get_string_from_ascii()
var l_data_buff:PackedByteArray = sfx_file.get_buffer(l_size)
print("---")
print(id_tag)
print("l_size: ",l_size)
print("l_type: ",l_type)
match l_type:
"INFO":
var inf_type:String = l_data_buff.slice(0,4).get_string_from_ascii()
var inf_size:int = l_data_buff.slice(4,8).decode_u32(0)
var inf_data:String = l_data_buff.slice(8,l_data_buff.size()).get_string_from_ascii()
print("---SUB---")
print("inf_type: ",inf_type)
print("inf_size: ",inf_size)
print("inf_data: ",inf_data)
var end_time = Time.get_unix_time_from_system()
var time_diff = end_time - start_time
print("---END---")
print("---Elapsed time---: ",time_diff,"s")
func read_wav_decode_new(wav_file_path:String):
var start_time = Time.get_unix_time_from_system()
var sfx_file = FileAccess.open(wav_file_path,FileAccess.READ)
var buff_file:PackedByteArray = sfx_file.get_buffer(sfx_file.get_length())
var offset:int = 0
print("---START---read_wav_decode_new---")
print("file_start: ",wav_file_path)
for i in range(0,buff_file.size()):
# Read data
if i < offset:
continue
offset+=4
var id_tag:String =buff_file.slice(i,offset).get_string_from_ascii()
match id_tag:
"RIFF":
var r_buff:PackedByteArray = buff_file.slice(offset,offset+8)
var r_size:int = r_buff.slice(0,4).decode_u32(0)
var r_format:String = r_buff.slice(4,8).get_string_from_ascii()
print("---")
print(id_tag)
print("size: ",r_size)
print("format: ",r_format)
offset+=8
"fmt ":
var f_buff:PackedByteArray = buff_file.slice(offset,offset+20)
var f_size:int = f_buff.slice(0,4).decode_u32(0)
var f_format:int = f_buff.slice(4,6)[0]
var f_num_channels:int = f_buff.slice(6,8)[0]
var f_sample_rate:int = f_buff.slice(8,12).decode_u32(0)
var f_byte_rate:int = f_buff.slice(12,16).decode_u32(0)
var f_block_align:int = f_buff.slice(16,18)[0]
var f_bits_per_sample:int = f_buff.slice(18,20)[0]
print("---")
print(id_tag)
print("f_size: ",f_size)
print("f_format: ",f_format)
print("f_num_channels: ",f_num_channels)
print("f_sample_rate: ",f_sample_rate)
print("f_byte_rate: ",f_byte_rate)
print("f_block_align: ",f_block_align)
print("f_bits_per_sample: ",f_bits_per_sample)
offset+=20
"data":
var d_buff:PackedByteArray = buff_file.slice(offset,offset+4)
var d_size:int = d_buff.decode_u32(0)
print("---")
print(id_tag)
print("d_size: ",d_size)
offset+=4
"bext":
var b_buff:PackedByteArray = buff_file.slice(offset,offset+4)
var b_size:int = b_buff.decode_u32(0)
var b_data:String = buff_file.slice(offset+4,offset+4+b_size).get_string_from_ascii()
print("---")
print(id_tag)
print("b_size: ",b_size)
print("b_data: ",b_data)
offset+=4+b_size
"iXML":
var i_buff:PackedByteArray = buff_file.slice(offset,offset+4)
var i_size:int = i_buff.decode_u32(0)
var i_data:String = buff_file.slice(offset+4,offset+4+i_size).get_string_from_ascii()
print("---")
print(id_tag)
print("i_size: ",i_size)
print("i_data: ",i_data)
offset+=4+i_size
"LIST":
var l_buff:PackedByteArray = buff_file.slice(offset,offset+8)
var l_size:int = l_buff.slice(0,4).decode_u32(0)
var l_type:String = l_buff.slice(4,8).get_string_from_ascii()
var l_data_buff:PackedByteArray = buff_file.slice(offset+8,offset+8+l_size)
print("---")
print(id_tag)
print("l_size: ",l_size)
print("l_type: ",l_type)
offset+=8+l_size
match l_type:
"INFO":
var inf_type:String = l_data_buff.slice(0,4).get_string_from_ascii()
var inf_size:int = l_data_buff.slice(4,8).decode_u32(0)
var inf_data:String = l_data_buff.slice(8,l_data_buff.size()).get_string_from_ascii()
print("---SUB---")
print("inf_type: ",inf_type)
print("inf_size: ",inf_size)
print("inf_data: ",inf_data)
var end_time = Time.get_unix_time_from_system()
var time_diff = end_time - start_time
print("---END---")
print("---Elapsed time---: ",time_diff,"s")
Note here:
As you can see below here i wait for the iterator to catch up with the desired offset to continue.
for i in range(0,buff_file.size()):
# Read data
if i < offset:
continue
I havent come up with a better way to loop with given position..