SHARE
TWEET

speakSystem.js

a guest May 28th, 2017 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ROUGH TTS SYSTEM PHANTOMBOT
  2. /**
  3.  *
  4.  *  Base64 encode / decode
  5.  *  http://www.webtoolkit.info/
  6.  * ADAPTED TO NOT INCLUDE DECODE (NOT NEEDED).
  7.  *
  8.  **/
  9. var Base64 = {
  10.     // private property
  11.     _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  12.     // public method for encoding
  13.     encode : function (input) {
  14.         var output = "";
  15.         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  16.         var i = 0;
  17.         input = Base64._utf8_encode(input);
  18.         while (i < input.length) {
  19.             chr1 = input.charCodeAt(i++);
  20.             chr2 = input.charCodeAt(i++);
  21.             chr3 = input.charCodeAt(i++);
  22.             enc1 = chr1 >> 2;
  23.             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  24.             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  25.             enc4 = chr3 & 63;
  26.             if (isNaN(chr2)) {
  27.                 enc3 = enc4 = 64;
  28.             } else if (isNaN(chr3)) {
  29.                 enc4 = 64;
  30.             }
  31.             output = output +
  32.              this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  33.              this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  34.         }
  35.         return output;
  36.     },
  37.     // private method for UTF-8 encoding
  38.     _utf8_encode : function (string) {
  39.         string = string.replace('/\r\n/g',"\n");
  40.         var utftext = "";
  41.         for (var n = 0; n < string.length; n++) {
  42.             var c = string.charCodeAt(n);
  43.             if (c < 128) {
  44.                 utftext += String.fromCharCode(c);
  45.             }
  46.             else if((c > 127) && (c < 2048)) {
  47.                 utftext += String.fromCharCode((c >> 6) | 192);
  48.                 utftext += String.fromCharCode((c & 63) | 128);
  49.             }
  50.             else {
  51.                 utftext += String.fromCharCode((c >> 12) | 224);
  52.                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  53.                 utftext += String.fromCharCode((c & 63) | 128);
  54.             }
  55.         }
  56.         return utftext;
  57.     }
  58. }
  59.  
  60. //Create text to speech object.
  61. //Heavily addapted from Iconica Bizau's TTS: https://github.com/IonicaBizau/text-to-speech-js
  62. var TTS = {
  63.     vid: "en_gb_amy",//Voice id.
  64.     lang: "en",
  65.     key: "addf7ce48a11e371d6fa2a7b6075b9937ab=a6e2f7e1be80d86db2f56bd67953b0bb",
  66.     speak: function(inputText){//Define function getting & playing TTS
  67.     //Encode text and lang:
  68.     var vid64 = Base64.encode(TTS.vid);
  69.     var lang64 = Base64.encode(TTS.lang);
  70.     var inputText64 = Base64.encode(inputText);
  71.     //Get audio from Amazon Polly:
  72.     speechUrl = "http://www.ivona.com/voicetest.php?rtr=1&t2r=" + inputText64 + "&v2r=" + vid64 + "&lang=" + lang64 + "&" + TTS.key;
  73.     //Return audio object.
  74.     //return new Audio(speechUrl);
  75.     $.say(speechUrl);
  76.     }
  77. };
  78. //Adding text to speech to PhantomBot
  79. (function(){
  80.     //Define cost of shouting:
  81.     var PRICE = 10;
  82.     //Define function calling tts.
  83.     function speak(textToSpeak){
  84.     TTS.speak(textToSpeak);
  85.     }
  86.     //Bind tts to a command.
  87.     $.bind('command',function(event){
  88.     var command = event.getCommand(),
  89.         inputText = event.getArgs()[0];
  90.     //Check command.
  91.     if (command.equalsIgnoreCase('shout')){
  92.         speak(inputText);
  93.     }
  94.     });
  95.     //Register command.
  96.     $.bind('initReady',function(){
  97.         if($.bot.isModuleEnabled('./systems/speakSystem.js')){
  98.         $.registerChatCommand('./systems/speakSystem.js','shout',PRICE);
  99.         }
  100.     });
  101. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top