Android SDK接入

一. 概述

DUI Lite SDK 离线ASR++可在无网的情况下,提供根据声音特征信息识别出 情绪信息的能力。

二. SDK使用说明

使用当前功能时,请先授权,否则禁止使用。请参考 授权说明

2.1 离线ASR++识别

2.1.1 准备

jar:DUI-lite-SDK-for-Android-x.x.x.jar

so:libca.so(授权模块), libasrpp.so(离线ASR++模块),libduiutils.so(通用处理模块),libvad.so(vad模块),libduidns.so(dns解析模块)

资源:(资源可以放在assets目录中,也可以自己手动放在磁盘中,比如/sdcard/)

vad_xxx.bin (本地vad资源)

asrpp_xxx.bin(性别、情绪、年龄识别资源)!注意:如果资源只包含 性别 识别,那 sdk 回调结果就只返回性别信息

 

asr++ 可以根据声音特征信息识别出 性别、情绪、年龄 信息的能力,具体识别的是哪种能力只与加载的资源有关,代码是一样的。即 性别资源识别性别的信息,年龄资源识别年龄的信息

如需同时识别  性别、情绪、年龄 ,可以创建多个 asr++ 对象,加载不同资源即可。

2.1.2 使用流程

  1. 创建引擎对象,创建引擎对象方法: createInstance (多实例)
  2. 初始化引擎,调用 init 方法
  3. 启动引擎,调用 start 方法
  4. 默认使用Android的录音机获取音频数据,如需自行输入音频数据,可以使用 feedData 方法
  5. 此时引擎内部处理音频数据中,有识别出性别、年龄等信息或其它错误信息会从回调中返回。
  6. 停止引擎,调用 stop 方法
  7. 停止引擎后可以调用 start 方法重新启动引擎
  8. 销毁引擎,调用 destroy方法

 

三. 示例代码

涉及引擎类:AILocalAsrppEngine,详细使用示例请参考 demo

3.1 设置配置信息

参考代码

AILocalAsrppConfig config = new AILocalAsrppConfig();
config.setAsrppResource(SampleConstants.ASRPP_RES);
config.setVadEnable(true);
config.setVadResource(SampleConstants.VAD_RES);
config.setVadPauseTime(500);

部分方法参数说明

方法名 说明 默认值 是否必须 备注
setVadEnable(boolean vadEnable) 设置是否启用本地vad true 建议使用  
setVadPauseTime(int vadPauseTime) 设置VAD右边界 300ms vad enable时使用 只有在setVadEnable(true)使用
setVadResource(String vadResource) 设置本地vad资源 NA 使用vad必须配置

1. 如在sd里设置为绝对路径 如/sdcard/speech/***.bin

2. 如在 assets 里设置为名称

setAsrppResource(String asrppResource) 设置性别识别资源 NA

1. 如在sd里设置为绝对路径 如/sdcard/speech/***.bin

2. 如在 assets 里设置为名称

以上方法均有配套的get和is判断方法,比如说isVadEnable()获取当前vadEnable状态,getVadResource()获取vad资源路径,请根据需要自行使用。

完整的方法说明可以参考 Javadoc

 

3.2 初始化识别engine

// 详细示例可参见 Demo 里的 LocalAsrpp.java
AILocalAsrppEngine mAsrppEngine;
mAsrppEngine = AILocalAsrppEngine.createInstance();
mAsrppEngine.init(config, new AILocalAsrppListenerImpl());

以下是 AILocalAsrppEngine 的方法说明。

方法 方法说明 参数说明 默认值 备注
createInstance() 创建实例对象 NA NA 非单例
init(final AILocalAsrppConfig config, AILocalAsrppListener localAsrppListener) 初始化引擎 config配置信息;localAsrppListener引擎回调消息,具体回调方法说明参照 3.3 NA NA
start(final AILocalAsrppIntent aiLocalAsrppIntent) 启动录音,开始语音识别 NA NA NA
feedData(byte[] data, int size) 传入数据 传入音频字节流 NA 不使用SDK内部录音机时调用
stop() 停止录音,等待识别结果 NA NA NA
destroy() 销毁本地识别引擎 NA NA NA

 

3.3 实现ASRPP回调方法

    /**
     * 引擎回调接口,用以接收相关事件
     */
    public class AILocalAsrppListenerImpl implements AILocalAsrppListener {

        @Override
        public void onBeginningOfSpeech() {
            showInfo("检测到说话");

        }

        @Override
        public void onEndOfSpeech() {
            showInfo("检测到语音停止,开始识别...");
        }

        @Override
        public void onReadyForSpeech() {
            showInfo("请说话...");
        }

        @Override
        public void onRmsChanged(float rmsdB) {
            showTip("RmsDB = " + rmsdB);
        }

        @Override
        public void onError(AIError error) {
            Log.d(TAG, error.toString());
            showInfo(error.toString());
            setAsrBtnState(true, "识别");
        }

        @Override
        public void onResults(AIResult results) {
            Log.i(TAG, results.toString());
            try {
                showInfo(new JSONObject(results.getResultObject().toString()).toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            setAsrBtnState(true, "识别");
        }

        @Override
        public void onInit(int status) {
            if (status == 0) {
                Log.i(TAG, "end of init asr engine");
                showInfo("识别引擎加载成功");
                setAsrBtnState(true, "识别");
            } else {
                showInfo("识别引擎加载失败");
            }
        }


        @Override
        public void onResultDataReceived(byte[] buffer, int size) {
        }

        @Override
        public void onRawDataReceived(byte[] buffer, int size) {

        }

    }

 

3.4 设置intent并启动识别引擎

AILocalAsrppIntent aiLocalAsrppIntent = new AILocalAsrppIntent();
aiLocalAsrppIntent.setNoSpeechTimeOut(0);
aiLocalAsrppIntent.setMaxSpeechTimeS(0);
mAsrppEngine.start(aiLocalAsrppIntent);

部分intent方法如下表,仅总结部分set方法,对应的get与状态判断is方法不再这里列出,请参考Javadoc

方法 方法说明 参数说明 默认值 备注
setFespxEngine(IFespxEngine fespxEngine) 设置关联的信号处理引擎AILocalSignalAndWakeupEngine实例 前端信号处理引擎 NA 只在使用内部录音机且多麦模式下才需要设置
setNoSpeechTimeOut(int milliSecond) 设置无语音超时时长,如果达到该设置值时,自动停止录音并放弃请求识别内核 单位毫秒 5000ms NA
setMaxSpeechTimeS(int seconds) 设置音频最大录音时长,达到该值将取消语音引擎并抛出异常 单位秒 60s NA
setUseCustomFeed(boolean useCustomFeed) 设置是否自行feed数据,不使用内部录音机(包括MockRecord和AIAudioRecord) true/false false 设置true之后需要调用engine.feedData传入音频流

 

3.5 引擎的停止与销毁

mAsrppEngine.stop();
mAsrppEngine.destroy();

在识别完成之后请在合适的位置调用这两个方法。

 

3.6 完整的代码块

可以直接参考下方代码进行自己的使用,更多api请参考 Javadoc

AILocalAsrppEngine
// 详细示例可参见 Demo 里的 LocalAsrpp.java
AILocalAsrppEngine mAsrppEngine;


AILocalAsrppConfig config = new AILocalAsrppConfig();
config.setAsrppResource(SampleConstants.ASRPP_RES);
config.setVadEnable(true);
config.setVadResource(SampleConstants.VAD_RES);
config.setVadPauseTime(500);

mAsrppEngine = AILocalAsrppEngine.createInstance();
mAsrppEngine.init(config, new AILocalAsrppListenerImpl());


AILocalAsrppIntent aiLocalAsrppIntent = new AILocalAsrppIntent();
aiLocalAsrppIntent.setNoSpeechTimeOut(0);
aiLocalAsrppIntent.setMaxSpeechTimeS(0);
mAsrppEngine.start(aiLocalAsrppIntent);


mAsrppEngine.stop();
mAsrppEngine.destroy();

 

在 init 和 start 时会有很多参数设置,Demo 已经为常见情况做了示例,详细说明请查看 JavaDoc

 

 

四. 日志信息介绍

查看 asr++ 的日志信息,可以在 logcat 种过滤以下信息:

AILocalAsrppEngine   LocalAsrppProcessor   AsrppKernel

 

五. 识别结果

5.1 字段说明

序号

字段名称

字段类型

必选

字段说明

合理范围

示例

备注

1
libversion
string 内核库工程版本号  
1.9.37
 
2
binversion
string 资源版本号  
asrpp_common_2019010
 
3
delay
double 识别延时/ms  
948.688721

 

4
speech
long 音频长度/ms  
152960

 

5
coreversion
string 内核版本  
2.0
 
6
emotion
jsonobject 情绪识别结果  
{
"emotion":"happy",
"libversion":"1.0.0",
"delay":"800.932617",
"binversion":"emotion_common_20181227"
}
 
7
gender
jsonobject 性别识别结果  
{
"gender":"male",
"libversion":"1.0.0",
"delay":"50.847900",
"binversion":"gender_common_20190110_male_female"
}
 
8
age
jsonobject 年龄识别结果  
{
"age":"elder",
"libversion":"1.0.0",
"delay":"135.515625",
"binversion":"age_common_20190108"
}
 

5.1.1 emotion字段

序号

字段名称

字段类型

必选

字段说明

合理范围

示例

备注

1 emotion string 情绪识别返回类型

枚举值:angry happy sad normal surprise  fear disgust contempt

 

happy  
2
libversion
string 情绪识别库版本号  
1.0.0
 
3
delay
string 识别延时/ms  
800.932617
 
4
binversion
string 情绪识别资源  
emotion_common_20181227
 

 

5.1.2 gender字段

序号

字段名称

字段类型

必选

字段说明

合理范围

示例

备注

1 gender string 性别识别返回类型

枚举值:male

female

male  
2
libversion
string 性别识别库版本号  
1.0.0
 
3
delay
string 识别延时/ms  
50.847900
 
4
binversion
string 性别识别资源  
gender_common_20190110_male_female
 

 

5.1.3 age字段

序号

字段名称

字段类型

必选

字段说明

合理范围

示例

备注

1 age string 年龄识别返回类型

枚举值:

child
adult 
elder
elder  
2
libversion
string 年龄识别库版本号  
1.0.0
 
3
delay
string 识别延时/ms  
135.515625
 
4
binversion
string 年龄识别资源  
age_common_20190108
 

5.2 例子

 折叠源码
{
    "libversion":"1.9.37",
    "binversion":"asrpp_common_2019010",
    "delay":"948.688721",
    "speech":"152960",
    "coreversion":"2.0",
    "emotion":{
        "emotion":"happy",
        "libversion":"1.0.0",
        "delay":"800.932617",
        "binversion":"emotion_common_20181227"
    },
    "gender":{
        "gender":"male",
        "libversion":"1.0.0",
        "delay":"50.847900",
        "binversion":"gender_common_20190110_male_female"
    },
    "age":{
        "age":"elder",
        "libversion":"1.0.0",
        "delay":"135.515625",
        "binversion":"age_common_20190108"
    }
}

六. 错误码

错误码

 

七. 接口文档

 

更多的接口内容与描述请参阅 javadoc