diff --git a/CMakeLists.txt b/CMakeLists.txt index 487e3da..d378da6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,7 +97,12 @@ if(NOT DEFINED ONNXRUNTIME_DIR) endif() # ---- espeak-ng --- +set (ESPEAK_NG_REPO $ENV{ESPEAK_NG_REPO}) +if(NOT DEFINED ESPEAK_NG_REPO) + set(ESPEAK_NG_REPO "https://github.com/rhasspy/espeak-ng") +endif() +set(ESPEAK_NG_DIR $ENV{ESPEAK_NG_DIR}) if(NOT DEFINED ESPEAK_NG_DIR) set(ESPEAK_NG_DIR "${CMAKE_CURRENT_BINARY_DIR}/ei") diff --git a/piper_phonemize/__init__.py b/piper_phonemize/__init__.py index c6072ae..6068c28 100644 --- a/piper_phonemize/__init__.py +++ b/piper_phonemize/__init__.py @@ -1,3 +1,5 @@ +import os +import sys from collections import Counter from enum import Enum from pathlib import Path @@ -17,6 +19,8 @@ _DIR = Path(__file__).parent _TASHKEEL_MODEL = _DIR / "libtashkeel_model.ort" +espeak_data_path = sys.prefix / "share" / "espeak-ng-data" +os.environ["ESPEAK_DATA_PATH"] = str(espeak_data_path) class TextCasing(str, Enum): """Casing applied to text for phonemize_codepoints""" diff --git a/setup.py b/setup.py index 5028e71..fbe7a75 100644 --- a/setup.py +++ b/setup.py @@ -1,47 +1,97 @@ import platform +import shutil +import sys + from pathlib import Path # Available at setup time due to pyproject.toml from pybind11.setup_helpers import Pybind11Extension, build_ext +from setuptools.command.install import install from setuptools import setup -_DIR = Path(__file__).parent -_ESPEAK_DIR = _DIR / "install" - __version__ = "1.2.0" +_DIR = Path(__file__).parent + # Platform-specific settings if platform.system() == "Windows": - _LIB_DIR = _DIR / "lib" / "Windows-AMD64" - _ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime" - libraries = ["espeak-ng", "onnxruntime"] - extra_compile_args = ["/MT"] # Example for static linking the MSVC runtime - library_dirs = [str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")] - include_dirs = [str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")] + _ESPEAK_DIR = _DIR / "build" / "ei" + _LIB_DIR = _DIR / "lib" + _ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime-win-x64-1.14.1" else: + _ESPEAK_DIR = _DIR / "install" _LIB_DIR = _DIR / "lib" / f"Linux-{platform.machine()}" _ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime" - libraries = ["espeak-ng", "onnxruntime"] - extra_compile_args = [] # Linux/macOS might not need specific flags here - library_dirs = [str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")] - include_dirs = [str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")] -ext_modules = [ - Pybind11Extension( - "piper_phonemize_cpp", - [ - "src/python.cpp", - "src/phonemize.cpp", - "src/phoneme_ids.cpp", - "src/tashkeel.cpp", - ], - define_macros=[("VERSION_INFO", __version__)], - include_dirs=include_dirs, - library_dirs=library_dirs, - libraries=libraries, - extra_compile_args=extra_compile_args, - ), -] +if platform.system() == "Windows": + ext_modules = [ + Pybind11Extension( + "piper_phonemize_cpp", + [ + "src/python.cpp", + "src/phonemize.cpp", + "src/phoneme_ids.cpp", + "src/tashkeel.cpp", + ], + define_macros=[("VERSION_INFO", __version__)], + include_dirs=[ + str(_ESPEAK_DIR / "include/espeak-ng"), + str(_ONNXRUNTIME_DIR / "include"), + ], + library_dirs=[str(_ESPEAK_DIR / "bin"), str(_ONNXRUNTIME_DIR / "lib")], + libraries=["espeak-ng", "onnxruntime"], + ), + ] +else: + ext_modules = [ + Pybind11Extension( + "piper_phonemize_cpp", + [ + "src/python.cpp", + "src/phonemize.cpp", + "src/phoneme_ids.cpp", + "src/tashkeel.cpp", + ], + define_macros=[("VERSION_INFO", __version__)], + include_dirs=[str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")], + library_dirs=[str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")], + libraries=["espeak-ng", "onnxruntime"], + ), + ] + + +class CustomInstallCommand(install): + """Customized setuptools install command to copy espeak-ng-data.""" + + def run(self): + # Call superclass install command + super().run() + + # Check if the operating system is Windows + if platform.system() != "Windows": + print("Skipping custom installation steps: not running on Windows") + return + + # Define the source directory for espeak-ng-data + source_dir = Path(__file__).parent / "build" / "ei" / "share" / "espeak-ng-data" + + # Define the target directory within the Python environment's site-packages + target_dir = Path(sys.prefix) / "share" / "espeak-ng-data" + + # Check if source_dir exists to prevent copying errors + if source_dir.exists(): + # Use shutil.copytree to copy the entire directory + if target_dir.exists(): + shutil.rmtree( + target_dir + ) # Remove target if it already exists to avoid conflicts + shutil.copytree(source_dir, target_dir) + print(f"Copied espeak-ng-data to {target_dir}") + else: + print( + f"Source directory {source_dir} does not exist. espeak-ng-data not copied." + ) + setup( name="piper_phonemize", @@ -60,7 +110,7 @@ }, include_package_data=True, ext_modules=ext_modules, - cmdclass={"build_ext": build_ext}, + cmdclass={"build_ext": build_ext, "install": CustomInstallCommand}, zip_safe=False, python_requires=">=3.7", )