IBM Watson语音到文本API中的1006错误代码



我正在使用棘轮连接到IBM Watson Websockets,并且它似乎总是适合较小的文件(我已经测试了66分钟的23 MB MP3文件),但对于较大的文件总是失败的(例如2小时56 MB MP3)。

这是我的日志:

[2019-03-17 21:43:23] local.DEBUG: RatchetClientconnect bf4e38983775f6e53b392666138b5a3a50e9c9c8  
[2019-03-17 21:43:24] local.DEBUG: startWatsonStream options = {"content-type":"audio/mpeg","timestamps":true,"speaker_labels":true,"smart_formatting":true,"inactivity_timeout":-1,"interim_results":false,"max_alternatives":1,"word_confidence":false,"action":"start"}  
[2019-03-17 21:43:24] local.DEBUG: Split audio into this many frames: 570222  
[2019-03-17 21:43:42] local.DEBUG: send action stop  
[2019-03-17 21:43:42] local.DEBUG: Received: {
   "state": "listening"
}  
[2019-03-17 21:43:42] local.DEBUG: Received first 'listening' message.  
[2019-03-17 22:56:31] local.DEBUG: Connection closed (1006 - Underlying connection closed)  

请注意接收第一个"听"消息,然后将连接与错误关闭之间的1H13M。

沃森说:" 1006表示该连接异常关闭。

https://www.rfc-editor.org/rfc/rfc6455说:

1006是保留的值,不得通过端点在近距离控制框架中设置为状态代码。它被指定为用于期望状态代码的应用程序使用,以表明该连接是异常关闭的,例如,不发送或接收近距离控制框架。

我可以调整我的代码的哪一部分,以便它可以处理更长的MP3文件而不丢弃1006错误?

RatchetClientconnect($url, [], $headers)->then(function(RatchetClientWebSocket $conn) use($contentType, $audioFileContents, $callback) {
    $conn->on('message', function($msg) use ($conn, $callback) {
        $this->handleIncomingWebSocketMessage($msg, $conn, $callback);
    });
    $conn->on('close', function($code = null, $reason = null) {
        Log::debug("Connection closed ({$code} - {$reason})");
    });
    $this->startWatsonStream($conn, $contentType);
    $this->sendBinaryMessage($conn, $audioFileContents); 
    Log::debug('send action stop');
    $conn->send(json_encode(['action' => 'stop']));
}, function (Exception $e) {
    Log::error("Could not connect: {$e->getMessage()} " . $e->getTraceAsString());
});

...

public function handleIncomingWebSocketMessage($msg, $conn, $callback) {
    Log::debug("Received: " . str_limit($msg, 100));
    $msgArray = json_decode($msg, true);
    $state = $msgArray['state'] ?? null;
    if ($state == 'listening') {
        if ($this->listening) {//then this is the 2nd time listening, which means audio processing has finished and has already been sent by server and received by this client.
            Log::debug("FINAL RESPONSE: " . str_limit($this->responseJson, 500));
            $conn->close(RatchetRFC6455MessagingFrame::CLOSE_NORMAL, 'Finished.'); 
            $callback($this->responseJson);
        } else {
            $this->listening = true;
            Log::debug("Received first 'listening' message.");
        }
    } else {
        $this->responseJson = $msg;
    }
}
public function sendBinaryMessage($conn, $fileContents) {
    $chunkSizeInBytes = 100; //probably just needs to be <= 4 MB according to Watson's rules
    $chunks = str_split($fileContents, $chunkSizeInBytes);
    Log::debug('Split audio into this many frames: ' . count($chunks));
    $final = true;
    foreach ($chunks as $key => $chunk) {
        $frame = new RatchetRFC6455MessagingFrame($chunk, $final, RatchetRFC6455MessagingFrame::OP_BINARY);
        $conn->send($frame);
    }
}

作为一般建议,基于文件的识别,尤其是在文件大于几个MB的情况下,应使用watson /recognitions api完成(更多详细信息:https://https://cloud Cloud.ibm.com/apidocs/语音到文本),这是异步的。您不需要保持连接几个小时,这不是一个好练习,因为您可以遇到阅读超时,您可能会失去网络连接等。,然后您可以每x分钟获得状态,也可以通过回调通知,无论对您有利的什么。

curl -X POST -u "apikey:{apikey}" --header "Content-Type: audio/flac" --data-binary @audio-file.flac "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?callback_url=http://{user_callback_path}/job_results&user_token=job25&timestamps=true"

顺便说一句。您的Websockets客户端是否使用乒乓帧来保持连接活力?我注意到您不要求临时结果({"content-type":"audio/mpeg","timestamps":true,"speaker_labels":true,"smart_formatting":true,"inactivity_timeout":-1,"interim_results":false,"max_alternatives":1,"word_confidence":false,"action":"start"}),这是保持连接打开但不太可靠的另一种方法。请检查乒乓框。

最新更新