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 {
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) {
callback.start(16000,
AudioFormat.ENCODING_PCM_16BIT, 1 );
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();
}
callback.done();
}
This is how I test my synthetic engine - in the making.
textToSpeech = new TextToSpeech(this, new OnInitListener() {
@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!");
}
}
private void convertToSpeech(String text) {
if (null == text || "".equals(text)) {
return;
}
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
});