Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

15.2. TTS(Text To Speech) 文本转语音

pyttsx3 - 语音朗读

TTS(Text To Speech) 译为从文本到语音,TTS是人工智能AI的一个模组,是人机对话的一部分,即让机器能够说话。

TTS是语音合成技术应用的一种,首先采集语音波形,然后进行优化处理,最后存储在数据库中,合成语音是提取波形转换成自然语音输出。

TTS 有哪些应用场景

  1. TTS 能帮助有视觉障碍的人阅读计算机上的信息
  2. 懒人听书,很多人没有时间读书,我们可以通过TTS将书中的内容朗读出来
  3. 与声音识别程序一起使用,实现人机交互,例如客服系统的对话脚本
  4. 不方便视觉交互场景,例如驾驶汽车,我们可以将短信朗读出来,来电电话号码朗读出来
  5. 公交车报站

15.2.1. 安装 pyttsx3

			
pip install pyttsx3			
			
			

15.2.1.1. Linux

				
[root@gitlab ~]# dnf install espeak-ng				
				
				

				
libespeak.so.1: cannot open shared object file: No such file or directory				
				
				

15.2.2. 演示

			
#coding=utf-8
import pyttsx3
pyttsx3.speak("Hello World!")			
			
			

15.2.3. 方法详解

15.2.3.1. say() 方法

speak() 实际上是下面代码的封装

				
#coding=utf-8
import pyttsx3
engine = pyttsx3.init()
engine.say("Hello World!")
engine.runAndWait()				
				
				

15.2.3.2. save_to_file()

				
engine.save_to_file(text, 'test.mp3')				
				
				

15.2.3.3. 调整人声类型

男性(voices[0].id)、女性(voices[1].id)

				
voices = engine.getProperty('voices')  
engine.setProperty('voice', voices[0].id)
				
				

15.2.3.4. 调整语速

一般范围一般在0~500之间

				
rate = engine.getProperty('rate')
engine.setProperty('rate', 200)    
				
				

15.2.3.5. 调整声量

范围在0~1之间

				
volume = engine.getProperty('volume')                         
engine.setProperty('volume',0.8) 
				
				

15.2.3.6. 查看语音引擎

				
voices = engine.getProperty('voices') 
for item in voices:
    print(item)				
				
				

15.2.4. 例子

			
import pyttsx3
engine = pyttsx3.init() # object creation

""" RATE"""
rate = engine.getProperty('rate')   # getting details of current speaking rate
print (rate)                        #printing current voice rate
engine.setProperty('rate', 125)     # setting up new voice rate


"""VOLUME"""
volume = engine.getProperty('volume')   #getting to know current volume level (min=0 and max=1)
print (volume)                          #printing current volume level
engine.setProperty('volume',1.0)    # setting up volume level  between 0 and 1

"""VOICE"""
voices = engine.getProperty('voices')       #getting details of current voice
#engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id)   #changing index, changes voices. 1 for female

engine.say("Hello World!")
engine.say('My current speaking rate is ' + str(rate))
engine.runAndWait()
engine.stop()

"""Saving Voice to a file"""
# On linux make sure that 'espeak' and 'ffmpeg' are installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()