+1 vote

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 = File.new()
if file.file_exists(audio):
    file.open(audio, file.READ)
    var buffer = file.get_buffer(file.get_len())
    var stream = AudioStreamSample.new()
    stream.data = 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 $AudioStreamPlayer.stream = 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.

in Engine by (64 points)

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.

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.

Original file vs Saved file after copying data into a buffer
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 :/


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)

1 Answer

+3 votes
Best answer

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:


by (64 points)
selected by

you're a saint for this

Thank youuuuuuu!

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

How do you import ogg files at runtime?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.