+1 vote

I have a script that allows the user to select an audio file (ogg or wav) and play it. If this file is outside the project folder, then copy it to the folder project:

var fileName = "res://Audio/SE/%s" % current_file
if !dir.file_exists(fileName):
    current_file = current_file.to_lower()
    dir.copy(path, fileName)

    # TODO: create import

    # PLAY AUDIO
    var stream = load(fileName)
    $MyAudioSreamPlayer.stream = stream
    $MyAudioSreamPlayer.play()

but you can not play it because .import file is missing. Then, how i create this import file to this audio?

Note: .import file is created for this new audio when i click over godot editor main window but i need create it just after line dir.copy(path, fileName) in my script

asked Jul 13, 2019 in Engine by newold (160 points)

Sorry I'm not familiar with importing resources at runtime, however just wanted to let you know that adding files to the project folder after the project has been exported will not work.

3 Answers

–1 vote

my script is a tool used to create a database for a dialog system and is only used while you are creating your game with godot editor.

I have been able to import images using this:

var tex = ImageTexture.new ()
var img = Image.new ()
img.load (fileName)
tex.create_from_image (img) # .import is created

But I do not know how to do it with audio files

preview:
enter image description here

preview tool:
enter image description here

enter image description here

answered Jul 13, 2019 by newold (160 points)

This isn't an answer, is it?

0 votes

hi, i found the way to get audiofiles loaded without importing them:
check this out and accomodate at your needs...

    var path = str(path)
var fname = str(audiofile(id))
var audio = path+"/"+fname

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()
    for i in 200:
        buffer.remove(buffer.size()-1) # removes pop sound at the end
        buffer.remove(0)
    stream.data = buffer
    stream.format = 1 # 16 bit
    stream.mix_rate = 44100
    stream.stereo = true
    file.close()
    var streamer = get_node("/root/.../AudioStreamPlayer")
    streamer.stream = stream
            ....

hope that can help you, in any case, you can contact me ;) bye

answered Sep 27, 2019 by uriel (107 points)

This code simply ignores the wave header, or worse, it even uses the wave header as audio data as well. Since the header is tiny, it only leads to a short noise. This hack only "works" when all assumptions about the wave file are right, and breaks e.g. for all wave files with different bit rates / number of channels. The proper solution is in this proposal: https://github.com/godotengine/godot-proposals/issues/732

0 votes

I had a similar issue and ended up making this: Hope it's useful!
https://github.com/Gianclgar/GDScriptAudioImport

There's a proposal about this issue in github:
https://github.com/godotengine/godot-proposals/issues/732

answered May 23 by Gianclgar (51 points)
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.