CoolFace
Apppublic

kwau/sovits-isla

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
resample.py99 linesDownload Raw Back to root
1import argparse2import concurrent.futures3import os4from concurrent.futures import ProcessPoolExecutor5from multiprocessing import cpu_count6 7import librosa8import numpy as np9from rich.progress import track10from scipy.io import wavfile11 12 13def load_wav(wav_path):14    return librosa.load(wav_path, sr=None)15 16 17def trim_wav(wav, top_db=40):18    return librosa.effects.trim(wav, top_db=top_db)19 20 21def normalize_peak(wav, threshold=1.0):22    peak = np.abs(wav).max()23    if peak > threshold:24        wav = 0.98 * wav / peak25    return wav26 27 28def resample_wav(wav, sr, target_sr):29    return librosa.resample(wav, orig_sr=sr, target_sr=target_sr)30 31 32def save_wav_to_path(wav, save_path, sr):33    wavfile.write(34        save_path,35        sr,36        (wav * np.iinfo(np.int16).max).astype(np.int16)37    )38 39 40def process(item):41    spkdir, wav_name, args = item42    speaker = spkdir.replace("\\", "/").split("/")[-1]43 44    wav_path = os.path.join(args.in_dir, speaker, wav_name)45    if os.path.exists(wav_path) and '.wav' in wav_path:46        os.makedirs(os.path.join(args.out_dir2, speaker), exist_ok=True)47 48        wav, sr = load_wav(wav_path)49        wav, _ = trim_wav(wav)50        wav = normalize_peak(wav)51        resampled_wav = resample_wav(wav, sr, args.sr2)52 53        if not args.skip_loudnorm:54            resampled_wav /= np.max(np.abs(resampled_wav))55 56        save_path2 = os.path.join(args.out_dir2, speaker, wav_name)57        save_wav_to_path(resampled_wav, save_path2, args.sr2)58 59 60"""61def process_all_speakers():62    process_count = 30 if os.cpu_count() > 60 else (os.cpu_count() - 2 if os.cpu_count() > 4 else 1)63 64    with ThreadPoolExecutor(max_workers=process_count) as executor:65        for speaker in speakers:66            spk_dir = os.path.join(args.in_dir, speaker)67            if os.path.isdir(spk_dir):68                print(spk_dir)69                futures = [executor.submit(process, (spk_dir, i, args)) for i in os.listdir(spk_dir) if i.endswith("wav")]70                for _ in tqdm(concurrent.futures.as_completed(futures), total=len(futures)):71                    pass72"""73# multi process74 75 76def process_all_speakers():77    process_count = 30 if os.cpu_count() > 60 else (os.cpu_count() - 2 if os.cpu_count() > 4 else 1)78    with ProcessPoolExecutor(max_workers=process_count) as executor:79        for speaker in speakers:80            spk_dir = os.path.join(args.in_dir, speaker)81            if os.path.isdir(spk_dir):82                print(spk_dir)83                futures = [executor.submit(process, (spk_dir, i, args)) for i in os.listdir(spk_dir) if i.endswith("wav")]84                for _ in track(concurrent.futures.as_completed(futures), total=len(futures), description="resampling:"):85                    pass86 87 88if __name__ == "__main__":89    parser = argparse.ArgumentParser()90    parser.add_argument("--sr2", type=int, default=44100, help="sampling rate")91    parser.add_argument("--in_dir", type=str, default="./dataset_raw", help="path to source dir")92    parser.add_argument("--out_dir2", type=str, default="./dataset/44k", help="path to target dir")93    parser.add_argument("--skip_loudnorm", action="store_true", help="Skip loudness matching if you have done it")94    args = parser.parse_args()95 96    print(f"CPU count: {cpu_count()}")97    speakers = os.listdir(args.in_dir)98    process_all_speakers()99