Attention | Topic was automatically imported from the old Question2Answer platform. | |
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 = 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.
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
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)
Gianclgar | 2020-04-19 08:59