wavfile.write('RIFF'.encode())# Marks the file as a riff file. Characters are each 1 byte long
wavfile.write(struct.pack('I',data_size+header_size))# Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically you’d fill this in after creation
wavfile.write('WAVE'.encode())# File Type Header. For our purposes, it always equals “WAVE”
# FMT HEADER
wavfile.write('fmt '.encode())# Format chunk marker. Includes trailing null (tr: Space works, null don't)
wavfile.write(struct.pack('I',16))# Length of format data as listed above
# FMT DATA
wavfile.write(struct.pack('H',1))# Type of format (1 is PCM) - 2 byte integer
wavfile.write(struct.pack('H',channels))# Number of Channels - 2 byte integer
wavfile.write(struct.pack('I',sample_rate))# Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz