Skip to content

Commit

Permalink
No characters length limitation - Speech synthesis optimization
Browse files Browse the repository at this point in the history
Optimization in the string processing to prevent blockage in the Speech
Synthesis API.
  • Loading branch information
sdkcarlos committed Oct 23, 2016
1 parent 49b3706 commit c07552c
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions dev-no-download/artyom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);

Expand Down

0 comments on commit c07552c

Please sign in to comment.