Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions GDScriptAudioImport.gd → gdscript_audio_import/audioLoader.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#GDScriptAudioImport v0.1

#MIT License
Expand Down Expand Up @@ -47,18 +48,16 @@ func report_errors(err, filepath):
else:
print("Unknown error with file ", filepath, " error code: ", err)

func loadfile(filepath):
var file = File.new()
var err = file.open(filepath, File.READ)
if err != OK:
report_errors(err, filepath)
file.close()
return AudioStreamSample.new()
func loadfile(filepath,loop=false):
var file = FileAccess.open(filepath, FileAccess.READ)
if file == null:
report_errors(FileAccess.get_open_error(), filepath)
return null

var bytes = file.get_buffer(file.get_len())
var bytes = file.get_buffer(file.get_length())
# if File is wav
if filepath.ends_with(".wav"):
var newstream = AudioStreamSample.new()
if filepath.to_lower().ends_with(".wav"):
var newstream = AudioStream.new()

#---------------------------
#parrrrseeeeee!!! :D
Expand Down Expand Up @@ -134,7 +133,7 @@ func loadfile(filepath):
var data_entry_point = (i+8)
print ("Audio data starts at byte " + str(data_entry_point))

var data = bytes.subarray(data_entry_point, data_entry_point+audio_data_size-1)
var data = bytes.slice(data_entry_point, data_entry_point+audio_data_size-1)

if bits_per_sample in [24, 32]:
newstream.data = convert_to_16bit(data, bits_per_sample)
Expand All @@ -150,20 +149,22 @@ func loadfile(filepath):
#get samples and set loop end
var samplenum = newstream.data.size() / 4
newstream.loop_end = samplenum
newstream.loop_mode = 1 #change to 0 or delete this line if you don't want loop, also check out modes 2 and 3 in the docs

#change to 0 or delete this line if you don't want loop, also check out modes 2 and 3 in the docs
#(Walmart) I don't really know what this actually does sooo... I've defaulted it to 1 for true and 0 for false
newstream.loop_mode = 1 if loop else 0
return newstream #:D

#if file is ogg
elif filepath.ends_with(".ogg"):
var newstream = AudioStreamOGGVorbis.new()
newstream.loop = true #set to false or delete this line if you don't want to loop
newstream.data = bytes
elif filepath.to_lower().ends_with(".ogg"):
var newstream = AudioStreamOggVorbis.load_from_file(filepath)
newstream.loop = loop
return newstream

#if file is mp3
elif filepath.ends_with(".mp3"):
elif filepath.to_lower().ends_with(".mp3"):
var newstream = AudioStreamMP3.new()
newstream.loop = true #set to false or delete this line if you don't want to loop
newstream.loop = loop
newstream.data = bytes
return newstream

Expand All @@ -181,9 +182,9 @@ func loadfile(filepath):
# And the 32bit case abour 50% slower
# I don't wanna risk it always being slower on other files
# And really, the solution would be to handle it in a low-level language
func convert_to_16bit(data: PoolByteArray, from: int) -> PoolByteArray:
func convert_to_16bit(data: PackedByteArray, from: int) -> PackedByteArray:
print("converting to 16-bit from %d" % from)
var time = OS.get_ticks_msec()
var time = Time.get_ticks_msec()
# 24 bit .wav's are typically stored as integers
# so we just grab the 2 most significant bytes and ignore the other
if from == 24:
Expand All @@ -200,13 +201,13 @@ func convert_to_16bit(data: PoolByteArray, from: int) -> PoolByteArray:
var single_float: float
var value: int
for i in range(0, data.size(), 4):
spb.data_array = data.subarray(i, i+3)
spb.data_array = data.slice(i, i+3)
single_float = spb.get_float()
value = single_float * 32768
data[i/2] = value
data[i/2+1] = value >> 8
data.resize(data.size() / 2)
print("Took %f seconds for slow conversion" % ((OS.get_ticks_msec() - time) / 1000.0))
print("Took %f seconds for slow conversion" % ((Time.get_ticks_msec() - time) / 1000.0))
return data


Expand Down
1 change: 1 addition & 0 deletions gdscript_audio_import/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions gdscript_audio_import/icon.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bdlayvv26bwt0"
path="res://.godot/imported/icon.svg-8244fdb92a09216e260599710de0d61b.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/gdscript_audio_import/icon.svg"
dest_files=["res://.godot/imported/icon.svg-8244fdb92a09216e260599710de0d61b.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
7 changes: 7 additions & 0 deletions gdscript_audio_import/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="gdscript_audio_import"
description=""
author=""
version=""
script="plugin.gd"
2 changes: 2 additions & 0 deletions gdscript_audio_import/plugin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tool
extends EditorPlugin