Files
JiboSDK/node_modules/jibo/lib/tts-service.js
2026-03-22 03:21:45 +02:00

1 line
6.4 KiB
JavaScript

"use strict";var t=function(t){return t&&t.__esModule?t["default"]:t},e=function(){function t(t,e){for(var i in e){var n=e[i];n.configurable=!0,n.value&&(n.writable=!0)}Object.defineProperties(t,e)}return function(e,i,n){return i&&t(e.prototype,i),n&&t(e,n),e}}(),i=function d(t,e,i){var n=Object.getOwnPropertyDescriptor(t,e);if(void 0===n){var r=Object.getPrototypeOf(t);return null===r?void 0:d(r,e,i)}if("value"in n&&n.writable)return n.value;var a=n.get;if(void 0!==a)return a.call(i)},n=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},a=t(require("async")),o=require("events").EventEmitter,s=t(require("./utils/utils")),c={SSML:"ssml",TEXT:"text"},u={TOKENS:"/tts_tokens",PHONES:"/tts_phones",STOP:"/tts_stop",SPEAK:"/tts_speak",TIMING:"/tts_token_times",EFFECTS:"/tts_effects",ANALYSIS:"/tts_analysis"},f={WORD:"word",PHONE:"phone",STOP:"stop",EFFECT:"effect",ANALYSIS:"analysis"},p={START:"START",STOP:"STOP",UPDATE:"UPDATE"},h={prompt:"",locale:"en-us",voice:"griffin",duration_stretch:1,pitch:.42,pitchBandwidth:.25,mode:c.TEXT,outputMode:"stream"},l=function(t){function o(){r(this,o),i(Object.getPrototypeOf(o.prototype),"constructor",this).call(this),this.isInitialized=!1,this.isTalking=!1,this.isStopping=!1,this.TTSEvents=f,this.TTSMode=c}return n(o,t),e(o,{init:{value:function(t,e){var i=this;if(!t.host||"string"!=typeof t.host)throw new Error("Missing required service.host");this.httpInterface="http://"+t.host+":"+t.port,this.socketUrl="ws:"+t.host+":"+t.port;var n=function(t){var e=function(e){i.effectsSocket=e,t()};i.createSocket(u.EFFECTS,"tts sound effects",f.EFFECT)(e)};a.parallel([this.createSocket(u.TOKENS,"tts tokens",f.WORD).bind(this),this.createSocket(u.PHONES,"tts phones",f.PHONE).bind(this),this.createSocket(u.ANALYSIS,"tts analysis",f.ANALYSIS).bind(this),n],function(){i.isInitialized=!0,e()})}},createSocket:{value:function(t,e,i){var n=this;return function(r){var a=new WebSocket(n.socketUrl+t);a.onerror=function(){console.error("Error opening "+e+" socket at "+n.socketUrl+t)},a.onmessage=function(t){n.logging&&console.log(t);var r=void 0;try{r=JSON.parse(t.data)}catch(a){if('{"token":"","timestamp":0.0,"status":"STOP","moreinfo":[]}'===t.data)return void n.emit(i,{token:"",timestamp:0,status:"STOP",moreinfo:[]});console.log("Could not JSON.parse() "+e+" event.data. Payload was: '"+t.data+"'")}r&&n.emit(i,r)},r(a)}}},_dispatchWordSchedule:{value:function(t){var e=this,i=Date.now(),n=0;t=t.tokentimes.tokens.filter(function(t){return"/pau/"!==t.name});var r=function(){var a=t[n];if(e.emit("word",{token:a.name,timestamp:a.start,status:"PLAY",moreinfo:[]}),n++,n<t.length){var o=1e3*t[n].start,s=Date.now()-i;setTimeout(r,Math.max(o-s,0))}};if(t.length){var a=1e3*t[n].start;setTimeout(r,a)}}},_speak:{value:function(t,e,i){var n=this;if(!this.isInitialized)return void i("TTS Service is not initialized");var r=function(){n._getWordTimings(t,e,function(r,a){r?i(r):!function(){n._dispatchWordSchedule(a);var r=new XMLHttpRequest,o=JSON.parse(JSON.stringify(h));o.prompt=t,o.duration_stretch=e.duration_stretch||o.duration_stretch,o.pitchBandwidth=e.pitchBandwidth||o.pitchBandwidth,o.pitch=e.pitch||o.pitch,o.mode=e.mode||o.mode,o=JSON.stringify(o),r.open("POST",n.httpInterface+u.SPEAK,!0),r.onreadystatechange=function(){4==r.readyState&&204==r.status?(n.isTalking=!1,i()):4==r.readyState&&0===r.status&&i("TTS Service is unavailable")},n.isTalking=!0,r.send(o)}()})};this.isStopping?!function(){var t=function(){n.removeListener("stopped",t),r()};n.on("stopped",t)}():this.isTalking?stop(r):r()}},speak:{value:function(t,e,i){var n=this,r=s.getOptionsAndCallback(e,i),a=r.callback,o=r.options;return a?void this._speak(t,o,a):new Promise(function(e,i){var r=function(t){t?i(t):e()};n._speak(t,o,r)})}},_stop:{value:function(t){var e=this;if(!this.isInitialized)return void t();this.isStopping=!0,this.isTalking=!1;var i=new XMLHttpRequest;i.timeout=2e3,i.onreadystatechange=function(){4===i.readyState&&200===i.status&&(e.isStopping=!1,e.emit("stopped"),t())},i.open("GET",this.httpInterface+u.STOP,!0),i.send(null)}},stop:{value:function(t){var e=this;return t?void this._stop(t):new Promise(function(t){e._stop(t)})}},_getWordTimings:{value:function(t,e,i){if("string"!=typeof t)throw new TypeError("Invalid params: "+t+", "+c);if(!this.isInitialized)throw new Error("Not initialized. Use .init()");var n=new XMLHttpRequest,r=JSON.parse(JSON.stringify(h));r.prompt=t,r.duration_stretch=e.duration_stretch||r.duration_stretch,r.pitchBandwidth=e.pitchBandwidth||r.pitchBandwidth,r.pitch=e.pitch||r.pitch,r=JSON.stringify(r),n.open("POST",this.httpInterface+u.TIMING,!0),n.onreadystatechange=function(){var t=4==n.readyState,e=n.status<200||n.status>=400;if(t&&200==n.status){var r=void 0;try{r=JSON.parse(n.response)}catch(a){i(a)}r&&(r.Status&&"ERROR"===r.Status?i(r):i(null,r))}else t&&e&&i(new Error("Request failed with status: "+n.status))},n.send(r)}},getWordTimings:{value:function(t,e,i){var n=this,r=s.getOptionsAndCallback(e,i),a=r.options,o=r.callback;return o?void this._getWordTimings(t,a,o):new Promise(function(e,i){var r=function(t,n){t?i(t):e(n)};n._getWordTimings(t,a,r)})}},_validateEffectNameParam:{value:function(t){if("string"!=typeof t)throw new TypeError("Invalid params, name must be string: "+t);if(!this.isInitialized)throw new Error("Not connected. Cannot send action.");if(!this.effectsSocket)throw new Error("Effect socket not initialized.")}},_validateEffectValueParam:{value:function(t){if("number"==typeof t){if(0>t||t>1)throw new TypeError("Value out of range [0, 1]: "+t)}else if("string"!=typeof t)throw new TypeError("Value must either be a number or string: "+t)}},startEffect:{value:function(t,e){this._validateEffectNameParam(t),this._validateEffectValueParam(e);var i={name:t,action:p.START,param:""+e};this.effectsSocket.send(JSON.stringify(i))}},stopEffect:{value:function(t){this._validateEffectNameParam(t);var e={name:t,action:p.STOP,param:"0"};this.effectsSocket.send(JSON.stringify(e))}},updateEffect:{value:function(t,e){this._validateEffectNameParam(t),this._validateEffectValueParam(e);var i={name:t,action:p.UPDATE,param:""+e};this.effectsSocket.send(JSON.stringify(i))}}},{TTSMode:{get:function(){return c}},TTSEvents:{get:function(){return f}}}),o}(o);module.exports=new l;