我的老师给出了这个 Java 示例,介绍如何从文本生成语音并保存到 Wav 文件。他要求我们修改它以将单词计时保存到磁盘。我在 SynthesizeOptions ( http://watson-developer-cloud.github.io/java-sdk/docs/java-sdk-7.2.0/com/ibm/watson/text_to_speech/v1/model/SynthesizeOptions.Builder.html ) 中看不到任何执行此操作的选项,即使 API 说这是可能的: https://cloud.ibm.com/docs/services/text-to-speech?topic=text-to-speech-timing#timingRequest
Authenticator authenticator = new IamAuthenticator("api_key");
TextToSpeech textToSpeech = new TextToSpeech(authenticator);
try {
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder()
.text(text)
.accept("audio/wav")
.voice("pt-BR_IsabelaV3Voice")
.timings(words)
.build();
// a callback is defined to handle certain events, like an audio transmission or a timing marker
// in this case, we'll build up a byte array of all the received bytes to build the resulting file
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
textToSpeech.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {
@Override
public void onAudioStream(byte[] bytes) {
// append to our byte array
try {
byteArrayOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});
// quick way to wait for synthesis to complete, since synthesizeUsingWebSocket() runs asynchronously
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// create file with audio data
String filename = id + ".wav";
OutputStream fileOutputStream = new FileOutputStream(filename);
byteArrayOutputStream.writeTo(fileOutputStream);
// clean up
byteArrayOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
最佳答案
你需要跳出框框思考。您有一个音频文件,单词计时是语音转文本服务的一项功能,而不是文本转语音服务的一项功能。
关于java - 是否可以在 Watson Java API 中获取从文本到语音的单词计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833133/