The main TTS engine for Android from the audio file

I have an mp3 file hello.mp3. I transfer mp3 to FileInputStream and convert the input stream to bytes, and then click bytes on SynthesisCallback.audioAvailable (bytes, offset, length), but this leads to simple noise. The hello.mp3 file plays just fine if I upload it to my Android music.

Why doesn't this work when I click bytes from a file on SnthesisCallback? I pasted my code below.

Here I am generating an audio stream from an mp3 file:

 class AudioStream {
    InputStream stream;
    int length;
}
private AudioStream getAudioStream(String text) throws IOException {
    // TODO parse text, and generate audio file.
    File hello = new File(Environment.getExternalStorageDirectory(), "hello.mp3");
    AudioStream astream = new AudioStream();
    astream.length = hello.length();
    astream.stream = new FileInputStream(hello);
    return astream;

}

This is my Inputstream to byte [] method.

  public byte[] inputStreamToByteArray(AudioStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[inStream.length];
    int bytesRead;
    while ((bytesRead = inStream.stream.read(buffer)) > 0) {
        baos.write(buffer, 0, bytesRead);
    }
    return baos.toByteArray();
}

This is my method onSynthesizeTextin my class TextToSpeechService.

 @Override
protected synchronized void onSynthesizeText(SynthesisRequest request,
        SynthesisCallback callback) {
    // TODO load language and other checks.

    // At this point, we have loaded the language 

    callback.start(16000,
            AudioFormat.ENCODING_PCM_16BIT, 1 /* Number of channels. */);

    final String text = request.getText().toLowerCase();
    try {
        Log.i(TAG, "Getting audio stream for text "+text);
        AudioStream aStream = getAudioStream(text);

         byte[] bytes = inputStreamToByteArray(aStream);
         final int maxBufferSize = callback.getMaxBufferSize();
         int offset = 0;
         while (offset < aStream.length) {
             int bytesToWrite = Math.min(maxBufferSize, aStream.length - offset);
             callback.audioAvailable(bytes, offset, bytesToWrite);
             offset += bytesToWrite;
         }

    } catch (Exception e) {
        e.printStackTrace();
        callback.error();
    }



    // Alright, we're done with our synthesis - yay!
    callback.done();
}

This is how I test my synthetic engine - in the making.

//initialize text speech
    textToSpeech = new TextToSpeech(this, new OnInitListener() {

        /**
         * a callback to be invoked indicating the completion of the TextToSpeech
         * engine initialization.
         */
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = textToSpeech.setLanguage(Locale.US);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("error", "Language is not supported");
                } else {
                    convertToSpeech("Hello");
                }
            } else {
                Log.e("error", "Failed  to Initilize!");
            }
        }


        /**
         * Speaks the string using the specified queuing strategy and speech parameters.
         */
        private void convertToSpeech(String text) {
            if (null == text || "".equals(text)) {
                return;
            }
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
    });
+4
1

audioAvailable(byte[] buffer, int offset, int length) PCM. .mp3 . .wav .mp3 .wav audioAvailable.

+4

All Articles