5

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • May be useful link [android-sdk-using-the-text-to-speech-engine](http://code.tutsplus.com/tutorials/android-sdk-using-the-text-to-speech-engine--mobile-8540) – Pratik Butani May 03 '16 at 08:53
  • 1
    Is synthesizing faster than speaking? If yes then why not synthesize and play back in smaller chunks? The first MP3 file would then be created and ready for play back more quickly and if the other chunks can be processed fast enough in the background then they'll always be ready waiting for playback when the previous one has been played back. – Markus Kauppinen May 03 '16 at 09:50
  • Is the only reason you need to synthesise prior to 'speaking' the utterance, due to needing the ability to pause the playback? How many characters are you attempting to synthesise? Engines have a limit on how many they can accept - this differs per engine. The output is wav/pcm not mp3 - are you running this through some conversion or is this an error on how you are labelling the file? – brandall May 04 '16 at 15:43

1 Answers1

3

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.

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66