# Buggy code
def _read_fmt_chunk(fid, is_big_endian):
    """
    Returns
    -------
    size : int
        size of format subchunk in bytes (minus 8 for "fmt " and itself)
    format_tag : int
        PCM, float, or compressed format
    channels : int
        number of channels
    fs : int
        sampling frequency in samples per second
    bytes_per_second : int
        overall byte rate for the file
    block_align : int
        bytes per sample, including all channels
    bit_depth : int
        bits per sample

    Notes
    -----
    Assumes file pointer is immediately after the 'fmt ' id
    """
    if is_big_endian:
        fmt = ">"
    else:
        fmt = "<"

    size = struct.unpack(fmt + "I", fid.read(4))[0]

    if size < 16:
        raise ValueError("Binary structure of wave file is not compliant")

    res = struct.unpack(fmt + "HHIIHH", fid.read(16))
    bytes_read = 16

    format_tag, channels, fs, bytes_per_second, block_align, bit_depth = res

    if format_tag == WAVE_FORMAT.EXTENSIBLE and size >= (16 + 2):
        ext_chunk_size = struct.unpack(fmt + "H", fid.read(2))[0]
        bytes_read += 2
        if ext_chunk_size >= 22:
            extensible_chunk_data = fid.read(22)
            bytes_read += 22
            raw_guid = extensible_chunk_data[2 + 4 : 2 + 4 + 16]
            # GUID template {XXXXXXXX-0000-0010-8000-00AA00389B71} (RFC-2361)
            # MS GUID byte order: first three groups are native byte order,
            # rest is Big Endian
            if is_big_endian:
                tail = b"\x00\x00\x00\x10\x80\x00\x00\xaa\x00\x38\x9b\x71"
            else:
                tail = b"\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71"
            if raw_guid.endswith(tail):
                format_tag = struct.unpack(fmt + "I", raw_guid[:4])[0]
        else:
            raise ValueError("Binary structure of wave file is not compliant")

    if format_tag not in KNOWN_WAVE_FORMATS:
        try:
            format_name = WAVE_FORMAT(format_tag).name
        except ValueError:
            format_name = f"{format_tag:#06x}"
        raise ValueError(
            f"Unknown wave file format: {format_name}. Supported "
            "formats: " + ", ".join(x.name for x in KNOWN_WAVE_FORMATS)
        )

    # move file pointer to next chunk
    if size > bytes_read:
        fid.read(size - bytes_read)

    return (size, format_tag, channels, fs, bytes_per_second, block_align, bit_depth)


# Traceback (ValueError)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "E:\alon_emanuel_drive\School\Year2\Semester2\67690_AI_as_a_Tool\venvs\prosody-venv\lib\site-packages\scipy\io\wavfile.py", line 248, in read
raise ValueError("Incomplete wav chunk.")
ValueError: Incomplete wav chunk.

# Fixed code
def _read_fmt_chunk(fid, is_big_endian):
    """
    Returns
    -------
    size : int
        size of format subchunk in bytes (minus 8 for "fmt " and itself)
    format_tag : int
        PCM, float, or compressed format
    channels : int
        number of channels
    fs : int
        sampling frequency in samples per second
    bytes_per_second : int
        overall byte rate for the file
    block_align : int
        bytes per sample, including all channels
    bit_depth : int
        bits per sample

    Notes
    -----
    Assumes file pointer is immediately after the 'fmt ' id
    """
    if is_big_endian:
        fmt = ">"
    else:
        fmt = "<"

    size = struct.unpack(fmt + "I", fid.read(4))[0]

    if size < 16:
        raise ValueError("Binary structure of wave file is not compliant")

    res = struct.unpack(fmt + "HHIIHH", fid.read(16))
    bytes_read = 16

    format_tag, channels, fs, bytes_per_second, block_align, bit_depth = res

    if format_tag == WAVE_FORMAT.EXTENSIBLE and size >= (16 + 2):
        ext_chunk_size = struct.unpack(fmt + "H", fid.read(2))[0]
        bytes_read += 2
        if ext_chunk_size >= 22:
            extensible_chunk_data = fid.read(22)
            bytes_read += 22
            raw_guid = extensible_chunk_data[2 + 4 : 2 + 4 + 16]
            # GUID template {XXXXXXXX-0000-0010-8000-00AA00389B71} (RFC-2361)
            # MS GUID byte order: first three groups are native byte order,
            # rest is Big Endian
            if is_big_endian:
                tail = b"\x00\x00\x00\x10\x80\x00\x00\xaa\x00\x38\x9b\x71"
            else:
                tail = b"\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71"
            if raw_guid.endswith(tail):
                format_tag = struct.unpack(fmt + "I", raw_guid[:4])[0]
        else:
            raise ValueError("Binary structure of wave file is not compliant")

    if format_tag not in KNOWN_WAVE_FORMATS:
        try:
            format_name = WAVE_FORMAT(format_tag).name
        except ValueError:
            format_name = f"{format_tag:#06x}"
        raise ValueError(
            f"Unknown wave file format: {format_name}. Supported "
            "formats: " + ", ".join(x.name for x in KNOWN_WAVE_FORMATS)
        )

    # move file pointer to next chunk
    if size > bytes_read:
        fid.read(size - bytes_read)

    # fmt should always be 16, 18 or 40, but handle it just in case
    _handle_pad_byte(fid, size)

    return (size, format_tag, channels, fs, bytes_per_second, block_align, bit_depth)