java - 是否可以在 Watson Java API 中获取从文本到语音的单词计时?

标签 java json ibm-watson watson-assistant

我的老师给出了这个 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/

相关文章:

java - JDBC 将警告视为数据截断错误

C#反序列化派生并具有引用的对象

javascript - 验证/清理 JSON 数据

json - 如何在angularJS中将数据写入文件

python - Watson Discovery "Access is denied due to invalid credentials"- Python

python - Bluemix Python App启动失败

java - 每个堆栈跟踪行上的 Wildfly 日志日期

java - 将多个表返回到 spring jdbc 模板的存储过程

java - 在 AWT 事件模型中冒泡?

machine-learning - Watson 机器学习 api - token 刷新 400 错误