basettswrapper

This module contains the following classes:

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:

  1. direct Python call
  2. Python C extension
  3. TTS executable via subprocess
Parameters:
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 to None.

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 via subprocess.

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 to None: 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:

  1. _synthesize_multiple_python()
  2. _synthesize_multiple_c_extension()
  3. _synthesize_multiple_subprocess()
Parameters:
  • text_file (TextFile) – the text file to be synthesized
  • output_file_path (string) – the path to the output audio file
  • quit_after (TimeValue) – stop synthesizing as soon as reaching this many seconds
  • backwards (bool) – if > 0, synthesize from the end of the text file
Return type:

tuple (anchors, total_time, num_chars)

Raises:

TypeError: if text_file is None or one of the text fragments is not a Unicode string

Raises:

ValueError: if self.rconf[RuntimeConfiguration.ALLOW_UNLISTED_LANGUAGES] is False and a fragment has a language code not supported by the TTS engine, or if text_file has no fragments or all its fragments are empty

Raises:

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:
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

clear()[source]

Clear the cache and remove all the files from disk.

get(fragment_info)[source]

Get the value associated with the given key.

Parameters:fragment_info (tuple of str (language, text)) – the text key
Raises:KeyError if the key is not present in the cache
is_cached(fragment_info)[source]

Return True if the given (language, text) key is present in the cache, or False otherwise.

Return type:bool
keys()[source]

Return the sorted list of keys currently in the cache.

Return type:list of tuples (language, text)