Skip to content

Instantly share code, notes, and snippets.

@brookslyrette
Created January 1, 2016 14:48
Show Gist options
  • Select an option

  • Save brookslyrette/f39c5e2c23d296c48a22 to your computer and use it in GitHub Desktop.

Select an option

Save brookslyrette/f39c5e2c23d296c48a22 to your computer and use it in GitHub Desktop.
//voices are loaded async
window.speechSynthesis.onvoiceschanged = function() {
for (var i = 0; i < window.speechSynthesis.getVoices().length; i++ ) {
//only non English voices should be added
var voice = window.speechSynthesis.getVoices()[i];
if (!voice.lang.startsWith('en')) {
//create option tag
var name = voice.name + " (" + voice.lang + ")";
$('.languageSpoken').append($('<option>', {value: i, text: name}));
}
}
//select french to start
if (speaking === undefined) {
selectVoice(6);
$('.languageSpoken').val(6);
}
};
//speak it back to me
var talkToMe = function(text) {
recording = false;
recognition.stop();
//translate from source to target
google.language.translate(
{text : text, type : google.language.ContentType.TEXT},
languageInput.translateCode,
//grab just the language from the code.
speaking.lang.split('-')[0],
function(result) {
var msg = new SpeechSynthesisUtterance();
msg.voice = speaking;
msg.text = result.translation;
window.speechSynthesis.speak(msg);
$('.translation').text(result.translation);
}
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment