-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcTTS.py
More file actions
67 lines (54 loc) · 2.4 KB
/
Copy pathcTTS.py
File metadata and controls
67 lines (54 loc) · 2.4 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import json
import logging
import os
__version__ = '0.0.1'
# This module will not do any text cleaning (numbers, dates, ...)!
# Ideas for following versions:
# =============================
# Adding a function returning raw audio data instead saving to disk directly
# Adding librosa to offer functions for
# - getting samplerate
# - getting audio length
VALID_END_OF_PHRASE = ['.',';','!','?']
def prepareText(text,addStopChar):
"""Add a dot at the end of text if no valid end of phrase character is found.
This will avoid "MAX_DECODER_STEP" error and generation of weird voice output.
Args:
text (string): Text to be checked for valid end of phrase char.
addStopChar (boolean): Should line ending be checked.
Returns:
string: String with valid end of phrase character (.)
"""
if addStopChar and text[-1] not in VALID_END_OF_PHRASE:
text = text + "."
return text
def synthesize(text, speaker_name=None, url="http://localhost:5002", addStopChar=True):
"""Synthesize a text (no additional text cleaning!) using a Coqui TTS server.
Args:
text (string): Text to be synthesized.
speaker_name (string): speaker name for multispeaker models.
url (string): URL of Coqui TTS server (default: http://localhost:5002)
addStopChar (boolean): If true a dot will be added as last char if
last char is not a common line ending character to avoid
max_decoder_steps error. Defaults to true.
Returns:
boolean: "True" if audio could be retrieved from Coqui TTS server api. Otherwise "False"
"""
if len(text) == 0:
logging.error("No text has been specified.")
return False
try:
req = requests.get(url + "/api/tts", params={'text': prepareText(text, addStopChar), 'speaker_id': speaker_name if speaker_name else ''})
except Exception as e:
logging.error("Error calling Coqui TTS server api")
print(e)
return False
if req.status_code == 200 and req.headers['Content-Type'] == 'audio/wav':
logging.info("Valid audio has been returned from Coqui TTS api.")
#logging.info("Length of content {} bytes.", req.headers['Content-Length'])
#logging.info("Request took {} microseconds.", req.elapsed)
return req.content
else:
logging.warning("No audio has been returned from Coqui TTS server.")
return False