【问题标题】:Java - How to convert InputStream into an appropriate format to use IBM Watson's Speech-to-Text service?Java - 如何将 InputStream 转换为适当的格式以使用 IBM Watson 的 Speech-to-Text 服务?
【发布时间】:2018-08-26 06:41:07
【问题描述】:

我正在尝试通过同时使用 Nexmo 和 IBM Watson 服务来实现电话对话的实时转录。我已经设置了一个 websocket 来接收来自 Nexmo 的二进制音频的InputStream。我还设置了与 IBM Watson 的语音到文本服务的 websocket 连接。我从 Nexmo 收到的音频流是 PCM 编码,频率为 8 kHz 或 16 kHz。从 Nexmo 获得的每条消息的帧大小为 20 毫秒。

IBM Watson Java SDK 的 Websocket 接口需要带有正确编码信息的 InputStream 才能成功转录。以下是我尝试过的数据调节:

  • 将从 Nexmo 获得的原始 InputStream 委托给 Watson 内容类型为"audio/l16; rate=16000; endianness=little-endian"
  • 使用static方法AudioSystem.getAudioInputStream(InputStream inputStream)获取AudioInputStream对象。此方法推测输入流的格式并返回AudioStream 对象。
  • 通过传递以下AudioFormat参数,使用静态方法AudioSystem.getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)获取AudioStream对象
    • audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16, 1, AudioSystem.NOT_SPECIFIED, 16, 16000, true);

但在上述所有尝试中,我未能从 IBM 服务获得任何类型的转录。当我第一次连接到服务时,我确实收到了来自 IBM 的 websocket 已连接并且位于 listening state 的响应。 IBM 云中没有可用的日志来查看发生了什么。我已经阅读了 SO 和 IBM 开发人员论坛,但找不到任何合适的示例。我非常确信,我对从 Nexmo 获得的数据进行调节的方式对于 Watson 服务来说并不理想。如何适当地调整我的数据以便使用 IBM Watson 转录语音?

这是我非常简化(以提高可读性)的代码示例(各种功能的不同方法加在一起)

// method to return Nexmo's NCCO, when the call is answered
public static String connectToWebSocket()
{
    JsonArray ncco = new JsonArray();

    JsonObject enclosingObject = new JsonObject();
    enclosingObject.addProperty("action", "connect");

    JsonObject webSocketEndpoint = new JsonObject();
    webSocketEndpoint.addProperty("type", "websocket");
    webSocketEndpoint.addProperty("uri", "ws://websocket-uri/call-stream");
    webSocketEndpoint.addProperty("content-type", "audio/l16;rate=16000");

    JsonObject header = new JsonObject();
    header.addProperty("app", "demo");

    webSocketEndpoint.add("header", header);

    JsonArray endpointArray = new JsonArray();
    endpointArray.add(webSocketEndpoint);

    enclosingObject.add("endpoint", endpointArray);

    ncco.add(enclosingObject);
    return ncco.toString();
}

// WebSocketController' onMessage method (receiving Nexmo's binary audio) 
@OnMessage
public void onMessage(InputStream inputStream, Session session)
{
    //transcriptionService.recognizeVoice(inputStream);
}

// IBMTransriptionService's recognizeVoice method
public void recognizeVoice(InputStream stream)
{
    if(stream == null) return;

    try
    {
        audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16, 1, AudioSystem.NOT_SPECIFIED, 16, 16000, true);
        RecognizeOptions recognizeOptions = new RecognizeOptions.Builder().audio(new AudioInputStream(stream, audioFormat, 16))
            .contentType("audio/l16; rate=16000; endianness=little-endian")
            .interimResults(true)
            .build();

        this.speechToText.setEndPoint("https://gateway-syd.watsonplatform.net/speech-to-text/api");
        this.speechToText.recognizeUsingWebSocket(recognizeOptions, this.transcriptionReceiver);
    }
    catch (Exception e)
    {
        logger.error("Failed when creating audio stream" + e.getMessage());
    }
}

其他链接:

  • 这是详细介绍二进制音频格式的 Nexmo 文档 通过websocket转发

    Watson Speech-to-Text API reference

【问题讨论】:

    标签: java websocket speech-to-text ibm-watson nexmo


    【解决方案1】:

    您使用什么模型向 Watson 发出请求? Nexmo 语音来自电话,因此即使您使用 16Khz,您也希望将其设置为窄带模型,例如 en-US_NarrowbandModel

    【讨论】:

    • 是的,我正在使用en-US_NarrowbandModel。还有什么我应该看的吗?
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2019-05-11
    • 2017-07-28
    • 1970-01-01
    • 2019-04-02
    • 2018-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多