JSFiddle - React, Tailwind, and code Playground
by ajai jothi
HTML
Skip to content
Sign up Sign in
This repository
Explore
Features
Enterprise
Blog
Watch 4 Star 53 Fork 10 janantala/speech-synthesis
branch: master speech-synthesis/src/polyfill.js
@janantalajanantala on May 22, 2014 feat: get speech synthesis and utterance
1 contributor
RawBlameHistory 376 lines (296 sloc) 8.837 kB
(function(window, document){
'use strict';
var splitText = function(text, delimeters, limit){
var sentences = [];
// split text by multiple delimeters
var reduce = function(text, index) {
if (delimeters[index] && text.trim().length) {
if (text.indexOf(delimeters[index]) > -1) {
var s = 1;
var splitted = text.split(delimeters[index]);
splitted.forEach(function(words){
if (words.length) {
var suffix = '';
if (s != splitted.length) {
suffix = delimeters[index];
}
words = (words + suffix).trim();
}
if (words.length && words.length <= limit) {
sentences.push(words);
}
else {
reduce(words, index + 1);
}
s++;
});
}
else {
reduce(text, index + 1);
}
}
else if (text.length) {
var regexp = new RegExp('.{1,' + limit + '}', 'g'); // /.{1,100}/g
var parts = text.match(regexp);
while (parts.length > 0) {
sentences.push(parts.shift().trim());
}
}
};
reduce(text, 0);
var result = [];
// merge short sentences
sentences.forEach(function(sentence){
if (! result.length) {
result.push(sentence);
}
else if (result[result.length - 1].length + sentence.length + 1 <= limit) {
result[result.length - 1] += ' ' + sentence;
}
else {
result.push(sentence);
}
});
return result;
};
var SpeechSynthesisUtterancePolyfill =...
JavaScript
/* var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function(event) {
var interim_transcript = '', final_transcript = '';
if (typeof(event.results) == 'undefined') {
recognition.stop();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
if (final_transcript || interim_transcript) {
console.log(final_transcript , interim_transcript);
switch(final_transcript.trim()){
case 'scroll up':
console.log(1);
$("html, body").animate({ scrollTop: 0 }, "slow");
break;
case 'scroll down':
console.log(1);
$("html, body").animate({ scrollTop: 1000 }, "slow");
break;
}
}
};
recognition.start(); */
function Questions(questions) {
this.questions = questions;
this.currentIndex = 0;
this.MAX = this.questions.length - 1;
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
this.recognition = new webkitSpeechRecognition();
this.recognition.continuous = true;
this.recognition.interimResults = true;
this.recognition.onresult = function(event) {
var last = event.results.length - 1;
var result = event.results[last][0].transcript;
console.log(event);
}
this.recognition.onspeechend = function() {
recognition.stop();
}
}
Questions.prototype.start = function() {
console.log(this.recognition);
this.recognition.start();
};
Questions.prototype.next = function() {
if (this.currentIndex < this.MAX) {
this.currentIndex++;
this.start();
}
};
window.onclick = function() {
var x = new Questions([]);
x.start();
};