Skip to content

Instantly share code, notes, and snippets.

@khalid283
Last active April 7, 2017 06:37
Show Gist options
  • Save khalid283/c406a8a375bbd8dd1b2e7ede3557ac08 to your computer and use it in GitHub Desktop.
Save khalid283/c406a8a375bbd8dd1b2e7ede3557ac08 to your computer and use it in GitHub Desktop.
This a complete JavaScript function to split long text while using speech synthesis in chrome or Mozilla.
function startPlay(speak_content, k){
var msg = new SpeechSynthesisUtterance();
msg.voice = window.speechSynthesis.getVoices().filter(function(voice) {
return voice.name == 'native';
})[0];
msg.rate = 1; // 0.1 to 10
msg.pitch = 1; //0 to 2
msg.text = speak_content;
msg.onend = function(event) {
console.log(msg);
};
speechSynthesis.speak(msg);
}
function SplitPlay (p_string){
var splitted = p_string.split(/\s*[.:\n]+\s*/);
splitted.push("..Thank you for listening");
var split_length = splitted.length;
var speak_content;
var max_word_length = 22;
for(var k = 0; k < split_length; k++){
var pre_speak_content = splitted[k];
var cake_length = p_string.split(" ").length;
if(cake_length > max_word_length){
var cake = pre_speak_content.split(" ");
var cake_length = cake.length;
var num_guest = parseInt((cake_length/max_word_length))+1;
for(var sp = 0; sp < num_guest; sp++){
var slice_pos_start = sp*max_word_length;
var slice_pos_end = (sp+1)*max_word_length;
sliced_cake = cake.slice(slice_pos_start, slice_pos_end);
cake_piece = sliced_cake.join(" ");
speak_content = cake_piece;
startPlay(speak_content, k);
}
}else {
speak_content = pre_speak_content;
startPlay(speak_content, k);
}
}
}
string = 'I believe in thematic investments. An investment always needs a theme. This investment was towards plastic pipes segment. I was betting in plastic pipes because of agricultural loans. In that pre-budget discussion arena it was a strong buzz that Goverment was going to give special deals to boost agricultural loans.So, I was betting on huge sales.Demonetization had ruined house loan companies and the price of plastic segment dropped significantly. My dad is a Nilkamal dealer. He was buying furnitures from Nilkamal at a heavily discounted rate.Also I was betting on profit margins. Astral started manufacturing its own CPVC compound from last December. The product price hence will be competitive and on another note, it has highest market share in residential chlorinated polyvinyl chloride pipes market.';
SplitPlay(string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment