Python reader, text-to-speech----have you got new skills

"  Text-to-speech----have you got the new skill "

I like reading novels, I don’t know why, it’s a mystery, (because I’m a mysterious man, haha), I feel my eyelids fighting when I look at it, and suddenly I think, if only someone could read it ( Here we obviously feel that the editor is derailed from the world), it is better to write an automatic reading software, and then there will be a voice reading artifact.

01—Text to speech

 

Before this, we have mentioned about speech-to-text, and in the article about text-to-speech, today we will look at the specific implementation of text-to-speech.

First, let's prepare the materials:

1. Prepare a string of text

Okay, no more.

 

 

02—Programming Ideas

Then there is the idea. We must be very clear about what we do. The same words can be used at any time.

Analysis: We need to convert text to voice, what do we need, how do we do it, and what to convert.

After a little research, we found a simple solution.

Idea analysis: We first call a third-party interface or something, convert the text into voice, and then play it out, it's not enough.

 

 

03

Start---text to speech

Then we will start. First, we call the api of a certain degree, and then let him pass the voice over. Here, let’s use Baidu Yaya’s voice!

 

Look at the specific code:

import sys
import json
import webbrowser
import os
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.parse import quote_plus
API_KEY = '4E1BG9lTnlSeIf1NQFlrSq6h'
SECRET_KEY = '544ca4657ba8002e3dea3ac2f5fdd241'
# 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
PER = 4
# 语速,取值0-15,默认为5中语速
SPD = 5
# 音调,取值0-15,默认为5中语调
PIT = 5
# 音量,取值0-9,默认为5中音量
VOL = 5
# 下载的文件格式, 3:mp3(default) 4: pcm-16k 5: pcm-8k 6. wav
AUE = 3
FORMATS = {3: "mp3", 4: "pcm", 5: "pcm", 6: "wav"}
FORMAT = FORMATS[AUE]
CUID = "123456PYTHON"
TTS_URL = 'http://tsn.baidu.com/text2audio'
class DemoError(Exception):
    pass
"""  TOKEN start """
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
SCOPE = 'audio_tts_post'  # 有此scope表示有tts能力,没有请在网页里勾选
def fetch_token():
    params = {'grant_type': 'client_credentials',
              'client_id': API_KEY,
              'client_secret': SECRET_KEY}
    post_data = urlencode(params)
    if (IS_PY3):
        post_data = post_data.encode('utf-8')
    req = Request(TOKEN_URL, post_data)
    try:
        f = urlopen(req, timeout=5)
        result_str = f.read()
    except URLError as err:
        result_str = err.read()
    if (IS_PY3):
        result_str = result_str.decode()
    result = json.loads(result_str)
    if ('access_token' in result.keys() and 'scope' in result.keys()):
        if not SCOPE in result['scope'].split(' '):
            raise DemoError('scope is not correct')
        return result['access_token']
    else:
        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')


"""  TOKEN end """
def test(TEXT):
    token = fetch_token()
    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode
    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,
              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数
    data = urlencode(params)
    req = Request(TTS_URL, data.encode('utf-8'))
    has_error = False
    try:
        f = urlopen(req)
        result_str = f.read()
        headers = dict((name.lower(), value) for name, value in f.headers.items())
        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
    except  URLError as err:
        result_str = err.read()
        has_error = True
    save_file = "error.txt" if has_error else '语音.' + FORMAT
    with open(save_file, 'wb') as of:
        of.write(result_str)

    if has_error:
        if (IS_PY3):
            result_str = str(result_str, 'utf-8')
    webbrowser.open(os.path.abspath(save_file))

 

At this point, the text-to-speech has been successfully solved, and then we will make an optimization to make it more beautiful.

 

04

Add interface optimization

 

Let's add the interface to him to optimize, make him look more beautiful, the code is as follows:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import tkinter
from tkinter import *
from baidu_ai_world_say import test as read

def window():
    top = tkinter.Tk()
    top.title("语音转换器")
    # top.overrideredirect(True)  # 设置关闭窗口按钮是否显示
    top.geometry('500x500+10+10')
    lable1 = Label(top, text="语音合成器",  # 标签的文字
                   # bg='green',  # 背景颜色
                   font=('楷体', 17),  # 字体和字体大小
                   )
    lable1.grid(row=0, column=2)
    global text1
    text1 = Text(top, width=67, height=30)  # 原始数据录入框
    text1.grid(row=1, column=0, rowspan=1, columnspan=5)
    button = Button(top, text="开始阅读", bg="lightblue", width=10,command=text)
    button.grid(row=2, column=4)
    top.mainloop()
def text():
    wen=text1.get(1.0, END).strip().replace("\n", "").encode().decode()
    read(wen)

if __name__ == '__main__':
    window()

Then, remember the previous packaging tool? Pack it up and use it as an exe. Haha! Reading novels can easily solve the reading problem in the future.

 

Like to remember to follow us!

Official account backstage reply " text-to-speech " to obtain source code and exe file

 

related suggestion

The New Year is coming soon. The full screen of the New Year’s greeting pop-up window is amazing and joyful. Even if the computer crashes, it is still happy-the New Year’s greeting pop-up window, find out.

 

How to automatically send text messages to girlfriend

 

Build your own voice chat robot

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/90812479