'; var svgspeak = ''; var svgend = ''; var playread = document.createElement("div"); playread.innerHTML = svgstart + svgend; playread.classList.add("microthumb","speaker","button","bg3","center","vmid","pointer","flex","justifycenter", "fixed"); document.getElementsByTagName("header")[0].appendChild(playread); playread.addEventListener("click", function () { Speech(content.innerText); }, false); } if(!synth){ var synth = window.speechSynthesis; var voices = []; var voice = ''; var speechvoice = ''; function populateVoiceList() { voices = window.speechSynthesis.getVoices().sort(function (a, b) { const aname = a.name.toUpperCase(); const bname = b.name.toUpperCase(); if(aname < bname){return -1;} else if (aname == bname){return 0;} else {return +1;} }); } if(localStorage.getItem('speechvoice')) {speechvoice = localStorage.getItem('speechvoice');} if(synth) { populateVoiceList(); if (window.speechSynthesis.onvoiceschanged !== undefined) { window.speechSynthesis.onvoiceschanged = populateVoiceList; } } } function Speech(speechtext) { audioContext(); let speech = new SpeechSynthesisUtterance(); /*speech.lang = "ru";*/ speech.text = speechtext; if(voices){ for (let i = 0; i < voices.length; i++) { if (voices[i].name === speechvoice) {voice = voices[i]; } } } if(voice){speech.voice = voice;} if(audio){ FadeOutAudio(audio); setTimeout(() => { synth.speak(speech);}, 1000); } else { synth.speak(speech); } if (readactive) { synth.cancel(); readactive = false; console.log("reader disabled"); if(playread) { playread.innerHTML = svgstart + svgend; } } else { synth.resume(); readactive = true; console.log("reader enabled"); if(playread) { playread.innerHTML = svgstart + svgspeak + svgend; } } speech.onend = (event) => { playread.innerHTML = svgstart + svgend; readactive = false; if(result){result.value="";} if(audio){ FadeInAudio(audio);} if(openlink && respons == "1"){window.location.href=openlink;} console.log('Speech elapsed time: ' + event.elapsedTime ); }; if (aianswer) { var aiterminaltext = document.createElement("div"); aiterminaltext.innerHTML = ">_ "+speechtext+" "; aianswer.appendChild(aiterminaltext); } } var AudioContext; /*Music*/ var audio; var audioplay; var result; var openlink; var musicpath = "https://yuai.ru/assets/music/"; var playlist = "playlist.json"; var playlistjson = ''; var playlistloaded = false; var trk = 1; function audioContext() { if(!AudioContext) {AudioContext = window.AudioContext || window.webkitAudioContext; var audioCtx = new AudioContext();} } function PlayAudio(musicpath, playlist, trk) { console.log("trk "+trk); if(audio){ audio.pause(); audio.currentTime = 0; } audioContext(); const fetchPromise = fetch(musicpath+playlist); fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((playlistjson) => { if( playlistjson.length < trk){trk = 1;} var trk_index = trk-1; console.log('Play Music: '+playlistjson[trk_index].author+' - '+playlistjson[trk_index].title); audio = new Audio(musicpath+playlistjson[trk_index].file); audio.play(); audio.muted = false; audio.volume = 1; document.getElementById("wrapper").classList.add("audioplay"); audioplay = true; trk++; audio.addEventListener("ended", function() { this.currentTime = 0; /*Playlist loop*/ PlayAudio(musicpath, playlist, trk); }, false); }); }); } function StopAudio() { if(audio){ audio.pause(); audio.currentTime = 0; document.getElementById("wrapper").classList.remove("audioplay"); audioplay = false; } } function FadeInAudio (audio) { if(audio){ var fadeIn = setInterval(function () { if (audio.volume >= 0.9) { clearInterval(fadeIn); } if (audio.volume < 0.9) {audio.volume += 0.1;} }, 200); } } function FadeOutAudio (audio) { if(audio){ var fadeOut = setInterval(function () { if (audio.volume <= 0.5) { clearInterval(fadeOut); } if (audio.volume > 0.3) {audio.volume -= 0.1;} }, 200); } }