Lua通过REST API模式实现百度语音合成
Song •
1925 次浏览 •
0个评论 •
2018年09月11日
从百度官网可以看到实现语音合成的方式为REST API
,同时官网也提供了JAVA SDK
,PHP SDK
,Python SDK
,C# SDK
,C++ SDK
,Node.js SDK
,我们在这里提供一下Lua SDK
环境需求
- Lua5.*
- socket.http
- cjson
实现代码
文档地址为:REST API文档
local http = require("socket.http")
local cjson = require("cjson")
-- API Key
local client_id = ""
-- Secret Key
local client_secret = ""
-- 获取access_token
function accessToken()
access_token_url = table.concat({"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=",client_id,"&client_secret=",client_secret})
result = http.request(access_token_url)
local data = cjson.decode(result);
local access_token = data["access_token"]
return access_token
end
-- urlEncode
local function urlEncode(s)
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
-- 语音合成
function getWav(text)
access_token = accessToken()
encodtext = urlEncode(text)
ttsurl = table.concat({"https://tsn.baidu.com/text2audio?tex=",encodtext,"&lan=zh&cuid=8712ja7263129we82&ctp=1&aue=6&tok=",access_token})
results = http.request(ttsurl)
file = io.open("ws.wav", "a")
io.output(file)
io.write(results)
io.close(file)
end
getWav("你好Ptorch")
用户评论
当前暂无评价,快来发表您的观点吧...
更多相关好文
当前暂无更多相关好文推荐...
-
微信公众号文章/菜单添加小程序时路径如何获取? 2021-12-22
-
如何轻松获取微信小程序路径path? 2021-12-22
-
cannot import name 'CUDA_HOME' from 'mmcv.utils' 2021-12-05
-
vgg的loss一轮达到ln(1/n)阈值,如何解决 2021-11-21
-
如何下载使用utils库 2021-10-27
热门文章
-
微信公众号文章/菜单添加小程序时路径如何获取? 2021-12-22
-
如何轻松获取微信小程序路径path? 2021-12-22
-
python/MySQL分页查询方法与性能优化 2021-06-23
-
mitmproxy & python 忽略所有的https/ssl请求 2021-04-19
-
如何使用邮件/邮箱推广微信公众号/小程序? 2021-01-28
栏目最新文章
公告提示
- pytorch中文文档
- pytorch官方文档
提交评论