Node. js development-speech synthesis example
A small example of speech synthesis (TTS) is provided for the project.OKVoice.
I want to test it on a PC. The quick access API of OKVoice can be used for my purposes. For more information, see http://dev.okvoice.com/file.php.
Go directly to the Code, okVoiceTts. js. The content is as follows:
Var http = require ('http'); var fs = require ('fs'); var crypto = require ('crypto '); var util = require ('util '); var apiSecretKey = 'authorization'; var options = {apiKey: '2594280bed1522810d28a717f57c64db', expires: 0, format: 'mp3', speed: 1, text: 'zhang Sanfeng is an ancestor of Wudang school. He is a well-known disciple of Song yuanqiao. ', 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', 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-% s', url); var encodedUri = encodeURI (url); console. log ('enableduri-% 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}'); savedAudio. on ('finish ', function () {console. log ('savedaudio finished. '); process. exit (0);}) res. pipe (savedAudio);} else if (res. status Code = 301 | res. statusCode == 302) {console. log ('redirect to-% 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 );
It is more convenient to use Node. js for some small experiments and gadgets. It has many built-in modules, suchHttp,Crypto(Encryption), easy to use.
Note that the OKVoice API uses UTF8 to generate signature data. The length of the text (the length of the text in the query string after calling encodeURI) cannot exceed 256.
In addition, when I enter Chinese text and select an English sound during the test, it will fail. That is to say, the English sound can only be read in English ...... If both Chinese and English are used, you must select the Chinese voice.
AndGet started with Node. js development-access the external world through httpDifferent, we useReadableThe pipe method of the stream directly connects the Readable to the Writable stream, which is much simpler.
In this small example, when I use the http module, I also process redirection messages such as 301 and 302 so that I can download the actual content.Get started with Node. js development-access the external world through httpThis is not taken into consideration.