基于javascript用olamisdk实现web端语音识别语义理解(speex压缩)

olami开放平台sdk除了支持功能外,更强大的在于支持语义理解功能,在平台和平台都有示例demo供大家下载。

在web端,基于用olami开放平台sdk也可以实现语音识别语义理解。本文就实现了这样一个小程序,web客户端本地用麦克风录音,录音的数据用speex压缩,然后跨域向服务器发送请求,返回识别的语音和语义字符串并显示。

先上图:

如下图刚载入的时候,未录音前界面

43508451_201707061706090111177289.jpg

点击开始录音button后

43508451_201707061706590189406347.jpg

一句话说完自动检测尾音结束标志然后压缩上传给服务器进行识别

43508451_201707061707200908436798.jpg

将从服务器获取的识别结果显示到界面上

43508451_201707061707340111727584.jpg

本例中说的语音是:“我要听三国演义这本书”,用的是平台听书app建立的语法。返回的json字串如下:

{

“data”: {

“asr”: {

“result”:“我要听三国演义这本书”,

“speech_status”: 0,

“final”: true,

“status”: 0

},

“nli”: [

{

“desc_obj”: {

“result”:“正在努力搜索中,请稍等”,

“status”: 0

},

“semantic”: [

{

“app”: “musiccontrol”,

“input”:“我要听三国演义这本书”,

“slots”: [

{

“name”:

“songname”,

“value”:“三国演义”

}

],

“modifier”: [

“play”

],

“customer”: “58df512384ae11f0bb7b487e”

}

],

“type”: “musiccontrol”

}

]

},

“status”: “ok”

}

通过解析这段json,可以得到app类型,songname(用于查询书名),modifier是play表示行为是播放。这段json的语法当然是用户自定义的,获得了json字串就可以解析得到程序需要的字段用于对应的操作,从而实现了语义理解功能。olami开放平台语法编写介绍

下面是建立的工程目录结构,发布后,网页打开运行在chrome或者QQ浏览器均可。

43508451_201707061708340955593587.jpg

下面讲述下voiceRecognize.html这个文件,其他都是min.,只需知道如何调用就可以了。

/p>

HTML>

voice recognize test

"load()">

点击开始button录音,点击停止button停止录音并进行识别

result:

window.AudioContext =

window.AudioContext || window.webkitAudioContext;

varaudioContext =newAudioContext();

varaudioInput =null,

realAudioInput =null,

inputPoint =null,

audioRecorder =null;

varrafID =null;

varanalyserContext =null;

varrecIndex =0;

varrecording =false;

varbRecorded =false;

functionload(){

initAudio();//初始化recorder

setAuthorization("http://portal.olavoice.com/cloudservice/api","51a4bb56ba954655a4fc834bfdc46af1","asr","68bff251789b426896e70e888f919a6d","nli");

setCallBackFromServerResult(getResultFromServer);

}

functioninitAudio(){

if(!navigator.getUserMedia)

navigator.getUserMedia =

navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

if(!navigator.cancelAnimationFrame)

navigator.cancelAnimationFrame = navigator.webkitCancelAnimationFrame ||

navigator.mozCancelAnimationFrame;

if(!navigator.requestAnimationFrame)

navigator.requestAnimationFrame = navigator.webkitRequestAnimationFrame

|| navigator.mozRequestAnimationFrame;

navigator.getUserMedia({audio:true}, gotStream,function(e){

alert('Error getting

audio');

console.log(e);

});

}

functiongotStream(stream){

inputPoint = audioContext.createGain();

// Create an AudioNode from the stream.

realAudioInput =

audioContext.createMediaStreamSource(stream);

audioInput = realAudioInput;

audioInput.connect(inputPoint);

audioRecorder =newRecorder(

inputPoint );

}

functionStartRecording()

{

// start recording

if(audioRecorder ==null)

{

initAudio();

alert("need initialize media");

}

audioRecorder.clear();

audioRecorder.record();

recording =true;

bRecorded =false;

ToggleLabels();

RegisterCallBackToRecorder();

}

functionStopRecording()

{

audioRecorder.stop();

audioRecorder.getBuffers(

gotBuffers );

}

functionRegisterCallBackToRecorder()

{//检测语音结束后回调

audioRecorder.setCallBack(speexEncode);

}

functionToggleLabels()

{

if(recording)

{

document .getElementById("recordbutton").value ="录音中?";

document .getElementById("speexEncodebutton").value ="停止录音";

varbtn = document .getElementById("recordbutton").value;

}else{

document .getElementById("speexEncodebutton").value ="识别中";

document .getElementById("recordbutton").value ="停止录音?";

}

}

window.record =function(e)

{

if(!recording)

{

StartRecording();

recording =true;

bRecorded =false;

}

else

{

StopRecording();

recording =false;

bRecorded =true;

}

ToggleLabels();

};

window.speexEncode =function()

{

exportSpeex();

};

functionexportSpeex()

{

recording =false;

bRecorded =true;

ToggleLabels();

audioRecorder.stop();

audioRecorder.exportPCM(uploadSpeexData);

}

functiongetResultFromServer()

{

document .getElementById('result').innerText =JSON.stringify(result);

document .getElementById("speexEncodebutton").value ="停止录音";

document .getElementById("recordbutton").value ="开始录音?"

}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