Windowsでお手軽Text to SpeechするPython
4442 ワード
環境
> ver
Microsoft Windows [Version 10.0.19042.868]
> python --version
Python 3.9.1
>jupyter lab --version
3.0.7
最近1日1回はAdoを聴いています。
import win32com.client
speaker = win32com.client.Dispatch('SAPI.SpVoice')
speaker.Speak('正しさとは、愚かさとは、それが何か見せつけてやる')
歌ってくれませんが喋ります。意外と大きな声でびっくりする場合があります。
小声で早口にしてもらいます(音量30%、速さの段階4)。
speaker.Volume = 30 # [0 to 100]
speaker.Rate = 4 # [-10 to 10]
speaker.Speak('正しさとは、愚かさとは、それが何か見せつけてやる')
速さ6にするとあまり聞こえなくなってきます。速さ10は速すぎて聞き取れません。音量0も当然小さすぎて聞き取れません。
応用として現在時刻を喋らせてみるには次のとおりです。
import datetime
speaker.Speak(datetime.datetime.now().strftime('%H時%M分です'))
しゃべる時計アプリも簡単に作れますね。
ファイルに書き込むにはこうします。Jupyterを使っているならブラウザ上で何度も再生が試せます。
from comtypes.client import CreateObject
engine = CreateObject('SAPI.SpVoice')
stream = CreateObject('SAPI.SpFileStream')
from comtypes.gen import SpeechLib
stream.Open('tts.wav', SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak('正しさとは、愚かさとは、それが何か見せつけてやる')
stream.close()
from IPython.display import Audio
Audio('tts.wav')
Rateをいじると再生時間が短くなっています。音声ファイルや動画もQiitaに貼れるようになると良いなぁ。
Author And Source
この問題について(Windowsでお手軽Text to SpeechするPython), 我々は、より多くの情報をここで見つけました https://qiita.com/masudaryo/items/4b5f240a13bebfb5e4b6著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .