-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_f0_stats_hifi_gan_w2w2_libriTTS.py
69 lines (57 loc) · 1.83 KB
/
get_f0_stats_hifi_gan_w2w2_libriTTS.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
import pkwrap
from tqdm import tqdm
import soundfile as sf
from multiprocessing import Pool
from pathlib import Path
import argparse
import numpy
import math
import torch
import os
import json
def calc_stats(file):
x, sr = sf.read(file)
f0 = pkwrap.hifigan.f0.get_f0(torch.tensor(x), None, cache_with_filename=str(file))
f0 = f0[f0 > 1.]
mu, std, var = f0.mean(), f0.std(), f0.var()
return {
"filename": os.path.basename(file),
"mu_s" : mu.item(),
"std_s": std.item(),
"var_s": var.item(),
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--srcdir", type=Path, required=True)
parser.add_argument("--outstats", type=Path, required=True)
parser.add_argument("--postfix", type=str, default="wav")
args = parser.parse_args()
files = list(Path(args.srcdir).glob(f"**/*{args.postfix}"))
pkwrap.hifigan.f0.set_yaapt_opts({
"frame_length": 35.0,
"frame_space": 20.0,
"nccf_thresh1": 0.25,
"tda_frame_length": 25.0,
})
with Pool(2) as p:
rets = list(tqdm(p.imap(calc_stats, files), total=len(files)))
speakers_mu = {}
speakers_var = {}
for r in rets:
spk = r["filename"].split("_")[0]
if spk not in speakers_mu:
speakers_mu[spk] = []
if spk not in speakers_var:
speakers_var[spk] = []
speakers_var[spk].append(r["var_s"])
speakers_mu[spk].append(r["mu_s"])
stats = {}
for k in speakers_mu.keys():
stats[k] = {}
stats[k]["f0_mean"] = numpy.array(speakers_mu[k]).mean()
stats[k]["f0_std"] = math.sqrt(numpy.array(speakers_var[k]).mean())
json_object = json.dumps(stats)
with open(args.outstats, "w") as outfile:
outfile.write(json_object)
if __name__ == "__main__":
main()