File size: 1,146 Bytes
5838d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Abstract TTS backend interface."""

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List

import numpy as np


@dataclass(frozen=True)
class Voice:
    """A speaker/voice exposed by a backend."""

    name: str
    language_code: str
    description: str = ""


@dataclass
class SynthesisResult:
    """Output of a single synthesis call: mono float32 audio at a known rate."""

    audio: np.ndarray
    sample_rate_hz: int


class TTSBackend(ABC):
    """Common interface for every TTS engine."""

    #: Short identifier stored in dataset metadata (e.g. "google_cloud_tts").
    source: str = "tts"

    @abstractmethod
    def prepare(self) -> None:
        """Validate credentials, download models and list voices.

        Must raise if the backend cannot be used.
        """

    @abstractmethod
    def voices(self) -> List[Voice]:
        """Return the selected voices to synthesize with."""

    @abstractmethod
    def synthesize(self, text: str, voice: Voice) -> SynthesisResult:
        """Synthesize ``text`` with ``voice`` into mono float32 audio."""