uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

Watson Speech to Text #128

Open uniquejava opened 6 years ago

uniquejava commented 6 years ago

首页: https://github.com/watson-developer-cloud/speech-javascript-sdk

安装

bower install watson-speech --save
bower install fetch --save

STT有N多拿来就用的例子,照着抄就行了: https://watson-speech.mybluemix.net/

stt

核心代码如下:

// speech to text
var stream;
$('#btnTranscribeAndPlay').click(function () {

  fetch('https://watson-speech.mybluemix.net/api/speech-to-text/token').then(function (response) {
    return response.text();
  }).then(function (token) {

    var containerId = createSpeech2TextOutputContainer();

    stream = WatsonSpeech.SpeechToText.recognizeFile({
      token: token,
      file: document.querySelector('#audiofile').files[0],
      play: true, // play the audio out loud
      outputElement: '#'+ containerId // CSS selector or DOM Element (optional)
    });

    stream.on('error', function (err) {
      console.log(err);
    });

  }).catch(function (error) {
    console.log(error);
  });

});

$('#btnStop').click(function () {
  if (stream) {
    stream.stop();
  }
});

当然会出现跨域问题, chrome中搜索CORS下载了一个叫Moesif Origin & CORS Changer的插件,将插件变成on的状态, 问题解决.

编辑声音文件.

从voa english随便下载一段mp3, 用quicktime player打开, Ctrl + T 将文件trim成13s的样子, 保存成.m4a格式的文件, 然后用itunes打开, 将其另存为mp3文件. 搞定! 参考: Converting m4a to mp3 in iTunes

bluemix的笔记见: https://github.com/uniquejava/blog/issues/76

uniquejava commented 6 years ago

IE11的限制

见这个issue: https://github.com/watson-developer-cloud/speech-javascript-sdk/issues/57 他们的库中用了fetch.js. 需要浏览器支持Promise. 并且他们的库不支持IE11的在线实时语音转文本.

speech model

上面这段代码是照着官方示例抄的. API的详细参数见: https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#recognize_sessionless_nonmp12

RecogizeStream的文档: https://www.npmjs.com/package/watson-speech

http://watson-developer-cloud.github.io/speech-javascript-sdk/master/speech-to-text_recognize-stream.js.html

比如可以指定 model参数改变语言(默认是英文)