PackedByteArray, Wav files and Headers

Godot Version

4.3

Question

Hello beautiful people. I’m 48, I’ve been in the digital music realm since about age 15. I have amassed an awesome sample library (going back to AKAI discs and more). Finding a media manager that doesn’t crash when I try to feed it my vast library has been impossible. The flaw they all seem to have is attempting to log each and every file internally while scanning/adding directories of sound files. I decided to develop my own and it’s going wonderfully. I’ll be releasing it for free/donation in the next 2 weeks. Prior to that I’ve run into a heck of a problem. It seems that the wav file format has had quite a history and the header (the first 44 bytes) can have a wide variety of configurations. My Godot Media Manager loads samples in realtime which has me reading the file as byte data after ripping it’s head(er) off to avoid any internal confusion. This has broadened my apps ability to load many different formats of wav file, but not all AND I also want to provide an export feature… if I simply pump the byte array of the stream I created during runtime loading to a file then I can only open that file in things like audacity as a RAW data file, but then it plays fine, which means that the audio data itself is being wonderfully preserved, so that means I need to put the head(er) back on the file data when I export. That’s where I’m stuck, I’m trying to learn all that I can about the wav header and manipulating byte data in Godot, but it’s a little outside my skillset at the moment. I was wondering if anyone has had deeper experience with this wav issue, file headers and packedbytearray data manipulation? - In hopes I can pick their brain or find an existing solution. Thank you

I could potentially help, I do have some understanding on wav headers and audio in general.

1 Like

The header is static so we should be able to setup a means to encode values into offsets of a packed data array.

I would make a human readable class that contains all the general information. (This could be organized in many ways)

class Wav extends resource:
  var chnk_desc_word1 = "riff"
  var chnk_desc_size = 1234
  var chnk_desc_word2 = "wave"
  ...
  var data : PackedByteArray

This class can have some helper functions that manipulate the header based on data contents and manipulations.

Then when you go do export it, you take each element and convert it into bytes and append in order into a single PackedByteArray.

The PackedByteArray has many functions of its own.