How to play wav file recorded at runtime

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

I would like to create a voice changer app in godot.

I created a minimal recorder in C++ that records mic input into a .wav file. I connected the recorder with GDScript via GDNative interface.

The wav file is output to “user://rec.wav”. The file is generated correctly - I’m able to play it in other audio players and also in godot(from res://).
However, when I try to load the file with load("user://rec.wav") I get the following error ```No loader found for resource: user://rec.wav


I'm using godot 3.0.1 that I've build from sources.

What is correct way of loading wav/pcm that is created during runtime?

I'm new to Godot, so it's possible that I've missed something obvious...but I really first searched the docs and qa before asking here.

[wrap=comment]
can you access file without problem? e.g. does`File.new().file_exists("user://rec.wav")` returns true value?
[wrap=footnote]Bartosz | 2018-03-23 23:01[/wrap]
[/wrap]

[wrap=comment]
Yes, ```file_exists``` returns ```true```
[wrap=footnote]Roman | 2018-03-23 23:32[/wrap]
[/wrap]
:bust_in_silhouette: Reply From: Bartosz

Update:
It is possible to load ogg and wav files during runtime by setting data format``stereo properties of appropriate audiostream object demo for stereo wav:

var file = File.new()
var stream
if file.file_exists(audio_file):
  file.open(audio_file, file.READ)
  var buffer = file.get_buffer(file.get_len())
  stream = AudioStreamSample.new()
  stream.format = AudioStreamSample.FORMAT_16_BITS		
  stream.data = buffer
  stream.stereo = true
  file.close()

old answer:
It looks like this is not possible for now.I’ve created an issue at official repository, if it became resolved I’ll update answer.

I still get the same error.
However, thank you for your answers. So you think that this is the right way and it should be possible to load from user://…

Roman | 2018-03-24 08:33

Sorry, I misinformed you - check my updated answer.

Bartosz | 2018-03-24 22:23

Ok, thank you for your help.
I was hoping I could do most of the stuff from GDScript/GDNative, but I’ll anyway have to create a C++ module for my other required features.

I think I’ll create a custom AudioStream/AudioStreamPlayback that will directly supply mic input to the AudioStreamPlayer. The stream could already save/load audio data as raw pcm.

I’ve asked another question here - I would like to get the processed output once audio effects have been applied. Could you please look at that one too? I don’t think it will be possible from GDScript, but I at least would like to find strategies how to do it from C++ module.

Roman | 2018-03-25 08:57

I’ve started learning Godot only few weeks ago so unfortunately I’m not familiar enough with engine c++ implementation to help with yours outher question.

Bartosz | 2018-03-25 13:17

after all it is possible to load audio files at runtime using gdscript. check updated answer

Bartosz | 2018-03-26 22:13

Perfect, now it works. I just had to strip the wav header, and then it worked.
Thanks again

Roman | 2018-03-27 10:13

Hello, i´m struggling with your same problem, your code works great but i noticed that it only works with some kind of wav files (the ones with a bit rate of 1411 kbps). Other files (higher bit rate) are executed as white sound.
Do you know a way to set bit rate in the audiostreamsample?

Andrea | 2018-07-05 10:06

I am able to play the wav by doing this.
However, the result is very distorted and noisy (pitch and duration are correct), whereas the file that is imported using the editor is clean and clear.
The settings are the same, because I compared what they should be from .import file.
Any idea how I can still improve this?

GForce Productions | 2021-12-25 20:20

:bust_in_silhouette: Reply From: Gianclgar

Check this out: Easily load external audio (WAV) files at run-time · Issue #732 · godotengine/godot-proposals · GitHub