File size: 1,714 Bytes
b49ed64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Render consistent CPU listening previews from an inference checkpoint."""
import argparse
import json
from pathlib import Path
import soundfile as sf
import torch
from little_tts.infer import load_model, speak
from little_tts.text import set_frontend

TEXTS = (
    "Hello! This is a sample of my voice as I learn to speak naturally.",
    "The morning sunlight streamed through the window, warming the quiet room.",
)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--checkpoint", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args()
    torch.set_num_threads(2)
    set_frontend("own")
    model = load_model(args.checkpoint, "cpu")
    args.output.mkdir(parents=True, exist_ok=True)
    records = []
    with torch.inference_mode():
        for voice in model.config.speakers:
            for index, text in enumerate(TEXTS, 1):
                wave, phones, _ = speak(model, text, voice, seed=0)
                peak = float(wave.abs().max())
                # Attenuate peaks above .95 for playback, without boosting quiet output.
                gain = min(1., .95 / max(peak, 1e-9))
                path = args.output / f"voice_{voice}_{index}.wav"
                sf.write(path, wave.numpy() * gain, model.config.sample_rate, subtype="PCM_16")
                records.append(dict(voice=voice, text=text, file=path.name,
                    seconds=len(wave)/model.config.sample_rate, raw_peak=peak, playback_gain=gain))
    (args.output / "samples.json").write_text(json.dumps(records, indent=2) + "\n")
    print("Listening samples ready:", args.output, flush=True)


if __name__ == "__main__":
    main()