o
    i                     @   s   d dl mZ d dlmZ d dlmZmZmZ d dlm	Z
 d dlmZ eeZG dd dZG dd	 d	ZddededefddZdS )    )BytesIO)Lock)CallableDictOptional)open)
get_loggerc                   @   s   e Zd ZU dZdZeed< dZeed< i Ze	e
ef ed< dZeed< e Zeed	< 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 )
AudioCachea  
    Cache of 'bytes' objects with audio data.
    It is used to cache the "command" type audio inputs.

    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.
    g    eͽAmax_cache_memoryd   max_cache_elements_AudioCache__cache_dictr   _AudioCache__cache_memory_AudioCache__lockkeyreturnc                 C   sP   | 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   )clsr    r   F/home/ubuntu/.local/lib/python3.10/site-packages/audiolab/av/lhotse.py	try_cache1   s   
$zAudioCache.try_cachevaluec                 C   s   t || jkr	dS | jQ t | j| jkst || j | jkrGtt| j}| j|}|  jt |8  _t | j| jkst || j | jks|| j|< |  jt |7  _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)	lenr
   r   r   r   r   nextiterpop)r   r   r   removed_keyremoved_valuer   r   r   add_to_cache>   s   
	"zAudioCache.add_to_cachec                 C   s   | j S )z<
        Return size of AudioCache values in bytes.
        )r   r   r   r   r   cache_memoryY   s   zAudioCache.cache_memoryNc                 C   s<   | j  | j  d| _W d   dS 1 sw   Y  dS )z3
        Clear the cache, remove the data.
        r   N)r   r   clearr   r   r   r   r   clear_cache`   s   
"zAudioCache.clear_cache)r   N)__name__
__module____qualname____doc__r
   int__annotations__r   r   r   strbytesr   r   r   classmethodr   r   r   propertyr   r!   r   r   r   r   r	      s   
 r	   c                   @   sX   e Zd ZU dZdZee ed< dZee	 ed< e
d
dee fddZe
ddd	ZdS )	SmartOpena  Wrapper class around smart_open.open method

    The smart_open.open attributes are cached as classed attributes - they play the role of singleton pattern.

    The SmartOpen.setup method is intended for initial setup.
    It imports the `open` method from the optional `smart_open` Python package,
    and sets the parameters which are shared between all calls of the `smart_open.open` method.

    If you do not call the setup method it is called automatically in SmartOpen.open with the provided parameters.

    The example demonstrates that instantiating S3 `session.client` once,
    instead using the defaults and leaving the smart_open creating it every time
    has dramatic performance benefits.
    Ntransport_params
smart_openc                 C   s4   | j d ur| j |krtd| j | || _ t| _d S )Nz]SmartOpen.setup second call overwrites existing transport_params with new version	
%s	
vs	
%s)r-   loggerwarningsm_openr.   )r   r-   r   r   r   setup}   s   
zSmartOpen.setuprbc                 K   s<   | j d u r| j|d |r|n| j}| j |f||d|S )N)r-   )moder-   )r.   r2   r-   )r   urir4   r-   kwargsr   r   r   r      s   
zSmartOpen.open)N)r3   N)r"   r#   r$   r%   r-   r   r   r'   r.   r   r*   dictr2   r   r   r   r   r   r,   j   s   
 
r,   Furlcacher   c                 C   sf   |rt | nd}|du r/t| d}| }W d   n1 s"w   Y  |r/t | | t|S )z
    Load an audio file from a URL.

    Args:
        url (str): The URL of the audio file.
        cache (bool): Whether to cache the audio file.
    Returns:
        The audio bytes.
    Nr3   )r	   r   r,   r   readr   r   )r8   r9   audio_bytesfr   r   r   load_url   s   

r=   N)F)ior   	threadingr   typingr   r   r   r.   r   r1   audiolab.av.utilsr   r"   r/   r	   r,   r(   boolr=   r   r   r   r   <module>   s   N+