LoginSignup
5
3

More than 1 year has passed since last update.

Text to speech with python

Last updated at Posted at 2022-05-23

初めに

PythonでText to Speechを実装してみました。

設置

pip install pyttsx3

If you are ubuntu user, you need to install libespeak-dev package.

sudo apt install libespeak-dev

使い方

とても簡単です。

import pyttsx3
#初期化
engine = pyttsx3.init()

#声の選択
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)

#喋りのレートを設定
engine.setProperty('rate', 150)

#喋らせる
engine.say('おはようございます!')

engine.runAndWait()

Voiceの確認。個人のパソコンの環境によって異なります。下記のプログラムでどんな言語に対応する声が用意されているか確認が可能です。

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')

print(len(voices))

for voice in voices:
    print(f'voice: {voice.name}')
    print(f'id: {voice.id}')
    print('')

私の環境では、三つの言語、声が用意されていました。

index[0] : 日本語 ハルカさん
index[1] : 英語 Ziraさん
index[2] :韓国語 Heamiさん

3

voice: Microsoft Haruka Desktop - Japanese
id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_JA-JP_HARUKA_11.0

voice: Microsoft Zira Desktop - English (United States)
id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0

voice: Microsoft Heami Desktop - Korean
id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_KO-KR_HEAMI_11.0

使い勝手を良くするため、speak関数を定義します。

def speak(str):
    engine.say(str)
    engine.runAndWait()

# 全体コード

import pyttsx3

#初期化
engine = pyttsx3.init()
voices = engine.getProperty('voices')
#声の選択
engine.setProperty('voice',voices[0].id)
print(voices[0].id)

#喋りのレートを設定
engine.setProperty('rate', 150)

def speak(str):
    engine.say(str)
    engine.runAndWait()

speak('おはようございます。')
speak('巨人軍は永久不滅です。')
speak('横浜ベイスターズ、頑張ってください。')

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3