【发布时间】:2017-04-24 04:31:59
【问题描述】:
在我的文本到语音的输出中,我需要将采样率设置为大约 32000 Hz,Pitch - 1 和 SpeechRate - 0.2(我已经这样做了)。但我无法设置采样率。
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.setSpeechRate((float) 0.2);
tts.setPitch((float) 1);
}
}
}, TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
我使用 AudioTrack 来设置采样率,但这需要很长时间,因为我必须先 TTS synthesizeToFile,然后在 AudioTrack 中播放。
HashMap<String, String> myHasRead = new HashMap<String, String>();
myHasRead.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, outPutS);
String StorePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File myF = new File(StorePath+"/tempAudio.wav");
try {
myF.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
tts.setOnUtteranceProgressListener(new TtsUtteranceListener());
tts.synthesizeToFile("Bla Bla bla",myHasRead, StorePath+"/tempAudio.wav");
....
private class TtsUtteranceListener extends UtteranceProgressListener {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onDone(String utteranceId) {
playWav();
}
@Override
public void onError(String utteranceId) {
}
}
public void playWav(){
int minBufferSize = AudioTrack.getMinBufferSize(32000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = 512;
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 32000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
int i = 0;
byte[] s = new byte[bufferSize];
try {
FileInputStream fin = new FileInputStream(filepath + "/tempAudio.wav");
DataInputStream dis = new DataInputStream(fin);
at.play();
while((i = dis.read(s, 0, bufferSize)) > -1){
at.write(s, 0, i);
}
at.stop();
at.release();
dis.close();
fin.close();
} catch (FileNotFoundException e) {
// TODO
e.printStackTrace();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}
有任何方法可以将采样率直接设置为 TTS,例如 tts.setSampleRate(32000);,或者从 TTS 获取 Stream 到 AudioTrack,例如 DataInputStream dis = new DataInputStream(tts.speak("bla bla bla").getDataInputStream);。 简而言之,我需要适用于 Android 的 Chipmunk 的 Text to Speech,但没有 synthesizeToFile 或在 AudioTrack 中直接流式传输 TTS 语音数据,而不保存 TTS 的输出。
【问题讨论】:
标签: android text-to-speech audiotrack