File size: 2,245 Bytes
76d09f0
 
3559195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d09f0
3559195
 
 
76d09f0
3559195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d09f0
3559195
 
 
76d09f0
3559195
 
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
"""Text -> speech. Phonemization, sentence splitting and respelling are the same as in semfence/pingo-ptbr (speak.py); the backend is PyTorch or MLX.
Requires: pip install git+https://github.com/SemFence/dizione-ptbr git+https://github.com/SemFence/tiny-voca-ptbr"""
import numpy as np
from .pronunciation import joined, spoken
from tiny_voca.infer import finished, sentences, shorter
from tiny_voca.text import PHONE_IDS, NothingToPronounce, phonemize

VOICES = {"F_s4": 0, "M_sm4": 1}
RATE, PEAK = 16000, 0.99


class Pingo:
    def __init__(self, backend="torch", path=None):
        kw = {"path": path} if path else {}
        if backend == "torch":
            from .acoustic_torch import AcousticTorch as A
            from .vocoder_torch import Vocoder as V
        elif backend == "mlx":
            import mlx.core as mx
            mx.set_default_device(mx.cpu)                         # tiny model, sequential steps: the CPU beats the GPU
            from .acoustic_mlx_edge import AcousticMLX as A
            from .vocoder_mlx import Vocoder as V
        else:
            raise ValueError("backend: torch or mlx")
        self.acoustic, self.vocoder, self.backend = A(**kw), V(**kw), backend

    def __call__(self, ph, voice, speed):
        mask = np.array([i in PHONE_IDS for i in ph.ids])
        feats, _ = self.acoustic.features(list(ph.ids), list(ph.stress), voice, speed, mask)
        return self.vocoder(feats)


def speak(backend, text, voice="F_s4", speed=1.0, respelling=True):
    waves = []
    if respelling: text = spoken(text)

    def read(piece):
        try: ph = joined(phonemize(piece)) if respelling else phonemize(piece)
        except NothingToPronounce: return
        if len(ph.ids) <= 400:
            try: waves.append(np.asarray(backend(ph, VOICES[voice], speed))); return
            except ValueError: pass
        parts = shorter(piece)
        if not parts: raise ValueError(f"word too long: {piece[:40]!r}")
        for part in parts: read(part)

    for sentence in sentences(finished(text)): read(sentence)
    if not waves: raise NothingToPronounce("nothing to read")
    wave = np.concatenate(waves); peak = float(np.abs(wave).max())
    return wave * (PEAK / peak) if peak > PEAK else wave