not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.1 KiB

  1. var Speech = (function() {
  2. var answerFinal = "";
  3. var playing = "";
  4. function init(settings){
  5. //No Initialization as of now.
  6. document.getElementById('speaklang').innerHTML = settings.language;
  7. meSpeak.loadConfig("mespeak_config.json");
  8. loadVoice(settings.language);
  9. }
  10. function getBotReply(question){
  11. var aimlInterpreter = new AIMLInterpreter({name:'WireInterpreter', age:'42'});
  12. var filesArray = ['js/alice/star.aiml'];
  13. var words = question.split(' ');
  14. var marked = new Array(256);
  15. for(i=0;i<256;i++){
  16. marked[i] = 0;
  17. }
  18. for(i=0; i<words.length; i++){
  19. if(words[i][0] >= 'a' && words[i][0] <= 'z'){
  20. if (marked[(words[i].charCodeAt(0)+'A'.charCodeAt(0)-'a'.charCodeAt(0))] == 0){
  21. marked[(words[i].charCodeAt(0)+'A'.charCodeAt(0)-'a'.charCodeAt(0))] = 1;
  22. filesArray.unshift("js/alice/" + String.fromCharCode(words[i].charCodeAt(0)+'A'.charCodeAt(0)-'a'.charCodeAt(0)) + ".aiml");
  23. }
  24. }
  25. if((words[i][0] >= 'A' && words[i][0] <= 'Z') || (words[i][0] >= '0' && words[i][0] <= '9')){
  26. if (marked[words[i].charCodeAt(0)] == 0){
  27. marked[words[i].charCodeAt(0)] = 1;
  28. filesArray.unshift("js/alice/" + words[i][0] + ".aiml");
  29. }
  30. }
  31. }
  32. //console.log(filesArray);
  33. aimlInterpreter.loadAIMLFilesIntoArray(filesArray);
  34. answerFinal = "Please ask me something else";
  35. var callback = function(answer, wildCardArray, input){
  36. if(answer == null){
  37. answer = "Please ask me something else";
  38. }
  39. answerFinal = answer.split('.')[0];
  40. };
  41. aimlInterpreter.findAnswerInLoadedAIMLFiles(question, callback);
  42. }
  43. function testVoice(voice, callback) {
  44. var req=new XMLHttpRequest();
  45. req.url = voice;
  46. try {
  47. req.open('GET', voice);
  48. req.onreadystatechange=function() {
  49. if (req.readyState==4) {
  50. callback((req.status==200 || req.status==0) && req.response.length > 0);
  51. }
  52. };
  53. req.send('');
  54. }
  55. catch(e) {
  56. callback(false);
  57. }
  58. }
  59. function loadVoice(name, callback) {
  60. var defaultVoice = meSpeak.getDefaultVoice();
  61. if (defaultVoice == name || defaultVoice.indexOf(name) == 0) {
  62. if (callback) callback();
  63. return;
  64. }
  65. var fname = "voices/"+name+".json";
  66. testVoice(fname, function(exist) {
  67. if (exist) {
  68. meSpeak.loadVoice(fname, callback);
  69. } else {
  70. document.getElementById('speaklang').innerHTML = "en";
  71. meSpeak.loadVoice("voices/en.json", callback);
  72. }
  73. });
  74. }
  75. hiddenArray(); // Access speakArray
  76. // Function containing the speakArray, which saves the recent talk array
  77. function hiddenArray() {
  78. speakArray = [];
  79. }
  80. function playVoice(language, text) {
  81. playing = text;
  82. //Adds option when text is spoken
  83. var addUserInput = document.createElement("OPTION");
  84. addUserInput.setAttribute("value", playing);
  85. addUserInput.text = playing;
  86. document.getElementById("combo-box").appendChild(addUserInput);
  87. document.getElementById("combo-box").value= playing;
  88. speakArray.push(playing); // Adds recent talks to speakArray
  89. if(document.getElementById('mode').innerHTML=="2"){
  90. //After the voice is loaded, playSound callback is called
  91. getBotReply(text);
  92. setTimeout(function(){
  93. loadVoice(language, playSound);
  94. }, 4000);
  95. }
  96. else{
  97. loadVoice(language, playSound);
  98. }
  99. }
  100. function playSound(){
  101. var text = playing;
  102. var pitch = document.getElementById('pitch').innerHTML;
  103. var speed = document.getElementById('rate').innerHTML;
  104. if(document.getElementById('mode').innerHTML=="2"){
  105. text = answerFinal;
  106. console.log(answerFinal);
  107. }
  108. function soundComplete(){
  109. document.getElementById('speaking').innerHTML = "0";
  110. }
  111. if (!/iPad|iPhone/.test(navigator.userAgent)) {
  112. meSpeak.speak(text, {speed: speed, pitch: pitch}, soundComplete);
  113. } else {
  114. // HACK: On iOS, need to be played into an HTMLAudio control
  115. var myDataUrl = meSpeak.speak(text, {speed: speed, pitch: pitch, rawdata: 'data-url'}, soundComplete);
  116. var sound = new Audio(myDataUrl);
  117. sound.addEventListener("ended", function(){
  118. document.getElementById('speaking').innerHTML = "0";
  119. });
  120. sound.play();
  121. }
  122. }
  123. return {
  124. init: init,
  125. playVoice: playVoice
  126. };
  127. });