o
    i@                  
   @   sv  d dl Z d dlZd dl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
mZmZ d dlmZmZ d dlm  mZ d dlmZ defdd	Zd
eejef fddZd-dedefddZdd Zdededejdejdef
ddZdd Zdd Z d.ddZ!d d! Z"G d"d# d#ed$Z#G d%d& d&e#Z$G d'd( d(e#Z%G d)d* d*e#Z&i Z'G d+d, d,e(Z)dS )/    N)Union)Path)	Generatorr   )ABCMetaabstractmethod)pad_listaudioc                 C   s   d}t t| D ]W}| | }|d |d ksJ dt|d tjs&J dt|d d t|d d  |d jd ksAJ d|dkrU|d | |d  d ksUJ d||d |d  7 }q|S )Nr      z$modelscope error: Wrong time stamps.   z"modelscope error: Wrong data type.>  zFmodelscope error: audio data in list is inconsistent with time length.)rangelen
isinstancenpndarrayintshape)r   	audio_duriseg r   P/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/campplus/utils.pycheck_audio_list   s   0 r   inputsc                 C   s   g }t t| D ]s}t| | trAt| | }tjt	|dd\}}t|j
dkr3|d d df }t|d}|d}n5t| | tjrrt| | j
dksVJ d| | }|jdv rg|d d}n|d}t|}ntd	|| q|S )
Nfloat32)dtyper
   r   r	   z.modelscope error: Input array should be [N, T])int16int32int64i   zOmodelscope error: The input type is restricted to audio address and nump array.)r   r   r   strFilereadsfloadioBytesIOr   torch
from_numpy	unsqueezesqueezer   r   r   astype
ValueErrorappend)r   outputr   
file_bytesdatafsr   r   r   sv_preprocess(   s*   

r1   r   vad_segmentsreturnc                    s@   ddd  fdd}g }t | D ]\}}||| q|S )Ng      ?g      ?)seg_dur	seg_shiftc           
         s   | d }| d }t  d  }t  d  }d}g }td|jd |D ]G}t|| |jd }||kr8 |S |}td|| }||| }	|	jd |k r\t|	d||	jd  fd}	|| | | | |	g q%|S )Nr   r
   r4   r5   constant)r   r   r   minmaxr   padr,   )
seg_dataseg_str/   	chunk_lenchunk_shiftlast_chunk_edseg_reschunk_stchunk_ed
chunk_dataconfigr0   r   r   	seg_chunkH   s$   "zsv_chunk.<locals>.seg_chunk)	enumerateextend)r2   r0   rE   segsr   sr   rC   r   sv_chunkB   s   rJ   c                 C   s|   g }g }g }| D ]*}t j|ddd}||jddd }|| ||jd  ||jd  qt|dd}|||fS )Nr   P   )num_mel_binsT)dimkeepdim)	pad_value)Kaldifbankr(   meanr,   r   r   )r   featuresfeature_timesfeature_lengthsaufeaturefeatures_paddedr   r   r   extract_featureb   s   

rY   segmentslabels
embeddingsc           
      C   s   t | t |ks
J t|}g }tt | D ]}|| | d | | d || g qt|}g }t| d D ]}|||k d}|| q9t|}dd }tdt |D ]/}|||d  d || d r|| d ||d  d  d }	|	|| d< |	||d  d< qZt	|}|S )Nr   r	   c                 S   s   | |d krdS dS )Ng-C6?TFr   )t1t2r   r   r   is_overlapped   s   z"postprocess.<locals>.is_overlappedr
   )
r   correct_labelsr   r,   merge_sequer8   rR   r   stacksmooth)
rZ   r2   r[   r\   distribute_resr   spk_embsspk_embr_   pr   r   r   postprocessr   s(   &
 rh   c                 C   sF   d}i }g }| D ]}||vr|||< |d7 }| ||  qt|S )Nr   r	   )r,   r   array)r[   	labels_idid2id
new_labelsr   r   r   r   r`      s   
r`   c                 C   sv   | d g}t dt| D ],}| | d |d d ks&| | d |d d kr.|| |  q| | d |d d< q|S )Nr   r	   r
   )r   r   r,   )rd   resr   r   r   r   ra      s   
0ra   ffffff?c                 C   s4  t | dk r| S tt | D ]}t| | d d| | d< t| | d d| | d< | | d | | d  |k r|dkrI| |d  d | | d< q|t | d kr^| |d  d | | d< q| | d | |d  d  | |d  d | | d  kr| |d  d | | d< q| |d  d | | d< qt| } | S )Nr
   r   r	   )r   r   roundra   )rn   mindurr   r   r   r   rc      s    8rc   c                 C   s   dd |D }| D ]=}|d }|d }d}d}|D ]&\}}}	t t||t || d}
