process method

Future<void> process(
  1. String chunk
)

Implementation

Future<void> process(String chunk) async {
  if (_stopped) {
    return;
  }
  // We generate one file for each sentence. So, we need to split the string
  // using the sentence separator characters.
  _currentSencence += chunk;

  Pattern separator = RegExp(r'\.|\?|!');

  if (!_currentSencence.contains(separator)) {
    // The current sentence is not completed. No more actions are needed at
    // this time.
    return;
  }

  // Handling sentence end and begin to process new sentence

  Iterable<String> parts =
      splitPreservingSeparator(_currentSencence, separator);

  // Filtering empty parts for cases like "...".
  parts = parts.map((x) => x.trim()).where((part) => part.trim().isNotEmpty);

  if (parts.isEmpty) {
    _currentSencence = '';
    return;
  }

  // Means that the sentence is completed. So we need to create a audio
  // file.
  var sentence = parts.first;

  // We need to begin the processing of the next sentences.
  var rest = parts.skip(1).join('.');
  _currentSencence = '';

  if (sentence.isNotEmpty) {
    _logger.debug('Completed reading a sentence: $sentence');
    if (onSentenceCompleted != null) {
      var newSentence = onSentenceCompleted!(sentence);
      if (newSentence != null) {
        sentence = newSentence;
      }
    }
    var audioFile = _generateAudio(sentence);
    _sentences.add((sentence, audioFile));
  }

  await process(rest);
}