python使用pyttsx3实现语音合成

xiaoxiao2022-06-03  11

目录

1、使用pip安装pyttsx3库

2、使用pip安装或升级PyObjC

3、简单的测试例子1

4、例子2

总结:使用pyttsx,我们可以借助其强大的API来实现我们基本的业务需求。很酷吧。


系统:mac

1、使用pip安装pyttsx3库

pip install pyttsx3

2、使用pip安装或升级PyObjC

pip install -U pyobjc

3、简单的测试例子1

# coding:utf-8 import pyttsx3 engine = pyttsx3.init() engine.say('hello world') engine.say('你好,世界') engine.runAndWait() # 朗读一次 # engine.endLoop()

4、例子2

# coding:utf-8 import pyttsx3 # 初始化 engine = pyttsx3.init() voices = engine.getProperty('voices') # 语速控制 rate = engine.getProperty('rate') print(rate) engine.setProperty('rate', rate-20) # 音量控制 volume = engine.getProperty('volume') print(volume) engine.setProperty('volume', volume-0.25) engine.say('hello world') engine.say('你好,世界') # 朗读一次 engine.runAndWait() engine.say('语音合成开始') engine.say('我会说中文了,开森,开森') engine.runAndWait() engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() # 更换语种--zh_HK engine.setProperty('voice', "com.apple.speech.synthesis.voice.sin-ji") engine.setProperty('voice', "VoiceGenderMale") engine.say('从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。') # 更换语种--zh_CN engine.setProperty('voice', "com.apple.speech.synthesis.voice.ting-ting.premium") engine.say('从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。') # 更换语种--zh_TW engine.setProperty('voice', "com.apple.speech.synthesis.voice.mei-jia") engine.say('从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。') engine.runAndWait() for voice in voices: print(voice) engine.setProperty("voice",voice.id) # engine.endLoop()

总结: 使用pyttsx,我们可以借助其强大的API来实现我们基本的业务需求。很酷吧。

pyttsx深入研究 做完上面的小实验,你肯定会觉得怎么这么不过瘾呢?  别担心,下面我们就一起走进pyttsx的世界,深入的研究一下其工作原理吧。

语音引擎工厂 类似于设计模式中的“工厂模式”,pyttsx通过初始化来获取语音引擎。当我们第一次调用init操作的时候,会返回一个pyttsx的engine对象,再次调用的时候,如果存在engine对象实例,就会使用现有的,否则再重新创建一个。

pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。如下:

1、drivename:由pyttsx.driver模块根据操作系统类型来调用,默认使用当前操作系统可以使用的最好的驱动

       sapi5 - SAPI5 on Windows        nsss - NSSpeechSynthesizer on Mac OS X        espeak - eSpeak on every other platform 2、debug: 这第二个参数是指定要不要以调试状态输出,建议开发阶段设置为True  

 

转载请注明原文地址: https://www.6miu.com/read-4915034.html

最新回复(0)