1. Introduction to pyttsx3
pyttsx3 is a Python library for implementing text-to-speech (TTS) functionality on various platforms. It is based on the underlying TTS engine and provides a simple and easy-to-use interface to convert text into sound output.
The following are some features and functions of pyttsx3:
- Cross-platform support: pyttsx3 supports multiple platforms, including Windows, Mac and Linux. This means you can use the same code for text-to-speech functionality on different operating systems.
- Multi-language support: The library supports multiple languages, including English, Chinese, French, German, Spanish, etc. You can choose different language settings according to your needs.
- Flexible sound control: pyttsx3 allows you to control the generated speech audio by adjusting parameters such as volume, speaking speed and pitch. You can customize these settings to suit your needs.
- Event-driven architecture: It adopts an event-driven design pattern and can listen for and process different events during the synthesis process. For example, you can capture events such as composition start, composition progress, and composition end.
- Asynchronous synthesis: pyttsx3 also supports asynchronous synthesis, which means you can perform text-to-speech synthesis in a background thread without blocking the execution of the main thread.
Installation command: `
conda:conda pip pyttsx3
pip:pip install pyttsx3
2. Convert text to audio
Code example:
import pyttsx3
def use_pyttsx3(text):
engine = pyttsx3.init() #初始化一个ptttsx3引擎
rate = engine.getProperty("rate") #获取engine的属性参数方法,有rate,volume,voice,voices,使用setProperty("",value)可以进行设置
engine.setProperty("rate",170) #设置rate语音速率为150
engine.setProperty("volue",1.0) #设置语音音量为1.0,范围为0-1
voices = engine.getProperty("voices")
#print(voices[0])
engine.setProperty("voice",voices[0].id)
#text = "黄河之水天上来,奔流到海不复回。"
a = engine.say(text) #进行转换文本至声音,并播放出来
engine.runAndWait() #启动引擎并等待语音播放完成。它会阻塞当前线程,直到语音播放完毕
engine.stop() #停止当前正在播放的语音
#将文本转成语言保存在本地:
#text:需要进行转换的文本
#filename:保存的文件路径
#name:可选参数,表示选择用于语音合成的声音类型。可以通过设置该参数来选择不同的语音。
engine.save_to_file(text,filename="ceshi.wav",name = 'test')
engine.runAndWait() #阻塞当前线程,等待保存完成
if __name__ == "__main__":
text = "注意看,这个男人是一个沸羊羊"
use_pyttsx3(text)
`