
    9i2                     (   S SK r S SKrS SKJrJr  S SKrS SKJs  J	r
  S SKrS SKrS SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJr  S S	KJr  \" 5       rS
rSrS r\R@                  " \RB                  \RD                  S9 " S S\5      5       r#g)    N)AnyDict)	Pipelines)
OutputKeys)Pipeline)	PIPELINES)LinearAECAndFbank)	ModelFileTasks)
get_loggerzfeature.DEY.mvn.txtzdey_mini.yamlc                 b    [         R                  " U S   5      n[        XS   5      " S0 U S   D6$ )a  According to config items, load specific module dynamically with params.
    1. Load the module corresponding to the "module" param.
    2. Call function (or instantiate class) corresponding to the "main" param.
    3. Send the param (in "args") into the function (or class) when calling ( or instantiating).

Args:
    module_cfg (dict): config items, eg:
        {
            "module": "models.model",
            "main": "Model",
            "args": {...}
        }

Returns:
    the module loaded.
modulemainargs )	importlibimport_modulegetattr)
module_cfgr   s     n/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/audio/linear_aec_pipeline.pyinitialize_configr      s6    " $$Z%9:F6f-.DF1CDD    )module_namec                      ^  \ rS rSrSrU 4S jrS rS\\\	4   S\\\	4   4S jr
S\\\	4   S\\\	4   4S jrS	 rS
rU =r$ )LinearAECPipeline0   a  AEC Inference Pipeline only support 16000 sample rate.

When invoke the class with pipeline.__call__(), you should provide two params:
    Dict[str, Any]
        the path of wav files, eg:{
        "nearend_mic": "/your/data/near_end_mic_audio.wav",
        "farend_speech": "/your/data/far_end_speech_audio.wav"}
    output_path (str, optional): "/your/output/audio_after_aec.wav"
        the file path to write generate audio.
c                    >^^^^	 [         T
U ]  " SSU0UD6  U R                  S5        [        R                  R                  5       U l        [        [        R                  R                  U R                  [        5      SS9 n[        R                  " UR                  5       5      U l        [        R                  R                  U R                  ["        5      U R                   S   S'   SSS5        U R%                  5         ['        U R                   S   5      U l        U R                   S   S	   S
   mU R                   S   S	   S   mTm	[        R*                  " T	SS9mUUUU	4S jnUUUU	4S jnX@l        XPl        g! , (       d  f       N= f)zu
use `model` and `preprocessor` to create a kws pipeline for prediction
Args:
    model: model id on modelscope hub.
modelzThis pipeline requires `trust_remote_code=True` to load the module defined in the `dey_mini.yaml`, setting this to True means you trust the code and files listed in this model repo.zutf-8)encodingiomvnNlossr   n_fft
hop_lengthF)periodicc                    > [         R                  " [         R                  " U TTTSTR                  U R                  5      SS95      $ )NFT)centerwindowreturn_complex)torchview_as_realstfttodevice)xr$   r#   r(   winlens    r   r,   (LinearAECPipeline.__init__.<locals>.stftX   sD    %%

 !99QXX.#')* *r   c           
         > [         R                  " [         R                  " U 5      TTTTR                  U R                  5      SUS9$ )NF)r(   r'   length)r*   istftview_as_complexr-   r.   )r/   slenr$   r#   r(   r0   s     r   r4   )LinearAECPipeline.__init__.<locals>.istftc   sA    ;;%%a(yy* r   r   )super__init__check_trust_remote_coder*   cudais_availableuse_cudaopenospathjoinr   CONFIG_YAMLyaml	full_loadreadconfigFEATURE_MVN_init_modelr	   preprocessorhamming_windowr,   r4   )selfr   kwargsfr,   r4   r$   r#   r(   r0   	__class__s         @@@@r   r9   LinearAECPipeline.__init__?   sB    	/u//$$*	+
 

//1TZZ5ILM..2DK')ww||DJJ'LDKKe$I 	-dkk$.?@F#F+G4[[(0>
%%fu=		* 		*	 	 	
EI Is   A(E??
Fc                 l   [         R                  " [        R                  R	                  U R
                  [        R                  5      SSS9n[        U R                  S   5      U l        U R                  (       a  U R
                  R                  5       U l        U R
                  R                  U5        g )NcpuT)map_locationweights_onlynnet)r*   loadr?   r@   rA   r   r
   TORCH_MODEL_BIN_FILEr   rF   r=   r;   load_state_dict)rK   
checkpoints     r   rH   LinearAECPipeline._init_modelp   st    ZZGGLLY%C%CD
 't{{6':;
==*DJ

"":.r   inputsreturnc                     U R                  US   US   5      nUR                  [        R                  5      R	                  5       n[
        R                  U0$ )zThe AEC process.

Args:
    inputs: dict={'feature': Tensor, 'base': Tensor}
        'feature' feature of input audio.
        'base' the base audio to mask.

Returns:
    output_pcm: generated audio array
featurebase)_processastypenpint16tobytesr   
OUTPUT_PCM)rK   rZ   output_datas      r   forwardLinearAECPipeline.forwardz   sK     mmF9$5vf~F!((2::<%%{33r   c           	          SUR                  5       ;   aa  [        R                  " US   U R                  R                  [
        R                  " U[        R                     [
        R                  S95        U$ )a#  The post process. Will save audio to file, if the output_path is given.

Args:
    inputs: a dict contains following keys:
        - output_pcm: generated audio array
    kwargs: accept 'output_path' which is the path to write generated audio

Returns:
    output_pcm: generated audio array
output_path)dtype)
keyswavwriterI   SAMPLE_RATEra   
frombufferr   rd   rb   )rK   rZ   rL   s      r   postprocessLinearAECPipeline.postprocess   sY     FKKM)II}%t'8'8'D'DfZ%:%:;288LN r   c                    U R                   (       a   UR                  5       nUR                  5       nU R                  R                  (       aT  [        R
                  " 5          U R                  UR                  S5      5      u  p4UR                  / SQ5      nS S S 5        OQ[        R
                  " 5          U R                  UR                  S5      5      nUR                  / SQ5      nS S S 5        U R                  U5      nUW-  nU R                  U[        U5      5      R                  5       R                  5       nU$ ! , (       d  f       N]= f! , (       d  f       Nn= f)Nr   )      r   )r=   r;   r   vadr*   no_grad	unsqueezepermuter,   r4   lenrQ   numpy)rK   fbanksmixturemasksru   spectrummasked_spec
masked_sigs           r   r_   LinearAECPipeline._process   s    ==[[]FllnG::>>!ZZ(8(8(;<
i0 ! 

6#3#3A#67i0 ! 99W%&ZZS\:>>@FFH
 ! !s   "6E64E
E
E )rF   r4   r   rI   r,   r=   )__name__
__module____qualname____firstlineno____doc__r9   rH   r   strr   rf   rp   r_   __static_attributes____classcell__)rN   s   @r   r   r   0   sc    	/b/4d38n 4c3h 4$sCx. tCH~ " r   r   )$r   r?   typingr   r   rz   ra   scipy.io.wavfiler    wavfilerl   r*   rC   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.preprocessorsr	   modelscope.utils.constantr
   r   modelscope.utils.loggerr   loggerrG   rB   r   register_moduleacoustic_echo_cancellationspeech_dfsmn_aec_psm_16kr   r   r   r   <module>r      s     	       ) ) . 2 6 6 .	#E* 	$$224v v4vr   