千锋扣丁学堂Python培训之利用pyttsx3文字转语音过程详解

2019-09-25 14:59:08 3330浏览

今天千锋扣丁学堂Python培训老师给大家分享一篇关于利用pyttsx3文字转语音过程详解,文中通过示例代码介绍的非常详细,下面我们一起来看一下吧。



# -*- coding: utf-8 -*-
import pyttsx3
engine = pyttsx3.init()
with open("all.txt",'r',encoding='utf-8') as f:
  while 1:
    line = f.readline()
    print(line, end = '')
    engine.say(line)
    engine.runAndWait()

import pyttsx3
with open('all.txt','r',encoding='utf-8') as f:
  line = f.read()#文件不大,一次性读取
  engine = pyttsx3.init()
  #调整频率
  rate = engine.getProperty('rate')
  engine.setProperty('rate', rate-50)
  # 调整音量
  volume = engine.getProperty('volume')
  engine.setProperty('volume', volume+0.25)
  engine.say(line)
  engine.runAndWait()

安装

pip install pyttsx3

语音引擎工厂

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

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

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

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

sapi5-SAPI5onWindows

nsss-NSSpeechSynthesizeronMacOSX

espeak-eSpeakoneveryotherplatform

2.debug:这第二个参数是指定要不要以调试状态输出,建议开发阶段设置为True


元数据音调

在pyttsx3.voice.Voice中,处理合成器的发音。

age

发音人的年龄,默认为None

gender

以字符串为类型的发音人性别:male,female,orneutral.默认为None

id

关于Voice的字符串确认信息.通过pyttsx3.engine.Engine.setPropertyValue()来设置活动发音签名.这个属性总是被定义。

languages

发音支持的语言列表,如果没有,则为一个空的列表。

name

发音人名称,默认为None.

更多测试

朗读文本

import pyttsx3
engine = pyttsx3.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

事件监听

import pyttsx3
def onStart(name):
 print 'starting', name
 
def onWord(name, location, length):
 print 'word', name, location, length
 
def onEnd(name, completed):
 print 'finishing', name, completed
 
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

打断发音

import pyttsx3
def onWord(name, location, length):
 print('word', name, location, length)
 if location > 10:
  engine.stop()
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

更换发音人声音

engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
 engine.setProperty('voice', voice.id)
 engine.say('The quick brown fox jumped over the lazy dog.')
 
engine.runAndWait()

语速控制

engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

音量控制

engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

执行一个事件驱动循环

engine = pyttsx3.init()
def onStart(name):
 print('starting', name)
 
def onWord(name, location, length):
 print('word', name, location, length)
 
def onEnd(name, completed):
 print('finishing', name, completed)
 if name == 'fox':
  engine.say('What a lazy dog!', 'dog')
 elif name == 'dog':
  engine.endLoop()
 
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop()

使用一个外部的驱动循环

engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop(False)
# engine.iterate() must be called inside externalLoop()
externalLoop()
engine.endLoop()

以上就是关于千锋扣丁学堂Python培训之利用pyttsx3文字转语音过程详解的全部内容,希望对大家的学习有所帮助,想要了解更多关于Python和人工智能方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Python开发环境搭建视频,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


      【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】



查看更多关于"Python开发资讯"的相关文章>

标签: Python培训 Python视频教程 Python在线视频 Python学习视频 Python培训班

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号