ecomfe / Basr
Licence: mit
Baidu Automatic Speech Recognition for JS
Programming Languages
javascript
184084 projects - #8 most used programming language
basr
Baidu Automatic Speech Recognition for JS
百度语音识别 JS SDK
使用 百度语音 的 语音识别API,为页面提供语音识别能力。
Usage
import basr from 'basr';
basr
.authorize({token: 'xxxx'}) // 语音识别 Access Token
.then(
() => {
// 开始语音采集
let voice = basr.start();
// 录音 5 秒
setTimeout(
() => {
voice
.end() // 结束语音采集
.result() // 进行语音识别
.then(
text => {
// text 就是识别结果
},
error => {
// Oops ... 识别出错了...
}
);
},
5000
);
},
() => alert('如要使用语音识别,请授权麦克风操作')
);
完整的演示请参考 demo/index.html。如要在本地运行 demo,请先确保已安装了 edp,并在 demo/index.html
的 94 行中填入已申请到的百度语音 Access Token,然后在项目根目录下使用 edp webserver
命令启动本地调试服务器,访问 http://127.0.0.1:8848/demo/index.html
查看最终的效果
API
Methods
authorize(options)
语音采集授权,提示用户授权麦克风的使用权限,进行语音采集之前必须调用此方法
-
options
{Object}
配置参数-
token
{string}
语音识别的 token, 具体请参见 百度语音关于 Access Token 的说明 -
url
{string=}
语音识别 API 地址,目前由于线上服务暂时不支持跨域通信,请设置此参数为本地的 API 代理地址
-
token
-
return
{Promise}
授权结果
start(options)
语音采集
-
options
{Object=}
录音参数-
lang
{string=}
语言种类,支持zh
(中文)、ct
(粤语)、en
(英文),默认为zh
-
sampleRate
{string=}
语音识别的采样率,可选8000
、16000
,默认为8000
,采样率与数据传输量成正比
-
lang
-
return
{Object}
语音对象
recognize(voice, options)
语音识别
-
voice
{Float32Array}
语音数据 -
options
{Object}
配置信息-
token
{string}
语音识别 token -
sampleRate
{number}
输入语音的采样率 -
lang
{string=}
语言种类,支持zh
(中文)、ct
(粤语)、en
(英文),默认为zh
-
outputSampleRate
{number=}
语音识别的采样率,可选8000
、16000
,默认为8000
-
token
Voice
语音对象
end()
停止语音采集
result()
获取语音识别结果
-
return
{Promise}
wav(sampleRate)
将语音导出成 wav 格式
-
sampleRate
{number}
WAV 的采样率,可选8000
、16000
,默认为8000
-
return
{ArrayBuffer}
...
let wav = voice.wav();
let audio = document.createElement('audio');
audio.setAttribute('autoplay', 'autoplay');
audio.src = URL.createObjectURL(new Blob(wav, {type: 'audio/wav'}));
document.body.appendChild(audio);
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].