0 votes

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
    file.close()
    stream.save_to_wav("/filepath/filename_saved.wav")

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.

asked Apr 18 in Projects by Gianclgar (51 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 :/

Edit:

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:
http://truelogic.org/wordpress/2015/09/04/parsing-a-wav-file-in-c/

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

+2 votes
Best answer

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

https://github.com/Gianclgar/GDScriptAudioImport

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

https://github.com/godotengine/godot-proposals/issues/732

answered Apr 20 by Gianclgar (51 points)
selected May 23 by Gianclgar

you're a saint for this

Thank youuuuuuu!

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 How to use this Q&A? before posting your first questions.