Debian Codegraph @

wyoming/tts.py

raw
1 """Text to speech."""
2
3 from dataclasses import dataclass
4 from enum import Enum
5 from typing import Any, Dict, Optional, Union
6
7 from .event import Event, Eventable
8
9 DOMAIN = "tts"
10 _SYNTHESIZE_TYPE = "synthesize"
11
12 # streaming
13 _SYNTHESIZE_START_TYPE = "synthesize-start"
14 _SYNTHESIZE_CHUNK_TYPE = "synthesize-chunk"
15 _SYNTHESIZE_STOP_TYPE = "synthesize-stop"
16 _SYNTHESIZE_STOPPED_TYPE = "synthesize-stopped"
17
18
19 class SynthesizeTextFormat(str, Enum):
20 """Format of text to synthesize."""
21
22 TEXT = "text"
23 SSML = "ssml"
24
25
26 @dataclass
27 class SynthesizeVoice:
28 """Information about the desired voice for synthesis."""
29
30 name: Optional[str] = None
31 """Voice name from tts info (overrides language)."""
32
33 language: Optional[str] = None
34 """Voice language from tts info."""
35
36 speaker: Optional[str] = None
37 """Voice speaker from tts info."""
38
39 def to_dict(self) -> Dict[str, str]:
40 if self.name is not None:
41 voice = {"name": self.name}
42 if self.speaker is not None:
43 voice["speaker"] = self.speaker
44 elif self.language is not None:
45 voice = {"language": self.language}
46 else:
47 voice = {}
48
49 return voice
50
51 @staticmethod
52 def from_dict(voice: Dict[str, Any]) -> "Optional[SynthesizeVoice]":
53 if "name" in voice:
54 return SynthesizeVoice(
55 name=voice["name"],
56 speaker=voice.get("speaker"),
57 )
58
59 if "language" in voice:
60 return SynthesizeVoice(language=voice["language"])
61
62 return None
63
64
65 @dataclass
66 class Synthesize(Eventable):
67 """Request to synthesize audio from text."""
68
69 text: str
70 """Text to synthesize."""
71
72 voice: Optional[SynthesizeVoice] = None
73 """Voice to use during synthesis."""
74
75 text_format: Optional[Union[str, SynthesizeTextFormat]] = None
76 """Format of text."""
77
78 context: Optional[Dict[str, Any]] = None
79 """Context for next interaction."""
80
81 @staticmethod
82 def is_type(event_type: str) -> bool:
83 return event_type == _SYNTHESIZE_TYPE
84
85 def event(self) -> Event:
86 data: Dict[str, Any] = {"text": self.text}
87 if self.voice is not None:
88 data["voice"] = self.voice.to_dict()
89 if isinstance(self.text_format, SynthesizeTextFormat):
90 data["text_format"] = self.text_format.value
91 elif self.text_format is not None:
92 data["text_format"] = self.text_format
93 if self.context is not None:
94 data["context"] = self.context
95
96 return Event(type=_SYNTHESIZE_TYPE, data=data)
97
98 @staticmethod
99 def from_event(event: Event) -> "Synthesize":
100 text_format: Optional[Union[str, SynthesizeTextFormat]] = event.data.get(
101 "text_format"
102 )
103 if text_format is not None:
104 try:
105 text_format = SynthesizeTextFormat(text_format)
106 except ValueError:
107 # Leave original value
108 pass
109
110 return Synthesize(
111 text=event.data["text"],
112 voice=SynthesizeVoice.from_dict(event.data.get("voice", {})),
113 text_format=text_format,
114 context=event.data.get("context"),
115 )
116
117
118 @dataclass
119 class SynthesizeStart(Eventable):
120 """Start of streaming request to synthesize audio from text."""
121
122 voice: Optional[SynthesizeVoice] = None
123 """Voice to use during synthesis."""
124
125 text_format: Optional[Union[str, SynthesizeTextFormat]] = None
126 """Format of text."""
127
128 context: Optional[Dict[str, Any]] = None
129 """Context for next interaction."""
130
131 @staticmethod
132 def is_type(event_type: str) -> bool:
133 return event_type == _SYNTHESIZE_START_TYPE
134
135 def event(self) -> Event:
136 data: Dict[str, Any] = {}
137 if self.voice is not None:
138 data["voice"] = self.voice.to_dict()
139 if isinstance(self.text_format, SynthesizeTextFormat):
140 data["text_format"] = self.text_format.value
141 elif self.text_format is not None:
142 data["text_format"] = self.text_format
143 if self.context is not None:
144 data["context"] = self.context
145
146 return Event(type=_SYNTHESIZE_START_TYPE, data=data)
147
148 @staticmethod
149 def from_event(event: Event) -> "SynthesizeStart":
150 text_format: Optional[Union[str, SynthesizeTextFormat]] = event.data.get(
151 "text_format"
152 )
153 if text_format is not None:
154 try:
155 text_format = SynthesizeTextFormat(text_format)
156 except ValueError:
157 # Leave original value
158 pass
159
160 return SynthesizeStart(
161 voice=SynthesizeVoice.from_dict(event.data.get("voice", {})),
162 text_format=text_format,
163 context=event.data.get("context"),
164 )
165
166
167 @dataclass
168 class SynthesizeChunk(Eventable):
169 """Text chunk from streaming request to synthesize audio from text."""
170
171 text: str
172 """Chunk of text to synthesize."""
173
174 @staticmethod
175 def is_type(event_type: str) -> bool:
176 return event_type == _SYNTHESIZE_CHUNK_TYPE
177
178 def event(self) -> Event:
179 return Event(type=_SYNTHESIZE_CHUNK_TYPE, data={"text": self.text})
180
181 @staticmethod
182 def from_event(event: Event) -> "SynthesizeChunk":
183 return SynthesizeChunk(text=event.data["text"])
184
185
186 @dataclass
187 class SynthesizeStop(Eventable):
188 """End of streaming request to synthesize audio from text."""
189
190 @staticmethod
191 def is_type(event_type: str) -> bool:
192 return event_type == _SYNTHESIZE_STOP_TYPE
193
194 def event(self) -> Event:
195 return Event(type=_SYNTHESIZE_STOP_TYPE)
196
197 @staticmethod
198 def from_event(event: Event) -> "SynthesizeStop":
199 return SynthesizeStop()
200
201
202 @dataclass
203 class SynthesizeStopped(Eventable):
204 """End of streaming response to streaming request."""
205
206 @staticmethod
207 def is_type(event_type: str) -> bool:
208 return event_type == _SYNTHESIZE_STOPPED_TYPE
209
210 def event(self) -> Event:
211 return Event(type=_SYNTHESIZE_STOPPED_TYPE)
212
213 @staticmethod
214 def from_event(event: Event) -> "SynthesizeStopped":
215 return SynthesizeStopped()