Remove file header from a .wav file in python3

4.4k Views Asked by At

I have been doing a project on encrypting .wav files using RSA algo and for that, I need to remove the file header to read the file properly. I need the sound data as a numpy array. Now I've searched the web for this and didn't understand what is the file header and how to remove it in python3. Looking forward to suggestions. Thank you.

1

There are 1 best solutions below

5
On BEST ANSWER
binarySound = bytearray()
binaryHeader = bytearray()

with open("a2002011001-e02.wav",'rb') as f:
        binaryHeader = f.read(44)
        binarySound = f.read()

This should be what you're looking for. This will read the first 44 bytes (supposedly the header) into the binaryHeader variable and the rest sound data into the binarySound variable.

To get your music file back you can simply add those two files back together

song = bytearray()

with open("header.bin","rb") as h:
        song = h.read()
        with open("data.bin","rb") as d:
                song += d.read()

with open("new.wav","wb") as f:
        f.write(song)

EDIT: To include the edit in OP for the need of a numpy array:

import numpy

binarySound = {}
binaryHeader = {}

song = {}

with open("a2002011001-e02.wav",'rb') as f:
        buffer = f.read(44)
        binaryHeader = numpy.frombuffer(buffer,dtype=numpy.uint8)
        buffer = f.read()
        binarySound = numpy.frombuffer(buffer,dtype=numpy.uint8)

with open("header.bin","wb") as f:
        f.write(binaryHeader)

with open("data.bin","wb") as f:
        f.write(binarySound)

with open("header.bin","rb") as h:
        song = h.read()
        with open("data.bin","rb") as d:
                song += d.read()

with open("new.wav","wb") as f:
        song = numpy.array(song)
        f.write(song.tobytes())