godot icon indicating copy to clipboard operation
godot copied to clipboard

AudioStream(Playback)WAV: Use Vectors instead of pointers

Open DeeJayLSP opened this issue 1 year ago • 2 comments
trafficstars

Changes the types of (AudioStreamPlaybackWAV::qoa.dec and AudioStreamWAV::data) to use Vectors instead of pointers. This was done with AudioStreamMP3's data between versions 3.x and 4.x of Godot.

Only private variables and function structures were modified, without altering return type, therefore does not break compatibility.

Alongside #95463, this PR makes the AudioStreamWAV and AudioStreamPlaybackWAV classes free of direct memalloc/free calls, and consequently the only memalloc/free calls within the /scene directory.

DeeJayLSP avatar Aug 23 '24 23:08 DeeJayLSP

Does anyone know if using Vector instead of pointers causes audio corruption?

I don't understand why AudioStreamMP3 doesn't match AudioStreamWAV and AudioStreamPlaybackWAV.

fire avatar Aug 26 '24 18:08 fire

Does anyone know if using Vector instead of pointers causes audio corruption?

Most of audio playback works in two ways.

In case of uncompressed (WAV 8/16 bit) it simply copies samples from the data, converts into 32 bit float, then goes into the audio buffer. And with compressed (MP3, QOA, IMA ADPCM) the data goes through a decode process before being sent to the buffer. All access uses some kind of uint8_t pointer.

The only change with this PR is how data is stored in memory. Everything is passed to buffer/decoders using the Vector's pointer anyway, with the advantage of not having explicit memalloc/memfree calls within the class.

Shouldn't cause corruption at all.

DeeJayLSP avatar Aug 26 '24 19:08 DeeJayLSP

One thing I noticed: p_src in do_resample() is essentially the same as base->data + AudioStreamWAV::DATA_PAD, so I changed the code to use it directly.

DeeJayLSP avatar Aug 28 '24 21:08 DeeJayLSP

Rebased on top of master following #95463 merge.

DeeJayLSP avatar Aug 30 '24 13:08 DeeJayLSP

One last force push. Forgot to set all values in the vector to 0 when setting data, this was causing an audible pop when looping a 16-bit stream.

DeeJayLSP avatar Sep 08 '24 05:09 DeeJayLSP

Thanks!

akien-mga avatar Sep 08 '24 21:09 akien-mga