Text to speech long text V2

Text to speech long text V2

by onigetoc

JavaScript

var CHARACTER_LIMIT = 200;
var lang = "en";

var text = "MLA format follows the author-page method of in-text citation. This means that the author's last name and the page number(s) from which the quotation or paraphrase is taken must appear in the text, and a complete reference should appear on your Works Cited page. The author's name may appear either in the sentence itself or in parentheses following the quotation or paraphrase, but the page number(s) should always appear in the parentheses, not in the text of your sentence. Joe waited for the train. The train was late. Mary and Samantha took the bus.";

speak(text, lang)

function speak(text, lang) {

  //Support for multipart text (there is a limit on characters)
  var multipartText = [];

  if (text.length > CHARACTER_LIMIT) {

    var tmptxt = text;

    while (tmptxt.length > CHARACTER_LIMIT) {

      //Split by common phrase delimiters
      var p = tmptxt.search(/[:!?.;]+/);
      var part = '';

      //Coludn't split by priority characters, try commas
      if (p == -1 || p >= CHARACTER_LIMIT) {
        p = tmptxt.search(/[,]+/);
      }

      //Couldn't split by normal characters, then we use spaces
      if (p == -1 || p >= CHARACTER_LIMIT) {

        var words = tmptxt.split(' ');

        for (var i = 0; i < words.length; i++) {

          if (part.length + words[i].length + 1 > CHARACTER_LIMIT)
            break;

          part += (i != 0 ? ' ' : '') + words[i];

        }

      } else {

        part = tmptxt.substr(0, p + 1);

      }

      tmptxt = tmptxt.substr(part.length, tmptxt.length - part.length);

      multipartText.push(part);
      //console.log(part.length + " - " + part);

    }

    //Add the remaining text
    if (tmptxt.length > 0) {
      multipartText.push(tmptxt);
    }

  } else {

    //Small text
    multipartText.push(text);
  }


  //Play multipart text
  for (var i = 0; i < multipartText.length; i++) {

    //Use SpeechSynthesis
   ...