Problems when importing audio files in runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gianclgar

I’m trying to make a program/game able to open any audio file located in the filesystem and playing it in loop. I’ve had some issues so I simplified the code in order to save the buffer to a new file and compare them

Here is my code (placed inside a function triggered by a button):

var audio = "/filepath/filename.wav"
var file =
if file.file_exists(audio):, file.READ)
	var buffer = file.get_buffer(file.get_len())
	var stream = = buffer
	stream.format = 1 # 16 bit
	stream.mix_rate = 44100
	stream.stereo = true
	stream.loop_end = buffer.size()
	stream.loop_mode = 1

I’m having these problems:

-Somehow, the new audio file is 44 bytes bigger than the original, and opening it in Audacity reveals a “click” at the beginning that wasn’t in the original file, and it’s also longer.
-When loading the original file into an audio stream via $ = stream and playing it, the click sounds, and it doesn’t loop.

I need to keep the exact same file size because I’ll be working with an audio file that need to be synced, and also the loop has to be clean and stay in time.

I’ve tried loading the files with the editor import settings and it works perfectly, but I need my program to be able to load external files (also files generated from mic input, etc) during runtime.

Audacity reveals a “click” at the beginning that wasn’t in the original file

Is it a single sound wave at the beginning or is it that the sound starts at a certain amplitude which will also cause it to clip, especially if it doesn’t match up to the amplitude of the last sample when looping.

Magso | 2020-04-19 01:00

Is a single sound wave at the beginning of the file, but it appears after saving it to a wav file. The original file starts completely silent. Looks like some “additional data” taken/generated from nowhere.

I’m making a godot project to show the issue, will post it here in a few mins.

Also there could be another way to do what I need to do, but I haven’t found anything yet :confused:


Seems that those extra bytes are the WAV header. It use to be the first 44bytes but can be any other length. Found info here:

Currently looking for a way to parse a WAV in GDScript, as it seems to be no built-in way (maybe i’m wrong)

Gianclgar | 2020-04-19 08:59

:bust_in_silhouette: Reply From: Gianclgar

Well… I finally managed to create a GDScript that somehow parses the .wav header and gets the right data into AudioStreamSample.

Would still be nice to have something like this built-in to be as easy as importing .ogg files, here’s the issue:

you’re a saint for this

zen3001 | 2020-05-15 16:32

Thank youuuuuuu!

fershopls | 2020-06-25 19:48

Thank you so much!! Your code works like a charm. God Bless you!

Harish | 2022-03-05 01:55

How do you import ogg files at runtime?

Aaron Franke | 2022-10-27 02:34