Java 来自音频文件的Android基本TTS引擎
我有一个mp3文件hello.mp3。我将mp3包装到FileInputStream中,并将输入流转换为字节,然后将字节推送到SynthesiscalBack.audioAvailable(字节、偏移量、长度),但这只会产生噪音。如果我将hello.mp3文件加载到Android音乐播放中,它的播放效果会很好 当我将字节从文件推送到snthesecallback时,为什么这不起作用?我已将代码粘贴到下面 这是我从mp3文件生成音频流的地方:Java 来自音频文件的Android基本TTS引擎,java,android,audio,io,text-to-speech,Java,Android,Audio,Io,Text To Speech,我有一个mp3文件hello.mp3。我将mp3包装到FileInputStream中,并将输入流转换为字节,然后将字节推送到SynthesiscalBack.audioAvailable(字节、偏移量、长度),但这只会产生噪音。如果我将hello.mp3文件加载到Android音乐播放中,它的播放效果会很好 当我将字节从文件推送到snthesecallback时,为什么这不起作用?我已将代码粘贴到下面 这是我从mp3文件生成音频流的地方: class AudioStream { In
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;
}
这是我的Inputstream to 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();
}
这是我的TextToSpeechService
类中的onSynthesizeText
方法
@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();
}
函数
audioAvailable(byte[]buffer,int offset,int length)
需要PCM样本作为输入。无法从.mp3文件中读取字节并将其用作函数的输入。您需要使用.wav文件或首先将.mp3文件转换为.wav文件,并将其用作audioAvailable
功能的输入。注意.mp3文件是压缩的,这意味着非常高的熵,因此当您按字面意思播放它们时,基本上会产生噪音。
//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);
}
});