AudioStreamPlayer.stream.data conversion from PoolByteArray to float samples

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

Is anyone familiar with the AudioStreamPlayer? I’m trying to extract audio amplitude from wav files and found that I can access the raw audio data in AudioStreamPlayer.stream.data, but it’s stored in a PoolByteArray. I need to convert this into a format where I can read the sample values (probably float?)…maybe PoolRealArray, but that doesn’t seem to be exposed to GDScript. StreamPeerBuffer is supposed to provide array conversion, but I don’t understand how to use it. I wrote a successful mock-up of this concept in RayLib, but the audio samples there are already provided as a float array. I can calculate a play head position with sufficient accuracy if I can figure out how to convert the byte data to actual samples.

:bust_in_silhouette: Reply From: shinyclaw

Hi, you can use StreamPeerBuffer. Just feed it with the data you have in AudioStreamPlayer.stream.data:

var buf = StreamPeerBuffer.new()
buf.data_array = AudioStreamPlayer.stream.data

and then get the values in any way you want: buf.get_float(), buf.get_u32(), etc.