src/NCBS/WebsiteBundle/Resources/views/General/text_to_speech_js.html.twig line 1

Open in your IDE?
  1. {# nColNum must be passed as an argument to the twig include #}
  2. <script>
  3. {% set nAudioRate = app.session.get("current_audio_rate") %}
  4. {% if not nAudioRate is null %}
  5.     MultiAudioElementTTS.globalRate = {{ nAudioRate }};
  6. {% endif %}
  7. const tts{{ nColNum }} = new MultiAudioElementTTS();
  8. let stt{{ nColNum }} = null;
  9. tts{{ nColNum }}.go = function() {
  10.     this.loadTextAndStartOrPause(
  11.         document.querySelector("#tts{{ nColNum }}").innerText,
  12.         "{{ app.session.get('user_id') }}"
  13.     );
  14. }
  15. tts{{ nColNum }}.next = function() {
  16.     const nextButton = document.querySelector("a.nav-arrow-next");
  17.     const url = URL.parse(nextButton.href);
  18.     url.searchParams.append("ttsautoplay", "{{ nColNum }}");
  19.     window.location = url;
  20. }
  21. tts{{ nColNum }}.onStateChange = function() {
  22.     if (this.isLoading){
  23.         $("#readbutton{{ nColNum }}").hide();
  24.         $("#loadingbutton{{ nColNum }}").show();
  25.         $("#stopbutton{{ nColNum }}").hide();
  26.         $("#pausebutton{{ nColNum }}").hide();
  27.     } else {
  28.         $("#loadingbutton{{ nColNum }}").hide();
  29.         if (this.hasStarted){
  30.             $("#stopbutton{{ nColNum }}").show();
  31.             if (this.canResume) {
  32.                 $("#readbutton{{ nColNum }}").show();
  33.                 $("#pausebutton{{ nColNum }}").hide();
  34.             } else {
  35.                 $("#readbutton{{ nColNum }}").hide();
  36.                 $("#pausebutton{{ nColNum }}").show();
  37.             }
  38.         } else {
  39.             $("#readbutton{{ nColNum }}").show();
  40.             $("#stopbutton{{ nColNum }}").hide();
  41.             $("#pausebutton{{ nColNum }}").hide();
  42.         }
  43.     }
  44. }
  45. tts{{ nColNum }}.onAllElementsEnded = function() {
  46.     const nextButton = document.querySelector("a.nav-arrow-next");
  47.     if (nextButton && nextButton.href) {
  48.         const endpoint = new URL("https://newchristianchatbot.org/api/tts/");
  49.         endpoint.searchParams.append("au", "{{ app.session.get('user_id') }}");
  50.         endpoint.searchParams.append("content", "{{ app.session.get('ui_str')['texttospeech.continueprompt']|raw }}");
  51.         const audio = document.createElement("audio");
  52.         audio.src = endpoint;
  53.         {# use setTimeout rather than `audio.addEventListener("ended", ...)` #}
  54.         {# to start listening for a response before the TTS finishes completely #}
  55.         setTimeout(() => {
  56.             if (stt{{ nColNum }} != null) {
  57.                 stt{{ nColNum }}.start();
  58.             } else if (confirm("{{ app.session.get('ui_str')['texttospeech.continueprompt']|raw }}")) {
  59.                 tts{{ nColNum }}.next();
  60.             }
  61.         }, 1500);
  62.         audio.play();
  63.     }
  64. }
  65. if (typeof SpeechRecognition !== "undefined" && typeof SpeechToText !== "undefined") {
  66.     stt{{ nColNum }} = new SpeechToText("{{ app.request.locale }}");
  67.     for (const phrase of ["{{ app.session.get('ui_str')['speechtotext.yes']|raw }}", "{{ app.session.get('ui_str')['speechtotext.okay']|raw }}"]) {
  68.         stt{{ nColNum }}.addPhrase(phrase, () => tts{{ nColNum }}.next());
  69.     }
  70. }
  71. {% if app.request.query.get("ttsautoplay") == nColNum~"" %}
  72.     addEventListener("DOMContentLoaded", () => tts{{ nColNum }}.go());
  73. {% endif %}
  74. </script>