Last active
August 29, 2015 13:55
-
-
Save paceaux/8690153 to your computer and use it in GitHub Desktop.
Speech Synthesis JS API
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
speech = function (msg, options, onend) { | |
this.text = msg; | |
this._defaults = { | |
voiceURI: 'native', | |
volume: 3, | |
rate: 2, | |
pitch: 1.5, | |
lang: 'en-US' | |
}; | |
this.defaults = options !== undefined ? options : this._defaults; | |
this._createSpeech = function () { | |
console.log(this.defaults); | |
var defaults = this.defaults, | |
speech = new SpeechSynthesisUtterance(); | |
speech.voiceURI = defaults.voiceURI; | |
speech.volume = defaults.volume; | |
speech.rate = defaults.rate; | |
speech.pitch = defaults.pitch; | |
speech.lang = defaults.lang; | |
return speech; | |
}, | |
this._onend = function (e) { | |
console.log('finished in '+ e.elapsedTime) | |
}; | |
var speech = this._createSpeech(); | |
speech.text = this.text; | |
speech.onend = onend !== undefined ? onend : this._onend; | |
return speechSynthesis.speak(speech); | |
} | |
var derp = new speech('Hi, My name is Tahza. How can I help you today.'); | |
var notFound = new speech('Trouble loading your worms. Please try again later', {volume: 9,rate: .2, pitch: .4}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Second version is an actual returnable object, where you can modify the options if necessary.