From c07552c50d193a44628a76d2c2a396a7b64fa8f3 Mon Sep 17 00:00:00 2001 From: Carlos Delgado Date: Sun, 23 Oct 2016 17:31:16 +0200 Subject: [PATCH] No characters length limitation - Speech synthesis optimization Optimization in the string processing to prevent blockage in the Speech Synthesis API. --- dev-no-download/artyom.js | 64 +++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/dev-no-download/artyom.js b/dev-no-download/artyom.js index cd8758d..d18177d 100644 --- a/dev-no-download/artyom.js +++ b/dev-no-download/artyom.js @@ -553,6 +553,33 @@ window.speechSynthesis.speak(msg); }; + /** + * Splits a string into an array of strings with a limited size (chunk_length). + * + * @param {String} input text to split into chunks + * @param {Integer} chunk_length limit of characters in every chunk + */ + var splitStringByChunks = function (input, chunk_length){ + input = input || ""; + chunk_length = chunk_length || 100; + + var curr = chunk_length; + var prev = 0; + var output = []; + + while (input[curr]) { + if (input[curr++] == ' ') { + output.push(input.substring(prev,curr)); + prev = curr; + curr += chunk_length; + } + } + + output.push(input.substr(prev)); + + return output; + } + /** * Process the given text into chunks and execute the private function artyom_talk * @@ -562,21 +589,38 @@ * @returns {undefined} */ artyom.say = function (message, callbacks) { + var artyom_say_max_chunk_length = 115; + if (artyom.speechSupported()) { if (typeof (message) == 'string') { if (message.length > 0) { - var finalTextA = message.split(","); - var finalTextB = message.split("."); - var definitive; - - //Declare final chunk container and clear any empty item ! - if ((finalTextA.length) > (finalTextB.length)) { - definitive = finalTextA.filter(function(e){return e;}); - } else { - definitive = finalTextB.filter(function(e){return e;}); + var definitive = []; + + // If the providen text is long, proceed to split it + if(message.length > artyom_say_max_chunk_length){ + // Split the given text by pause reading characters [",",":",";","."] to provide a natural reading feeling. + var naturalReading = message.split(/,|:|\.|;/); + + naturalReading.forEach(function(chunk, index){ + // If the sentence is too long and could block the API, split it to prevent any errors. + if(chunk.length > artyom_say_max_chunk_length){ + // Process the providen string into strings (withing an array) of maximum aprox. 115 characters to prevent any error with the API. + var temp_processed = splitStringByChunks(chunk, artyom_say_max_chunk_length); + // Add items of the processed sentence into the definitive chunk. + definitive.push.apply(definitive, temp_processed); + }else{ + // Otherwise just add the sentence to being spoken. + definitive.push(chunk); + } + }); + }else{ + definitive.push(message); } - //Process given text into chunks ! + // Clean any empty item in array + definitive = definitive.filter(function(e){return e;}); + + // Finally proceed to talk the chunks and assign the callbacks. definitive.forEach(function (chunk, index) { var numberOfChunk = (index + 1);