-
-
Save brettz9/c4b4196c895af8cc36b8 to your computer and use it in GitHub Desktop.
See https://gist.github.com/brettz9/0cfda9e04751090f4a7a for an example use on a particular Mediawiki installation (not abstracted to an extension, but could easily be so)
This file contains 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
/** | |
* Chunkify | |
* Google Chrome Speech Synthesis Chunking Pattern | |
* Fixes inconsistencies with speaking long texts in speechUtterance objects | |
* Licensed under the MIT License | |
* | |
* Peter Woolley and Brett Zamir | |
*/ | |
var speechUtteranceChunker = function (utt, settings, callback) { | |
settings = settings || {}; | |
var newUtt; | |
var txt = (settings && settings.offset !== undefined ? utt.text.substring(settings.offset) : utt.text); | |
if (utt.voice && utt.voice.voiceURI === 'native') { // Not part of the spec | |
newUtt = utt; | |
newUtt.text = txt; | |
newUtt.addEventListener('end', function () { | |
if (speechUtteranceChunker.cancel) { | |
speechUtteranceChunker.cancel = false; | |
} | |
if (callback !== undefined) { | |
callback(); | |
} | |
}); | |
} | |
else { | |
var chunkLength = (settings && settings.chunkLength) || 160; | |
var pattRegex = new RegExp('^[\\s\\S]{' + Math.floor(chunkLength / 2) + ',' + chunkLength + '}[.!?,]{1}|^[\\s\\S]{1,' + chunkLength + '}$|^[\\s\\S]{1,' + chunkLength + '} '); | |
var chunkArr = txt.match(pattRegex); | |
if (chunkArr[0] === undefined || chunkArr[0].length <= 2) { | |
//call once all text has been spoken... | |
if (callback !== undefined) { | |
callback(); | |
} | |
return; | |
} | |
var chunk = chunkArr[0]; | |
newUtt = new SpeechSynthesisUtterance(chunk); | |
var x; | |
for (x in utt) { | |
if (utt.hasOwnProperty(x) && x !== 'text') { | |
newUtt[x] = utt[x]; | |
} | |
} | |
newUtt.addEventListener('end', function () { | |
if (speechUtteranceChunker.cancel) { | |
speechUtteranceChunker.cancel = false; | |
return; | |
} | |
settings.offset = settings.offset || 0; | |
settings.offset += chunk.length - 1; | |
speechUtteranceChunker(utt, settings, callback); | |
}); | |
} | |
if (settings.modifier) { | |
settings.modifier(newUtt); | |
} | |
console.log(newUtt); //IMPORTANT!! Do not remove: Logging the object out fixes some onend firing issues. | |
//placing the speak invocation inside a callback fixes ordering and onend issues. | |
setTimeout(function () { | |
speechSynthesis.speak(newUtt); | |
}, 0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment