File size: 1,801 Bytes
2d4936d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""RULE.md: the reference's frames, logits, stop values and sound for the 40 sentences (no voice), and its 3 synthetic
reference clips' prefix vectors."""
import sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
import numpy as np, torch, soundfile as sf
import codec, reference as R, texts
OUT = Path(__file__).resolve().parents[1] / "runs" / "ref"; OUT.mkdir(parents=True, exist_ok=True)
half = np.array([4, 3, 3, 3] * 8, np.float32)
r, c = R.Reference(), codec.load()
t0 = time.perf_counter(); total = 0
for i, t in enumerate(texts.TEXTS):
    trace = []; codes = r.generate(t, seed=1000 + i, trace=trace); total += len(codes)
    z = torch.from_numpy((codes.astype(np.float32) - half) / half).T[None]
    with torch.no_grad(): a, _ = c.decode_audio(inputs=z, input_len=torch.tensor([len(codes)]))
    np.savez(OUT / f"{i:02d}.npz", codes=codes, logits=np.stack([x[0] for x in trace]), stop=np.array([x[1] for x in trace], np.float32))
    sf.write(OUT / f"{i:02d}.wav", a[0].numpy(), 22050)
    if i % 10 == 9: print(i + 1, "sentences,", total, "frames,", round(time.perf_counter() - t0), "s", flush=True)
# synthetic reference clips: three of the sentences just made become reference voices
for k, i in enumerate((12, 29, 36)):
    x, sr = sf.read(OUT / f"{i:02d}.wav", dtype="float32")
    with torch.no_grad():
        tok, n = c.encode(audio=torch.from_numpy(x)[None], audio_len=torch.tensor([len(x)]))
        L4 = torch.tensor([8, 7, 6, 6]); base = torch.tensor([1, 8, 56, 336])
        un = ((tok[0].T[:, :, None] // base) % L4).reshape(tok.shape[2], -1)[None]                # [1, T, 32]
        pre = r.comp(un)[0]
    np.savez(OUT / f"voice{k}.npz", codes=un[0].numpy(), prefix=pre.numpy(), wav=x)
    print("voice", k, "frames", un.shape[1], flush=True)