o
    ॵi                     @   s   d dl Z d dlmZm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 d dlmZmZ d dlmZ d dlmZ d d	lmZ ejejejd
G dd deZdS )    N)AnyDict)File)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)
audio_norm)Tasks)module_namec                       s   e Zd ZdZdZ fddZdedeee	f fddZ
deee	f deee	f fd	d
Zdeee	f deee	f fddZ  ZS )ANSPipelinezANS (Acoustic Noise Suppression) Inference Pipeline .

    When invoke the class with pipeline.__call__(), it accept only one parameter:
        inputs(str): the path of wav file
    >  c                    s2   t  jdd|i| | j  |dd| _dS )z
        use `model` and `preprocessor` to create a kws pipeline for prediction
        Args:
            model: model id on modelscope hub.
        modelstream_modeFN )super__init__r   evalgetr   )selfr   kwargs	__class__r   [/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/audio/ans_pipeline.pyr      s   
zANSPipeline.__init__inputsreturnc                 K   s   | j rtdt|trtt|\}}nt|tr,t	|}tt|\}}n
tdt
| dt|jdkrE|d d df }|| jkrStj||| jd}t|}|tj}t|d|jd g}||jd dS )Nz(This model does not support stream mode!zUnsupported type .   r   )orig_sr	target_sr)ndarraynsamples)r   	TypeError
isinstancebytessfreadioBytesIOstrr   typelenshapeSAMPLE_RATElibrosaresampler
   astypenpfloat32reshape)r   r   preprocess_paramsdata1fs
file_bytesdatar   r   r   
preprocess)   s$   



zANSPipeline.preprocessc                 K   s  |d }t |tjr|  }|d }d}d}t|d }td|j |j\}}	|	|d kr3d}|	|k rJt	
|t	|jd	 ||	 fgd
}nM|	|| k rn|| |	 }
td|
 t	
|t	|jd	 |
fgd
}n)|	| | d	kr|	|	| | |  }
td|
 t	
|t	|jd	 |
fgd
}td|j t  tt	|| j}|j\}}	|rt	|	}|| d }d	}|| |	krtd| t|d d ||| f d}| |d d	   }|| | }|d	kr|d |  |||< n|||  ||| |< ||7 }|| |	ksn| t|dd d	   }W d    n	1 s9w   Y  |d | d t	j }tj|iS )Nr!   r"   Fr   g      ?z	inputs:{}x   Tr   r   zpadding: {}zinputs after padding:{}   zcurrent_idx: {})noisywav_l2i   )r$   torchTensorcpunumpyintprintformatr-   r2   concatenatezerosno_grad
from_numpyr3   todevicedictr   r1   int16tobytesr   
OUTPUT_PCM)r   r   forward_paramsr!   r"   decode_do_segementwindowstridebtpaddingoutputsgive_up_lengthcurrent_idx	tmp_input
tmp_output	end_indexr   r   r   forward=   s   





zANSPipeline.forwardc                 K   s6   d|  v rt|d tj|tj tjd| j |S )Noutput_path)dtype)	keysr&   writer2   
frombufferr   rO   rM   r.   )r   r   r   r   r   r   postprocessv   s   zANSPipeline.postprocess)__name__
__module____qualname____doc__r.   r   r   r   r*   r   r:   r]   rc   __classcell__r   r   r   r   r      s    


*9r   )r(   typingr   r   r/   rB   r2   	soundfiler&   r?   modelscope.fileior   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr	   "modelscope.utils.audio.audio_utilsr
   modelscope.utils.constantr   register_moduleacoustic_noise_suppressionspeech_frcrn_ans_cirm_16kr   r   r   r   r   <module>   s$   