basettswrapper¶
This module contains the following classes:
TTSCache
, a TTS cache;BaseTTSWrapper
, an abstract wrapper for a TTS engine.
-
class
aeneas.ttswrappers.basettswrapper.
BaseTTSWrapper
(rconf=None, logger=None)[source]¶ An abstract wrapper for a TTS engine.
It calls the TTS executable or library, passing parameters like the text string and languages, and it produces a WAVE file on disk and a list of time anchors.
In case of multiple text fragments, the resulting WAVE files will be joined together in a single WAVE file.
The TTS parameters, their order, and the switches can be configured in the concrete subclass for a specific TTS engine.
For example, it might perform one or more calls like
$ echo "text" | tts -v voice_code -w output_file.wav or $ tts -eval "(voice_code)" -i text_file.txt -o output_file.wav
The call methods will be attempted in the following order:
- direct Python call
- Python C extension
- TTS executable via
subprocess
Parameters: - rconf (
RuntimeConfiguration
) – a runtime configuration - logger (
Logger
) – the logger object
Raises: NotImplementedError: if none of the call methods is available
-
CLI_PARAMETER_TEXT_PATH
= 'TEXT_PATH'¶ Placeholder to specify the path to the UTF-8 encoded file containing the text to be synthesized, to be read by the TTS engine.
-
CLI_PARAMETER_TEXT_STDIN
= 'TEXT_STDIN'¶ Placeholder to specify that the TTS engine reads the text to be synthesized from stdin.
-
CLI_PARAMETER_VOICE_CODE_FUNCTION
= 'VOICE_CODE_FUNCTION'¶ Placeholder to specify a list of arguments for the TTS engine to select the TTS voice to be used for synthesizing the text.
-
CLI_PARAMETER_VOICE_CODE_STRING
= 'VOICE_CODE_STRING'¶ Placeholder for the voice code string.
-
CLI_PARAMETER_WAVE_PATH
= 'WAVE_PATH'¶ Placeholder to specify the path to the audio file to be synthesized by the TTS engine.
-
CLI_PARAMETER_WAVE_STDOUT
= 'WAVE_STDOUT'¶ Placeholder to specify that the TTS engine outputs the audio data to stdout.
-
CODE_TO_HUMAN
= {}¶ Map from voice code to human-readable name.
-
CODE_TO_HUMAN_LIST
= []¶ List of all language codes with their human-readable names.
-
C_EXTENSION_NAME
= ''¶ If the TTS wrapper can invoke the TTS engine via a C extension call, set here the name of the corresponding Python C/C++ extension.
-
DEFAULT_LANGUAGE
= None¶ The default language for this TTS engine. Concrete subclasses must populate this class field, according to the languages supported by the TTS engine they wrap.
-
DEFAULT_TTS_PATH
= None¶ The default path for this TTS engine, when called via
subprocess
, otherwise set it toNone
.
-
HAS_C_EXTENSION_CALL
= False¶ If
True
, the TTS wrapper can invoke the TTS engine via a C extension call.
-
HAS_PYTHON_CALL
= False¶ If
True
, the TTS wrapper can invoke the TTS engine via a direct Python call.
-
HAS_SUBPROCESS_CALL
= False¶ If
True
, the TTS wrapper can invoke the TTS engine viasubprocess
.
-
LANGUAGE_TO_VOICE_CODE
= {}¶ Map a language code to a voice code. Concrete subclasses must populate this class field, according to the language and voice codes supported by the TTS engine they wrap.
-
OUTPUT_AUDIO_FORMAT
= None¶ A tuple
(codec, channels, rate)
specifying the format of the audio file generated by the TTS engine, for example("pcm_s16le", 1, 22050)
. If unknown, set it toNone
: in this case, the audio file will be converted to PCM16 mono WAVE (RIFF) as needed.
-
clear_cache
()[source]¶ Clear the TTS cache, removing all cache files from disk.
New in version 1.6.0.
-
set_subprocess_arguments
(subprocess_arguments)[source]¶ Set the list of arguments that the wrapper will pass to
subprocess
.Placeholders
CLI_PARAMETER_*
can be used, and they will be replaced by actual values in the_synthesize_multiple_subprocess()
and_synthesize_single_subprocess()
built-in functions. Literal parameters will be passed unchanged.The list should start with the path to the TTS engine.
This function should be called in the constructor of concrete subclasses.
Parameters: subprocess_arguments (list) – the list of arguments to be passed to the TTS engine via subprocess
-
synthesize_multiple
(text_file, output_file_path, quit_after=None, backwards=False)[source]¶ Synthesize the text contained in the given fragment list into a WAVE file.
Return a tuple (anchors, total_time, num_chars).
Concrete subclasses must implement at least one of the following private functions:
_synthesize_multiple_python()
_synthesize_multiple_c_extension()
_synthesize_multiple_subprocess()
Parameters: Return type: tuple (anchors, total_time, num_chars)
Raises: TypeError: if
text_file
isNone
or one of the text fragments is not a Unicode stringRaises: ValueError: if
self.rconf[RuntimeConfiguration.ALLOW_UNLISTED_LANGUAGES]
isFalse
and a fragment has a language code not supported by the TTS engine, or iftext_file
has no fragments or all its fragments are emptyRaises: OSError: if output file cannot be written to
output_file_path
Raises: RuntimeError: if both the C extension and the pure Python code did not succeed.
-
class
aeneas.ttswrappers.basettswrapper.
TTSCache
(rconf=None, logger=None)[source]¶ A TTS cache, that is, a dictionary whose keys are pairs
(fragment_language, fragment_text)
and whose values are pairs(file_handler, file_path)
.An item in the cache means that the text of the key has been synthesized to the file located at the path of the corresponding value.
Note that it is not enough to store the string of the text as the key, since the same text might be pronounced in a different language.
Also note that the values also store the file handler, since we might want to close it explicitly before removing the file from disk.
Parameters: - rconf (
RuntimeConfiguration
) – a runtime configuration - logger (
Logger
) – the logger object
-
add
(fragment_info, file_info)[source]¶ Add the given
(key, value)
pair to the cache.Parameters: - fragment_info (tuple of str
(language, text)
) – the text key - file_info (tuple
(handler, path)
) – the path value
Raises: ValueError if the key is already present in the cache
- fragment_info (tuple of str
-
get
(fragment_info)[source]¶ Get the value associated with the given key.
Parameters: fragment_info (tuple of str (language, text)
) – the text keyRaises: KeyError if the key is not present in the cache
- rconf (