o mTg@#@sddlZddlZddlZddlmZejejeje ddl Z ddl Z ddl Z ddlmZddlmZddlmZddlmZddlmZmZmZmZeeZdd Zd ed efd d Z d efddZ!ddZ"defddZ#ddZ$edkr e %Z&e&j'deddde&j'deddde&j'deddde&j'd edd!de&j'd"ed#d$e&j'd%ed&d$e&j'd'ed(d$e&j'd)e(dd*d+e&j'd,d-d.d/e&j'd0d1d2d3e&j'd4e)d5d6d+e&j'd7e(d8d9d+e&j'd:d;dd;d?d=e&j'd@d-dAe&*Z+e$e+dSdS)BN)Path) OmegaConf)write)SynthesizerInfer)load_csv_pitch) IRetrievalDummyRetrievalFaissIndexRetrievalload_retrieve_indexcCsddl}|dd}d|S)u<1から10までのランダムな数値を生成する関数rN u生成された数値: )randomrandint)r numberrM/Users/a14886/Desktop/projects/private_work/whisper-vits-svc/svc_inference.py click_tests  r speaker_pathreturncCsd|j}|j}||S)N)joinsuffixesnamerstrip)rrfilenamerrrget_speaker_name_from_paths  rcCs|js tdtStdtttj}tddd|}|j r*|j }n |j d}||}|j r;|j }n |j d}||}t t ||j|jdt ||j|jdd S) Nzinfer without retrivalzload index retrival model.Zdata_svcindexesz hubert.indexz whisper.index)filepathratioZn_nearest_vectors)Z hubert_indexZ whisper_index)Zenable_retrievalloggerinforrrargsspkabsoluteZhubert_index_pathZretrieval_index_prefixZwhisper_index_pathr r Zretrieval_ratioZn_retrieval_vectors)Zcli_argsZ speaker_name base_pathZhubert_index_filepath index_nameZwhisper_index_filepathrrrcreate_retrivals4    r'cCstj|sJtj|dd}|d}|}i}|D]\}}z||||<Wqtd||||<Yq|||S)Ncpu)Z map_locationZmodel_gz%s is not in the checkpoint) ospathisfiletorchload state_dictitemsprintZload_state_dict)Zcheckpoint_pathmodelZcheckpoint_dictZsaved_state_dictr.Znew_state_dictkvrrrload_svc_modelCs   r4 retrievalc Cs^|d}|d} |d} t|| } t| | } |d| }|d| ddf}|d| ddf}t|d|}|d|} || } || } td|j j | |j j }| }d}d}d}g}||kr|dkryd}d}n||}||}||||kr|}d}n |||}d||}| |||ddf}| |||ddf}|d|}|d|}|||d|}t||g|}| dddd||||f|}|||||||}|dj }|||}||||}||kspt|}Wd|S1s(wY|S)Nrzsvc_out_pit.wavr i )rr)sizeminr,Zno_gradZ unsqueezetoZ pitch2sourceZ source2wavrdata sampling_rate hop_lengthZretriv_whisperZ retriv_hubertZ LongTensor inferencer(detachnumpyextendnpasarray)r1r5r#pitppgvechpdeviceZlen_pitZlen_vecZlen_ppgZlen_minsourceZpitwavZhop_sizeZ all_frameZ hop_frameZ out_chunkZ out_index out_audioZcut_sZ cut_s_outZcut_eZ cut_e_outZsub_ppgZsub_vecZsub_pitZsub_lenZsub_harsub_outrrr svc_inferSsr                //rKc Cst|jdkr!d|_td|jd|jtd|jd|j|jdkrBd|_td|jd|jtd|jd|j|jdkrcd |_td |jd|jtd |jd|j|jrntj tj d ntj tj d t t jr~d nd}t|j}t|jjdd|jj|jj|}t|j|t|}|||t|j}t |}t|j}t |dd}t |}t|j}t |dd}t |}t!|j}td|j"|j"dkrn4t#|}||dk} | $} | %} | &} td| dd| dd| d|j"} d| d} || }t |}t'||||||||}t(d|jj)|dS)Nzsvc_tmp.ppg.npyz*Auto run : python whisper/inference.py -w z -p zpython whisper/inference.py -w zsvc_tmp.vec.npyz)Auto run : python hubert/inference.py -w z -v zpython hubert/inference.py -w zsvc_tmp.pit.csvz(Auto run : python pitch/inference.py -w zpython pitch/inference.py -w )levelcudar(r rz pitch shift: zsource pitch statics: mean=z0.1fz, min=z, max= z svc_out.wav)*rDr0waver)systemrErCdebuglogging basicConfigDEBUGINFOr,rGrM is_availablerr-configrr:Z filter_lengthZ segment_sizer<r4r1r'evalr9rAr#Z FloatTensorrepeatrshiftarraymeanr8maxrKrr;)r"rGrFr1r5r#rDrErCrHZ source_aveZ source_minZ source_maxr[rIrrrmainsx                     r___main__z--configTzyaml file for config.)typerequiredhelpz--modelzpath of model for evaluationz--wavezPath of raw audio.z--spkzPath of speaker.z--ppgzPath of content vector.)rarcz--veczPath of hubert vector.z--pitzPath of pitch csv file.z--shiftzPitch shift key.)radefaultrcz--enable-retrieval store_truezEnable index feature retrieval)actionrcz--retrieval-index-prefixrzVretrieval index file prefix. Will load file %prefix%hubert.index/%prefix%whisper.index)rdrcz--retrieval-ratiog?z8ratio of feature retrieval effect. Must be in range 0..1z--n-retrieval-vectorszFget n nearest vectors from retrieval index. Works stably in range 1..3z--hubert-index-pathFzSpath to hubert index file. Default data_svc/indexes/speaker.../%prefix%hubert.index)rbrcz--whisper-index-pathzUpath to whisper index file. Default data_svc/indexes/speaker.../%prefix%whisper.indexz--debug)rf),rSsysr)pathlibrr*appenddirnameabspath__file__r,argparser?rAZ omegaconfrZscipy.io.wavfilerZ vits.modelsrpitchrZfeature_retrievalrrr r getLogger__name__r rstrrr'r4rKr_ArgumentParserparser add_argumentintfloat parse_argsr"rrrrs      $< E