Skip to content

Commit

Permalink
release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
batopa committed Nov 12, 2015
1 parent 4fd0bdd commit 0a688b3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 19 deletions.
116 changes: 101 additions & 15 deletions dist/favella.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ if ('speechSynthesis' in window) {
volume: 1,
rate: 1,
pitch: 0,
lang: 'en-US'
lang: 'en-US',
onstart: function(e) {},
onend: function(e) {},
onerror: function(e) {},
onpause: function(e) {},
onboundary: function(e) {},
onmark: function(e) {},
};

/**
Expand All @@ -59,16 +65,11 @@ if ('speechSynthesis' in window) {
var muteConsole = false;

/**
* List of speechSynthesisVoices available
* List of SpeechSynthesisVoice available
* @type {Array}
*/
var voices = [];

// wait on voices to be loaded before fetching list
window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices();
};

var Favella = {

/**
Expand Down Expand Up @@ -150,6 +151,7 @@ if ('speechSynthesis' in window) {
*/
getVoice: function(lang) {
var voice = null;
var voices = this.getVoices();
if (voices.length) {
voices.forEach(function(v) {
if (v.lang == lang) {
Expand Down Expand Up @@ -189,7 +191,7 @@ if ('speechSynthesis' in window) {
}
});
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
var voices = this.getVoices();
msg.voice = this.getVoice(options.lang);
console.log('Voice selected: ' + msg.voice.name);
msg.voiceURI = msg.voice.voiceURI;
Expand All @@ -199,15 +201,89 @@ if ('speechSynthesis' in window) {
msg.lang = msg.voice.lang;
msg.text = message;

msg.onend = function(e) {
console.log('Finished to speak');
};
// add events
['onstart', 'onend', 'onerror', 'onboundary', 'onmark']
.forEach(function(name) {
if (options[name] && typeof options[name] == 'function') {
msg[name] = options[name];
}
});

window.speechSynthesis.speak(msg);
},

/**
* Wrap speechSynthesis.getVoices() and save it in private voices var
* Return a list of SpeechSynthesisVoice available
*
* @param {boolean} force if you want to force to get voices from speechsynthesis
* @return {void}
*/
getVoices: function(force) {
if (!voices.length || force) {
voices = window.speechSynthesis.getVoices();
}
return voices;
},

/**
* Wrap speechSynthesis.pause()
* Pause any utterances that are being spoken
*
* @return {void}
*/
pause: function() {
window.speechSynthesis.pause();
},

/**
* Wrap speechSynthesis.resume()
* Resume an utterances that was previously paused
*
* @return {void}
*/
resume: function() {
window.speechSynthesis.resume();
},

/**
* Wrap speechSynthesis.cancel()
* Stop speaking and remove all utterances from the queue
*
* @return {void}
*/
cancel: function() {
window.speechSynthesis.cancel();
},

msg.onerror = function(e) {
console.log(e);
};
/**
* Wrap speechSynthesis.speaking
* Return true if Favella is speaking
*
* @return {boolean}
*/
isSpeaking: function() {
return window.speechSynthesis.speaking;
},

speechSynthesis.speak(msg);
/**
* Wrap speechSynthesis.pending
* Return true if there are utterances in the queue that have not yet started speaking
*
* @return {boolean}
*/
isPending: function() {
return window.speechSynthesis.pending;
},

/**
* Wrap speechSynthesis.paused
* Return true if Favella is paused
*
* @return {boolean}
*/
isPaused: function() {
return window.speechSynthesis.paused;
},

/**
Expand Down Expand Up @@ -246,6 +322,16 @@ if ('speechSynthesis' in window) {

};

// cancel pending speaking
if (window.speechSynthesis.speaking) {
window.speechSynthesis.cancel();
}

// wait on voices to be loaded before fetching list
window.speechSynthesis.onvoiceschanged = function() {
Favella.getVoices(true);
};

/**
* Override standard console.error()
* Before write in console it speaks the error
Expand Down
4 changes: 2 additions & 2 deletions dist/favella.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/favella.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "favella",
"version": "0.1.0",
"version": "0.2.0",
"description": "Make your console speak",
"main": "src/favella.js",
"directories": {
Expand Down

0 comments on commit 0a688b3

Please sign in to comment.