tts.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. {%- macro basic_tts() %}
  2. <script type="text/javascript" src="{{ url_for("static", filename=static_prefix+"js/articulate.min.js") }}"></script>
  3. <script type="text/javascript">
  4. <!--
  5. try {
  6. var bl_speaking = true;
  7. if (!("speechSynthesis" in window)) { do_alert("This browser does not support TTS output"); bl_speaking = false; }
  8. function speak(s_content) {
  9. if (bl_speaking!=true) { alert(s_content); return false; }
  10. speech = new SpeechSynthesisUtterance();
  11. speech.text = s_content;
  12. var rate = 1.10;
  13. var pitch = 1;
  14. var volume = 1;
  15. speech.rate = rate;
  16. speech.pitch = pitch;
  17. speech.volume = volume;
  18. window.speechSynthesis.speak(speech);
  19. }// end of speak function
  20. function pause() {
  21. window.speechSynthesis.pause();
  22. };// end of pause
  23. function resume() {
  24. window.speechSynthesis.resume();
  25. };// end of resume
  26. function stop() {
  27. window.speechSynthesis.cancel();
  28. };// end of stop
  29. } catch(e) {
  30. var s_err = String(e.name) + "\nmessage:" + String(e.message);
  31. s_err = (typeof(e.lineNumber)!="undefined") ? s_err + "\nline:" + String(e.lineNumber) : s_err;
  32. alert("TTS Error! " + s_err);
  33. }//end of catch
  34. -->
  35. </script>
  36. {% endmacro -%}
  37. {%- macro full_tts() %}
  38. <script type="text/javascript" src="{{ url_for("static", filename=static_prefix+"js/articulate.min.js") }}"></script>
  39. <script type="text/javascript">
  40. <!--
  41. try {
  42. var bl_speaking = true;
  43. if (!("speechSynthesis" in window)) { alert("This browser does not support TTS output"); bl_speaking = false; }
  44. var rate = 1.10;
  45. var pitch = 1;
  46. var volume = 1;
  47. var voices = [];
  48. var chrome = /chrome/i.test( navigator.userAgent );
  49. var edge = /edge/i.test( navigator.userAgent );
  50. var isChrome = ((chrome) && (!edge));
  51. // copied from articulate.js
  52. function voiceObj(name,language) {
  53. this.name = name;
  54. this.language = language;
  55. }// end of voiceObj object instantiation definition
  56. function populate_voice_list() {
  57. voices = [];
  58. var systemVoices = speechSynthesis.getVoices();
  59. for(var i = 0; i < systemVoices.length ; i++) {
  60. var s_name = systemVoices[i].name;
  61. var s_lang = systemVoices[i].lang;
  62. voices.push(new voiceObj(systemVoices[i].name,systemVoices[i].lang));
  63. } // end of looping through system voices
  64. return voices;
  65. }// end of populate_voice_list function
  66. function speak(s_in) {
  67. if (bl_speaking!=true) { alert("no speech!"); return false; }
  68. s_content = (String(s_in).length>0) ? String(s_in) : "don't you want to hear me...?";
  69. speech = new SpeechSynthesisUtterance();
  70. speech.text = s_content;
  71. if (!isNaN($("#rate").val())) { rate = Number($("#rate").val()); }
  72. speech.rate = rate;
  73. if (!isNaN($("#pitch").val())) { pitch = Number($("#pitch").val()); }
  74. speech.pitch = pitch;
  75. if (!isNaN($("#volume").val())) { volume = Number($("#volume").val()); }
  76. speech.volume = volume;
  77. // if (isChrome) { speech.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == "native"; })[0]; };
  78. // if (voiceUserDefault !== undefined) { speech.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == voiceUserDefault; })[0]; };
  79. if ($("#sel_voice").prop("selectedIndex")>-1) { speech.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == $("#sel_voice").val(); })[0]; }
  80. window.speechSynthesis.speak(speech);
  81. }// end of speak function
  82. function pause() {
  83. window.speechSynthesis.pause();
  84. };// end of pause
  85. function resume() {
  86. window.speechSynthesis.resume();
  87. };// end of resume
  88. function stop() {
  89. window.speechSynthesis.cancel();
  90. };// end of stop
  91. $("input[class='btn_voices']").click( function(event) {
  92. var s_which = String($(this).attr("id"));
  93. if (s_which=="btn_pause") {
  94. pause();
  95. } else if (s_which=="btn_resume") {
  96. resume();
  97. } else if (s_which=="btn_stop") {
  98. stop();
  99. }
  100. });// end of input buttons click event
  101. } catch(e) {
  102. var s_err = String(e.name) + "\nmessage:" + String(e.message);
  103. s_err = (typeof(e.lineNumber)!="undefined") ? s_err + "\nline:" + String(e.lineNumber) : s_err;
  104. alert("TTS Error! " + s_err);
  105. }//end of catch
  106. -->
  107. </script>
  108. {% endmacro -%}