seeReality23
seeReality23

Reputation: 23

Size of header in wav file in order to strip it out

I need to strip out the header from wav files. However, they can be either 44,46, or literally any number of bytes greater than that. How do I consistently remove the header from a wav file?

Upvotes: 2

Views: 2932

Answers (2)

jaket
jaket

Reputation: 9341

The only way to consistently do anything is to first understand it. The WAV file specification defines the contents of a header and understanding that you'd have all of the information necessary to strip it. Believe it or not, nearly every software program that reads a WAV file is able to consistently find the data.

Short of taking the time to understand the format, the audio data is preceded by the 4 bytes 'd','a','t','a' and a 4 byte length. You could just search for the text, skip 4 more bytes and find the data. Be warned though that without examining the skipped information you may not have the information necessary to interpret the audio data.

If you are interested in understanding a bit about the format, have a look at this link: http://soundfile.sapp.org/doc/WaveFormat/

Upvotes: 2

VC.One
VC.One

Reputation: 15906

Since the PCM (audio) data always begins after these bytes :

64 61 74 61 

Maybe you can search for the above and then select their position as the "end" part of your "remove the header" range?

Pseudo-code for a search within your byte Array (or file reader)...

//# where... var NAME : DATA TYPE = VALUE;

Try this logic in your programing language.

var myWAVbytes : ByteArray = your_file_Bytes_with_header; //# WAV data
var byteVal : Int = -1; //# stores value of a byte
var posVal : Int = 0; //# stores position (offset) of a byte

while (true)
{
    byteVal = myWAVbytes[ posVal ]; //read value at this offset

    if( byteVal == 0x64) //# check for beginning "64" byte
    {
        //if issues also try as: posVal-1 ...in case offset is ahead by one
        byteVal = myWAVbytes[ posVal ].readInteger32(); //# check 4 byte sequence of: "64 61 74 61"

        if( byteVal == 0x64617461 ) //# found "data" sequence bytes
        {
            Print_Log( "0x64617461 data begins at : " + posVal );
            break;
        }
    }

    posVal++; //# move forward through bytes    
}

Alternate version with manual checking (no readInteger type option used)...

while (true)
{
    byteVal = myWAVbytes[ posVal ]; //read value at this offset

    if( byteVal == 0x64) //# check for beginning "64" byte
    {
        if( (myWAVbytes[posVal] == 0x64) && (myWAVbytes[posVal+1] == 0x61) && (myWAVbytes[posVal+2] == 0x74) && (myWAVbytes[posVal+3] == 0x61) ) 
        {
            //# found "data" sequence bytes
            Print_Log( "0x64617461 data begins at : " + posVal );
            break;
        }
    }

    posVal++; //# move forward through bytes    
}

Hope it helps.

Upvotes: 1

Related Questions