-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.py
88 lines (76 loc) · 2.44 KB
/
convert.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import argparse
import logging
from pathlib import Path
from tqdm import tqdm
import torch
import torchaudio
import torchaudio.functional as AF
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
SPEAKERS = ["p228", "p268", "p225", "p232", "p257", "p231", "LJSpeech"]
def convert(args):
logging.info("Loading HuBERT-Soft checkpoint")
hubert = torch.hub.load("bshall/hubert:main", "hubert_soft", trust_repo=True).cuda()
logging.info("Loading Urhythmic checkpoint")
urhythmic, encode = torch.hub.load(
"bshall/urhythmic:main",
args.model,
source_speaker=args.source,
target_speaker=args.target,
trust_repo=True,
)
urhythmic.cuda()
logging.info(f"Coverting {args.in_dir} to {args.target}")
for in_path in tqdm(list(args.in_dir.rglob(f"*{args.extension}"))):
wav, sr = torchaudio.load(in_path)
wav = AF.resample(wav, sr, 16000)
wav = wav.unsqueeze(0).cuda()
with torch.inference_mode():
units, log_probs = encode(hubert, wav)
wav = urhythmic(units, log_probs)
out_path = args.out_dir / in_path.relative_to(args.in_dir)
out_path.parent.mkdir(parents=True, exist_ok=True)
torchaudio.save(
out_path.with_suffix(args.extension), wav.squeeze(0).cpu(), 16000
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert audio samples using Urhythmic."
)
parser.add_argument(
"model",
help="available models (Urhythmic-Fine or Urhythmic-Global).",
choices=["urhythmic_fine", "urhythmic_global"],
)
parser.add_argument(
"source",
metavar="source-speaker",
help=f"the source speaker: {', '.join(SPEAKERS)}",
choices=SPEAKERS,
)
parser.add_argument(
"target",
metavar="target-speaker",
help=f"the target speaker: {', '.join(SPEAKERS)}",
choices=SPEAKERS,
)
parser.add_argument(
"in_dir",
metavar="in-dir",
help="path to the dataset directory.",
type=Path,
)
parser.add_argument(
"out_dir",
metavar="out-dir",
help="path to the output directory.",
type=Path,
)
parser.add_argument(
"--extension",
help="extension of the audio files (defaults to .wav).",
default=".wav",
type=str,
)
args = parser.parse_args()
convert(args)