o
    Si                     @   s   d Z ddlZddlZddlZddlZedZdZdej	e
 dejfddZdej	e
 d	ed
e
fddZdej	e
 fddZG dd dZG dd dZdd ZedkrYe  dS dS )z`Raw binary format for Encodec compressed audio. Actual compression API is in `encodec.compress`.    Nz!4sBIs   ECDCfometadatac                 C   sF   t |d}d}tt|t|}| | | | |   d S )Nutf-8r   )	jsondumpsencode_encodec_header_structpack_ENCODEC_MAGIClenwriteflush)r   r   meta_dumpedversionheader r   B/home/ubuntu/.local/lib/python3.10/site-packages/encodec/binary.pywrite_ecdc_header   s   

r   sizereturnc                 C   sR   d}t ||k r'| |}|std| d||7 }|t |8 }t ||k s|S )N    z0Impossible to read enough data from the stream, z bytes remaining.)r   readEOFError)r   r   bufnew_bufr   r   r   _read_exactly    s   

r   c                 C   sV   t | tj}t|\}}}|tkrtd|dkrtdt | |}t|dS )NzFile is not in ECDC format.r   zVersion not supported.r   )	r   r   r   unpackr
   
ValueErrorr   loadsdecode)r   header_bytesmagicr   	meta_size
meta_bytesr   r   r   read_ecdc_header,   s   
r$   c                   @   s>   e Zd ZdZdedeje fddZdefddZ	d	d
 Z
dS )	BitPackeraK  Simple bit packer to handle ints with a non standard width, e.g. 10 bits.
    Note that for some bandwidth (1.5, 3), the codebook representation
    will not cover an integer number of bytes.

    Args:
        bits (int): number of bits per value that will be pushed.
        fo (IO[bytes]): file-object to push the bytes to.
    bitsr   c                 C   s   d| _ d| _|| _|| _d S )Nr   )_current_value_current_bitsr&   r   selfr&   r   r   r   r   __init__@   s   
zBitPacker.__init__valuec                 C   sx   |  j || j> 7  _ |  j| j7  _| jdkr:| j d@ }|  jd8  _|  j dL  _ | jt|g | jdksdS dS )z|Push a new value to the stream. This will immediately
        write as many uint8 as possible to the underlying file-object.      N)r'   r(   r&   r   r   bytes)r*   r,   lower_8bitsr   r   r   pushF   s   

zBitPacker.pushc                 C   s4   | j r| jt| jg d| _d| _ | j  dS )zZFlushes the remaining partial uint8, call this at the end
        of the stream to encode.r   N)r(   r   r   r/   r'   r   )r*   r   r   r   r   Q   s
   zBitPacker.flushN)__name__
__module____qualname____doc__inttpIOr/   r+   r1   r   r   r   r   r   r%   7   s
    r%   c                   @   s<   e Zd ZdZdedeje fddZdej	e fddZ
d	S )
BitUnpackerzBitUnpacker does the opposite of `BitPacker`.

    Args:
        bits (int): number of bits of the values to decode.
        fo (IO[bytes]): file-object to push the bytes to.
        r&   r   c                 C   s*   || _ || _d|> d | _d| _d| _d S )N   r   )r&   r   _maskr'   r(   r)   r   r   r   r+   b   s
   
zBitUnpacker.__init__r   c                 C   s   | j | jk r+| jd}|sdS |d }|  j|| j > 7  _|  j d7  _ | j | jk s| j| j@ }|  j| jL  _|  j | j8  _ |S )z
        Pull a single value from the stream, potentially reading some
        extra bytes from the underlying file-object.
        Returns `None` when reaching the end of the stream.
        r:   Nr   r-   )r(   r&   r   r   r'   r;   )r*   r   	characteroutr   r   r   pulli   s   zBitUnpacker.pullN)r2   r3   r4   r5   r6   r7   r8   r/   r+   Optionalr>   r   r   r   r   r9   [   s    r9   c                  C   sH  dd l } | d tdD ]}| ddd }| ddd }| d	| |f }g }t }t||}|D ]}|	| q9|
  |d t||}		 |	 }
|
d u rYn||
 qPt|t|ksqJ t|t|ft|t|d|  ksJ t|t||ftt||D ]\}\}}||ksJ |||fqqd S )Nr   i     
   i  )r:   r:         Tr-   )torchmanual_seedrangerandintitemtolistioBytesIOr%   r1   r   seekr9   r>   appendr   	enumeratezip)rD   replengthr&   tokensrebuiltr   packertokenunpackerr,   idxabr   r   r   test}   s4   




$.rZ   __main__)r5   rJ   r   structtypingr7   Structr   r
   r8   r/   Anyr   r6   r   r$   r%   r9   rZ   r2   r   r   r   r   <module>   s    
	$"
