o
    æS™i1  ã                   @   sB   d dl mZ d dlmZmZ d dlZd dlmZ G dd„ dƒZ	dS )é    )Úasdict)ÚOptionalÚTupleN)ÚSecondsc                       sŒ   e Zd ZdZi Z‡ fdd„Zdefdd„Zededd fdd	„ƒZ	d
e
jdede
jfdd„Zdedee dedeeee f fdd„Z‡  ZS )ÚAudioTransformao  
    Base class for all audio transforms that are going to be lazily applied on
    ``Recording`` during loading the audio into memory.

    Any ``AudioTransform`` can be used like a Python function, that expects two arguments:
    a numpy array of samples, and a sampling rate. E.g.:

        >>> fn = AudioTransform.from_dict(...)
        >>> new_audio = fn(audio, sampling_rate)

    Since we often use cuts of the original recording, they will refer to the timestamps
    of the augmented audio (which might be speed perturbed and of different duration).
    Each transform provides a helper method to recover the original audio timestamps:

        >>> # When fn does speed perturbation:
        >>> fn.reverse_timestamps(offset=5.055555, duration=10.1111111, sampling_rate=16000)
        ... (5.0, 10.0)

    Furthermore, ``AudioTransform`` can be easily (de)serialized to/from dict
    that contains its name and parameters.
    This enables storing recording and cut manifests with the transform info
    inside, avoiding the need to store the augmented recording version on disk.

    All audio transforms derived from this class are "automagically" registered,
    so that ``AudioTransform.from_dict()`` can "find" the right type given its name
    to instantiate a specific transform object.
    All child classes are expected to be decorated with a ``@dataclass`` decorator.
    c                    s.   | j tjvr| tj| j < tƒ jdi |¤Ž d S )N© )Ú__name__r   ÚKNOWN_TRANSFORMSÚsuperÚ__init_subclass__)ÚclsÚkwargs©Ú	__class__r   úQ/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/augmentation/transform.pyr   )   s   z AudioTransform.__init_subclass__Úreturnc                 C   s   t | ƒ}t| ƒj|dœS )N)Únamer   )r   Útyper   )ÚselfÚdatar   r   r   Úto_dict.   s   zAudioTransform.to_dictr   c                 C   s<   | d t jv sJ d| d › ƒ‚t j| d  di | d ¤ŽS )Nr   zUnknown transform type: r   r   )r   r	   )r   r   r   r   Ú	from_dict2   s   ÿzAudioTransform.from_dictÚsamplesÚsampling_ratec                 C   ó   t ‚)zQ
        Apply transform.

        To be implemented in derived classes.
        ©ÚNotImplementedError)r   r   r   r   r   r   Ú__call__9   s   zAudioTransform.__call__ÚoffsetÚdurationc                 C   r   )a
  
        Convert ``offset`` and ``duration`` timestamps to be adequate for the audio before the transform.
        Useful for on-the-fly augmentation when a particular chunk of audio needs to be read from disk.

        To be implemented in derived classes.
        r   )r   r   r   r   r   r   r   Úreverse_timestampsA   s   	z!AudioTransform.reverse_timestamps)r   Ú
__module__Ú__qualname__Ú__doc__r	   r   Údictr   Ústaticmethodr   ÚnpÚndarrayÚintr   r   r   r   r    Ú__classcell__r   r   r   r   r   	   s"    ÿÿÿþr   )
Údataclassesr   Útypingr   r   Únumpyr&   Úlhotse.utilsr   r   r   r   r   r   Ú<module>   s
    