import argparse, json, wave

def inspect(path):
    with wave.open(str(path), "rb") as w:
        channels, width, rate, frames, comp, _ = w.getparams()
        expected = frames * channels * width
        actual = 0
        while True:
            block = w.readframes(65536)
            if not block:
                break
            actual += len(block)
    return {"channels":channels,"sample_width_bytes":width,"sample_rate_hz":rate,
      "header_frames":frames,"duration_from_header_seconds":frames/rate,
      "expected_pcm_bytes":expected,"read_pcm_bytes":actual,
      "frame_aligned":actual % (channels*width) == 0,
      "header_data_length_matches":actual == expected,"compression":comp}

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("file")
    args = parser.parse_args()
    try:
        print(json.dumps(inspect(args.file), indent=2))
    except (wave.Error, EOFError, OSError) as exc:
        raise SystemExit(f"WAV could not be checked: {type(exc).__name__}")
