electron duix sdk
npm install electron-duix``shell`
npm i electron-duix -S
const mainWindow = new BrowserWindow({
...
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
nodeIntegrationInWorker: true,
webSecurity: false,
}
});
`快速开始
`jsconst duix = require('electron-duix')
// 添加事件监听
duix.on('progress', data => {
// 加载进度 0-1
})
duix.on('show', () => {
// 数字人已显示
})
duix.on('speakStart', res => {
// speak开始
})
duix.on('speakSection', res => {
// 播放音频片段
})
duix.on('speakEnd', res => {
// speak结束
})
duix.on('openAsrSuccess', () => {
// 开启asr成功
})
duix.on('asrClose', () => {
// asr已关闭
})
duix.on('asrResult', res => {
// asr失败结果
})
duix.on('bye', (res) => {
// 会话结束
})
duix.on('error', res => {
// 会话异常
if (res.code == 5001) {
console.info('模型加载失败')
}
})
const res = await duix.init({
modelDir: 'xxx',
appId: 'xxx',
appKey: 'xxx',
containerLable: '.remote-container'
})
if (!res?.err) {
duix.start({ conversationId: 'xxx' })
}
`
方法
#### init(option: object): Promise
>
`js
> duix.init({
> modelDir: 'xxx',
> appId: 'xxx',
> appKey: 'xxx',
> containerLable: '.remote-container'
> })
> `参数
| 名称| 类型 |必填 | 描述 |
| :-----------------| :-------------- | :----------------------------------------------------------- | :------ |
| modelDir | string | 是 | 模型存放文件夹,长度不宜过长,请使用英文路径。 |
| appId | string | 是 | appId |
| appKey | string | 是 | app秘钥 |
| containerLable | string | 是 | 数字人容器。数字人将渲染到这个Dom中。 |
#### start(option:object): Promise
>
`js
> duix.start({
> conversationId: '',
> })
> `参数
| key | 类型 | 必填 | 默认值 | 说明 |
| ----------- | ------- | ---- | ------ | ------------------------------------------------------------ |
| conversationId| number | 是 | | 平台会话id|
#### break()
打断数字人说话
#### speak(option: Object): Promise
驱动数字人说话,支持文本驱动和音频文件驱动。
`javascript
duix.speak({content: '', audio: 'https://your.website.com/xxx/x.wav'})
`参数
| 名称| 类型 |必填 | 描述 |
| :-----------------| :-------------- | :----------------------------------------------------------- | ------ |
| content | string | 是 | 数字人回答的文本 |
| audio | string | 否 | 数字人回答音频地址,可以通过
getAnswer获取平台配置的答案|
| interrupt | boolean | 否 | 是否打断之前说的话 |#### answer(option: Object): Promise
数字人回答问题
`javascript
duix.answer({question: 'xxx'})
`参数
| 名称| 类型 |必填 | 描述 |
| :-----------------| :-------------- | :----------------------------------------------------------- | ------ |
| question | string | 是 | 问题文本 |
| interrupt | boolean | 否 | 是否打断之前说的话 |
| userId | number | 否 | 业务层用户唯一id,开启记忆功能时使用,用来关联用户历史记录. 如果为空,默认使用appId |
| isHistory | boolean | 否 | 是否开启历史记录 |
| isVector | boolean | 否 | 决策是否带入之前的相似回答 |
#### openAsr():Promise
开启语音实时识别(注意此方法需在show触发时候调用)。开启语音实时识别后,可通过监听
asrResult 事件,接收识别(文本)结果。#### closeAsr():Promise
关闭语音实时识别。
#### stop()
停止当前会话。建议在页卸载事件中调用此方法,以防止刷新或关闭网页时当前会话资源未及时释放。
#### getLocalStream()
获取本地语音流,方便外部做音频可视化等功能
#### on(eventname, callback)
监听事件。
##### 参数
###### eventname
事件名称,详见下方表格。
###### callback
回调函数
返回格式说明
对于返回Promise的方法,参数格式为 { err, ...data } ,如果err不为空,则代表调用失败。事件
| 名称 | 描述 |
| :------------- | :----------------------------------------------------------- |
| error | 有未捕获错误时触发。 |
| bye | 会话结束时触发。 |
| show | 出数字人已显示。 |
| progress | 数字人加载进度。 |
| speakSection | 数字人说话当前的音频和文本片段(answer方法会采用流式获取结果,如果单独调用speak,该事件与speakStart一致) |
| speakStart | 驱动数字人说话,到数字人实际说话之间有一点延迟,此事件表示数字人实际开始说话了 |
| speakEnd | 数字人说话结束 |
| openAsrSuccess | 语音实时识别开启成功后触发 |
| asrResult | 语音实时识别结果 |
| asrClose | 关闭语音实时识别成功时触发 |
$3
`js
{
code: '', // 错误码
message: '', // 错误信息
data: {} // 错误内容
}
`error code
| 名称 | 描述 | data |
| :------------- | :----------| :---------- |
| 5001 | 模型加载失败| |
$3
number类型的进度,0-1
$3
`javascript{
audio: '', // 音频地址
content: '', // 文本内容
}
`$3
`javascript{
audio: '', // 音频地址
content: '', // 文本内容
}
`$3
`javascript{
audio: '', // 音频地址
content: '', // 文本内容
}
``字符串类型,asr识别文本结果
0.1.5
1. 优化渲染逻辑,解决音画不同步问题。