首页 > 其他 > 详细

实现语音合成功能

时间:2020-03-08 18:53:33      阅读:89      评论:0      收藏:0      [点我收藏+]

语音合成和多种方法,这时介绍通过百度AI可以实现语音合成功能,而且是免费使用的。

首先注册帐号:https://ai.baidu.com/,登陆后点击语音技术-创建应用

技术分享图片

 

管理应用,可以看到AppID,API Key,Secret Key

技术分享图片

 

 

Ptyhon代码实现如下: 

 1 # -*- coding: utf-8 -*-
 2 
 3 import os
 4 from urllib import quote
 5 import urllib2
 6 import json
 7 import time
 8 
 9 
10 appKey = xxxx
11 appSecret = xxxx
12 
13 token = ‘‘
14 token_time_stamp = 0
15 token_expires_in = 0
16 
17 get_access_token_url = https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}
18 get_voice_url = http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok={}&tex={}&vol=9&per={}&spd=5&pit=5&aue=3
19 
20 def speak(text, per=0):
21     text_encoded = quote(text.encode(utf8))
22     #下面两行是修改前的
23     #token = get_token()
24     #voice_url = "http://tsn.baidu.com/text2audio?tex=" + text_encoded + "&lan=zh&per=0&cuid=784f436aa242&ctp=1&tok=" + token
25     token = get_custome_token()
26     voice_url = get_voice_url.format(token, text_encoded, per)
27     print("Now read: ")
28     #print(text)
29     os.system(mpg123 -q "%s" % voice_url)
30 
31 
32 def get_token():
33     url = "https://ilangbd.azurewebsites.net/token.txt"
34     return urllib2.urlopen(url).read()
35 
36 def get_custome_token(appkey=appKey, appsecret=appSecret):
37     global token_time_stamp, token_expires_in, token
38     if time.time() > token_time_stamp + token_expires_in:
39         #print "get_token"
40         url = get_access_token_url.format(appkey, appSecret)
41         content = json.loads(urllib2.urlopen(url).read())
42 
43         token_expires_in = int(content[expires_in])
44         token_time_stamp = time.time()
45         token = content[access_token]
46     return token
47 
48 if __name__ == "__main__":
49     speak(u"你好,欢迎使用百度语音合成.")
50     time.sleep(1)
51     speak(u"你好,欢迎使用百度语音合成.", per=1)
52     time.sleep(1)
53     speak(u"你好,欢迎使用百度语音合成.", per=103)

 注:mpg123要先安装才能正常播放,安装命令样例: $ sudo apt-get install mpg123 

 

实现语音合成功能

原文:https://www.cnblogs.com/bongem/p/12443882.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!