|
|kr3|
}|	}|
dkr?||	kr?||
7 }qt||d< q	| S )Nc                 S   s$   g | ]\}}}|d  |d  |fqS )i  r   ).0spk_stspk_edspkr   r   r   
<listcomp>   s   $ z"distribute_spk.<locals>.<listcomp>startendr   ru   )r8   r7   r   )sentence_listsd_time_listdsentence_startsentence_endsentence_spkmax_overlaprs   rt   ru   overlapr   r   r   distribute_spk   s    r   c                
   @   s~   e Zd ZdZedefddZedefddZedede	ee
f dd	fd
dZeddede	ee
f dedd	fddZd	S )StoragezAbstract class of storage.

    All backends need to implement two apis: ``read()`` and ``read_text()``.
    ``read()`` reads the file as a byte stream and ``read_text()`` reads
    the file as texts.
    filepathc                 C      d S Nr   selfr   r   r   r   r!         zStorage.readc                 C   r   r   r   r   r   r   r   	read_text   r   zStorage.read_textobjr3   Nc                 C   r   r   r   r   r   r   r   r   r   write   r   zStorage.writeutf-8encodingc                 C   r   r   r   r   r   r   r   r   r   r   
write_text   r   zStorage.write_textr   )__name__
__module____qualname____doc__r   r   r!   r   bytesr   r   r   r   r   r   r   r   r      s     *r   )	metaclassc                	   @   s   e Zd ZdZdeeef defddZddeeef dedefdd	Z	d
edeeef ddfddZ
dd
edeeef deddfddZejdeeef deeeef ddf fddZdS )LocalStoragezLocal hard disk storager   r3   c                 C   s8   t |d}| }W d   |S 1 sw   Y  |S )Read data from a given ``filepath`` with 'rb' mode.

        Args:
            filepath (str or Path): Path to read data.

        Returns:
            bytes: Expected bytes object.
        rbNopenr!   )r   r   fcontentr   r   r   r!      s   	

zLocalStorage.readr   r   c                 C   s<   t |d|d}| }W d   |S 1 sw   Y  |S )>  Read data from a given ``filepath`` with 'r' mode.

        Args:
            filepath (str or Path): Path to read data.
            encoding (str): The encoding format used to open the ``filepath``.
                Default: 'utf-8'.

        Returns:
            str: Expected text reading from ``filepath``.
        rr   Nr   )r   r   r   r   	value_bufr   r   r   r      s   

zLocalStorage.read_textr   Nc                 C   sd   t j|}|rt j|st j|dd t|d}|| W d   dS 1 s+w   Y  dS ))  Write data to a given ``filepath`` with 'wb' mode.

        Note:
            ``write`` will create a directory if the directory of ``filepath``
            does not exist.

        Args:
            obj (bytes): Data to be written.
            filepath (str or Path): Path to write data.
        Texist_okwbNospathdirnameexistsmakedirsr   r   )r   r   r   r   r   r   r   r   r     s   "zLocalStorage.writec                 C   sh   t j|}|rt j|st j|dd t|d|d}|| W d   dS 1 s-w   Y  dS )  Write data to a given ``filepath`` with 'w' mode.

        Note:
            ``write_text`` will create a directory if the directory of
            ``filepath`` does not exist.

        Args:
            obj (str): Data to be written.
            filepath (str or Path): Path to write data.
            encoding (str): The encoding format used to open the ``filepath``.
                Default: 'utf-8'.
        Tr   wr   Nr   )r   r   r   r   r   r   r   r   r   r     s   "zLocalStorage.write_textc                 c   s    |V  dS z$Only for unified API and do nothing.Nr   r   r   r   r   as_local_path3  s   
