For project needs, a small example of speech synthesis (TTS) was used, using okvoice.
I want to test on the PC, Okvoice's fast access API can achieve my purpose, documentation here: http://dev.okvoice.com/file.php.
Directly on the code bar, Okvoicetts.js, the contents are as follows:
var http = require (' http '), VAR fs =require (' FS '), var crypto = require (' crypto '); var util = require (' util '); var apisecretk ey = ' 1a3c1500ab253b636100c053e1ebea87 '; var options = {apiKey: ' 2594280bed1522810d28a717f57c64db ', expires:0, format: ' MP3 ', speed:1, text: ' Zhang Sanfeng is the Jingshan of Wudang faction, well-known disciples have Song and so on. ', Voice: ' Cnfemale '};var curdate = new Date (); options.expires = DATE.UTC (Curdate.getfullyear (), Curdate.getmonth (), cur Date.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 = =) {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);
Using node. js To do some small experiments, gadgets or more convenient, it built up a lot of modules, such as http,crypto(encryption), easy to use.
One thing to note is that the Okvoice API, the data passed in when using HMAC to generate signature, is UTF8 data. Text Length (This is the length of text in query string after calling encodeURI) cannot be greater than 256.
In addition, I am in the test, input Chinese text, choose English Voice, will fail, that is, English voice can only read in plain English ... If you mix Chinese and English, you have to choose the Mandarin voice.
Getting Started with node. JS-Using HTTP to access the outside world is different, our processing, using readable flow of the pipe method, the readable left directly connected to the writable flow, much simpler.
This small example uses the HTTP module, I also processed the 301, 302, and other redirect messages so that the actual content can be downloaded. Introduction to node. JS Development--Using HTTP to access the outside world is not considered.
Other articles:
- node. JS Development Primer--UDP Programming
- Get started with node. JS-access to the outside world using HTTP
- Getting Started with node. JS Development-SOCKET (socket) programming
- node. JS Development Primer--notepad++ for node. js
- Get started with node. JS-Use dialog box Ngdialog
- Introduction to node. JS Development--Introducing Uibootstrap
- Get started with node. JS-Transform Logindemo with MongoDB
- node. JS Development Primer--mongodb and Mongoose
- Get started with node. JS Development-Use cookies to stay signed in
- Getting Started with node. JS-Using ANGULARJS built-in services
- node. JS Development Primer--angular Simple Example
- Introduction to node. JS Development-Using ANGULARJS
- Getting Started with node. JS Development-Using the Jade template engine
- node. JS Development Starter--express Routing and middleware
- node. JS Development Primer--express Installation and use
- node. JS Development Primer--http File Server
- node. JS Development Primer--helloworld Re-analysis
- Introduction to node. JS Development--Environment building and HelloWorld
Copyright NOTICE: This article is Foruok original article, without BO Master permission cannot reprint.
Introduction to node. JS Development-Speech Synthesis sample