o
    Si                     @   sj   d dl Z d dlmZ d dlmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ eG dd dZdS )	    N)	dataclass)ListLiteralOptionalTupleUnion)CutSet)Codec)resolve_seedc                   @   s   e Zd ZU dZee ed< dZee	e
e	e	f f ed< dZeee	  ed< dZeed< d	Ze	ed
< dZeeed f ed< dZeej ed< dZeed< dddZdedefddZdS )Compressa  
    Applies a lossy compression algorithm filter to each Cut in a CutSet. The audio is decompressed back to raw waveforms.

    The compression is applied with a probability of ``p``. The codec is
    randomly selected the list of provided codecs,
    with optional weights controlling the selection.
    If compression level is provided as an interval,
    then the actual value is sampled uniformly from the provided interval.

    :param codecs: A list of codecs (supported: "opus", "mp3", "vorbis", "gsm")
    :param compression_level: A single value or an interval. 0.0 = lowest compression (highest bitrate), 1.0 = highest compression (lowest bitrate). If an interval is provided, the value is sampled uniformly.
    :param codec_weights: Optional weights for each codec (default: equal weights).
    :param p: The probability of applying the low-pass filter (default: 0.5).
    :param randgen: An optional random number generator (default: a new instance).
    :param preserve_id: Whether to preserve the original cut ID (default: False).
    codecsg?compression_levelNcodec_weightsFcompress_custom_fieldsg      ?p*   )trng
randomizedseedrngpreserve_idreturnc                 C   sD  t | jt tt| jksJ dt| jttfr<t| jdks)J d| j | j\}}||k s<J d| d| d| j	  krGdksPn J d| j	 | j
ryt| j
t| jksjJ d	| j
 d
| j dtdd | j
D sxJ dn	dd | jD | _
| jd ur| jd urtd| jd u rtt| j| _d S d S )Nzduplicate codecs   zHExpected compression_level to be a tuple or a list with two values, got z0Expected min_compression < max_compression, got z >= r      z+Probability p must be between 0 and 1, got zLExpected codec_weights to be a list with the same length as codecs, got len(z	) != len()c                 s   s    | ]}|d kV  qdS )r   N ).0wr   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/dataset/cut_transforms/compress.py	<genexpr>8   s    
z)Compress.__post_init__.<locals>.<genexpr>z&All codec weights must be non-negativec                 S   s   g | ]}d qS )g      ?r   )r   _r   r   r   
<listcomp>=   s    z*Compress.__post_init__.<locals>.<listcomp>z-Either rng or seed must be provided, not both)sortedr   listset
isinstancer   r   r   lenr   r   allr   r   
ValueErrorrandomRandomr
   )selfmin_compressionmax_compressionr   r   r   __post_init__&   s6   $


(


zCompress.__post_init__cutsc           
      C   s   g }|D ]V}| j  | jkrUt| jttfr'| j\}}| j  ||  | }n| j}| j j| j| j	d^}}|j
||| jd}	| jsO|	j d| d|d|	_||	 q|| qt|S )N)weights)codecr   r   r    z.2f)r   r)   r   r%   r   r   r   choicesr   r   compressr   r   idappendr   )
r+   r/   compressed_cutscutr,   r-   r   r1   r    new_cutr   r   r   __call__D   s*   
zCompress.__call__)r   N)__name__
__module____qualname____doc__r   r	   __annotations__r   r   floatr   r   r   r   boolr   r   intr   r   r)   r*   r   r.   r   r9   r   r   r   r   r   
   s   
 
r   )r)   dataclassesr   typingr   r   r   r   r   lhotser   lhotse.augmentation.compressr	   lhotse.dataset.dataloadingr
   r   r   r   r   r   <module>   s    