Speak Japanese text with OpenJTalk + python

Shows the method on Ubuntu and Windows. I'm sorry that Mac has no environment.

Talk with OpenJTalk + Python on Ubuntu 14.04 LTS

Open JTalk setup

Install open-jtalk (1.07) with ʻapt-get`.

Terminal


$ sudo apt-get install open-jtalk open-jtalk-mecab-naist-jdic hts-voice-nitech-jp-atr503-m001

The mecab dictionary is installed in/ var / lib / mecab / dic / open-jtalk / naist-jdic /.

Audio file setup

Voice data is diverted from MMDAgent. The MMDAgent_Example-1.6.zip is 11MB.

Terminal


$ wget https://sourceforge.net/projects/mmdagent/files/MMDAgent_Example/MMDAgent_Example-1.6/MMDAgent_Example-1.6.zip/download -O MMDAgent_Example-1.6.zip

Then extract the .htsvoice file

Terminal


$ unzip MMDAgent_Example-1.6.zip MMDAgent_Example-1.6/Voice/*

Copy the file to the same location as hts-voice-nitech

Terminal


$ sudo cp -r MMDAgent_Example-1.6/Voice/mei/ /usr/share/hts-voice

Playback with Python

http://raspi.seesaa.net/article/415530289.html I was allowed to refer to.

Save the following code as jtalk.py in a file.

jtalk.py


#coding: utf-8
import subprocess
from datetime import datetime

def jtalk(t):
    open_jtalk=['open_jtalk']
    mech=['-x','/var/lib/mecab/dic/open-jtalk/naist-jdic']
    htsvoice=['-m','/usr/share/hts-voice/mei/mei_normal.htsvoice']
    speed=['-r','1.0']
    outwav=['-ow','open_jtalk.wav']
    cmd=open_jtalk+mech+htsvoice+speed+outwav
    c = subprocess.Popen(cmd,stdin=subprocess.PIPE)
    c.stdin.write(t.encode())
    c.stdin.close()
    c.wait()
    aplay = ['aplay','-q','open_jtalk.wav']
    wr = subprocess.Popen(aplay)
    
def say_datetime():
    d = datetime.now()
    text = '%s month%s day,%s time%s minutes%s seconds' % (d.month, d.day, d.hour, d.minute, d.second)
    jtalk(text)

if __name__ == '__main__':
    say_datetime()

In the execution test

Terminal


$ python jtalk.py

Hopefully speak the current time When using from other modules

from_module_1.py


#coding: utf-8
import jtalk

jtalk.jtalk('Tell me something')

Set up Open JTalk on Windows

On Ubuntu, I could install it smoothly by doing ʻapt-get`, On Windows you build it yourself.

Obtaining the source code

Get OpenJTalk 1.09 from http://open-jtalk.sourceforge.net/

Download the speech synthesis library hts_engineAPI required for compilation from http://hts-engine.sourceforge.net/

