Tts

From WikiName
Jump to: navigation, search

Sample code for TTS implement by Python and SAPI

Requirement[edit]

Install comtypes to Python

pip install comtypes

Sample Code[edit]

from comtypes.client import CreateObject
from comtypes.gen import SpeechLib

engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")

infile = "test.txt"
outfile = "test-audio.mp3"

# read text file for input
f = open(infile, 'r')
theText = f.read()
f.close()

# select voice
engine.voice = engine.getvoices()[0]
engine.rate = -1
# Speak
engine.speak(theText)

# Save to file
stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(theText)
stream.Close()

Misc[edit]

To change voice pitch Add <pitch middle='X'> in front of speak text when 'X' in number of pitch

Notes[edit]

  1. This code test with Windows 10
  2. Voice depends on your OS