How to generate a waveform preview of an audio file imported at runtime

Godot Version

4.4.1

Question

How do i get loudness data from an audio file to make an image like this
image
The audio files are imported at runtime btw

I tried doing stream.data and getting loudness from that but i get only values 255.0 or 0.0

does anyone have a good solution?

You should be able to get the data from the stream, in the form of a PackedByteArray. You’ll also need to either look at the format (or just know it) to interpret that data; probably it’s 16 bit data, so you likely need to take the bytes in pairs.

Assuming you’re using something uncompressed like .wav, of course. I’m not sure what you’ll get if you’re using something like .ogg or .mp3, since those are decompressed on the fly.

1 Like

Yeah I was struggling especially with chatgpt bcs i forgot to tell him it’s audio files imported at runtime

If anyone needs the solution in the future you can only use AudioStreamWAV which has the .get_data() function for files imported at runtime

Then there’s some differences for processing it depending on the sample rate (samples/second), stereo/mono (stereo has 2x samples, which are grouped L, R, L, R…) and if it’s 16-bit a sample contains 2 8-bit components which u need to combine into a single 16-bit value

If u need more help I can share some code from chatgpt but honestly I didn’t look at it much as i was way too tired atp

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.