pyopenjtalk

repository·master·Indexed 42 days ago

https://github.com/r9y9/pyopenjtalk

A Python wrapper for OpenJTalk providing a text processing frontend and a speech synthesis (TTS) backend using HTSEngine. It supports Japanese linguistic analysis, Grapheme-to-Phoneme (G2P) conversion, and voice synthesis. Features include a high-level API for functions like tts(), g2p(), and extract_fullcontext(), as well as support for DNN-based accent estimation via the marine extra.

Tokens
3K
Snippets
17
Records
24
Agent score
54%

What's inside pyopenjtalk

  1. Use the high-level pyopenjtalk API

    master

    The pyopenjtalk module provides a high-level functional interface for text processing and waveform synthesis. Most common tasks can be performed using these top-level functions, which internally manage global instances of pyopenjtalk.openjtalk.OpenJTalk and pyopenjtalk.htsengine.HTSEngine for ease of use.

    Key high-level functions include:

    • tts: Performs Text-to-Speech synthesis.
    • g2p: Performs Grapheme-to-Phoneme conversion.
    • extract_fullcontext: Extracts full context information from text.
    • synthesize: Synthesizes audio from text.
  2. Manually instantiate OpenJTalk and HTSEngine for full control

    master

    While the high-level API uses global instances for convenience, you should manually instantiate pyopenjtalk.openjtalk.OpenJTalk and pyopenjtalk.htsengine.HTSEngine if you require advanced control, such as:

    • Using an external dictionary.
    • Using a specific htsvoice.
  3. Use the `run_marine` option for accent estimation

    master

    If you have installed pyopenjtalk[marine], you can use the run_marine=True flag to estimate Japanese accent using a DNN-based method. This can be applied to both TTS and text processing:

    import pyopenjtalk
    
    # For TTS
    x, sr = pyopenjtalk.tts("おめでとうございます", run_marine=True)
    
    # For text processing
    labels = pyopenjtalk.extract_fullcontext("こんにちは", run_marine=True)
    import pyopenjtalk
    
    x, sr = pyopenjtalk.tts("おめでとうございます", run_marine=True)
    label = pyopenjtalk.extract_fullcontext("こんにちは", run_marine=True)
  4. Create and apply a user dictionary

    master

    You can add custom words to the dictionary to improve pronunciation and recognition.

    1. Prepare a CSV file (e.g., user.csv) with the custom word entry following the MeCab dictionary format:

      GNU,,,1,名詞,一般,*,*,*,*,GNU,グヌー,グヌー,2/3,*
    2. Compile the CSV into a dictionary file using mecab_dict_index():

      import pyopenjtalk
      pyopenjtalk.mecab_dict_index("user.csv", "user.dic")
    3. Apply the dictionary using update_global_jtalk_with_user_dict():

      import pyopenjtalk
      pyopenjtalk.update_global_jtalk_with_user_dict("user.dic")
    import pyopenjtalk
    
    # 1. Compile
    pyopenjtalk.mecab_dict_index("user.csv", "user.dic")
    
    # 2. Apply
    pyopenjtalk.update_global_jtalk_with_user_dict("user.dic")
  5. Install pyopenjtalk

    master

    Install the standard version of pyopenjtalk using pip:

    pip install pyopenjtalk

    Build Requirements

    Because the package uses Cython to create Python bindings for open_jtalk and hts_engine_API, you must have the following installed on your system:

    • C/C++ compilers
    • cmake
    • cython

    Supported Platforms

    • Linux
    • Mac OSX
    • Windows (MSVC)
  6. Fix ValueError: numpy.ndarray size changed

    master

    If you encounter a ValueError: numpy.ndarray size changed, may indicate binary incompatibility (e.g., Expected 96 from C header, got 88 from PyObject), it is due to NumPy's ABI breaking changes.

    To resolve this, ensure numpy is installed in your environment first, then install pyopenjtalk using the --no-build-isolation flag. This prevents pip from creating a separate build environment and forces it to use your pre-installed NumPy version during the build process.

    # Install numpy first
    pip install numpy
    
    # Then install pyopenjtalk without build isolation
    pip install pyopenjtalk --no-build-isolation
    
    # Or install directly from the GitHub repository
    pip install git+https://github.com/r9y9/pyopenjtalk --no-build-isolation
  7. Convert Grapheme-to-Phoneme (G2P)

    master

    Convert text to its phonetic representation using pyopenjtalk.g2p(). You can optionally pass kana=True to get the output in Katakana.

    import pyopenjtalk
    
    # Default output (Latin characters)
    print(pyopenjtalk.g2p("こんにちは")) # 'k o N n i ch i w a'
    
    # Kana output
    print(pyopenjtalk.g2p("こんにちは", kana=True)) # 'コンニチワ'
    import pyopenjtalk
    
    pyopenjtalk.g2p("こんにちは")
    pyopenjtalk.g2p("こんにちは", kana=True)
  8. Perform Text-to-Speech (TTS)

    master

    Use pyopenjtalk.tts() to synthesize speech from text. It returns a tuple containing the audio waveform (as a NumPy array) and the sampling rate.

    import pyopenjtalk
    import numpy as np
    from scipy.io import wavfile
    
    # Synthesize audio
    x, sr = pyopenjtalk.tts("おめでとうございます")
    
    # Save to a WAV file
    wavfile.write("test.wav", sr, x.astype(np.int16))
    import pyopenjtalk
    from scipy.io import wavfile
    import numpy as np
    
    x, sr = pyopenjtalk.tts("おめでとうございます")
    wavfile.write("test.wav", sr, x.astype(np.int16))
  9. Run text processing frontend only

    master

    If you only need the text processing capabilities (without speech synthesis), use pyopenjtalk.extract_fullcontext(). This returns a list of full-context labels used for linguistic analysis.

    import pyopenjtalk
    
    labels = pyopenjtalk.extract_fullcontext("こんにちは")
    print(labels)
    import pyopenjtalk
    
    pyopenjtalk.extract_fullcontext("こんにちは")