# cPy - print the header information of a wav file
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-12-22  initial release

# usage: python3.12 wav_header train_crossing.wav
# see: https://en.wikipedia.org/wiki/WAV

import  os;             os.system('clear')
import  sys
from    struct  import  unpack

# ============================ main ==============================

# for VSC debugging: Run and Debug | Create a launch.json file Python Debugger |
#    Python file with arguments | "args": ["train_crossing.wav"], | Debug run F5
def main():
    assert len(sys.argv) == 2
    fn = sys.argv[1]
    with open(fn, 'rb') as f:
        header = f.read(44)
    unpacked = unpack('4si4s4sihhiihh4si', header)
    print('Header of *.wav file:', fn, '\n')
    print(f'FileTypeBlocID   {unpacked[ 0].decode()}')
    print(f'FileSize         {unpacked[ 1]}')
    print(f'FileFormatID     {unpacked[ 2].decode()}')
    print()
    print(f'FormatBlocID     {unpacked[ 3].decode()}')
    print(f'BlockSize        {unpacked[ 4]}')
    print(f'AudioFormat      {unpacked[ 5]}')
    print(f'NbrChannels      {unpacked[ 6]}')
    print(f'Frequency        {unpacked[ 7]}')
    print(f'BytePerSec       {unpacked[ 8]}')
    print(f'BytePerBloc      {unpacked[ 9]}')
    print(f'BitsPerSample    {unpacked[10]}')
    print()
    print(f'DataBlocID       {unpacked[11].decode()}')
    print(f'DataSize         {unpacked[12]}\n')


main()

# ============================ end ===============================
