import os import sys import json import librosa import numpy as np from pathlib2 import Path import torch from scipy.io.wavfile import read # We're using the audio processing from TacoTron2 to make sure it matches sys.path.insert(0, '/ssdata1/lsen/CODE/Audio2Expression/3rdpart/') import tacotron2 from tacotron2.layers import TacotronSTFT total_pid = ["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10" ] if __name__ == "__main__": path_data = Path('/yrfs/lsen/DATA/GRID/processing') # '../data/trinity/source' # STFT model model = TacotronSTFT(filter_length=1024, hop_length=256, win_length=1024, sampling_rate=22050, mel_fmin=0.0, mel_fmax=8000) # dataset load v_ids = sorted(path_data.glob("*"), reverse=False) print(f"Have {len(v_ids)} dir files, \n {v_ids[:3]}") # for each speaker for i, ipath_id in enumerate(v_ids): id_name = ipath_id.stem if id_name not in total_pid: continue dir_video = path_data.joinpath(id_name) v_dir_videos = sorted(dir_video.glob("*/video/video_orignal.mp4")) print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}") # for each video for iv, ipath_video in enumerate(v_dir_videos): ipath_data = ipath_video.parent.parent ipath_audio = ipath_data.joinpath("audio/audio_extract.wav") ipath_save = ipath_data.joinpath("audio/feat_mel_no_align_trim.npy") # feat_mel_trim_split if ipath_save.exists(): continue if not ipath_audio.exists(): print(f"ERROR: NOT Exist {ipath_audio}") continue # load trim audio audio, sr = librosa.load(str(ipath_audio), mono=True) # 16484 = fps * 550 让这个sr是fps的整数, if sr != 22050: raise ValueError(f"{sr} SR doesn't match target 22050 SR") audio = torch.from_numpy(audio).float() audio = audio.unsqueeze(0) print(f"audio shape = {audio.shape}, audio min-max({audio.min():.4f}, {audio.max():.4f})") audio[audio > 1.0] = 1.0 audio[audio < -1.0] = -1.0 melspec = model.mel_spectrogram(audio) melspec = torch.squeeze(melspec, 0) # (80, 20374) print(f"Melspec shape = {melspec.shape}, min-max({melspec.min():.4f}, {melspec.max():.4f}), rate = {melspec.shape[1] / 75}") # save melspec = melspec.permute(1,0) # (nsplits, nsteps, 80) np.save(ipath_save, melspec) # (nsplit, 173, 80) print(f"melspec shape = {melspec.shape}, save to --> {ipath_save}") # break