Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Last active March 14, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tos-kamiya/b4e770a9f98b7b98a8feb84b2223b85e to your computer and use it in GitHub Desktop.
Save tos-kamiya/b4e770a9f98b7b98a8feb84b2223b85e to your computer and use it in GitHub Desktop.
jtalkを用いた読み上げコマンド
#!/usr/bin/env python3
# 参考
# https://qiita.com/kkoba84/items/b828229c374a249965a9
# https://mekou.com/linux-magazine/open-jtalklinux-%E6%97%A5%E6%9C%AC%E8%AA%9E%E9%9F%B3%E5%A3%B0%E5%90%88%E6%88%90%E3%82%A8%E3%83%B3%E3%82%B8%E3%83%B3/
# Ubuntu 20.04で動作検証
SETUP = """
# (1) Docoptのインストール
sudo python3 -m pip install docopt
# (2) Open JTalkのセットアップ
sudo apt-get install open-jtalk open-jtalk-mecab-naist-jdic hts-voice-nitech-jp-atr503-m001
# (3) 音声ファイルのセットアップ
wget https://sourceforge.net/projects/mmdagent/files/MMDAgent_Example/MMDAgent_Example-1.8/MMDAgent_Example-1.8.zip/download -O MMDAgent_Example-1.8.zip
unzip MMDAgent_Example-1.8.zip
sudo cp -r MMDAgent_Example-1.8/Voice/mei/ /usr/share/hts-voice
"""
import re
import sys
import subprocess
from docopt import docopt
wav_file_template = "/tmp/open_jtalk_%d.wav"
def speech_lines(lines, show_text=False):
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']
wav_gen_cmd = open_jtalk + mech + htsvoice + speed + outwav
play_cmd = ['aplay','-q']
speech_process = None
for i, L in enumerate(lines):
wav_file = wav_file_template % (i % 2)
c = subprocess.Popen(wav_gen_cmd + [wav_file], stdin=subprocess.PIPE)
c.stdin.write(L.encode())
c.stdin.close()
c.wait()
if speech_process is not None:
speech_process.wait()
if show_text:
print(L, file=sys.stderr)
speech_process= subprocess.Popen(play_cmd + [wav_file])
if speech_process is not None:
speech_process.wait()
def now_text():
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
d = datetime.now()
wd = d.strftime('%A') # 曜日
text = '%s月%s日%s、%s時%s分%s秒' % (d.month, d.day, wd, d.hour, d.minute, d.second)
return text
__doc__ = """日本語テキストを読み上げます。
Usage:
jtalk [options] [<textfile>]
Options:
-N 改行で文を区切らないようにする
-t 発声されているテキストを表示する
"""
def main():
args = docopt(__doc__)
if not args['<textfile>']:
text = now_text()
elif args['<textfile>'] == '-':
text = sys.stdin.read()
else:
with open(args['<textfile>']) as inp:
text = inp.read()
if args['-N']:
text = text.replace('\n\n', '。')
text = text.replace('\n', '')
lines = re.split(r"。", text)
else:
lines = re.split(r"[。\n]", text)
lines = [L for L in lines if L]
speech_lines(lines, show_text=args['-t'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment