Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextToSpeech takes too much time while synthesizeToFile in Android

I have used below code for synthesizing .txt file to .mp3 file using Android built-in TTS Engine.

Code:

 textToSpeech.synthesizeToFile(readFileText, utterParam, destinationFileName);

 textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(final String utteranceId) {
                    Log.e(TAG, "onStart...");
                }

                @Override
                public void onDone(final String utteranceId) {
                    Log.e(TAG, "onDone...");
                }

                @Override
                public void onError(String utteranceId) {
                    Log.e(TAG, "onError...");
                }
            });

Above is sample code. Here is flow of application execution:

  1. Get file from SD card
  2. Synthesize file to mp3
  3. Play a mp3 file

Issue : When file Synthesization is done then only I can play mp3 file. For even file of size 1 mb it is taking around 1 minute.

Is there any improvement I can do over?

Note : We need to use MediaPlayer as we need to play/pause reader.

Thanks.

like image 265
AndiGeeky Avatar asked Oct 19 '22 10:10

AndiGeeky


1 Answers

I have solved this problem to converting whole file into chunks of paragraphs and add paragraphs into TTS Engine and played directly.

 public static String[] convertFileToParagraph(String fileContent) {

//        String pattern = "(?<=(rn|r|n))([ \t]*$)+";
        String pattern = "([ \\t\\r]*\\n[ \\t\\r]*)+";
        return Pattern.compile(pattern, Pattern.MULTILINE).split(fileContent);
    }

/**
     * Divides files in to paragraphs
     */
    private void divideFileToChunks() {
        try {
            currentFileChunks = convertFileToParagraph(fileContent);
            currentFileChunks = makeSmallChunks(currentFileChunks);
            addChunksToTTS();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Divides file paragraphs into sentences of 200 characters
     *
     * @param currentFileChunks : list of paragraphs
     * @return : list of divided file
     */
    private String[] makeSmallChunks(String[] currentFileChunks) {
        try {
            ArrayList<String> smallChunks = new ArrayList<>();
            for (int i = 0; i < currentFileChunks.length; i++) {
                String chunk = currentFileChunks[i];
                if (chunk != null && chunk.length() > 200) {
                    int length = chunk.length();
                    int count = length / 200;
                    int modulo = length % 200;
                    for (int j = 0; j < count; j++) {
                        smallChunks.add(chunk.substring(200 * j, (200 * j) + 199));
                    }
                    if (modulo > 0) {
                        smallChunks.add(chunk.substring(chunk.length() - 1 - modulo, chunk.length() - 1));
                    }
                } else {
                    smallChunks.add(chunk);
                }
            }
            return smallChunks.toArray(new String[smallChunks.size()]);
        } catch (Exception e) {
            e.printStackTrace();
            return currentFileChunks;
        }
    }

    /**
     * Add all chunks to TTS(Text to Speech) Engine
     */
    private void addChunksToTTS() {
        try {
            String[] chunks = getCurrentFileChunks();
            if (chunks != null && chunks.length > 0) {
                for (int i = currentChunk; i < chunks.length; i++) {
                    utterParam.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i));
                    textToSpeech.speak(chunks[i], TextToSpeech.QUEUE_ADD, utterParam);
                    imgBtnT2SPlay.setImageResource(R.drawable.icon_pause_white);
                    edtT2SFileContents.setEnabled(false);
                    isPlaying = true;
                }
            }

            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Thanks.

like image 131
AndiGeeky Avatar answered Nov 04 '22 20:11

AndiGeeky