o
    Sig                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZmZ d dl	Z
d dlmZ ed Zg dZg dZeG dd	 d	eZdS )
    N)	dataclass)ClassVarLiteralOptionalTuple)AudioTransform)opusmp3vorbisgsm)@  .  >  ]  逻  )	r   i+  r   r   i"V  r   i }  iD  r   c                   @   sz   e Zd ZU dZeeeZe	e
e  ed< eed< dZee ed< dd Zdejd	efd
dZdefddZdd ZdS )Compressad  
    Modifies audio by running it through a lossy codec.

    :param codec: Used lossy audio codec. One of ``"opus"``, ``"mp3"``, ``"vorbis"``, or ``"gsm"``.
    :param compression_level: The level of compression to apply. 0.0 is for the lowest amount of compression, 1.0 is for highest. Ignored for ``"gsm"``.
    :return: The modified audio samples.
    supported_codecscodecNcompression_levelc                 C   sF   | j | jvrtd| j  d| j  krdks!td tdd S )NUnsupported augmentation codec r      z)Compression level must be between 0 and 1)r   r   
ValueErrorr   )self r   P/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/augmentation/compress.py__post_init__)   s   zCompress.__post_init__samplessampling_ratec              
   C   s  dd l }|j\}}|dd}t "}|j||f| jdkr |nddd|   | }W d    n1 s8w   Y  t|/}| jdkr_|j	|d| jdkrS|nd|dd	t
jd
\}	}
n|j	|dt
jd\}	}
W d    n1 stw   Y  |	dd}	|	S )Nr   r   r   r   F)
samplerateclosefdTRAWGSM610)	always_2dr   channelsformatsubtypedtype)r"   r&   )	soundfileshape	transposeioBytesIOwriter   prepare_sf_argumentsgetvaluereadnpfloat32)r   r   r   sfr#   _bufferdatafsamples_compressedsampling_rate_compressedr   r   r   __call__/   sB   	


	


zCompress.__call__returnc                 C   sz   dd l }| jdkr| jd|ddS | jdkr| jdddS | jdkr+| jdd	dS | jd
kr5dddS td| j )Nr   r	   MP3)r   r$   r%   r   OGGOPUSr
   VORBISr   r    r!   )r$   r%   r   )r'   r   r   default_subtypeNotImplementedError)r   r2   r   r   r   r-   c   s&   




zCompress.prepare_sf_argumentsc                 C   s   ||fS )Nr   )r   offsetdurationr   r   r   r   reverse_timestamps}   s   zCompress.reverse_timestamps)__name__
__module____qualname____doc__tupletypingget_argsCodecr   r   r   __annotations__r   r   floatr   r0   ndarrayintr9   dictr-   rC   r   r   r   r   r      s   
 
4r   )r*   loggingrI   dataclassesr   r   r   r   r   numpyr0   lhotse.augmentation.transformr   rK   OPUS_SUPPORTED_SAMPLING_RATESMP3_SUPPORTED_SAMPLING_RATESr   r   r   r   r   <module>   s    