-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathangular-adaptive-speech.min.js
6 lines (6 loc) · 3.54 KB
/
angular-adaptive-speech.min.js
1
2
3
4
5
6
/*!
* angular-adaptive-speech v0.3.0
* The MIT License
* Copyright (c) 2014 Jan Antala
*/
!function(){var a=function(a,b,c,d){d=d||".+";var e=[];angular.isArray(a)?e=a:e.push(a),e.forEach(function(a){if(a.lang!==b)return!1;var e=a.regex||null;c.match(e)&&c.match(new RegExp(d,"ig"))&&a.call(c)})},b=angular.module("adaptive.speech",[]);b.provider("$speechCorrection",function(){this.STORAGE_ID="adaptive:speech:correction",this.$get=function(){var a=this.STORAGE_ID,b=JSON.parse(localStorage.getItem(a)||"{}"),c=function(a,b){localStorage.setItem(a,JSON.stringify(b))},d=function(a,c,d){b[d]=b[d]||{},b[d][a]=c},e=function(a){delete b.lang[a]},f=function(a,c){b[a]=b[a]||{},b[a]=c},g=function(a){delete b[a]},h=function(){return b},i=function(a){return b[a]},j=function(a,c){return b[c]&&b[c][a]||a};return{addUtterance:function(e,f,g){d(e,f,g),c(a,b)},removeUtterance:function(d,f){e(d,f),c(a,b)},addLangMap:function(d,e){f(d,e),c(a,b)},clearLangMap:function(d){g(d),c(a,b)},getCorrectionMap:function(){return h()},getLangMap:function(a){return i(a)},getCorrection:function(a,b){return j(a,b)}}}}),b.provider("$speechSynthetis",function(){this.corsProxyServer="http://www.corsproxy.com/",this.$get=function(){var a=this.corsProxyServer,b=!1,c=function(c,d){if(!c)return!1;var e=[a,"translate.google.com/translate_tts?ie=UTF-8&q=",c,"&tl=",d].join(""),f=new Audio;f.addEventListener("play",function(){},!1),f.addEventListener("ended",function(){b=!0},!1),f.addEventListener("error",function(){},!1),f.autoplay=!0,f.src=e};return{speak:function(a,b){c(a,b)},justSpoke:function(){return b},recognised:function(){b=!1}}}}),b.provider("$speechRecognition",function(){this.DEST_LANG="en-US",this.setLang=function(a){this.DEST_LANG=a},this.$get=["$rootScope","$speechSynthetis","$speechCorrection",function(b,c,d){var e=this.DEST_LANG,f=function(){this.start=function(){this.onerror({code:0,error:"speech recognition is not supported"})}.bind(this),this.stop=function(){this.onerror({code:0,error:"speech recognition is not supported"})}.bind(this)};window.SpeechRecognition=window.SpeechRecognition||window.webkitSpeechRecognition||f;var g,h=function(){g=new window.SpeechRecognition,g.continuous=!0,g.interimResults=!0,g.maxAlternatives=3,g.onresult=function(a){r&&r(a)},g.onstart=function(a){i&&i(a)},g.onend=function(a){q&&q(a)},g.onerror=function(a){j&&j(a)}};h();var i,j,k=!0,l=!1,m=function(){l||(h(),g.start()),l=!0},n=function(){l&&g.stop(),l=!1},o=function(a){a=d.getCorrection(a,e),b.$emit("adaptive.speech:utterance",{lang:e,utterance:a})},p=function(a){e=a,g.lang=a},q=function(){k=!1,g=null},r=function(a){if(a.results.length){var b=a.results[a.resultIndex];if(b.isFinal){if(c.justSpoke())return c.recognised(),!1;var d=b[0].transcript.trim();k&&o(d)}}},s=function(c){return b.$on("adaptive.speech:utterance",function(b,d){var f=d.utterance;a(c,e,f)})};return{onstart:function(a){i=a},onerror:function(a){j=a},onUtterance:function(a){var c=b.$on("adaptive.speech:utterance",function(b,c){a(c.utterance)});b.$on("destroy",c)},setLang:function(a){p(a)},getLang:function(){return e},payAttention:function(){k=!0},ignore:function(){k=!1},listen:function(){m()},stopListening:function(){n()},command:function(a){o(a)},listenUtterance:function(a){return s(a)}}}]}),b.directive("speechrecognition",["$rootScope","$speechRecognition",function(b,c){return{restrict:"A",link:function(d,e,f){var g=function(){return angular.extend({},d.$eval(f.speechrecognition))},h=g(),i=b.$on("adaptive.speech:utterance",function(b,d){var e=c.getLang(),f=d.utterance;a(h.tasks,e,f,h.reference)});e.bind("$destroy",function(){i()})}}}])}();