If you expand ʻopen_jtalk-1.09.tar.gz to c: \ temp \ open_jtalk-1.09, then hts_engine_API-1.10.tar.gz is c: \ temp \ open_jtalk-1.09 \ hts_engine_API- Expanding to 1.10` will reduce problems when compiling.

Compile in Visual Studio

Launch the command tool in Visual Studio. Type nmake to make sure it works.

First, compile from hts_engine_API-1.10.

cmd


cd c:\temp\open_jtalk-1.09\hts_engine_API-1.10
nmake /f Makefile.mak
nmake /f Makefile.mak install

If it compiles successfully, a file will be created in c: \ hts_engine_API.

Next, compile open_jtalk.

cmd


cd c:\temp\open_jtalk-1.09
nmake /f Makefile.mak
nmake /f Makefile.mak install

When the character string to convert the dictionary is displayed near the end, the characters are garbled I'm curious, but if you're worried, Open JTalk has a prebuilt dictionary, so You can also use it.

This will generate c: \ open_jtalk \ bin \ open_jtalk.exe.

Get Voice data

From MMDAgent https://sourceforge.net/projects/mmdagent/files/MMDAgent_Example/MMDAgent_Example-1.6/ Download MMDAgent_Example-1.6.zip from, and copy Voice / mei / *. Htvoice to c: \ open_jtalk \ bin .

Execution test

Assuming that a file with Japanese input is created as input.txt in c: \ open_jtalk \ bin

cmd


c:\open\jtalk\bin
open_jtalk -m mei_normal.htsvoice -x ../dic -ow output.wav input.txt

I will try to execute. Now, if output.wav is created in the same folder, put it Double-click from Explorer to see if it plays.

Run OpenJTalk with Python on Windows

In ubuntu, I used the aplay command to play the file. On Windows Use the winsound module. Also, since the dictionary generation in this compilation on Windows was shift-jis, it is necessary to convert the encoding from the internal encoding of python when passing it to stdin.

Below, save the sample program as jtalk.py in c: \ open_jtalk \ bin.

jtalk.py


#coding: utf-8
# call OpenJTalk for windows
import subprocess
import winsound
from datetime import datetime


def jtalk(t):
    # depend on your install folder 
    OPENJTALK_BINPATH = 'c:/open_jtalk/bin'
    OPENJTALK_DICPATH = 'c:/open_jtalk/dic'
    OPENJTALK_VOICEPATH = 'c:/open_jtalk/bin/mei_normal.htsvoice'
    open_jtalk=[OPENJTALK_BINPATH + '/open_jtalk.exe']
    mech=['-x',OPENJTALK_DICPATH]
    htsvoice=['-m',OPENJTALK_VOICEPATH]
    speed=['-r','1.0']
    outwav=['-ow','open_jtalk.wav']
    cmd=open_jtalk+mech+htsvoice+speed+outwav
    c = subprocess.Popen(cmd,stdin=subprocess.PIPE)

    # convert text encoding from utf-8 to shitf-jis
    c.stdin.write(t.encode('shift-jis'))
    c.stdin.close()
    c.wait()

    # play wav audio file with winsound module
    winsound.PlaySound('open_jtalk.wav', winsound.SND_FILENAME)

    
def say_datetime():
    d = datetime.now()
    text = '%s month%s day,%s time%s minutes%s seconds' % (d.month, d.day, d.hour, d.minute, d.second)
    print(text)
    jtalk(text)

if __name__ == '__main__':
    say_datetime()

Do this. I have anaconda python installed, so

cmd


python jtalk.py

Then, speak the current date and time.

When using as a Python module

jtalk.py is saved in utf-8 encoding. When executing python from the command prompt, the Japanese character string is still Shift-JIS or is not unified.

Therefore, when entering Japanese, add ʻu" Japanese " and ʻu to indicate that it is utf-8.

from_module_2.py


import jtalk

jtalk.jtalk(u'I'll speak Japanese') 

Recommended Posts

Speak Japanese text with OpenJTalk + python
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
Speaking Japanese with OpenJtalk
Speaking Japanese with OpenJtalk (reading a text file)
Python: Japanese text: Morphological analysis
Japanese morphological analysis with Python
GOTO in Python with Sublime Text 3
Text extraction with AWS Textract (Python3.6)
Text mining with Python ① Morphological analysis
Enable Python raw_input with Sublime Text 3
[python] Extract text from pdf and read characters aloud with Open-Jtalk
English speech recognition with python [speech to text]
Generate Japanese test data with Python faker
Make Raspberry Pi speak Japanese using OpenJtalk
Notes on doing Japanese OCR with Python
Extract Japanese text from PDF with PDFMiner
How to display python Japanese with lolipop
[Python] Let's make matplotlib compatible with Japanese
How to enter Japanese with Python curses
Text mining with Python ② Visualization with Word Cloud
Read text in images with python OCR
Statistics with python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Japanese with matplotlib
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Extract text from PowerPoint with Python! (Compatible with tables)
Text extraction with GCP Cloud Vision API (Python3.6)
Try it with Word Cloud Japanese Python JupyterLab.
Speaking Japanese with gTTS (reading a text file)
Create a python3 build environment with Sublime Text3
Handle zip files with Japanese filenames in Python 3
Create an image with characters in python (Japanese)
Extract zip with Python (Japanese file name support)
Wav file generation from numeric text with python
[Python] How to handle Japanese characters with openCV
Clustering text in Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Text mining with Python ① Morphological analysis (re: Linux version)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1