U
    ,:%e!                     @   s   d Z ddlZ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
Z
ddlZeeZe	ejjd ZedddZeed	d
dZdd Zdd ZedddZddddgZdd Zdd Zd&ddZdd Zd d! Zd"d# Zd$d% Z dS )'zModule to implement logics used for initializing extensions.

The implementations here should be stateless.
They should not depend on external state.
Anything that depends on external state should happen in __init__.py
    Nwraps)Pathlib)r   c                 C   s(   t jdkrdnd}t|  d|  }|S )NntZpydso.)osname_LIB_DIR)r   suffixpath r   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torchaudio/_extension/utils.py_get_lib_path   s    r   )r   returnc                 C   s0   t | }| sdS tj| tj| dS )a  Load extension module

    Note:
        In case `torchaudio` is deployed with `pex` format, the library file
        is not in a standard location.
        In this case, we expect that `libtorchaudio` is available somewhere
        in the search path of dynamic loading mechanism, so that importing
        `_torchaudio` will have library loader find and load `libtorchaudio`.
        This is the reason why the function should not raising an error when the library
        file is not found.

    Returns:
        bool:
            True if the library file is found AND the library loaded without failure.
            False if the library file is not found (like in the case where torchaudio
            is deployed with pex format, thus the shared library file is
            in a non-standard location.).
            If the library file is found but there is an issue loading the library,
            (such as missing dependency) then this function raises the exception as-is.

    Raises:
        Exception:
            If the library file is found, but there is an issue loading the library file,
            (when underlying `ctype.DLL` throws an exception), this function will pass
            the exception as-is, instead of catching it and returning bool.
            The expected case is `OSError` thrown by `ctype.DLL` when a dynamic dependency
            is not found.
            This behavior was chosen because the expected failure case is not recoverable.
            If a dependency is missing, then users have to install it.
    FT)r   existstorchopsZload_libraryclasses)r   r   r   r   r   	_load_lib   s    r   c                  C   sF   t d dd l} | jjd dd l}tjj	  |
tjjj d S )NZlibtorchaudio_soxr   )r   Ztorchaudio.lib._torchaudio_soxr   Z_torchaudio_soxset_verbosityatexitr   r   
torchaudioZ"sox_effects_initialize_sox_effectsregisterZ sox_effects_shutdown_sox_effects)r   r   r   r   r   	_init_soxE   s    r   c                 C   sB   ddddt   }dddd|  }|j|d	}tjj| d S )
Nzlibavutil.so.{ver}zlibavutil.{ver}.dylibzavutil-{ver}.dll)LinuxDarwinWindows:   9   8   )654)ver)platformsystemformatr   r   _torchaudioZfind_avutil)
ffmpeg_verZlibname_templateZ
avutil_verZ	libavutilr   r   r   _try_access_avutilQ   s    r+   )r*   c                 C   sl   t d|  d|  }d|  }t| s<td|  d| rHt|  t| t d|  td| S )Nz%Attempting to load FFmpeg version %s.Zlibtorchaudio_ffmpeg_torchaudio_ffmpegzFFmpeg z extension is not available.zFound FFmpeg version %s.ztorchaudio.lib.)	_LGdebugr   r   RuntimeErrorr+   r   	importlibimport_module)r*   Zlibrary	extensionr   r   r   !_find_versionsed_ffmpeg_extension\   s    

r3   r"   r#   r$    c              	   C   sb   |r
t jnt j}| D ]:}zt|W   S  tk
rL   |d|dd Y qY qX qtd|  d S )Nz#Failed to load FFmpeg %s extension.T)exc_infoz6Failed to intialize FFmpeg extension. Tried versions: )r-   errorr.   r3   	ExceptionImportError)ffmpeg_vers
show_errorloggerr*   r   r   r   _find_ffmpeg_extension|   s    r<   c                  C   s   ddddg} dd | D S )Nr"   r#   r$   r4   c                 S   s"   g | ]}t d |  r|qS )r,   )r   r   .0vr   r   r   
<listcomp>   s      z._find_available_ffmpeg_ext.<locals>.<listcomp>r   )r9   r   r   r   _find_available_ffmpeg_ext   s    rA   Fc                 C   s   t  }|stdtjd }rd|dgkr8td n,||kr^td| ddd |D  |g}t|| }|	  |
 d	kr|d	 |S )
Nz[TorchAudio is not built with FFmpeg integration. Please build torchaudio with USE_FFMPEG=1.ZTORCHAUDIO_USE_FFMPEG_VERSIONr4   zTTorchAudio is built in single FFmpeg mode. TORCHAUDIO_USE_FFMPEG_VERSION is ignored.zThe FFmpeg version zT (read from TORCHAUDIO_USE_FFMPEG_VERSION) is not available. Available versions are c                 S   s   g | ]}|r|qS r   r   r=   r   r   r   r@      s      z _init_ffmpeg.<locals>.<listcomp>   )rA   r/   r	   environgetwarningswarn
ValueErrorr<   initZget_log_levelZset_log_level)r:   r9   r*   extr   r   r   _init_ffmpeg   s$    


rJ   c               	   C   sN   t jdddD ]4} t j| rzt |  W q tk
rF   Y qX qd S )NPATHr4   ;)r	   rC   rD   splitr   r   Zadd_dll_directoryr7   )r   r   r   r   _init_dll_path   s    rN   c                  C   s   t jj } | d k	rtjjd k	rt| }|d d  d|d  }tjjd}|d  d|d  }||krt	d| d| d| S )	Nr   r      zjDetected that PyTorch and TorchAudio were compiled with different CUDA versions. PyTorch has CUDA version z% whereas TorchAudio has CUDA version zJ. Please install the TorchAudio version that matches your PyTorch version.)
r   r   r)   Zcuda_versionr   versioncudastrrM   r/   )rR   version_strZ
ta_versionZ	t_versionr   r   r   _check_cuda_version   s    rV   c                    s   t   fdd}|S )Nc               
      sJ   z
t   W n4 tk
r> } zt j d|W 5 d }~X Y nX  | |S )Nzm requires sox extension which is not available. Please refer to the stacktrace above for how to resolve this.)r   r7   r/   __name___args_kwargserrfuncr   r   wrapped   s    

z#_fail_since_no_sox.<locals>.wrappedr   r]   r^   r   r\   r   _fail_since_no_sox   s    r`   c                    s   t   fdd}|S )Nc               
      sN   zt dd W n4 tk
rB } zt j d|W 5 d }~X Y nX  | |S )NT)r:   zp requires FFmpeg extension which is not available. Please refer to the stacktrace above for how to resolve this.)rJ   r7   r/   rW   rX   r\   r   r   r^      s    
z&_fail_since_no_ffmpeg.<locals>.wrappedr   r_   r   r\   r   _fail_since_no_ffmpeg   s    ra   )F)!__doc__r0   loggingr	   r&   rE   	functoolsr   pathlibr   r   r   	getLoggerrW   r-   __file__parentr   rT   r   boolr   r   r+   r3   Z_FFMPEG_VERSr<   rA   rJ   rN   rV   r`   ra   r   r   r   r   <module>   s0   
'
