Skip to content

Commit 762c3a7

Browse files
📝 Add docstrings to marytts
Docstrings generation was requested by @JarbasAl. * #81 (comment) The following files were modified: * `ovos_tts_server/__init__.py`
1 parent 40d6fa4 commit 762c3a7

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

ovos_tts_server/__init__.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,34 @@ def __init__(self, plugin_name: str, cache: bool = False):
4040
@property
4141
def langs(self):
4242
"""
43-
Return the list of languages supported by the wrapped TTS engine.
43+
List languages supported by the wrapped TTS engine.
4444
4545
Returns:
46-
list[str]: Engine-reported available language codes, or a list containing the wrapper's default language if the engine does not expose available languages.
46+
list[str]: Engine-reported language codes, or a single-item list containing the wrapper's default language if the engine does not provide available languages.
4747
"""
4848
return self.engine.available_languages or [self.lang]
4949

5050
@property
5151
def voices(self):
5252
"""
53-
Attempt to retrieve available voices from the plugin.
54-
Returns a list of dictionaries or strings depending on the plugin.
53+
Return the available voices exposed by the wrapped TTS engine.
54+
55+
A list of available voices — each item is typically a dict or a string depending on the engine implementation. Returns an empty list if the wrapped engine does not expose an `available_voices` attribute.
5556
"""
5657
if hasattr(self.engine, "available_voices"):
5758
return self.engine.available_voices
5859
return []
5960

6061
def synthesize(self, utterance: str, **kwargs) -> Tuple[str, Optional[str]]:
6162
"""
62-
Synthesize spoken audio from the given text or SSML.
63+
Synthesize speech audio from the provided text or SSML.
6364
6465
Parameters:
65-
utterance (str): Text or SSML to synthesize.
66-
kwargs: Plugin-specific synthesis parameters forwarded to the underlying TTS engine.
66+
utterance (str): Text or SSML to synthesize.
67+
**kwargs: Plugin-specific synthesis options forwarded to the underlying TTS engine (e.g., lang, voice, format).
6768
6869
Returns:
69-
tuple (str, Optional[str]): `(audio_path, phonemes)` where `audio_path` is the file path to the generated audio and `phonemes` is the phoneme data produced by the engine, or `None` if not available.
70+
tuple: `(audio_path, phonemes)` where `audio_path` is the file path to the generated audio and `phonemes` is the phoneme data produced by the engine, or `None` if not available.
7071
"""
7172
utterance = self.engine.validate_ssml(utterance)
7273
audio, phonemes = self.engine.synth(utterance, **kwargs)
@@ -114,8 +115,11 @@ def status() -> dict:
114115
@app.get("/locales")
115116
def mary_locales():
116117
"""
117-
MaryTTS Compatibility: Returns a newline-separated list of supported locales.
118-
Format: [locale]\n...
118+
Provide supported locales in MaryTTS-compatible plain-text format.
119+
120+
Returns:
121+
A plain-text HTTP response whose body contains supported locale identifiers,
122+
one per line (newline-separated).
119123
"""
120124
langs = tts_engine.langs
121125
# Ensure we return plain text, not JSON
@@ -124,9 +128,12 @@ def mary_locales():
124128
@app.get("/voices")
125129
def mary_voices():
126130
"""
127-
MaryTTS Compatibility: Returns a list of supported voices.
128-
Format: [name] [locale] [gender]\n...
129-
Note: Name must be space-free.
131+
Provide a MaryTTS-compatible plain-text listing of available voices.
132+
133+
Each line has the format: "<name> <locale> <gender>". The generated name must not contain spaces; the response currently emits a single default voice line based on the server's TTS engine.
134+
135+
Returns:
136+
A FastAPI Response with media_type "text/plain" whose body is newline-separated voice entries.
130137
"""
131138
lines = []
132139

@@ -138,8 +145,15 @@ def mary_voices():
138145
@app.api_route("/process", methods=["GET", "POST"])
139146
def mary_process(params: MaryTTSInput = Depends()):
140147
"""
141-
MaryTTS Compatibility: Processes input text and returns a wav file.
142-
Accepts both GET and POST parameters validated by Pydantic.
148+
Handle MaryTTS-compatible /process requests and return the synthesized audio as a WAV FileResponse.
149+
150+
Maps MaryTTS parameters to TTS engine options (LOCALE → lang, VOICE underscores replaced with spaces) and synthesizes INPUT_TEXT via the injected TTS engine.
151+
152+
Parameters:
153+
params (MaryTTSInput): Validated MaryTTS request parameters injected via Depends(); contains INPUT_TEXT, INPUT_TYPE, LOCALE, VOICE, OUTPUT_TYPE, and AUDIO.
154+
155+
Returns:
156+
FileResponse: A response serving the synthesized audio as a WAV file.
143157
"""
144158
# Map MaryTTS specific params to OVOS synthesize params
145159
synth_kwargs = {}
@@ -160,13 +174,14 @@ def mary_process(params: MaryTTSInput = Depends()):
160174
@app.get("/synthesize/{utterance}")
161175
async def synth_legacy(utterance: str, request: Request) -> FileResponse:
162176
"""
163-
Generate and return synthesized audio for the given utterance, forwarding query parameters to the TTS plugin.
177+
Produce synthesized audio for an utterance using query parameters from the incoming request.
164178
165179
Parameters:
166-
request (Request): The incoming FastAPI request whose query parameters are forwarded to the TTS plugin.
180+
utterance (str): Text to synthesize.
181+
request (Request): Incoming FastAPI request whose query parameters are forwarded to the TTS plugin as synthesis options.
167182
168183
Returns:
169-
FileResponse: A response serving the synthesized audio file.
184+
FileResponse: A response serving the generated audio file.
170185
"""
171186
audio_path, _ = tts_engine.synthesize(utterance, **request.query_params)
172187
return FileResponse(audio_path)
@@ -197,14 +212,14 @@ async def synth_v2(request: Request) -> FileResponse:
197212

198213
def start_tts_server(tts_plugin: str, cache: bool = False) -> Tuple[FastAPI, TTSEngineWrapper]:
199214
"""
200-
Initialize TTS engine and create FastAPI app.
201-
202-
Args:
203-
tts_plugin: TTS plugin name to load.
204-
cache: Whether to persist cached audio across reboots.
205-
215+
Create and configure a FastAPI application wired to a TTSEngineWrapper for the specified plugin.
216+
217+
Parameters:
218+
tts_plugin (str): Name of the TTS plugin to load.
219+
cache (bool): If True, persist synthesized audio cache across restarts.
220+
206221
Returns:
207-
Tuple of FastAPI app and TTS engine wrapper.
222+
Tuple[FastAPI, TTSEngineWrapper]: The configured FastAPI app and the initialized TTS engine wrapper.
208223
"""
209224
tts_engine = TTSEngineWrapper(plugin_name=tts_plugin, cache=cache)
210225
app = create_app(tts_engine)

0 commit comments

Comments
 (0)