Node.js开发入门—语音合成示例
2015-09-28 07:08
856 查看
出于项目需要,搞了一个语音合成(TTS)的小示例,使用的是OKVoice。
我想在PC上测试,OKVoice的快速接入API可以实现我的目的,文档在这里:http://dev.okvoice.com/file.php。
直接上代码吧,okVoiceTts.js,内容如下:
用Node.js做一些小实验、小工具还是比较方便,它内置了很多模块,比如http、crypto(加密),简单好用。
需要注意的一点是OKVoice的API,在使用hmac生成signature时传入的数据,是UTF8数据。文本长度(这里是调用encodeURI之后query string里text的长度)不能大于256。
另外我在测试时,输入中文文本,选择英文声音,会失败,也就是说,英文声音只能读取纯英文……中英文混合的,就得选择中文声音。
与Node.js开发入门——使用http访问外部世界不同,我们这次的处理,使用Readable流的pipe方法,把Readable留直接连到了Writable流上,简单多了。
这个小示例使用http模块时,我还处理了301、302等重定向消息,以便能下载到实际的内容。Node.js开发入门——使用http访问外部世界则没考虑这一点。
其它文章:
Node.js开发入门——UDP编程
Node.js开发入门——使用http访问外部世界
Node.js开发入门——套接字(socket)编程
Node.js开发入门——notepad++ for Node.js
Node.js开发入门——使用对话框ngDialog
Node.js开发入门——引入UIBootstrap
Node.js开发入门——用MongoDB改造LoginDemo
Node.js开发入门——MongoDB与Mongoose
Node.js开发入门——使用cookie保持登录
Node.js开发入门——使用AngularJS内置服务
Node.js开发入门——Angular简单示例
Node.js开发入门——使用AngularJS
Node.js开发入门——使用jade模板引擎
Node.js开发入门——Express里的路由和中间件
Node.js开发入门——Express安装与使用
Node.js开发入门——HTTP文件服务器
Node.js开发入门——HelloWorld再分析
Node.js开发入门——环境搭建与HelloWorld
我想在PC上测试,OKVoice的快速接入API可以实现我的目的,文档在这里:http://dev.okvoice.com/file.php。
直接上代码吧,okVoiceTts.js,内容如下:
var http = require('http'); var fs =require('fs'); var crypto = require('crypto'); var util = require('util'); var apiSecretKey = '1a3c1500ab253b636100c053e1ebea87'; var options = { apiKey: '2594280bed1522810d28a717f57c64db', expires: 0, format: 'MP3', speed: 1, text: '张三丰是武当派的开山祖师,知名的弟子有宋远桥等。', voice: 'cnfemale' }; var curDate = new Date(); options.expires = Date.UTC(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getHours(), curDate.getMinutes(), curDate.getSeconds())/1000 + 100; var query = util.format('apiKey=%s&expires=%d&format=%s&speed=%d&text=%s&voice=%s', options.apiKey, options.expires, options.format, options.speed, options.text, options.voice); console.log('query - %s\n', query); var hmac = crypto.createHmac('sha1', apiSecretKey); var data = new Buffer(query, 'utf8'); hmac.update(data); var signatureResult = hmac.digest('hex'); console.log('signatureResult - %s', signatureResult); var url = "http://api.okvoice.com/tts?" + query + "&signature=" + signatureResult; console.log('url - \n%s', url); var encodedUri = encodeURI(url); console.log('encodedUri - \n%s', encodedUri); var req = http.get(encodedUri); function handleError(e){ console.log(e); } function handleResponse(res){ console.log('statusCode - ', res.statusCode); console.log('contentLength - ', res.headers['content-length']); if(res.statusCode == 200){ var savedAudio = fs.createWriteStream('okVoice_en_cn.mp3'); savedAudio.on('finish', function(){ console.log('savedAudio finished.'); process.exit(0); }) res.pipe(savedAudio); }else if(res.statusCode == 301 || res.statusCode == 302){ console.log('redirect to -\n%s', res.headers['location']); req = http.get(res.headers['location']); req.on('error', handleError); req.on('response', handleResponse); }else{ console.log('statusCode - %d', res.statusCode); process.exit(1); } } req.on('error', handleError); req.on('response', handleResponse);
用Node.js做一些小实验、小工具还是比较方便,它内置了很多模块,比如http、crypto(加密),简单好用。
需要注意的一点是OKVoice的API,在使用hmac生成signature时传入的数据,是UTF8数据。文本长度(这里是调用encodeURI之后query string里text的长度)不能大于256。
另外我在测试时,输入中文文本,选择英文声音,会失败,也就是说,英文声音只能读取纯英文……中英文混合的,就得选择中文声音。
与Node.js开发入门——使用http访问外部世界不同,我们这次的处理,使用Readable流的pipe方法,把Readable留直接连到了Writable流上,简单多了。
这个小示例使用http模块时,我还处理了301、302等重定向消息,以便能下载到实际的内容。Node.js开发入门——使用http访问外部世界则没考虑这一点。
其它文章:
Node.js开发入门——UDP编程
Node.js开发入门——使用http访问外部世界
Node.js开发入门——套接字(socket)编程
Node.js开发入门——notepad++ for Node.js
Node.js开发入门——使用对话框ngDialog
Node.js开发入门——引入UIBootstrap
Node.js开发入门——用MongoDB改造LoginDemo
Node.js开发入门——MongoDB与Mongoose
Node.js开发入门——使用cookie保持登录
Node.js开发入门——使用AngularJS内置服务
Node.js开发入门——Angular简单示例
Node.js开发入门——使用AngularJS
Node.js开发入门——使用jade模板引擎
Node.js开发入门——Express里的路由和中间件
Node.js开发入门——Express安装与使用
Node.js开发入门——HTTP文件服务器
Node.js开发入门——HelloWorld再分析
Node.js开发入门——环境搭建与HelloWorld
相关文章推荐
- Node.js web
- nodejs
- nodejs 异步优化
- 0--node安装
- Node.js实践第一天
- ubuntu14.04 64位 安装H3C iNode客户端
- GDataXMLNode
- "_stringprep", referenced from:+[LibIDN prepNode:] in LibIDN.o
- nodejs 基本操作
- NodeJS
- node express session
- 部署Nodeclub系统大坑总结
- leetcode 222:Count Complete Tree Nodes
- Swap Nodes in Pairs
- Blog Node.js vs Golang: Battle of the Next-Gen Languages
- node csv
- [LeetCode]237 Delete Node in a Linked List
- LeetCode 19_Remove Nth Node From End of List
- nodejs 上传文件
- nodejs 教程