Created
November 24, 2016 03:08
-
-
Save Yuffster/cf1284ad59a03493ab4d8d08b05bdff8 to your computer and use it in GitHub Desktop.
Speech synthesis for a string by character.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SpeechGenerator { | |
constructor() { | |
this._word_pause = 8; | |
this._char_pause = 8; | |
this._ready = false; | |
this._queued = []; | |
this._voice_index = 0; | |
this._rate = 1; | |
var my = this; | |
window.speechSynthesis.addEventListener('voiceschanged', function() { | |
my._voice = window.speechSynthesis.getVoices()[my._voice_index]; | |
my._ready = true; | |
for (let str of my._queued) { | |
my.keyString(str); | |
} | |
}); | |
} | |
get voice() { | |
return this._voice; | |
} | |
keyString(str) { | |
if (!this._ready) { | |
this._queued.push(str); | |
return; | |
} | |
var speech = ""; | |
for (let c of str) { | |
if (c == " ") { | |
for (let i=0;i<=this._word_pause;i++) speech += "... "; | |
} else { | |
speech += c.toUpperCase(); | |
for (let i=0;i<=this._char_pause;i++) speech += '... '; | |
} | |
} | |
var msg = new SpeechSynthesisUtterance(speech); | |
msg.voice = this.voice; | |
msg.rate = this._rate; | |
window.speechSynthesis.speak(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment