o
    Si                     @   s<  U d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZmZmZmZ dZeed< eaeed< eG d	d
 d
ZdefddZdeddfddZG dd deZG dd deZG dd deZG dd deZed$defddZed$defddZde
de
fd d!Zdefd"d#Z dS )%    N)contextmanager)asdict	dataclass)CallableOptional)NonPositiveEnergyErrorSecondsfastcopysuppress_and_warng      ?1_DEFAULT_LHOTSE_AUDIO_DURATION_MISMATCH_TOLERANCE)_LHOTSE_AUDIO_DURATION_MISMATCH_TOLERANCEc                   @   s   e Zd ZU dZeed< 	 eed< 	 eed< 	 eed< 	 edefddZ	edefd	d
Z
dddZededd fddZdefddZdS )	VideoInfozM
    Metadata about video content in a :class:`~lhotse.audio.Recording`.
    fps
num_framesheightwidthreturnc                 C   s   | j | j S N)r   r   self r   F/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/audio/utils.pyduration#   s   zVideoInfo.durationc                 C   s
   d| j  S )Ng      ?)r   r   r   r   r   frame_length'   s   
zVideoInfo.frame_lengthc                 K   s   t | fi |S r   )r	   )r   kwargsr   r   r   	copy_with+      zVideoInfo.copy_withdatac                 C   s   t di |S )Nr   )r   )clsr   r   r   r   	from_dict.   s   zVideoInfo.from_dictc                 C   s   t | S r   )r   r   r   r   r   to_dict2   s   zVideoInfo.to_dictN)r   r   )__name__
__module____qualname____doc__float__annotations__intpropertyr   r   r   r   classmethoddictr   r    r   r   r   r   r      s$   
 
r   r   c                   C   s(   t tkrt S dtjv rttjd S t S )zBRetrieve the current audio duration mismatch tolerance in seconds.(LHOTSE_AUDIO_DURATION_MISMATCH_TOLERANCE)r   r   osenvironr%   r   r   r   r   %get_audio_duration_mismatch_tolerance6   s   
r.   deltac                 C   s:   t dt d|  d | tk rtdt d | adS )au  
    Override Lhotse's global threshold for allowed audio duration mismatch between the
    manifest and the actual data.

    Some scenarios when a mismatch can happen:

        - the :class:`.Recording` manifest duration is rounded off too much
            (i.e., bad user input, but too inconvenient to go back and fix the manifests)

        - data augmentation changes the number of samples a bit in a difficult to predict way

    When there is a mismatch, Lhotse will either trim or replicate the diff to match
    the value found in the :class:`.Recording` manifest.

    .. note:: We don't recommend setting this lower than the default value, as it could
        break some data augmentation transforms.

    Example::

        >>> import lhotse
        >>> lhotse.set_audio_duration_mismatch_tolerance(0.01)  # 10ms tolerance

    :param delta: New tolerance in seconds.
    zThe user overrided tolerance for audio duration mismatch between the values in the manifest and the actual data. Old threshold: zs. New threshold: zs.zRThe audio duration mismatch tolerance has been set to a value lower than default (zPs). We don't recommend this as it might break some data augmentation transforms.N)logginginfor   r   warningswarn)r/   r   r   r   %set_audio_duration_mismatch_toleranceD   s   r4   c                   @      e Zd ZdS )VideoLoadingErrorNr!   r"   r#   r   r   r   r   r6   m       r6   c                   @   r5   )AudioLoadingErrorNr7   r   r   r   r   r9   q   r8   r9   c                   @   r5   )AudioSavingErrorNr7   r   r   r   r   r:   u   r8   r:   c                   @   r5   )DurationMismatchErrorNr7   r   r   r   r   r;   y   r8   r;   Tenabledc                 c   s@    t tttt| d dV  W d   dS 1 sw   Y  dS zl
    Context manager that suppresses errors related to audio loading.
    Emits warning to the console.
    r<   N)r
   r9   r;   r   ConnectionResetErrorr>   r   r   r   suppress_audio_loading_errors}   s   "r@   c                 c   sB    t ttttt| d dV  W d   dS 1 sw   Y  dS r=   )r
   r6   r9   r;   r   r?   r>   r   r   r   suppress_video_loading_errors   s   "rA   funcc                    s    t  dtf fdd}|S )aJ  
    This is a decorator that makes a function return None when reading audio with Lhotse failed.

    Example::

        >>> @null_result_on_audio_loading_error
        ... def func_loading_audio(rec):
        ...     audio = rec.load_audio()  # if this fails, will return None instead
        ...     return other_func(audio)

    Another example::

        >>> # crashes on loading audio
        >>> audio = load_audio(cut)
        >>> # does not crash on loading audio, return None instead
        >>> maybe_audio: Optional = null_result_on_audio_loading_error(load_audio)(cut)
    r   c                     s8   t    | i |W  d    S 1 sw   Y  d S r   )r@   )argsr   rB   r   r   wrapper   s   $z3null_result_on_audio_loading_error.<locals>.wrapper)	functoolswrapsr   )rB   rE   r   rD   r   "null_result_on_audio_loading_error   s   rH   c                   C   s   t jddkS )N&LHOTSE_AUDIO_LOADING_EXCEPTION_VERBOSE1)r,   r-   getr   r   r   r    verbose_audio_loading_exceptions   r   rL   )T)!rF   r0   r,   r2   
contextlibr   dataclassesr   r   typingr   r   lhotse.utilsr   r   r	   r
   r   r&   r   r   r.   r4   	Exceptionr6   r9   r:   r;   boolr@   rA   rH   rL   r   r   r   r   <module>   s2   
 
$)