Skip to content

Instantly share code, notes, and snippets.

@paceaux
Last active August 29, 2015 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paceaux/8690153 to your computer and use it in GitHub Desktop.
Save paceaux/8690153 to your computer and use it in GitHub Desktop.
Speech Synthesis JS API
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});
@paceaux
Copy link
Author

paceaux commented Sep 30, 2014

Second version is an actual returnable object, where you can modify the options if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment