HTML5 has text to speech & speech recognition options. Here’s the javascript code. for Text 2 Speech…
var x = hypeDocument.getElementById('text').innerHTML;
var u = new SpeechSynthesisUtterance();
u.text = x;
u.lang = 'en-US';
u.rate = .8;
speechSynthesis.speak(u);
edited: with a voice selection…
var x = hypeDocument.getElementById('text').innerHTML;
var u = new SpeechSynthesisUtterance();
var voices = speechSynthesis.getVoices();
u.voice = voices.filter(function(voice) { return voice.name == 'Trinoids'; })[0];
u.text = x;
u.lang = 'en-US';
u.rate = .8;
speechSynthesis.speak(u);
Looks good, especially the voices. But i think the next real event is Speech to Text to become more widely supported. That is what i am waiting for. Not found any library that will do this on all browsers yet
I know this thread is really old but fingers crossed .
I downloaded and tested the speaks.hype that Greg posted for us - Thanks @gasspence !
It seems to only read the original text Id asset, so I duplicated the scene and changed the text inside that same text field but it still only read the original text from the first scene.
Then I tried setting an inner html point and changed the text there, but it still only read the text from the first scene.
I'd like to set this up so that it will read different text on each scene. One sentence per scene would be better than none, but if I could get it to read a few different text boxes, that would be best.
I'm making an English language app for Japanese kids so this function would be great.
Making a separate js file for each sentence would be too much work to make it practical.
Any thoughts or work-arounds?
Thanks!
The issue on why it won't work for more than one scene is this line from the text2speech() function:
var x = hypeDocument.getElementById('text').innerHTML
It is using getElementById and each entire document can only have a ID on a single element. Therefore it is looking at the single element from the first scene with the text ID.
If you want any given text element to speak when clicked on, you would just need to change this line to:
var x = element.innerText;
And accordingly add a mouse click action to Run JavaScript… with the text2speech() function.
There would be other solutions if you want a different controlling element, like a button that speaks from a different text element. Let me know if that's what you'd prefer and I can write that up.
Hey Jonathan! Long time!
Thank you. My meager JS knowledge led me to believe that was the problem but I couldn't find a way to fix it.
I changed var x as you said and I set a text field to hype's click action to run the javascript but nothing happened. The console error says :
HYPE-740.thin.min.js:84 Error in undefined: TypeError: Cannot read properties of undefined (reading 'innerText'HYPE-740.thin.min.js:84 Error in undefined: TypeError: Cannot read properties of undefined (reading 'innerText')
If I could get the app to read a text box that's been tapped or clicked on, it would be epic! haha
Thank you!
I tested both Jonathans and Max's files in Chrome, Edge, Vivaldi, Safari and Firefox. I was surprised to see (hear? - hehe) that both codes only worked in Firefox. I was hoping it would play nicely with the Chrome render engine .
Thanks guys! This code will still be very useful for stuff that I make for students in my school.
My sentences are for beginner English students in Japan so the sentences are quite short.
Does changing the name of the voice in the code change the voice? Does anyone know of a list of voices the JS can access?
If it works for Chrome, it might work for Vivaldi so I'm getting a bit hopeful. Using mp3 is not realistic because of the number of text boxes.
The demo shows all the voices available, but no doubt this is OS/Version dependent.
Also voices that aren't installed won't show up. The speaks-element.hype.zip seems to want Trinoids, but on my machine it just winds up using Alex since I don't have that.
(also the example I posted works in Chrome for me... maybe check the developer console log and see if there's any errors?)