8000 feat: add option to use custom espeak data path by thewh1teagle · Pull Request #191 · bootphon/phonemizer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add option to use custom espeak data path #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions phonemizer/backend/espeak/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class EspeakAPI:

"""

def __init__(self, library: Union[str, Path]):
def __init__(self, library: Union[str, Path], data_path: Union[str, Path, None]):
# set to None to avoid an AttributeError in _delete if the __init__
# method raises, will be properly initialized below
self._library = None

if data_path is not None:
data_path = str(data_path).encode('utf-8')

# Because the library is not designed to be wrapped nor to be used in
# multithreaded/multiprocess contexts (massive use of global variables)
Expand Down Expand Up @@ -83,7 +86,7 @@ def __init__(self, library: Union[str, Path]):
# AUDIO_OUTPUT_SYNCHRONOUS in the espeak API
self._library = ctypes.cdll.LoadLibrary(str(espeak_copy))
try:
if self._library.espeak_Initialize(0x02, 0, None, 0) <= 0:
if self._library.espeak_Initialize(0x02, 0, data_path, 0) <= 0:
< 8000 /td> raise RuntimeError( # pragma: nocover
'failed to initialize espeak shared library')
except AttributeError: # pragma: nocover
Expand Down
51 changes: 48 additions & 3 deletions phonemizer/backend/espeak/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class EspeakWrapper:
# on the system. The user can choose an alternative espeak library with
# the method EspeakWrapper.set_library().
_ESPEAK_LIBRARY = None
_ESPEAK_DATA_PATH = None

def __init__(self):
# the following attributes are accessed through properties and are
Expand All @@ -57,7 +58,7 @@ def __init__(self):
self._voice = None

# load the espeak API
self._espeak = EspeakAPI(self.library())
self._espeak = EspeakAPI(self.library(), self.data_path)

# lazy loading of attributes only required for the synthetize method
self._libc_ = None
Expand Down Expand Up @@ -113,6 +114,21 @@ def set_library(cls, library: str):

"""
cls._ESPEAK_LIBRARY = library

@classmethod
def set_data_path(cls, data_path: str):
"""Sets the path for the data to be used by the espeak backend.

If this is not set, the backend uses the default data path from the system installation.

Parameters
----------
data_path : str
The path to the data to be used by the espeak backend. Set `data_path` to None
to restore the default.

"""
cls._ESPEAK_DATA_PATH = data_path

@classmethod
def library(cls):
Expand Down Expand Up @@ -179,8 +195,37 @@ def library_path(self):

@property
def data_path(self):
"""The espeak data directory as a pathlib.Path instance"""
if self._data_path is None:
"""Returns the espeak library used as backend

The following precedence rule applies for library lookup:

1. As specified by BaseEspeakBackend.set_library()
2. Or as specified by the environment variable
PHONEMIZER_ESPEAK_LIBRARY
3. Or the default espeak library found on the system

Raises
------
RuntimeError if the espeak library cannot be found or if the
environment variable PHONEMIZER_ESPEAK_LIBRARY is set to a
non-readable file

"""
if self._ESPEAK_DATA_PATH:
data_path = pathlib.Path(self._ESPEAK_DATA_PATH)
if not (data_path.is_dir() and os.access(self._ESPEAK_DATA_PATH, os.R_OK)):
raise RuntimeError(f'{self._ESPEAK_DATA_PATH} is not a readable directory')
self._data_path = data_path.resolve()
elif 'PHONEMIZER_ESPEAK_DATA_PATH' in os.environ:
data_path = pathlib.Path(os.environ['PHONEMIZER_ESPEAK_DATA_PATH'])
if not (data_path.is_dir() and os.access(data_path, os.R_OK)):
raise RuntimeError( # pragma: nocover
f'PHONEMIZER_ESPEAK_DATA_PATH={data_path} '
f'is not a readable directory')
self._data_path = data_path.resolve()

# Fetch path dynamically after initialize
if self._data_path is None and hasattr(self, '_espeak'):
self._fetch_version_and_path()
return self._data_path

Expand Down
0