o
    2wi                     @   s8  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 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)	dataclass)CallableOptional)NonPositiveEnergyErrorSecondsfastcopysuppress_and_warng?1_DEFAULT_LHOTSE_AUDIO_DURATION_MISMATCH_TOLERANCE)_LHOTSE_AUDIO_DURATION_MISMATCH_TOLERANCEc                   @   sh   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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   O/home/ubuntu/sommelier/.venv/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_withN)r   r   )__name__
__module____qualname____doc__float__annotations__intpropertyr   r   r   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_tolerance/   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_tolerance=   s   r-   c                   @      e Zd ZdS )VideoLoadingErrorNr   r   r   r   r   r   r   r/   f       r/   c                   @   r.   )AudioLoadingErrorNr0   r   r   r   r   r2   j   r1   r2   c                   @   r.   )AudioSavingErrorNr0   r   r   r   r   r3   n   r1   r3   c                   @   r.   )DurationMismatchErrorNr0   r   r   r   r   r4   r   r1   r4   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.
    r5   N)r	   r2   r4   r   ConnectionResetErrorr7   r   r   r   suppress_audio_loading_errorsv   s   "r9   c                 c   sB    t ttttt| d dV  W d   dS 1 sw   Y  dS r6   )r	   r/   r2   r4   r   r8   r7   r   r   r   suppress_video_loading_errors   s   "r:   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   )r9   )argsr   r;   r   r   wrapper   s   $z3null_result_on_audio_loading_error.<locals>.wrapper)	functoolswrapsr   )r;   r>   r   r=   r   "null_result_on_audio_loading_error   s   rA   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   rE   )T) r?   r)   r%   r+   
contextlibr   dataclassesr   typingr   r   lhotse.utilsr   r   r   r	   r
   r!   r   r   r'   r-   	Exceptionr/   r2   r3   r4   boolr9   r:   rA   rE   r   r   r   r   <module>   s2   
 
)