来自音频文件的Android基本TTS引擎

时间:2014-08-21 07:55:37

标签: java android audio io text-to-speech

我有一个mp3文件hello.mp3。我将mp3包装到FileInputStream并将输入流转换为字节,然后将字节推送到SynthesisCallback.audioAvailable(字节,偏移量,长度),但这只会产生噪音。如果我将文件加载到hello.mp3文件就好了我的Android音乐播放。

当我将文件中的字节推送到SnthesisCallback时,为什么这不起作用?我在下面粘贴了我的代码。

这是我从mp3文件生成音频流的地方:

 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;

}

这是我对byte []方法的输入流。

  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();
}

这是我onSynthesizeText课程中的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();
}

这就是我正在测试我的合成引擎的方法。

//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);
        }
    });

1 个答案:

答案 0 :(得分:4)

函数audioAvailable(byte[] buffer, int offset, int length)期望PCM样本作为输入。您无法从.mp3文件中读取字节并将其用作函数的输入。您需要使用.wav文件或首先将.mp3文件转换为.wav文件,并将其用作audioAvailable函数的输入。