File size: 1,626 Bytes
27d8618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Command-line entry point, same shape as PiVo's:

  python inference.py synthesize --voice F_s4 --text "Olá! Esta é a voz da SemFence." --output hello.wav
  python inference.py synthesize --model weights/pingo-ptbr-w96.bin --voice M_sm4 --text-file text.txt --speed 1.1 --output out.wav
"""
import argparse, sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE / "pingo"))
import soundfile as sf
import speak


def main():
    ap = argparse.ArgumentParser(description="pingo-ptbr: Brazilian Portuguese text-to-speech in under 1 MB")
    sub = ap.add_subparsers(dest="command", required=True)
    s = sub.add_parser("synthesize", help="text -> wav (16 kHz, mono)")
    s.add_argument("--model", default=str(HERE / "weights" / "pingo-ptbr.bin"))
    s.add_argument("--voice", choices=list(speak.VOICES), default="F_s4")
    s.add_argument("--text"); s.add_argument("--text-file")
    s.add_argument("--speed", type=float, default=1.0, help="0.5 to 2.0; larger is faster")
    s.add_argument("--no-respelling", action="store_true", help="do not rewrite English words (see pingo/pronunciation.py)")
    s.add_argument("--output", "-o", default="out.wav")
    a = ap.parse_args()
    if bool(a.text) == bool(a.text_file):
        ap.error("pass exactly one of --text or --text-file")
    text = Path(a.text_file).read_text() if a.text_file else a.text
    audio = speak.speak(speak.Pingo(a.model), text, a.voice, a.speed, not a.no_respelling)
    sf.write(a.output, audio, speak.RATE)
    print(f"{a.output}: {len(audio) / speak.RATE:.2f} s")


if __name__ == "__main__":
    main()