-
Notifications
You must be signed in to change notification settings - Fork 167
/
vits_resample.py
53 lines (40 loc) · 1.87 KB
/
vits_resample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import librosa
import argparse
import numpy as np
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
from scipy.io import wavfile
def resample_wave(wav_in, wav_out, sample_rate):
wav, _ = librosa.load(wav_in, sr=sample_rate)
wav = wav / np.abs(wav).max() * 0.6
wav = wav / max(0.01, np.max(np.abs(wav))) * 32767 * 0.6
wavfile.write(wav_out, sample_rate, wav.astype(np.int16))
def process_file(file, wavPath, outPath, sr):
if file.endswith(".wav"):
file = file[:-4]
resample_wave(f"{wavPath}/{file}.wav", f"{outPath}/{file}.wav", sr)
def process_files_with_thread_pool(wavPath, outPath, sr, thread_num=None):
files = [f for f in os.listdir(f"./{wavPath}") if f.endswith(".wav")]
with ThreadPoolExecutor(max_workers=thread_num) as executor:
futures = {executor.submit(process_file, file, wavPath, outPath, sr): file for file in files}
for future in tqdm(as_completed(futures), total=len(futures), desc=f'Processing {sr}'):
future.result()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--wav", help="wav", dest="wav", required=True)
parser.add_argument("-o", "--out", help="out", dest="out", required=True)
parser.add_argument("-s", "--sr", help="sample rate", dest="sr", type=int, required=True)
parser.add_argument("-t", "--thread_count", help="thread count to process, set 0 to use all cpu cores", dest="thread_count", type=int, default=1)
args = parser.parse_args()
print(args.wav)
print(args.out)
print(args.sr)
os.makedirs(args.out, exist_ok=True)
wavPath = args.wav
outPath = args.out
if args.thread_count == 0:
process_num = os.cpu_count() // 2 + 1
else:
process_num = args.thread_count
process_files_with_thread_pool(wavPath, outPath, args.sr, process_num)