o
    Sij                     @   s   d dl mZmZ d dlmZ d dlmZmZmZm	Z	 da
i i dadeddfd	d
ZdefddZdedefddZG dd dZdS )    )	lru_cachewraps)Lock)AnyCallableDictOptionalF)cached	noncachedenabledreturnNc                 C   sB   t | tsJ | at|  | std  D ]}|  qd S d S )Nr	   )
isinstanceboolLHOTSE_CACHING_ENABLED
AudioCacheenableLHOTSE_CACHED_METHOD_REGISTRYvaluescache_clear)r   method r   B/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/caching.pyset_caching_enabled   s   

r   c                   C   s   t S N)r   r   r   r   r   is_caching_enabled   s   r   r   c                    sb   | j   td v rtd  d| td  < tdd| td  < t| dtf fdd	}|S )
a4  
    Least-recently-used cache decorator.

    It enhances Python's built-in ``lru_cache`` with a dynamic
    lookup of whether to apply the cached, or noncached variant
    of the decorated function.

    To disable/enable caching globally in Lhotse, call::

        >>> from lhotse import set_caching_enabled
        >>> set_caching_enabled(True)   # enable
        >>> set_caching_enabled(False)  # disable

    Currently it hard-codes the cache size at 512 items
    (regardless of the array size).

    Check :meth:`functools.lru_cache` for more details.
    r	   zMethod 'z' is already cached. We don't support caching different methods which have the same __qualname__ attribute (i.e., class name + method name).r
   i   )maxsizer   c                     s.   t  r
td   }ntd   }|| i |S )Nr	   r
   )r   r   )argskwargsmnamer   r   wrapperB   s   z"dynamic_lru_cache.<locals>.wrapper)__qualname__r   
ValueErrorr   r   r   )r   r!   r   r   r   dynamic_lru_cache"   s   

r$   c                   @   s   e Zd ZU dZdZeed< dZeed< dZ	eed< i Z
eeef ed< e Zeed	< edddZedefddZededee fddZededefddZedefddZedddZdS )r   a  
    Cache of 'bytes' objects with audio data.
    It is used to cache the "command" type audio inputs.

    By default it is disabled, to enable call `set_caching_enabled(True)`
    or `AudioCache.enable()`.

    The cache size is limited to max 100 elements and 500MB of audio.

    A global dict `__cache_dict` (static member variable of class AudioCache)
    is holding the wavs as 'bytes' arrays.
    The key is the 'source' identifier (i.e. the command for loading the data).

    Thread-safety is ensured by a threading.Lock guard.
    F_AudioCache__enabledg    eͽAmax_cache_memoryd   max_cache_elements_AudioCache__cache_dict_AudioCache__lockTc                 C   s   || _ |s|   d S d S r   )r%   _AudioCache__clear_cache)clsr   r   r   r   r   i   s   zAudioCache.enabler   c                 C   s   | j S r   )r%   r,   r   r   r   r   o   s   zAudioCache.enabledkeyc                 C   sZ   | j sdS | j || jv r| j| W  d   S 	 W d   dS 1 s&w   Y  dS )zo
        Test if 'key' is in the chache. If yes return the bytes array,
        otherwise return None.
        N)r%   r*   r)   )r,   r.   r   r   r   	try_caches   s   
$zAudioCache.try_cachevaluec                 C   s   | j sdS t|| jkrdS | jJ t| j| jkr-| jtt| j t| j| jkst|t	
  | jkrN| jtt| j t|t	
  | jks8|| j|< W d   dS 1 s^w   Y  dS )z
        Add the new (key,value) pair to cache.
        Possibly free some elements before adding the new pair.
        The oldest elements are removed first.
        N)r%   lenr&   r*   r)   r(   popnextiterr   _AudioCache__cache_memory)r,   r.   r0   r   r   r   add_to_cache   s   "zAudioCache.add_to_cachec                 C   s(   d}| j  D ]
\}}|t|7 }q|S )zn
        Return size of AudioCache values in bytes.
        (internal, not to be called from outside)
        r   )r)   itemsr1   )r,   ansr.   r0   r   r   r   __cache_memory   s   zAudioCache.__cache_memoryNc                 C   s6   | j  | j  W d   dS 1 sw   Y  dS )z3
        Clear the cache, remove the data.
        N)r*   r)   clearr-   r   r   r   __clear_cache   s   "zAudioCache.__clear_cache)T)r   N)__name__
__module__r"   __doc__r%   r   __annotations__r&   intr(   r)   r   strbytesr   r*   classmethodr   r   r   r/   r6   r5   r+   r   r   r   r   r   P   s&   
 
r   )	functoolsr   r   	threadingr   typingr   r   r   r   r   r   r   r   r   r$   r   r   r   r   r   <module>   s    
.