zLocalStorage.as_local_pathr   )r   r   r   r   r   r   r   r   r!   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r      s     $2r   c                	   @   s   e Zd ZdZdd Zdd Zejdede	e
eef ddf fd	d
Zdede
eef ddfddZddede
eef deddfddZdS )HTTPStoragezHTTP and HTTPS storage.c                 C      t |}|  |jS r   )requestsgetraise_for_statusr   r   urlr   r   r   r   r!   <  s   
zHTTPStorage.readc                 C   r   r   )r   r   r   textr   r   r   r   r   B  s   
zHTTPStorage.read_textr   r3   Nc              	   c   P    zt jdd}|| | |  |jV  W t|j dS t|j w )ah  Download a file from ``filepath``.

        ``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
        can be called with ``with`` statement, and when exists from the
        ``with`` statement, the temporary path will be released.

        Args:
            filepath (str): Download a file from ``filepath``.

        Examples:
            >>> storage = HTTPStorage()
            >>> # After existing from the ``with`` clause,
            >>> # the path will be removed
            >>> with storage.get_local_path('http://path/to/file') as path:
            ...     # do something here
        FdeleteNtempfileNamedTemporaryFiler   r!   closenamer   remover   r   r   r   r   r   r   G     
zHTTPStorage.as_local_pathr   r   c                 C      t d)Nz&write is not supported by HTTP StorageNotImplementedError)r   r   r   r   r   r   r   a     zHTTPStorage.writer   r   c                 C   r   )Nz+write_text is not supported by HTTP Storager   )r   r   r   r   r   r   r   r   d  r   zHTTPStorage.write_textr   )r   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   9  s    &(r   c                	   @   s   e Zd ZdZdddZdd Zddd	Zejd
e	de
ee	ef ddf fddZded
ee	ef ddfddZdde	d
ee	ef de	ddfddZdS )
OSSStoragezOSS storage.Nc                 C   r   )Nz3OSSStorage.__init__ to be implemented in the futurer   )r   oss_config_filer   r   r   __init__k  s   zOSSStorage.__init__c                 C   r   )Nz/OSSStorage.read to be implemented in the futurer   r   r   r   r   r!   o  r   zOSSStorage.readr   c                 C   r   )Nz4OSSStorage.read_text to be implemented in the futurer   )r   r   r   r   r   r   r   r  r   zOSSStorage.read_textr   r3   c              	   c   r   )ag  Download a file from ``filepath``.

        ``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
        can be called with ``with`` statement, and when exists from the
        ``with`` statement, the temporary path will be released.

        Args:
            filepath (str): Download a file from ``filepath``.

        Examples:
            >>> storage = OSSStorage()
            >>> # After existing from the ``with`` clause,
            >>> # the path will be removed
            >>> with storage.get_local_path('http://path/to/file') as path:
            ...     # do something here
        Fr   Nr   r   r   r   r   r   u  r   zOSSStorage.as_local_pathr   c                 C   r   )Nz0OSSStorage.write to be implemented in the futurer   r   r   r   r   r     r   zOSSStorage.writer   c                 C   r   )Nz5OSSStorage.write_text to be implemented in the futurer   r   r   r   r   r     r   zOSSStorage.write_textr   r   )r   r   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   h  s    

&(r   c                
   @   s   e Zd ZU eeeedZeed< e	dd Z
e	dedefddZe	ddeeef d
edefddZe	dedeeef ddfddZe	ddeded
eddfddZejdedeeeef ddf fddZdS )r    )osshttphttpslocal_prefix_to_storagec                 C   s   t | tsJ dt|  d| vrd}n	| d\}}|}|tjv s2J d|  dttj  |tvr>tj|  t|< t| S )Nz uri should be str type, but got z://r   zUnsupported uri z, valid prefixs: )	r   r   typesplitr    r   listkeys
G_STORAGES)uristorage_typeprefix_r   r   r   _get_storage  s   zFile._get_storager   r3   c                 C   s   t | }|| S )r   )r    r   r!   )r   storager   r   r   r!     s   


z	File.readr   r   c                 C   s   t | }|| S )r   )r    r   r   )r   r   r   r   r   r   r     s   

zFile.read_textr   Nc                 C   s   t |}|| |S )r   )r    r   r   )r   r   r   r   r   r   r     s   
z
File.writec                 C   s   t |}|| |S )r   )r    r   r   )r   r   r   r   r   r   r   r     s   
zFile.write_textc                 c   sB    t | }|| }|V  W d   dS 1 sw   Y  dS r   )r    r   r   )r   r   
local_pathr   r   r   r     s
   
"zFile.as_local_pathr   )r   r   r   r   r   r   r   dict__annotations__staticmethodr   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s$   
 
" *r    )r   )ro   )*r$   r   r&   r   r   r   numpyr   librosar"   typingr   pathlibr   r   abcr   r   torchaudio.compliance.kaldi
compliancekaldirP   *funasr.models.transformer.utils.nets_utilsr   r   r   r   r1   rJ   rY   rh   r`   ra   rc   r   r   r   r   r   r   objectr    r   r   r   r   <module>   sN    
$

K/.