我正在使用IBM Watson語音到文本的tutorial,使用WebSocket進行實時轉錄。我正在使用Angular。IBM Watson語音到文本發送麥克風數據關閉連接
前25行代碼從API reference複製而來。此代碼成功連接並啓動識別請求。沃森給我發消息{ "state": "listening" }
。
我寫了function onClose()
,記錄連接關閉時的日誌。
我做了一個運行處理程序$scope.startSpeechRecognition
的按鈕。這使用getUserMedia()
來傳輸來自麥克風的音頻和websocket.send()
以將數據流式傳輸到Watson。這不起作用。點擊此按鈕關閉連接。我假設我發送了錯誤的數據類型,Watson正在關閉連接?
我將websocket.send(blob);
從onOpen
移至我的處理程序$scope.startSpeechRecognition
。我將websocket.send(blob);
更改爲websocket.send(mediaStream);
。我可能有這個錯誤:'content-type': 'audio/l16;rate=22050'
。如何知道麥克風的比特率?
是否有JavaScript教程?當我在頂部的谷歌「IBM沃森語音對文本JavaScript教程」是8000-line SDK。 SDK是必需的還是我可以編寫一個簡單的程序來了解該服務的工作原理?
這裏是我的控制器:
'use strict';
app.controller('WatsonController', ['$scope', 'watsonToken', function($scope, watsonToken) {
console.log("Watson controller.");
var token = watsonToken;
var wsURI = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
+ "?watson-token=" + token + '&model=en-US_BroadbandModel';
var websocket = new WebSocket(wsURI); // opens connection to Watson
websocket.onopen = function(evt) { onOpen(evt) }; // executes when a connection opens
websocket.onclose = function(evt) { onClose(evt) }; // executes when a connection closes
websocket.onmessage = function(evt) { onMessage(evt) }; // logs messages from Watson to the console
websocket.onerror = function(evt) { onError(evt) }; // logs errors to the console
function onOpen(evt) {
var message = {
action: 'start',
'content-type': 'audio/flac',
'interim_results': true,
'max-alternatives': 3,
keywords: ['colorado', 'tornado', 'tornadoes'],
'keywords_threshold': 0.5
};
websocket.send(JSON.stringify(message));
// Prepare and send the audio file.
// websocket.send(blob);
// websocket.send(JSON.stringify({action: 'stop'}));
}
function onClose() {
console.log("Connection closed.");
};
function onMessage(evt) {
console.log(evt.data); // log the message to the console
}
$scope.startSpeechRecognition =() => {
console.log("Starting speech recognition.");
var constraints = { audio: true, video: false };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
console.log("Streaming audio.");
websocket.send(mediaStream);
})
.catch(function(err) { console.log(err.name + ": " + err.message); }); // log errors
};
$scope.stopSpeechRecognition =() => { // handler for button
console.log("Stopping speech recognition.");
websocket.send(JSON.stringify({action: 'stop'}));
};
$scope.closeWatsonSpeechToText =() => { // handler for button
console.log("Closing connection to Watson.");
websocket.close(); // closes connection to Watson?
};
}]);
這是我的模板:
<div class="row">
<div class="col-sm-2 col-md-2 col-lg-2">
<p>Watson test.</p>
</div>
</div>
<div class="row">
<div class="col-sm-2 col-md-2 col-lg-2">
<button type="button" class="btn btn-primary" ng-click="startSpeechRecognition()">Start</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2">
<button type="button" class="btn btn-warning" ng-click="stopSpeechRecognition()">Stop</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2">
<button type="button" class="btn btn-danger" ng-click="closeWatsonSpeechToText()">Close</button>
</div>
</div>
托馬斯,有一個javascript sdk,將使您的生活更輕鬆:https://github.com/watson-developer-cloud/speech-javascript-sdk看看那裏的例子。 –