


How to use the Python third-party library gTTs/pyttsx3/speech
Python text to speech (research & finished product function)
Due to project needs, I need toconvert text to speech, so the first step is to conduct research
What is speech synthesis technology?
Speech synthesis (text to speech), referred to as TTS. It is a technology that converts text into speech. It allows the computer to simulate the human mouth and express what it wants to express through different timbres. It is part of the human-computer dialogue.
TTS can intelligently convert text into natural speech flow through the design of neural network. The greatly facilitates the use of visually impaired patients and also improves the readability of the text. TTS applications include speech-driven hardware and sound-sensitive systems, and are often used with voice recognition programs.
Now many manufacturers have launched their own speech synthesis services or APIs, and you can also check them out by yourself. This article only conducts research on third-party speech synthesis libraries in the Python environment
How to implement it with code?
As mentioned above, although there are many products on the market,As a developer, I want a free tool that can debug code , After searching for materials, I found that the gTTs library, pyttsx3 library, and speech library can meet my needs. Let's make a horizontal comparison, which can save everyone from taking detours.
Third-party library name | Requires internet connection | Supports Chinese and English | Supports Japanese | Adjustable speaking speed | Like human voice level |
---|---|---|---|---|---|
ggts | √ | √ | √ | X | is very similar to navigation |
pyttsx3 | X | √ | X | √ | Suitable for reading novels |
speech | X | √ | X | X | Much like faster navigation |
gTTS library
gTTS Library (Google Text-to-Speech): Used to interact with Google Translate's text-to-speech API. Write voice mp3 data to a file
Advantages: Supports multiple languages including Chinese, English and Japanese, supported by Google Translate API, the human voice is quite nice
Disadvantages: Does not support speech speed adjustment, must be used every time Scientific Internet access cannot be used on a stand-alone computer
In the voice playback function, we have chosen two methods
The first is to automatically play the audio in the playsound library (the playback progress cannot be adjusted)
The second is that the os library calls the system’s own player (adjustable progress)
Please seeplaysound library play& GTTS library to textfunction
# 函数功能: 用gtts库阅读文本,保存为.mp3文件后, 用系统内置的浏览器阅读出来, 打开mp3文件, 函数执行结束(播放方式为os库) def gtts_os_debug(text,mp3_filepath,language):#参数说明:参数1是朗读的文字,参数2是保存路径,参数3是数字{0英文,1中文,2日语} #大成功,可惜的是os调用自带播放器, 实际上只执行了"打开mp3"的操作, 它并不会在音频播报完后再进行下一条语句 from gtts import gTTS import os # 已知zh-tw版本违和感较高,所以我们用zh-CN来进行后续工作 if int(language) ==0 : s = gTTS(text=text, lang='en', tld='com') # s = gTTS(text=text, lang='en', tld='co.uk')#我比较喜欢美音,但是如果你喜欢英国口音可以尝试这个 elif int(language) ==1 : s = gTTS(text=text, lang='zh-CN') elif int(language) ==2 : s = gTTS(text=text, lang='ja') try: s.save(mp3_filepath) except: os.remove(mp3_filepath) print(mp3_filepath,"文件已经存在,但是没有关系!已经删掉了") s.save(mp3_filepath) print(mp3_filepath,"保存成功") os.system(mp3_filepath)#调用系统自带的播放器播放MP3 gtts_os_debug(text="I'm gtts library,from google Artificial Intelligence & Google Translate.",mp3_filepath="gtts英文测试.mp3",language=0) gtts_os_debug(text="我是gtts库, 你想听听我的声音吗",mp3_filepath="gtts中文测试.mp3",language=1) gtts_os_debug(text="真実はいつもひとつ" ,mp3_filepath="gtts日语测试.mp3",language=2)
Please seeos library playback & GTTS library to textfunction
# 函数功能: 用gtts库阅读文本,保存为.mp3文件后, 用playsound库阅读出来, 阅读完毕, 函数执行结束 def gtts_debug(text,mp3_filepath,language):#参数说明:参数1是朗读的文字,参数2是保存路径,参数3是数字{0英文,1中文,2日语} #大成功,已经实现了定制化文字转语音,但是播放的playsound需要改进(playsound库本身可能会出现bug...) from gtts import gTTS from playsound import playsound import os if int(language) ==0 : s = gTTS(text=text, lang='en', tld='com') # s = gTTS(text=text, lang='en', tld='co.uk')#我比较喜欢美音,但是如果你喜欢英国口音可以尝试这个 elif int(language) ==1 : s = gTTS(text=text, lang='zh-CN') elif int(language) ==2 : s = gTTS(text=text, lang='ja') try: s.save(mp3_filepath) except: os.remove(mp3_filepath) print(mp3_filepath,"文件已经存在,但是没有关系!已经删掉了") s.save(mp3_filepath) print(mp3_filepath,"保存成功") playsound(mp3_filepath) gtts_debug(text="I'm gtts library,from google Artificial Intelligence & Google Translate.",mp3_filepath="gtts英文测试.mp3",language=0) gtts_debug(text="我是gtts库, 你想听听我的声音吗",mp3_filepath="gtts中文测试.mp3",language=1) gtts_debug(text="真実はいつもひとつ" ,mp3_filepath="gtts日语测试.mp3",language=2)
pyttsx3 library
pyttsx3 library: It is a text-to-speech conversion library in Python, it can work offline
Advantages: Can work offline, supports direct speech reading, adjustable volume and speed
Disadvantages: Initially only English (Female) and Chinese (Female) voice packs, voice packs in other languages need to be downloaded separately
Please seepyttsx3 library to convert text & self-readingFunction
def pyttsx3_debug(text,language,rate,volume,filename,sayit=0): #参数说明: 六个重要参数,阅读的文字,语言(0-英文/1-中文),语速,音量(0-1),保存的文件名(以.mp3收尾),是否发言(0否1是) import pyttsx3 engine = pyttsx3.init() # 初始化语音引擎 engine.setProperty('rate', rate) # 设置语速 #速度调试结果:50戏剧化的慢,200正常,350用心听小说,500敷衍了事 engine.setProperty('volume', volume) # 设置音量 voices = engine.getProperty('voices') # 获取当前语音的详细信息 if int(language)==0: engine.setProperty('voice', voices[0].id) # 设置第一个语音合成器 #改变索引,改变声音。0中文,1英文(只有这两个选择) elif int(language)==1: engine.setProperty('voice', voices[1].id) if int(sayit)==1: engine.say(text) # pyttsx3->将结果念出来 elif int(sayit)==0: print("那我就不念了哈") engine.save_to_file(text, filename) # 保存音频文件 print(filename,"保存成功") engine.runAndWait() # pyttsx3结束语句(必须加) engine.stop() # pyttsx3结束语句(必须加) pyttsx3_debug(text="我是pyttsx3, 初次见面, 给您拜个早年",language=0,rate=200,volume=0.9,filename="ptttsx3中文测试.mp3",sayit=1) pyttsx3_debug(text="I'm fake Siri, your smart voice Manager",language=1,rate=200,volume=0.9,filename="ptttsx3英文测试.mp3",sayit=1)
speech Library
speech: Windows-based speech synthesis module, one line of code can realize reading
Advantages: Relying on Windows system, installation and use are extremely simple and super convenient.
Suitable for the code debugging process, let the cold AI language wake me up from writing bugs QAQ
Disadvantages: only system language (Chinese & English), does not support speech speed adjustment and audio export
Please seespeech to textfunction
import speech speech.say("甘霖娘,又出bug了") speech.say("Don't ask me .I have no idea why bug exist again") # 如你所见, 代码编译究极简单, 而且单机, 但是!每次使用都会呼出微软语音助手...
The above is the detailed content of How to use the Python third-party library gTTs/pyttsx3/speech. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Question: How to view the Redis server version? Use the command line tool redis-cli --version to view the version of the connected server. Use the INFO server command to view the server's internal version and need to parse and return information. In a cluster environment, check the version consistency of each node and can be automatically checked using scripts. Use scripts to automate viewing versions, such as connecting with Python scripts and printing version information.

Navicat's password security relies on the combination of symmetric encryption, password strength and security measures. Specific measures include: using SSL connections (provided that the database server supports and correctly configures the certificate), regularly updating Navicat, using more secure methods (such as SSH tunnels), restricting access rights, and most importantly, never record passwords.
