diff --git a/sdk/README.md b/sdk/README.md new file mode 100644 index 0000000..0f19198 --- /dev/null +++ b/sdk/README.md @@ -0,0 +1,30 @@ +# asr-linux-cpp-demo +## 简介 + +Linux C++ SDK 仅有在线长语音功能,包含在线语义。没有任何离线功能。 + + + +- 仅支持x64 linux 操作系统, g++ 4.8 以上版本 + +在centos 4 6 7 及ubuntu 14.04, g++ 4.8 版本测试通过 + +**SDK内部限制10个线程并发,即最多支持10个音频的实时识别。 ** + + + +## 运行 + +sample目录下有三个实例工程 + +sample/asr 运行 sh build_and_run.sh +sample/asrDemo2 下 run目录中运行 sh build_and_run.sh 。同时也是为Clion工程 +sample/asrDemo-srt 阅读该目录下的readme文件,再运行 + +## 完整文档 + +http://ai.baidu.com/docs#/ASR-Linux-SDK/top + +## 项目地址 + +https://github.com/Baidu-AIP/asr-linux-cpp-demo diff --git a/sdk/changelog.txt b/sdk/changelog.txt new file mode 100644 index 0000000..511665b --- /dev/null +++ b/sdk/changelog.txt @@ -0,0 +1,2 @@ +3.0.0.30628d440-V1 +1. ѵƽ̨֧֣LMID \ No newline at end of file diff --git a/sdk/clean.sh b/sdk/clean.sh new file mode 100644 index 0000000..759d19c --- /dev/null +++ b/sdk/clean.sh @@ -0,0 +1,6 @@ +cd sample +cd asr && make clean && rm core main *.log +cd .. +cd asrDemo2/run && make clean +cd ../.. +cd asrDemo-srt && rm -rf run/* diff --git a/sdk/include/ASR/bds_ASRDefines.hpp b/sdk/include/ASR/bds_ASRDefines.hpp new file mode 100644 index 0000000..566c6a5 --- /dev/null +++ b/sdk/include/ASR/bds_ASRDefines.hpp @@ -0,0 +1,234 @@ +// +// bds_ASRDefines.hpp +// SDKTester +// +// Created by baidu on 16/2/23. +// Copyright © 2016年 baidu. All rights reserved. +// + +#ifndef bds_ASRDefines_h +#define bds_ASRDefines_h + +#include +//#include "bds_int_types.h" +#include +namespace bds { + +#pragma mark - 设定采样率 +typedef enum TBDVoiceRecognitionRecordSampleRateFlags +{ + EVoiceRecognitionRecordSampleRateAuto = 0, + EVoiceRecognitionRecordSampleRate8K, + EVoiceRecognitionRecordSampleRate16K, +} TBDVoiceRecognitionRecordSampleRateFlags; + +#pragma mark - 语音识别类型 +typedef enum TBDVoiceRecognitionProperty +{ + EVoiceRecognitionPropertyMusic = 10001, // 音乐 + EVoiceRecognitionPropertyVideo = 10002, // 视频 + EVoiceRecognitionPropertyApp = 10003, // 应用 + EVoiceRecognitionPropertyWeb = 10004, // web + EVoiceRecognitionPropertySearch = 10005, // 热词 + EVoiceRecognitionPropertyEShopping = 10006, // 电商&购物 + EVoiceRecognitionPropertyHealth = 10007, // 健康&母婴 + EVoiceRecognitionPropertyCall = 10008, // 打电话 + EVoiceRecognitionPropertySong = 10009, // 录歌识别 + EVoiceRecognitionPropertyShake = 10010, // 摇一摇拾台 + EVoiceRecognitionPropertyMedicalCare = 10052, // 医疗 + EVoiceRecognitionPropertyCar = 10053, // 汽车 + EVoiceRecognitionPropertyCatering = 10054, // 娱乐餐饮 + EVoiceRecognitionPropertyFinanceAndEconomics = 10055, // 财经 + EVoiceRecognitionPropertyGame = 10056, // 游戏 + EVoiceRecognitionPropertyCookbook = 10057, // 菜谱 + EVoiceRecognitionPropertyAssistant = 10058, // 助手 + EVoiceRecognitionPropertyRecharge = 10059, // 话费充值 + EVoiceRecognitionPropertyMap = 10060, // 地图 + EVoiceRecognitionPropertyInput = 20000, // 输入 + // 离线垂类 + EVoiceRecognitionPropertyContacts = 100014, // 联系人指令 + EVoiceRecognitionPropertySetting = 100016, // 手机设置 + EVoiceRecognitionPropertyTVInstruction = 100018, // 电视指令 + EVoiceRecognitionPropertyPlayerInstruction = 100019, // 播放器指令 + EVoiceRecognitionPropertyRadio = 100020, // 收音机 +} TBDVoiceRecognitionProperty; + +#pragma mark - 设置识别语言 +typedef enum TBDVoiceRecognitionLanguage +{ + EVoiceRecognitionLanguageChinese = 0, + EVoiceRecognitionLanguageCantonese, + EVoiceRecognitionLanguageEnglish, + EVoiceRecognitionLanguageSichuanDialect, +} TBDVoiceRecognitionLanguage; + +#pragma mark - 语音识别请求资源类型 +typedef enum TBDVoiceRecognitionProtocol +{ + EPROTOCOL_DEFAULT = 0, + EPROTOCOL_SEARCH_NBEST = 1, + EPROTOCOL_INPUT_NBEST_PROTOCOL = 2, + EPROTOCOL_POST_PROTOCOL = 101, + EPROTOCOL_WISE_PROTOCOL = 300, + EPROTOCOL_WISE_TEXT_PROTOCOL = 301, + EPROTOCOL_AUDIO_DA_PROTOCOL = 302, + EPROTOCOL_NLU_PROTOCOL = 303, + EPROTOCOL_NLU_TEXT_PROTOCOL = 304, + EPROTOCOL_WISE_NLU_PROTOCOL = 305, + EPROTOCOL_TALK_PROTOCOL = 306, + EPROTOCOL_SEARCH_MUSIC_PROTOCOL = 1000, +} TBDVoiceRecognitionProtocol; + +#pragma mark - 调试日志级别 +typedef enum TBDVoiceRecognitionDebugLogLevel +{ + EVRDebugLogLevelOff = 0, + EVRDebugLogLevelFatal = 1, + EVRDebugLogLevelError = 2, + EVRDebugLogLevelWarning = 3, + EVRDebugLogLevelInformation = 4, + EVRDebugLogLevelDebug = 5, + EVRDebugLogLevelTrace = 6 +} TBDVoiceRecognitionDebugLogLevel; + +#pragma mark - 语音识别状态 +typedef enum TBDVoiceRecognitionClientWorkStatus +{ + EVoiceRecognitionClientWorkStatusStartWorkIng, // 识别工作开始,开始采集及处理数据 + EVoiceRecognitionClientWorkStatusStart, // 检测到用户开始说话 + EVoiceRecognitionClientWorkStatusEnd, // 本地声音采集结束,等待识别结果返回并结束录音 + EVoiceRecognitionClientWorkStatusNewRecordData, // 录音数据回调 + EVoiceRecognitionClientWorkStatusFlushData, // 连续上屏 + EVoiceRecognitionClientWorkStatusFinish, // 语音识别功能完成,服务器返回正确结果 + EVoiceRecognitionClientWorkStatusMeterLevel, // 当前音量回调 + EVoiceRecognitionClientWorkStatusCancel, // 用户取消 + EVoiceRecognitionClientWorkStatusError, // 发生错误 + /* 离线引擎状态 */ + EVoiceRecognitionClientWorkStatusLoaded, // 离线引擎加载完成 + EVoiceRecognitionClientWorkStatusUnLoaded, // 离线引擎卸载完成 + /* CHUNK状态 */ + EVoiceRecognitionClientWorkStatusChunkThirdData, // CHUNK: 识别结果中的第三方数据 + EVoiceRecognitionClientWorkStatusChunkVPRes, // CHUNK: 声纹会议返回信息 vp_res + EVoiceRecognitionClientWorkStatusChunkNlu, // CHUNK: 识别结果中的语义结果 + EVoiceRecognitionClientWorkStatusChunkEnd, // CHUNK: 识别过程结束 + /* LOG */ + EVoiceRecognitionClientWorkStatusFeedback, // Feedback: 识别过程反馈的打点数据 + /* Only for iOS */ + EVoiceRecognitionClientWorkStatusRecorderEnd, // 录音机关闭,页面跳转需检测此时间,规避状态条 (iOS) + /* LONG SPEECH END */ + EVoiceRecognitionClientWorkStatusLongSpeechEnd // 长语音结束状态 +} TBDVoiceRecognitionClientWorkStatus; + +#pragma mark - 语音识别策略 +typedef enum TBDVoiceRecognitionStrategy +{ + EVR_STRATEGY_ONLINE = 0, // 在线识别 + EVR_STRATEGY_OFFLINE, // 离线识别 + EVR_STRATEGY_ONLINE_PRI, // 在线优先 + EVR_STRATEGY_OFFLINE_PRI, // 离线优先 + EVR_STRATEGY_BOTH, // 并行模式 +} TBDVoiceRecognitionStrategy; + +#pragma mark - 语音识别离线引擎类型 +typedef enum TBDVoiceRecognitionOfflineEngineType +{ + EVR_OFFLINE_ENGINE_INPUT = 0, // 离线引擎输入法模式 + EVR_OFFLINE_ENGINE_NAVI, // 离线引擎导航模式 + EVR_OFFLINE_ENGINE_GRAMMER, // 离线引擎语法模式 +} TBDVoiceRecognitionOfflineEngineType; + +#pragma mark - 识别结果类型 +typedef enum TBDVoiceRecognitionResultType +{ + EVR_RESULT_TYPE_ERROR = -1, // Error code + EVR_RESULT_TYPE_EMPTY = 0, // Empty reply + EVR_RESULT_TYPE_PARTIAL = 1, // Partial result + EVR_RESULT_TYPE_NBEST = 2, // Nbest result + EVR_RESULT_TYPE_CN = 3, // CN result + EVR_RESULT_TYPE_RESOURCE = 4, // NLU & resource result + EVR_RESULT_TYPE_THIRD = 5, // CHUNK: Third party data + EVR_RESULT_TYPE_FINISH = 6, // CHUNK: Finish + EVR_RESULT_TYPE_END = 7, // CHUNK: End + EVR_RESULT_TYPE_VP_RES = 8, // vp_res +} TBDVoiceRecognitionResultType; + +#pragma mark - 网络类型 +typedef enum TBDVoiceRecognitionNetworkType +{ + EVR_NETWORK_TYPE_NO = 0, + EVR_NETWORK_TYPE_2G, + EVR_NETWORK_TYPE_3G, + EVR_NETWORK_TYPE_4G, + EVR_NETWORK_TYPE_WIFI, +} TBDVoiceRecognitionNetworkType; + +#pragma mark - 语音压缩类型 +typedef enum TBDVoiceRecognitionAudioCompressionType +{ + EVR_AUDIO_COMPRESSION_MIN = 0, + EVR_AUDIO_COMPRESSION_PCM = 1, + EVR_AUDIO_COMPRESSION_BV32 = 2, + EVR_AUDIO_COMPRESSION_AMR = 3, + EVR_AUDIO_COMPRESSION_MAX = 4, +} TBDVoiceRecognitionAudioCompressionType; + +#pragma mark - 语音识别错误通知状态分类 +typedef enum TVoiceRecognitionClientErrorDomain +{ + EVRClientErrorDomainRecord = 10, // 录音设备出错 + EVRClientErrorDomainVAD = 20, // 语音数据处理过程出错 + EVRClientErrorDomainOnline = 30, // 在线识别引擎出错 + EVRClientErrorDomainLocalNetwork = 31, // 本地网络联接出错 + EVRClientErrorDomainHTTP = 32, // HTTP协议错误 + EVRClientErrorDomainServer = 33, // 服务器返回错误 + EVRClientErrorDomainOffline = 34, // 离线引擎返回错误 + EVRClientErrorDomainCommon = 40, // 其他错误 +} TVoiceRecognitionClientErrorDomain; + +#pragma mark - 语音识别错误通知状态 +typedef enum TVoiceRecognitionClientErrorCode +{ + EVRClientErrorCodeRecoderException = (EVRClientErrorDomainRecord << 16) | (0x0000FFFF & 1), // 录音设备异常 + EVRClientErrorCodeRecoderNoPermission = (EVRClientErrorDomainRecord << 16) | (0x0000FFFF & 2), // 无录音权限 + EVRClientErrorCodeRecoderUnAvailable = (EVRClientErrorDomainRecord << 16) | (0x0000FFFF & 3), // 录音设备不可用 + EVRClientErrorCodeInterruption = (EVRClientErrorDomainRecord << 16) | (0x0000FFFF & 4), // 录音中断 + + EVRClientErrorCodeVADException = (EVRClientErrorDomainVAD << 16) | (0x0000FFFF & 1), // 前端库异常 + EVRClientErrorCodeNoSpeech = (EVRClientErrorDomainVAD << 16) | (0x0000FFFF & 2), // 用户未说话 + EVRClientErrorCodeShort = (EVRClientErrorDomainVAD << 16) | (0x0000FFFF & 3), // 用户说话声音太短 + + EVRClientErrorCodeOnlineExceptioin = (EVRClientErrorDomainOnline << 16) | (0x0000FFFF & 1), // 在线识别引擎异常 + EVRClientErrorCodeOnlineNetworkUnavailable = (EVRClientErrorDomainOnline << 16) | (0x0000FFFF & 2), // 网络不可用 + EVRClientErrorCodeOnlineTokenFailed = (EVRClientErrorDomainOnline << 16) | (0x0000FFFF & 3), // 获取token失败 + EVRClientErrorCodeOnlineResolveUrlFailed = (EVRClientErrorDomainOnline << 16) | (0x0000FFFF & 4), // 解析url失败 + EVRClientErrorCodeLocalTimeout = (EVRClientErrorDomainLocalNetwork << 16) | (0x0000FFFF & 1), // 请求超时 + + EVRClientErrorCodeServerParamError = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3001), // 协议参数错误 + EVRClientErrorCodeServerRecognError = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3002), // 识别过程出错 + EVRClientErrorCodeServerNoFindResult = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3003), // 没有找到匹配结果 + EVRClientErrorCodeServerAppNameUnknownError = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3004), // AppnameUnkown错误 + EVRClientErrorCodeServerSpeechQualityProblem = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3005), // 声音不符合识别要求 + EVRClientErrorCodeServerSpeechTooLong = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3006), // 语音过长 + EVRClientErrorCodeServerSpeechParamsUnknow = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3008), // 上行参数未知,(可能是gzip解压失败) + EVRClientErrorCodeServerSpeechNoUploadLink = (EVRClientErrorDomainServer << 16) | (0x0000FFFF & -3011), // 只有下行链接没有上行链接 + + EVRClientErrorCodeCommonBusy = (EVRClientErrorDomainCommon << 16) | (0x0000FFFF & 4095), // 识别器忙 + EVRClientErrorCodeCommonPropertyListInvalid = (EVRClientErrorDomainCommon << 16) | (0x0000FFFF & 2), // 垂类设置有误 + EVRClientErrorCodeCommonEnqueueError = (EVRClientErrorDomainCommon << 16) | (0x0000FFFF & 3) // 语音数据enqueue失败 +} TVoiceRecognitionClientErrorCode; + +#pragma mark -- INTERNAL DEFINES + + +#pragma mark - 语音识别模式 +typedef enum TBDVoiceRecognitionMode { + EVR_MODE_MULTI_SENTENCE = 0, + EVR_MODE_SINGLE_SENTENCE, + EVR_MODE_MUSIC, + EVR_MODE_SHAKE, +} TBDVoiceRecognitionMode; + +} + +#endif /* bds_ASRDefines_h */ + diff --git a/sdk/include/ASR/bds_WakeupDefines.hpp b/sdk/include/ASR/bds_WakeupDefines.hpp new file mode 100644 index 0000000..de11f0d --- /dev/null +++ b/sdk/include/ASR/bds_WakeupDefines.hpp @@ -0,0 +1,54 @@ +// +// bds_WakeupDefines.hpp +// BDASRCore +// +// Created by baidu on 16/5/20. +// Copyright © 2016年 baidu. All rights reserved. +// + +#ifndef bds_WakeupDefines_hpp +#define bds_WakeupDefines_hpp + +namespace bds { + +#pragma mark - 唤醒引擎状态 +typedef enum TWakeupEngineWorkStatus +{ + EWakeupEngineWorkStatusStarted, // 引擎开始工作 + EWakeupEngineWorkStatusStopped, // 引擎关闭完成 + EWakeupEngineWorkStatusLoaded, // 唤醒引擎加载完成 + EWakeupEngineWorkStatusUnLoaded, // 唤醒引擎卸载完成 + EWakeupEngineWorkStatusTriggered, // 命中唤醒词 + EWakeupEngineWorkStatusError, // 引擎发生错误 +} TWakeupEngineWorkStatus; + +#pragma mark - 唤醒引擎错误分类 +typedef enum TWakeupEngineErrorDomain +{ + EWakeupEngineErrorDomainRecord = 10, // 录音设备出错 + EWakeupEngineErrorDomainEngine = 38, // 录音设备出错 +} TWakeupEngineErrorDomain; + +#pragma mark - 唤醒引擎错误状态 +typedef enum TWakeupEngineErrorCode +{ + EWakeupEngineRecoderException = (EWakeupEngineErrorDomainRecord << 16) | (0x0000FFFF & 1), // 录音设备异常 + EWakeupEngineRecoderNoPermission = (EWakeupEngineErrorDomainRecord << 16) | (0x0000FFFF & 2), // 无录音权限 + EWakeupEngineRecoderUnAvailable = (EWakeupEngineErrorDomainRecord << 16) | (0x0000FFFF & 3), // 录音设备不可用 + EWakeupEngineRecoderInterruption = (EWakeupEngineErrorDomainRecord << 16) | (0x0000FFFF & 4), // 录音中断 + + EWakeupEngineExceptioin = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 1), // 唤醒引擎异常 + EWakeupEngineNoLicense = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 2), // 无授权文件 + EWakeupEngineLicenseInvalid = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 3), // 授权文件异常 + EWakeupEngineWakeupWordsInvalid = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 4), // 唤醒次异常 + EWakeupEngineDatFileInvalid = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 5), // 模型文件异常 + EWakeupEngineInitializeFailed = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 6), // 引擎初始化失败 + EWakeupEngineAllocMemFailed = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 7), // 内存分配失败 + EWakeupEngineResetFailed = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 8), // 引擎重置失败 + EWakeupEngineFreeFailed = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 9), // 引擎释放失败 + EWakeupEngineArchiNotSupportted = (EWakeupEngineErrorDomainEngine << 16) | (0x0000FFFF & 10), // 引擎不支持该架构 +} TWakeupEngineErrorCode; + +} + +#endif /* bds_WakeupDefines_hpp */ diff --git a/sdk/include/ASR/bds_asr_key_definitions.hpp b/sdk/include/ASR/bds_asr_key_definitions.hpp new file mode 100644 index 0000000..4b9660b --- /dev/null +++ b/sdk/include/ASR/bds_asr_key_definitions.hpp @@ -0,0 +1,581 @@ +#ifndef _BD_ASR_DEFINITIONS_H_ +#define _BD_ASR_DEFINITIONS_H_ + +#include + +namespace bds { +/* + * SDKType definition to load ASR SDK from + * BDSpeechSDK::get_instance(const std::string &SDKType, std::string &outErrorDescription); + */ + extern const std::string SDK_TYPE_ASR; + + /*================================ Message names for using ASR SDK ===============================*/ + extern const std::string ASR_CMD_CONFIG; + extern const std::string ASR_CMD_START; + extern const std::string ASR_CMD_PUSH_AUDIO; + extern const std::string ASR_CMD_STOP; + extern const std::string ASR_CMD_CANCEL; + extern const std::string ASR_CMD_KWS_LOAD; + extern const std::string ASR_CMD_KWS_UNLOAD; + extern const std::string BDS_COMMAND_SET_WRITABLE_LIBRARY_DATA_PATH; + + /*=============================== Message names for Events from ASR ==============================*/ + extern const std::string asr_callback_name; + + extern const std::string CALLBACK_ASR_STATUS; + extern const std::string CALLBACK_ASR_RESULT; + extern const std::string CALLBACK_ASR_LEVEL; + extern const std::string CALLBACK_ERROR_DESC; + extern const std::string CALLBACK_ERROR_CODE; + extern const std::string CALLBACK_ERROR_DOMAIN; + extern const std::string CALLBACK_ERROR_SERIAL_NUM; + + /*================================ ASR SDK parameter keys ===============================*/ + /* + * ASR_PARAM_KEY_CHUNK_KEY + * Value explanation: Chunk协议授权字段 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_CHUNK_KEY; + + /* + * ASR_PARAM_KEY_CUID + * Value explanation: CUID + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_CUID; + + /* + * ASR_PARAM_KEY_CHUNK_PARAM + * Value explanation: Chunk协议透传字段 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_CHUNK_PARAM; + + /* + * ASR_PARAM_KEY_CHUNK_ENABLE + * Value explanation: Chunk协议开关 + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_CHUNK_ENABLE; + + /* + * ASR_PARAM_KEY_ENABLE_LONG_SPEECH + * Value explanation: 长语音开关 + * Value type: bool + * Valid value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_LONG_SPEECH; + + extern const std::string BDS_PARAM_KEY_WRITABLE_LIBRARY_DATA_PATH; + +/* + * ASR_PARAM_KEY_SAVE_AUDIO_ENABLE + * Value explanation: ASR 保存音频文件开关 + * Value type: bool + * Default value: false + */ +extern const std::string ASR_PARAM_KEY_SAVE_AUDIO_ENABLE; + +/* + * ASR_PARAM_KEY_SAVE_AUDIO_PATH + * Value explanation: ASR 保存音频文件路径 + * Value type: string + * Default value: "./sdk_save_audio.d" + */ +extern const std::string ASR_PARAM_KEY_SAVE_AUDIO_PATH; + +/*================================ Params with Command ===============================*/ + /* + * ASR_PARAM_KEY_REALTIME_DATA + * Value explanation: 实时透传参数,随命令同步发送 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_REALTIME_DATA; + +/*================================ Debug ===============================*/ + /* + * COMMON_PARAM_KEY_DEBUG_LOG_LEVEL + * Value explanation: 指定调试日志级别 + * Value type: TBDVoiceRecognitionDebugLogLevel (int) + * Valid value: Be listed in enum TBDVoiceRecognitionDebugLogLevel + * Default value: EVRDebugLogLevelOff + */ + extern const std::string COMMON_PARAM_KEY_DEBUG_LOG_LEVEL; + +/*================================ AUDIO ===============================*/ + /* + * MIC_PARAM_KEY_AUDIO_FILE_PATH + * Value explanation: 设置音频文件路径(数据源) + * Value type: std::string + * Default value: - + */ + extern const std::string MIC_PARAM_KEY_AUDIO_FILE_PATH; + + /* + * MIC_PARAM_KEY_NEED_CACHE + * Value explanation: Need cached data in recorder. + * Value type: int + * Value requirement: Optional + * Default value: 0 + */ + extern const std::string MIC_PARAM_KEY_NEED_CACHE; + + /* + * MIC_PARAM_KEY_DISABLE_AUDIO_OPERATION + * Value explanation: Disable sdk audio operation (Set audio session disactive). + * Value type: int + * Value requirement: Optional + * Default value: 0 + */ + extern const std::string MIC_PARAM_KEY_DISABLE_AUDIO_OPERATION; + +/*================================ BASIC ===============================*/ + /* + * ASR_PARAM_KEY_SDK_VERSION + * Value explanation: SDK version + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_SDK_VERSION; + + /* + * ASR_PARAM_KEY_START_TONE + * Value explanation: 录音开始提示音 + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_START_TONE; + + /* + * ASR_PARAM_KEY_STRATEGY + * Value explanation: 语音识别策略 + * Value type: TBDVoiceRecognitionStrategy (int) + * Valid value: Be listed in enum TBDVoiceRecognitionStrategy + * Default value: EVR_STRATEGY_ONLINE + */ + extern const std::string ASR_PARAM_KEY_STRATEGY; + + /* + * ASR_PARAM_KEY_SAMPLE_RATE + * Value explanation: 设置录音采样率,自动模式根据当前网络情况自行调整 + * Value type: TVoiceRecognitionRecordSampleRateFlags (int) + * Valid value: Be listed in enum TVoiceRecognitionRecordSampleRateFlags + * Default value: EVoiceRecognitionRecordSampleRate16K + */ + extern const std::string ASR_PARAM_KEY_SAMPLE_RATE; + +/*================================ VAD-MFE ===============================*/ + /* + * ASR_PARAM_KEY_MAX_SPEECH_PAUSE + * Value explanation: 设置切分门限 + * Value type: float (帧数,每帧大小为10ms) + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_MAX_SPEECH_PAUSE; + + /* + * ASR_PARAM_KEY_MAX_WAIT_DURATION + * Value explanation: 设置最大等待语音时间 + * Value type: float (该值为帧数,每帧大小为10ms) + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_MAX_WAIT_DURATION; + + /* + * ASR_PARAM_KEY_MFE_DNN_DAT_FILE + * Value explanation: 设置MFE模型文件 + * Value type: string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_MFE_DNN_DAT_FILE; + + /* + * ASR_PARAM_KEY_MFE_CMVN_DAT_FILE + * Value explanation: 设置MFE CMVN文件路径 + * Value type: string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_MFE_CMVN_DAT_FILE; + +/*================================ SERVER FUNCTIONS ===============================*/ + /* + * ASR_PARAM_KEY_DISABLE_PUNCTUATION + * Value explanation: 是否关闭标点 + * Value type: int + * Valid Value: Disable (1) or Not Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_DISABLE_PUNCTUATION; + + /* + * ASR_PARAM_KEY_ENABLE_SERVER_VAD + * Value explanation: 服务器端VAD + * Value type: int + * Valid Value: Enable (1) or Disable (0) + * Default value: 1 + * 注意:这个参数不管用,控制不了服务端的VAD,老版本的SDK才可以 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_SERVER_VAD; + + /* + * ASR_PARAM_KEY_ENABLE_CONTACTS + * Value explanation: 开启通讯录识别功能,将优先返回通讯录识别结果,需事先上传通讯录 + * Value type: int + * Valid Value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_CONTACTS; + + /* + * ASR_PARAM_KEY_ENABLE_EARLY_RETURN + * Value explanation: 服务端开启提前返回,即允许服务端在未收到客户端发送的结束标志前提前结束识别过程 + * Value type: int + * Valid Values: Enable (1) or Disable (0) + * Default value: 1 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_EARLY_RETURN; + + +/*================================ SERVER ===============================*/ + /* + * ASR_PARAM_KEY_API_SECRET_KEY + * Value explanation: 设置SECRET_KEY + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_SECRET_KEY; + + /* + * ASR_PARAM_KEY_SERVER_URL + * Value explanation: 设置服务器地址 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_SERVER_URL; + + /* + * ASR_PARAM_KEY_BROWSER_UA + * Value explanation: 设置浏览器标识(Http request header),资源返回时会根据UA适配 + * Value type: std::string + * Default value: -(可通过[UIWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]获取) + */ + extern const std::string ASR_PARAM_KEY_BROWSER_USER_AGENT; + + /* + * ASR_PARAM_KEY_APP_ID + * Value explanation: 设置APP_ID + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_APP_ID; + + /* + * ASR_PARAM_KEY_LMID + * Value explanation: 设置LMID + * Value type: int + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_LMID; + + /* + * ASR_PARAM_KEY_VP_PARAMS + * Value explanation: 设置vp_params + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_VP_PARAMS; + + /* + * ASR_PARAM_KEY_API_KEY + * Value explanation: 设置API_KEY + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_API_KEY; + +/*================================ Recognition ===============================*/ + /* + * ASR_PARAM_KEY_PROPERTY_LIST + * Value explanation: 设置识别类型列表,输入法不可与其他类型复合 + * Value type: std::string(JSON array) + * Default value: {"property_list":[EVoiceRecognitionPropertySearch]} + + * NOTE: Give a JSON string containing "property_list" key. The value for this key + * should be a json array of integers.\ + * + *Example: To pass in EVoiceRecognitionPropertyMusic + * and EVoiceRecognitionPropertyVideo the JSON should be as follows: + * {"property_list":[10001,10002]} + */ + extern const std::string ASR_PARAM_KEY_PROPERTY_LIST; + + /* + * ASR_PARAM_KEY_PRODUCT_ID + * Value explanation: 设置产品ID + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_PRODUCT_ID; + + /* + * ASR_PARAM_KEY_CITY_ID + * Value explanation: 设置城市ID,仅对地图识别类型有效 + * Value type: int + * Default value: 1(全国) + */ + extern const std::string ASR_PARAM_KEY_CITY_ID; + + /* + * ASR_PARAM_KEY_PROTOCOL + * Value explanation: 设置协议类型 + * Value type: TBDVoiceRecognitionProtocol (int) + * Valid value: Be listed in enum TBDVoiceRecognitionProtocol + * Default value: EPROTOCOL_DEFAULT + */ + extern const std::string ASR_PARAM_KEY_PROTOCOL; + + /* + * ASR_PARAM_KEY_LANGUAGE + * Value explanation: 设置识别语言 + * Value type: TBDVoiceRecognitionLanguage (int) + * Valid value: Be listed in enum TBDVoiceRecognitionLanguage + * Default value: EVoiceRecognitionLanguageChinese + */ + extern const std::string ASR_PARAM_KEY_LANGUAGE; + + /* + * ASR_PARAM_KEY_ENABLE_NLU + * Value explanation: 开启语义解析,将返回包含语音的json串 + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_NLU; + + /* + * ASR_PARAM_KEY_ENABLE_LOCAL_VAD + * Value explanation: 是否需要对录音数据进行端点检测 + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 1 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_LOCAL_VAD; + + /* + * ASR_PARAM_KEY_ENABLE_MODEL_VAD + * Value explanation: 是否使用modelVAD + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 0 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_MODEL_VAD; + + /* + * ASR_PARAM_KEY_MODEL_VAD_DAT_FILE + * Value explanation: modelVAD所需资源文件 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_MODEL_VAD_DAT_FILE; + + /* + * ASR_PARAM_KEY_COMPRESSION_TYPE + * Value explanation: 录音数据压缩算法 + * Value type: TBDVoiceRecognitionAudioCompressionType (int) + * Valid value: Be listed in enum TBDVoiceRecognitionAudioCompressionType + * Default value: EVR_AUDIO_COMPRESSION_BV32 + */ + extern const std::string ASR_PARAM_KEY_COMPRESSION_TYPE; + + /* + * ASR_PARAM_KEY_ENABLE_DRC + * Value explanation: 是否进行车载环境下的噪声消除 + * Value type: int + * Valid value: Enable (1) or Disable (0) + * Default value: 1 + */ + extern const std::string ASR_PARAM_KEY_ENABLE_DRC; + +/*================================ 扩展参数 ===============================*/ + /* + * ASR_PARAM_KEY_PAM + * Value explanation: 扩展参数,多轮对话需要的信息 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_PAM; + + /* + * ASR_PARAM_KEY_STC + * Value explanation: 扩展参数,统计信息 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_STC; + + /* + * ASR_PARAM_KEY_LTP + * Value explanation: 扩展参数,轻应用参数(uid) + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_LTP; + + /* + * ASR_PARAM_KEY_TXT + * Value explanation: 扩展参数,上传文本,如果设置了该字段,将略过语音输入和识别阶段 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_TXT; + + /* + * ASR_PARAM_KEY_BUA + * Value explanation: 浏览器标识, use for wise + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_BUA; + +/*================================ Configuration for contract with server ===============================*/ + /* + * ASR_PARAM_KEY_NETWORK_STATUS + * Value explanation: 当前网络状态 + * Value type: TBDVoiceRecognitionNetworkType (int) + * Valid value: Be listed in enum TBDVoiceRecognitionNetworkType + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_NETWORK_STATUS; + + /* + * ASR_PARAM_KEY_APP + * Value explanation: App name + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_APP; + + /* + * ASR_PARAM_KEY_PLATFORM + * Value explanation: Platform + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_PLATFORM; + + /* + * ASR_PARAM_KEY_COK + * Value explanation: Cookie, use for wise + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_COK; + + /* + * ASR_PARAM_KEY_PU + * Value explanation: Pu, use for wise + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_PU; + + /* + * ASR_PARAM_KEY_FRM + * Value explanation: From, use for wise + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_FRM; + + /* + * ASR_PARAM_KEY_RSV + * Value explanation: Reserve, use for wise + * Value type: std::string (json) + * Default value: - + * Note: Give a json formatted string containing string parameters with keys. + * Example: To pass in 2 parameters (reserved_1 and reserved_2 with values "value 1" and "value 2") + * pass following json string. + * {"reserved_1":"value 1","reserved_2":"value 2"} + */ + extern const std::string ASR_PARAM_KEY_RSV; + +/*================================ Offline Engine Verify ===============================*/ + /* + * OFFLINE_PARAM_KEY_APP_CODE + * Value explanation: 离线授权所需APPCODE + * Value type: std::string + * Default value: - + */ + extern const std::string OFFLINE_PARAM_KEY_APP_CODE; + + /* + * OFFLINE_PARAM_KEY_LICENSE_FILE_PATH + * Value explanation: 离线授权文件路径 + * Value type: std::string + * Default value: - + */ + extern const std::string OFFLINE_PARAM_KEY_LICENSE_FILE_PATH; + +/*================================ Offline Engine KWS ===============================*/ + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_TYPE + * Value explanation: 离线识别引擎类型 + * Value type: TBDVoiceRecognitionOfflineEngineType (int) + * Valid value: Be listed in enum TBDVoiceRecognitionOfflineEngineType + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_TYPE; + + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_DAT_FILE_PATH + * Value explanation: 离线识别资源文件路径 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_DAT_FILE_PATH; + + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_GRAMMER_FILE_PATH + * Value explanation: 离线识别语法文件路径 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_GRAMMER_FILE_PATH; + + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_GRAMMER_SLOT + * Value explanation: 语法模式离线语法槽 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_GRAMMER_SLOT; + + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_WAKEUP_WORDS_FILE_PATH + * Value explanation: 唤醒词文件路径,使用了唤醒并使用离线识别的情况下需要设置,其他情况请忽略该参数 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_WAKEUP_WORDS_FILE_PATH; + + /* + * ASR_PARAM_KEY_OFFLINE_ENGINE_TRIGGERED_WAKEUP_WORD + * Value explanation: 当前触发唤醒词,唤醒后立即调用识别的情况下配置,其他情况请忽略该参数 + * Value type: std::string + * Default value: - + */ + extern const std::string ASR_PARAM_KEY_OFFLINE_ENGINE_TRIGGERED_WAKEUP_WORD; + extern const std::string PARAM_TYPE_MAX_ASR_INSTANCE_NUM; +} + +#endif diff --git a/sdk/include/ASR/bds_wakeup_key_definitions.hpp b/sdk/include/ASR/bds_wakeup_key_definitions.hpp new file mode 100644 index 0000000..adfc526 --- /dev/null +++ b/sdk/include/ASR/bds_wakeup_key_definitions.hpp @@ -0,0 +1,137 @@ +// +// bds_WakeupParameters.hpp +// BDASRCore +// +// Created by baidu on 16/5/19. +// Copyright © 2016年 baidu. All rights reserved. +// + +#ifndef bds_WakeupParameters_hpp +#define bds_WakeupParameters_hpp + +#include + +namespace bds { +/* + * SDKType definition to load Wakeup SDK from + * BDSpeechSDK::get_instance(const std::string &SDKType, std::string &outErrorDescription); + */ +extern const std::string SDK_TYPE_WAKEUP; + +/*================================ Message names for using Wakeup SDK ===============================*/ + +extern const std::string WAK_CMD_CONFIG; +extern const std::string WAK_CMD_START; +extern const std::string WAK_CMD_PUSH_AUDIO; +extern const std::string WAK_CMD_STOP; +extern const std::string WAK_CMD_LOAD_ENGINE; +extern const std::string WAK_CMD_UNLOAD_ENGINE; +extern const std::string BDS_COMMAND_SET_WRITABLE_LIBRARY_DATA_PATH; +extern const std::string BDS_COMMAND_GET_EVENT_MANAGER_VERSION; + +/*============================= Message names for events from Wakeup SDK ============================*/ +extern const std::string wakeup_callback_name; + +extern const std::string CALLBACK_WAK_STATUS; +extern const std::string CALLBACK_WAK_RESULT; +extern const std::string CALLBACK_ERROR_DESC; +extern const std::string CALLBACK_ERROR_DOMAIN; +extern const std::string CALLBACK_ERROR_CODE; +extern const std::string BDS_CALLBACK_EVENT_MANAGER_VERSION; + +/*================================ Wakeup command parameters ===============================*/ + +#pragma mark - Debug + +/* + * COMMON_PARAM_KEY_DEBUG_LOG_LEVEL + * Value explanation: 指定调试日志级别 + * Value type: TBDVoiceRecognitionDebugLogLevel (int) + * Default value: EVRDebugLogLevelOff + */ +extern const std::string COMMON_PARAM_KEY_DEBUG_LOG_LEVEL; + +#pragma mark - Offline Engine Verify + +/* + * OFFLINE_PARAM_KEY_APP_CODE + * Value explanation: 离线授权所需APPCODE + * Value type: std::string + * Default value: - + */ +extern const std::string OFFLINE_PARAM_KEY_APP_CODE; + +/* + * OFFLINE_PARAM_KEY_LICENSE_FILE_PATH + * Value explanation: 离线授权文件路径 + * Value type: std::string + * Default value: - + */ +extern const std::string OFFLINE_PARAM_KEY_LICENSE_FILE_PATH; + +/* + * OFFLINE_PARAM_KEY_APP_NAME + * Value explanation: 离线授权所需APPNAME (Linux) + * Value type: std::string + * Default value: - + */ +extern const std::string OFFLINE_PARAM_KEY_APP_NAME; + +/* + * OFFLINE_PARAM_KEY_CUID + * Value explanation: 离线授权所需CUID (Android) + * Value type: std::string + * Default value: - + */ +extern const std::string OFFLINE_PARAM_KEY_CUID; + +/* + * OFFLINE_PARAM_KEY_JNI_CONTEXT + * Value explanation: 离线授权所需JNI_CONTEXT (Android) + * Value type: std::string + * Default value: - + */ +extern const std::string OFFLINE_PARAM_KEY_JNI_CONTEXT; + + +#pragma mark - Offline Engine Wakeup + +/* + * WP_PARAM_KEY_ENABLE_DNN_WAKEUP + * Value explanation: 使用DNN唤醒,功耗低 + * Value type: bool + * Default value: false + */ +extern const std::string WP_PARAM_KEY_ENABLE_DNN_WAKEUP; + +/* + * WP_PARAM_KEY_WAKEUP_WORDS + * Value explanation: 唤醒词列表 + * Value type: std::vector + * Value type: std::string (json format list of words, UTF-8 encoded, list key "words") + * Default value: - + * Example: std::string("{\"words\":[\"小度你好\",\"百度你好\"]}") + */ +extern const std::string WP_PARAM_KEY_WAKEUP_WORDS; + +/* + * WP_PARAM_KEY_WAKEUP_WORDS_FILE_PATH + * Value explanation: 唤醒词文件路径 + * Value type: std::string + * Default value: - + */ +extern const std::string WP_PARAM_KEY_WAKEUP_WORDS_FILE_PATH; + +/* + * WP_PARAM_KEY_WAKEUP_DAT_FILE_PATH + * Value explanation: 唤醒引擎模型文件路径 + * Value type: std::string + * Default value: - + */ +extern const std::string WP_PARAM_KEY_WAKEUP_DAT_FILE_PATH; + +extern const std::string BDS_PARAM_KEY_WRITABLE_LIBRARY_DATA_PATH; + +} + +#endif /* bds_WakeupParameters_hpp */ diff --git a/sdk/include/BDSSDKMessage.hpp b/sdk/include/BDSSDKMessage.hpp new file mode 100644 index 0000000..c76e007 --- /dev/null +++ b/sdk/include/BDSSDKMessage.hpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * + * Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved + * + **************************************************************************/ + +/** + * @file BDSSDKMessage.hpp + * @author Vaino(lappivaeinoe@baidu.com) + * @date 2016/10/26 17:51:32 + * @brief Message container to pass commands to SDK and receive events/callbacks from the SDK + * + **/ + +#ifndef _BDS_SDK_MESSAGE_HPP_ +#define _BDS_SDK_MESSAGE_HPP_ +#include +#include +#include + +namespace bds { + + extern const std::string DATA_CHUNK; + + class BDSSDKMessageImpl; + class BDSSDKMessage{ + public: + BDSSDKMessage(const std::string &p_name); + BDSSDKMessage(); + BDSSDKMessage(const BDSSDKMessage &m); + virtual BDSSDKMessage& operator=(const BDSSDKMessage &m); + + std::string name; + /* + char* data; + unsigned int length; + */ + virtual ~BDSSDKMessage(); + + /* Set parameters to message */ + void set_parameter(const std::string &key, const std::string &value); + void set_parameter(const std::string &key, int value); + void set_parameter(const std::string &key, float value); + void set_parameter(const std::string &key, const char* value, int valueLen); + void set_parameter(const std::string &key, const std::vector &value); + + /* Get keys for parameters set to message */ + std::vector string_param_keys(); + std::vector int_param_keys(); + std::vector float_param_keys(); + std::vector char_param_keys(); + std::vector vector_param_keys(); + + /* Get parameters from message */ + bool get_parameter(const std::string &key, std::string &outValue); + bool get_parameter(const std::string &key, int &outValue); + bool get_parameter(const std::string &key, float &outValue); + bool get_parameter(const std::string &key, const char* &outValue, int &outValueLen); + bool get_parameter(const std::string &key, std::vector &outValue); + private: + BDSSDKMessageImpl* _impl; + friend class BDSSDKMessageImpl; + }; +} + +#endif diff --git a/sdk/include/BDSpeechSDK.hpp b/sdk/include/BDSpeechSDK.hpp new file mode 100644 index 0000000..55d6076 --- /dev/null +++ b/sdk/include/BDSpeechSDK.hpp @@ -0,0 +1,49 @@ +/*************************************************************************** + * + * Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved + * + **************************************************************************/ + +/** + * @file BDSpeechSDK.hpp + * @author Vaino(lappivaeinoe@baidu.com) + * @date 2016/10/26 17:52:07 + * @brief Main interface for speech SDK for Linux. + * + **/ + +#ifndef _BDS_SPEECH_SDK_HPP_ +#define _BDS_SPEECH_SDK_HPP_ + +#include +#include "BDSSDKMessage.hpp" + +namespace bds { + class BDSpeechSDK{ + public: + static BDSpeechSDK* get_instance(const std::string &SDKType, std::string &outErrorDescription); + virtual void set_event_listener(void(*listener)(BDSSDKMessage&,void*), void* userParam) = 0; + static void release_instance(BDSpeechSDK* instance); + + virtual bool post(BDSSDKMessage &message, std::string &outErrorDescription) = 0; + /* + * Cleanup for closing the program. + * This function will clean up some globals and makes sure that + * everything has been stopped before returning. + * Call is needed only when the whole program is about to shut down + * Not calling this function before returning from main() function of the program may lead to a crash due to async nature of release_instance() functions. + */ + static void do_cleanup(); + + static void open_log_file(const char *logFileName, int fileSize = 0); + static void close_log_file(); + static std::string get_sdk_version(); + + static int set_global_param(const std::string& param_type, void* value, std::string &outErrorDescription); + protected: + BDSpeechSDK(); + virtual ~BDSpeechSDK(); + }; +} + +#endif diff --git a/sdk/lib/libBDSpeechSDK.a b/sdk/lib/libBDSpeechSDK.a new file mode 100644 index 0000000..acb41e4 Binary files /dev/null and b/sdk/lib/libBDSpeechSDK.a differ diff --git a/sdk/resources/asr_resource/bds_easr_basic_model.dat b/sdk/resources/asr_resource/bds_easr_basic_model.dat new file mode 100644 index 0000000..826c81f Binary files /dev/null and b/sdk/resources/asr_resource/bds_easr_basic_model.dat differ diff --git a/sdk/resources/asr_resource/bds_easr_dnn_wakeup_model.dat b/sdk/resources/asr_resource/bds_easr_dnn_wakeup_model.dat new file mode 100644 index 0000000..f6dfb59 Binary files /dev/null and b/sdk/resources/asr_resource/bds_easr_dnn_wakeup_model.dat differ diff --git a/sdk/resources/asr_resource/bds_easr_gramm.dat b/sdk/resources/asr_resource/bds_easr_gramm.dat new file mode 100644 index 0000000..7eda192 --- /dev/null +++ b/sdk/resources/asr_resource/bds_easr_gramm.dat @@ -0,0 +1 @@ +%7B%0A%20%20%20%20%22version%22%3A%20%220.1%22%2C%0A%20%20%20%20%22slots%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E5%BC%A0%E4%B8%89%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E6%9D%8E%E5%9B%9B%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E7%8E%8B%E4%BA%94%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22appname%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E5%BE%AE%E4%BF%A1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E7%9F%AD%E4%BF%A1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%E8%AE%A1%E7%AE%97%E5%99%A8%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22msgbody%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22.%2B%22%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22rules%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22telephone.call%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%89%93%E7%94%B5%E8%AF%9D%E7%BB%99%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%89%93%E7%94%B5%E8%AF%9D%E7%BB%99(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%89%93%E7%BB%99%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%89%93%E7%BB%99(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E5%91%BC%E5%8F%AB%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E5%91%BC%E5%8F%AB(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E7%BB%99%3Cname%3E%E6%89%93%E7%94%B5%E8%AF%9D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E7%BB%99(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%E6%89%93%E7%94%B5%E8%AF%9D%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22contacts.view%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%9F%A5%E7%9C%8B%3Cname%3E%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%9F%A5%E7%9C%8B(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%9F%A5%E7%9C%8B%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%9F%A5%E7%9C%8B(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22contacts.create%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%96%B0%E5%BB%BA%E8%81%94%E7%B3%BB%E4%BA%BA%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%96%B0%E5%BB%BA%E8%81%94%E7%B3%BB%E4%BA%BA(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22contacts.remove%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E5%88%A0%E9%99%A4%3Cname%3E%E7%9A%84%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E5%88%A0%E9%99%A4(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%E7%9A%84%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22message.view%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%9F%A5%E7%9C%8B%E6%9C%AA%E8%AF%BB%E7%9F%AD%E4%BF%A1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%9F%A5%E7%9C%8B%E6%9C%AA%E8%AF%BB%E7%9F%AD%E4%BF%A1%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22message.send%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%E5%86%85%E5%AE%B9%E6%98%AF%3Cmsgbody%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99(%E5%BC%A0%E4%B8%89%7C%E6%9D%8E%E5%9B%9B%7C%E7%8E%8B%E4%BA%94)%E5%86%85%E5%AE%B9%E6%98%AF(.%2B)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22msgbody%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22app.open%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%89%93%E5%BC%80%3Cappname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%89%93%E5%BC%80(%E5%BE%AE%E4%BF%A1%7C%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%7C%E7%9F%AD%E4%BF%A1%7C%E8%AE%A1%E7%AE%97%E5%99%A8)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22appname%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E5%90%AF%E5%8A%A8%3Cappname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E5%90%AF%E5%8A%A8(%E5%BE%AE%E4%BF%A1%7C%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%7C%E7%9F%AD%E4%BF%A1%7C%E8%AE%A1%E7%AE%97%E5%99%A8)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22appname%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22app.search%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E6%90%9C%E7%B4%A2%3Cappname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E6%90%9C%E7%B4%A2(%E5%BE%AE%E4%BF%A1%7C%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%7C%E7%9F%AD%E4%BF%A1%7C%E8%AE%A1%E7%AE%97%E5%99%A8)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22appname%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22app.download%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22origin%22%3A%20%22%E4%B8%8B%E8%BD%BD%3Cappname%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pattern%22%3A%20%22%5E%E4%B8%8B%E8%BD%BD(%E5%BE%AE%E4%BF%A1%7C%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%7C%E7%9F%AD%E4%BF%A1%7C%E8%AE%A1%E7%AE%97%E5%99%A8)%24%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22groups%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22appname%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22grammar%22%3A%20%22%3Cname%3E%20%3D%20%E5%BC%A0%E4%B8%89%7C%20%5Cn%E6%9D%8E%E5%9B%9B%7C%20%5Cn%E7%8E%8B%E4%BA%94%3B%5Cn%3Cappname%3E%20%3D%20%E5%BE%AE%E4%BF%A1%7C%20%5Cn%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%7C%20%5Cn%E7%9F%AD%E4%BF%A1%7C%20%5Cn%E8%AE%A1%E7%AE%97%E5%99%A8%3B%5Cn%3Cmsgbody%3E%20%3D%20%E8%AF%8D%E6%9D%A1%E9%BB%98%E8%AE%A4%E5%80%BC%3B%5Cn%3Cauto_create_node%3E%20%3D%20%E6%9F%A5%E7%9C%8B%E6%9C%AA%E8%AF%BB%E7%9F%AD%E4%BF%A1%3B%5Cn%3C_wakeup%3E%20%3D%20%E5%94%A4%E9%86%92%E8%AF%8D%E5%8D%A0%E4%BD%8D%E7%AC%A6%3B%5Cn%5Cn%5Cn_SCENE_ID_%200%5Cn%5Cn(%20%3Cauto_create_node%3E%20)%5Cn%5Cn(%20%3C_wakeup%3E%3Cauto_create_node%3E%20)%5Cn%5Cn(%20%E6%89%93%E7%94%B5%E8%AF%9D%E7%BB%99%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%89%93%E7%94%B5%E8%AF%9D%E7%BB%99%3Cname%3E%20)%5Cn(%20%E6%89%93%E7%BB%99%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%89%93%E7%BB%99%3Cname%3E%20)%5Cn(%20%E5%91%BC%E5%8F%AB%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E5%91%BC%E5%8F%AB%3Cname%3E%20)%5Cn(%20%E7%BB%99%3Cname%3E%E6%89%93%E7%94%B5%E8%AF%9D%20)%5Cn(%20%3C_wakeup%3E%E7%BB%99%3Cname%3E%E6%89%93%E7%94%B5%E8%AF%9D%20)%5Cn(%20%E6%9F%A5%E7%9C%8B%3Cname%3E%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%20)%5Cn(%20%3C_wakeup%3E%E6%9F%A5%E7%9C%8B%3Cname%3E%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%20)%5Cn(%20%E6%9F%A5%E7%9C%8B%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%9F%A5%E7%9C%8B%3Cname%3E%20)%5Cn(%20%E6%96%B0%E5%BB%BA%E8%81%94%E7%B3%BB%E4%BA%BA%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%96%B0%E5%BB%BA%E8%81%94%E7%B3%BB%E4%BA%BA%3Cname%3E%20)%5Cn(%20%E5%88%A0%E9%99%A4%3Cname%3E%E7%9A%84%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%20)%5Cn(%20%3C_wakeup%3E%E5%88%A0%E9%99%A4%3Cname%3E%E7%9A%84%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%20)%5Cn(%20%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%20)%5Cn(%20%3C_wakeup%3E%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%20)%5Cn(%20%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%E5%86%85%E5%AE%B9%E6%98%AF%3Cmsgbody%3E%20)%5Cn(%20%3C_wakeup%3E%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%E5%86%85%E5%AE%B9%E6%98%AF%3Cmsgbody%3E%20)%5Cn(%20%E6%89%93%E5%BC%80%3Cappname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%89%93%E5%BC%80%3Cappname%3E%20)%5Cn(%20%E5%90%AF%E5%8A%A8%3Cappname%3E%20)%5Cn(%20%3C_wakeup%3E%E5%90%AF%E5%8A%A8%3Cappname%3E%20)%5Cn(%20%E6%90%9C%E7%B4%A2%3Cappname%3E%20)%5Cn(%20%3C_wakeup%3E%E6%90%9C%E7%B4%A2%3Cappname%3E%20)%5Cn(%20%E4%B8%8B%E8%BD%BD%3Cappname%3E%20)%5Cn(%20%3C_wakeup%3E%E4%B8%8B%E8%BD%BD%3Cappname%3E%20)%5Cn%22%2C%0A%20%20%20%20%22origin_slots%22%3A%20%22name%20%3D%20%E5%BC%A0%E4%B8%89%2C%20%E6%9D%8E%E5%9B%9B%2C%20%E7%8E%8B%E4%BA%94%5Cnappname%20%3D%20%E5%BE%AE%E4%BF%A1%2C%20%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%2C%20%E7%9F%AD%E4%BF%A1%2C%20%E8%AE%A1%E7%AE%97%E5%99%A8%5Cnmsgbody%20%3D%20*%22%2C%0A%20%20%20%20%22origin_rules%22%3A%20%22telephone.call%20%20%20%20%20%3D%20%E6%89%93%E7%94%B5%E8%AF%9D%E7%BB%99%3Cname%3E%2C%20%E6%89%93%E7%BB%99%3Cname%3E%2C%20%E5%91%BC%E5%8F%AB%3Cname%3E%2C%E7%BB%99%3Cname%3E%E6%89%93%E7%94%B5%E8%AF%9D%5Cncontacts.view%20%20%20%20%20%3D%20%E6%9F%A5%E7%9C%8B%3Cname%3E%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%2C%20%E6%9F%A5%E7%9C%8B%3Cname%3E%5Cncontacts.create%20%20%3D%20%E6%96%B0%E5%BB%BA%E8%81%94%E7%B3%BB%E4%BA%BA%3Cname%3E%5Cncontacts.remove%3D%20%E5%88%A0%E9%99%A4%3Cname%3E%E7%9A%84%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%5Cnmessage.view%20%20%20%20%20%3D%20%E6%9F%A5%E7%9C%8B%E6%9C%AA%E8%AF%BB%E7%9F%AD%E4%BF%A1%5Cnmessage.send%20%20%20%20%3D%20%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%2C%20%E5%8F%91%E7%9F%AD%E4%BF%A1%E7%BB%99%3Cname%3E%E5%86%85%E5%AE%B9%E6%98%AF%3Cmsgbody%3E%5Cnapp.open%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%E6%89%93%E5%BC%80%3Cappname%3E%2C%20%E5%90%AF%E5%8A%A8%3Cappname%3E%5Cnapp.search%20%20%20%20%20%20%20%20%20%3D%20%E6%90%9C%E7%B4%A2%3Cappname%3E%5Cnapp.download%20%20%20%20%3D%20%E4%B8%8B%E8%BD%BD%3Cappname%3E%22%0A%7D \ No newline at end of file diff --git a/sdk/resources/asr_resource/bds_easr_input_model.dat b/sdk/resources/asr_resource/bds_easr_input_model.dat new file mode 100644 index 0000000..bc34a05 Binary files /dev/null and b/sdk/resources/asr_resource/bds_easr_input_model.dat differ diff --git a/sdk/resources/asr_resource/bds_easr_mfe_cmvn.dat b/sdk/resources/asr_resource/bds_easr_mfe_cmvn.dat new file mode 100644 index 0000000..342b96f --- /dev/null +++ b/sdk/resources/asr_resource/bds_easr_mfe_cmvn.dat @@ -0,0 +1,54 @@ +vec 26 +1.485246e+01 +1.579741e+01 +1.663629e+01 +1.698390e+01 +1.708107e+01 +1.745045e+01 +1.752832e+01 +1.758490e+01 +1.746364e+01 +1.743286e+01 +1.744198e+01 +1.735617e+01 +1.735499e+01 +1.730315e+01 +1.726894e+01 +1.729055e+01 +1.728198e+01 +1.718504e+01 +1.712417e+01 +1.710584e+01 +1.715915e+01 +1.723995e+01 +1.730050e+01 +1.728485e+01 +1.728487e+01 +1.729697e+01 +vec 26 +2.764144e-01 +2.703758e-01 +2.584008e-01 +2.514187e-01 +2.496835e-01 +2.436650e-01 +2.391809e-01 +2.393867e-01 +2.416821e-01 +2.443231e-01 +2.473226e-01 +2.507182e-01 +2.540205e-01 +2.566869e-01 +2.590701e-01 +2.611003e-01 +2.630226e-01 +2.658522e-01 +2.699072e-01 +2.724641e-01 +2.723619e-01 +2.699174e-01 +2.670183e-01 +2.660550e-01 +2.658852e-01 +2.637595e-01 diff --git a/sdk/resources/asr_resource/bds_easr_mfe_dnn.dat b/sdk/resources/asr_resource/bds_easr_mfe_dnn.dat new file mode 100644 index 0000000..956531a Binary files /dev/null and b/sdk/resources/asr_resource/bds_easr_mfe_dnn.dat differ diff --git a/sdk/resources/asr_resource/bds_easr_wakeup_words.dat b/sdk/resources/asr_resource/bds_easr_wakeup_words.dat new file mode 100644 index 0000000..06ef067 --- /dev/null +++ b/sdk/resources/asr_resource/bds_easr_wakeup_words.dat @@ -0,0 +1 @@ +fAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZBfAEYGGtEYXgB_ZB \ No newline at end of file diff --git a/sdk/resources/asr_resource/bds_license.dat b/sdk/resources/asr_resource/bds_license.dat new file mode 100644 index 0000000..7eb96b2 --- /dev/null +++ b/sdk/resources/asr_resource/bds_license.dat @@ -0,0 +1 @@ +66af2eef7a9e6cf37646e6a0c9c0abcbb2149789b4a8c5532796731a694e7254b2f9c4815e0dd162633078eec0e09d31d88485feb9c9980c7d47bd98b0cb413e749e35f7751fa6dc05efe64003bbf8e1c145e4c9b1a625e268cb9bca9b123f7c13992c4a751a85bf356019726c17197279b637d6868a5fe93a59b54cedf4d60d844782908589335de28e5fe6eeba404ee827431a2ee60edaf013c2ca14ba062a2e8e3ee99ec7664a48e646e216a1e7267971fdad4d14f14dda1912723e87408cb244ac92321f6abf04df4a6bf6c2a05a0aecd4d307ed10c461a109315a7bf0b8dd2870d59c4e8ea1b5395fa15da2a4f3f7081cc6bb8543b8f4e240838eb41290 \ No newline at end of file diff --git a/sdk/resources/asr_resource/chuangmi/bds_easr_dnn_wakeup_model.dat b/sdk/resources/asr_resource/chuangmi/bds_easr_dnn_wakeup_model.dat new file mode 100644 index 0000000..2318dc4 Binary files /dev/null and b/sdk/resources/asr_resource/chuangmi/bds_easr_dnn_wakeup_model.dat differ diff --git a/sdk/resources/asr_resource/end2end_wakeup_far.dat b/sdk/resources/asr_resource/end2end_wakeup_far.dat new file mode 100644 index 0000000..22709e4 Binary files /dev/null and b/sdk/resources/asr_resource/end2end_wakeup_far.dat differ diff --git a/sdk/resources/asr_resource/global.cmvn b/sdk/resources/asr_resource/global.cmvn new file mode 100644 index 0000000..342b96f --- /dev/null +++ b/sdk/resources/asr_resource/global.cmvn @@ -0,0 +1,54 @@ +vec 26 +1.485246e+01 +1.579741e+01 +1.663629e+01 +1.698390e+01 +1.708107e+01 +1.745045e+01 +1.752832e+01 +1.758490e+01 +1.746364e+01 +1.743286e+01 +1.744198e+01 +1.735617e+01 +1.735499e+01 +1.730315e+01 +1.726894e+01 +1.729055e+01 +1.728198e+01 +1.718504e+01 +1.712417e+01 +1.710584e+01 +1.715915e+01 +1.723995e+01 +1.730050e+01 +1.728485e+01 +1.728487e+01 +1.729697e+01 +vec 26 +2.764144e-01 +2.703758e-01 +2.584008e-01 +2.514187e-01 +2.496835e-01 +2.436650e-01 +2.391809e-01 +2.393867e-01 +2.416821e-01 +2.443231e-01 +2.473226e-01 +2.507182e-01 +2.540205e-01 +2.566869e-01 +2.590701e-01 +2.611003e-01 +2.630226e-01 +2.658522e-01 +2.699072e-01 +2.724641e-01 +2.723619e-01 +2.699174e-01 +2.670183e-01 +2.660550e-01 +2.658852e-01 +2.637595e-01 diff --git a/sdk/resources/asr_resource/hmm_wakeup_far.dat b/sdk/resources/asr_resource/hmm_wakeup_far.dat new file mode 100644 index 0000000..2a98822 Binary files /dev/null and b/sdk/resources/asr_resource/hmm_wakeup_far.dat differ diff --git a/sdk/resources/asr_resource/vad.dnn b/sdk/resources/asr_resource/vad.dnn new file mode 100644 index 0000000..956531a Binary files /dev/null and b/sdk/resources/asr_resource/vad.dnn differ diff --git a/sdk/sample/asr/.BDSpeechSDK2.supp.swp b/sdk/sample/asr/.BDSpeechSDK2.supp.swp new file mode 100644 index 0000000..2c84309 Binary files /dev/null and b/sdk/sample/asr/.BDSpeechSDK2.supp.swp differ diff --git a/sdk/sample/asr/BDS_Ident.dat b/sdk/sample/asr/BDS_Ident.dat new file mode 100644 index 0000000..676ec20 --- /dev/null +++ b/sdk/sample/asr/BDS_Ident.dat @@ -0,0 +1 @@ +79bb1333ee01a55a5e1f51286d7c4bc4 \ No newline at end of file diff --git a/sdk/sample/asr/Makefile b/sdk/sample/asr/Makefile new file mode 100644 index 0000000..020441a --- /dev/null +++ b/sdk/sample/asr/Makefile @@ -0,0 +1,39 @@ +## asr Makefile ## +CC=g++ +#AR=ar + +#FILE_NAME=$(src) +FILE_NAME=src/main.cpp +INC_PATH= -I../../include\ + -I../../include/ASR\ + +SRC_PATH=./src +OBJ_PATH=. +TARGET=$(basename $(FILE_NAME)) +LIB_PATH=../../lib +EXTERN_PATH=../../extern/lib +TARGET_PATH=./ + +CPPFLAGS1=-Wall -O0 -fPIC -g -D__LINUX__ -Wno-unknown-pragmas -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 +CPPFLAGS= $(CPPFLAGS1) $(LIB_PATH)/libBDSpeechSDK.a $(EXTERN_PATH)/libcurl.a $(EXTERN_PATH)/libiconv.a $(EXTERN_PATH)/libz.a $(EXTERN_PATH)/libssl.a $(EXTERN_PATH)/libcrypto.a $(EXTERN_PATH)/libuuid.a -lrt -ldl -lpthread +SRC_FILE=$(shell cd $(SRC_PATH)&&echo *.cpp) +SRC:=$(foreach n,$(SRC_FILE),$(SRC_PATH)/$(n)) + +OBJ_FILE=$(SRC_FILE:.cpp=.o) +OBJ:=$(foreach n,$(OBJ_FILE),$(OBJ_PATH)/$(n)) + +DEP_FILE=$(SRC_FILE:.cpp=.d) +DEP:=$(foreach n,$(DEP_FILE),$(OBJ_PATH)/$(n)) + +$(TARGET):$(SRC) + $(CC) -o $(TARGET) ./$(FILE_NAME) $(INC_PATH) $(CPPFLAGS) + -mv $@ $(TARGET_PATH) +clean: + -rm -f $(OBJ) + -rm -f $(TARGET) + +cleanall: + -rm -f $(OBJ) + -rm -f $(TARGET) + + diff --git a/sdk/sample/asr/README.md b/sdk/sample/asr/README.md new file mode 100644 index 0000000..cfd15f5 --- /dev/null +++ b/sdk/sample/asr/README.md @@ -0,0 +1,9 @@ +# ASR demo + + + +简单地并发测试sdk,测试pcm目录下的音频, 原始音频目前仅支持16k采样率 + + + +测试成功后,可以修改 asr_set_config_params内的设置参数 \ No newline at end of file diff --git a/sdk/sample/asr/asr.log b/sdk/sample/asr/asr.log new file mode 100644 index 0000000..e69de29 diff --git a/sdk/sample/asr/build_and_run.sh b/sdk/sample/asr/build_and_run.sh new file mode 100644 index 0000000..457cd0c --- /dev/null +++ b/sdk/sample/asr/build_and_run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +make && echo && echo "build success, wait 3s to run" && sleep 3 && ./main diff --git a/sdk/sample/asr/main b/sdk/sample/asr/main new file mode 100644 index 0000000..962f525 Binary files /dev/null and b/sdk/sample/asr/main differ diff --git a/sdk/sample/asr/pcm/0.pcm b/sdk/sample/asr/pcm/0.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/0.pcm differ diff --git a/sdk/sample/asr/pcm/1.pcm b/sdk/sample/asr/pcm/1.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/1.pcm differ diff --git a/sdk/sample/asr/pcm/16k-0.pcm b/sdk/sample/asr/pcm/16k-0.pcm new file mode 100644 index 0000000..0501041 Binary files /dev/null and b/sdk/sample/asr/pcm/16k-0.pcm differ diff --git a/sdk/sample/asr/pcm/16k-1.pcm b/sdk/sample/asr/pcm/16k-1.pcm new file mode 100644 index 0000000..199d30e Binary files /dev/null and b/sdk/sample/asr/pcm/16k-1.pcm differ diff --git a/sdk/sample/asr/pcm/2.pcm b/sdk/sample/asr/pcm/2.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/2.pcm differ diff --git a/sdk/sample/asr/pcm/3.pcm b/sdk/sample/asr/pcm/3.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/3.pcm differ diff --git a/sdk/sample/asr/pcm/4.pcm b/sdk/sample/asr/pcm/4.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/4.pcm differ diff --git a/sdk/sample/asr/pcm/5.pcm b/sdk/sample/asr/pcm/5.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/5.pcm differ diff --git a/sdk/sample/asr/pcm/6.pcm b/sdk/sample/asr/pcm/6.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/6.pcm differ diff --git a/sdk/sample/asr/pcm/7.pcm b/sdk/sample/asr/pcm/7.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/7.pcm differ diff --git a/sdk/sample/asr/pcm/8.pcm b/sdk/sample/asr/pcm/8.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/8.pcm differ diff --git a/sdk/sample/asr/pcm/8k-0.pcm b/sdk/sample/asr/pcm/8k-0.pcm new file mode 100644 index 0000000..01f9ad6 Binary files /dev/null and b/sdk/sample/asr/pcm/8k-0.pcm differ diff --git a/sdk/sample/asr/pcm/9.pcm b/sdk/sample/asr/pcm/9.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asr/pcm/9.pcm differ diff --git a/sdk/sample/asr/src/main.cpp b/sdk/sample/asr/src/main.cpp new file mode 100644 index 0000000..ea65da3 --- /dev/null +++ b/sdk/sample/asr/src/main.cpp @@ -0,0 +1,430 @@ +#include +#include +#include +#include +#include + +//SDK interface +#include "BDSpeechSDK.hpp" +#include "bds_ASRDefines.hpp" +#include "bds_asr_key_definitions.hpp" + +using namespace std; + +/** + * 0. demo不用修改任何代码,可以直接运行。测试成功后请进行以下步骤 + * 1. 请修改asr_set_config_params 函数,配置您的参数。 + * 2. THREAD_NUM 修改同时进行的识别线程, + * 3. 测试完毕后,请确认修改asr_set_start_params里面app参数 + */ + +char audio_dir[256] = "./pcm"; // 与THREAD_NUM一起决定测试的文件名 +const int THREAD_NUM = 1; // 测试的线程数,最大不能超过10 +pthread_t thread_ids[THREAD_NUM]; // 线程信息 +pthread_mutex_t thread_mutexes[THREAD_NUM]; // 锁信息,用于同步SDK内部回调线程和用户的调用线程的asr_finish_tags +bool asr_finish_tags[THREAD_NUM] = {0}; //线程是否结束识别 +int thread_sequeces[THREAD_NUM] = {0}; +char file_names[THREAD_NUM][256]; // 每个线程识别的文件名 + +/** + * 格式化时间 + */ +std::string get_gmt_time() { + struct timeval tv; + gettimeofday(&tv, NULL); + + struct tm current_time; + localtime_r(&tv.tv_sec, ¤t_time); + + int year = (1900 + current_time.tm_year); + int month = (1 + current_time.tm_mon); + int day = current_time.tm_mday; + int hour = current_time.tm_hour; + int minute = current_time.tm_min; + int sec = current_time.tm_sec; + int msec = (int) (tv.tv_usec / 1000); + + char time_ch_buf[128] = {0}; + snprintf(time_ch_buf, sizeof(time_ch_buf) / sizeof(char), "%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, + hour, minute, sec, msec); + return std::string(time_ch_buf); +} + +/** + * 请根据文档说明设置参数 + */ +void asr_set_config_params(bds::BDSSDKMessage &cfg_params) { + //const bds::TBDVoiceRecognitionDebugLogLevel sdk_log_level = bds::EVRDebugLogLevelTrace; + const bds::TBDVoiceRecognitionDebugLogLevel sdk_log_level = bds::EVRDebugLogLevelOff; // 关闭详细日志 + + // app_id app_key app_secret 请测试成功后替换为您在网页上申请的appId appKey和appSecret + const std::string app_id = "10555002"; + const std::string chunk_key = "jhRA15uv8Lvd4r9qbtmOODMv"; + const std::string secret_key = "f0a12f8261e1121861a1cd3f4ed02f68"; + + const std::string product_id = "15362"; + // const std::string product_id = "1536";// 普通话搜索模型:1536,普通话搜索模型+语义理解 15361, 普通话输入法模型(有逗号) 1537 , 8001 8002 + + cfg_params.name = bds::ASR_CMD_CONFIG; + + cfg_params.set_parameter(bds::ASR_PARAM_KEY_APP_ID, app_id); + + // 自训练平台上线模型的调用参数,与product_id 8001 或 8002连用。 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_LMID, 1068); // 设为 product_id = 8002 + + cfg_params.set_parameter(bds::ASR_PARAM_KEY_CHUNK_KEY, chunk_key); + + cfg_params.set_parameter(bds::ASR_PARAM_KEY_PRODUCT_ID, product_id); + cfg_params.set_parameter(bds::COMMON_PARAM_KEY_DEBUG_LOG_LEVEL, sdk_log_level); + + // float vad_pause_frames = 30; //设置vad语句静音切分门限(帧), 30帧 = 300ms + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_MAX_SPEECH_PAUSE, vad_pause_frames); + + + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_SAVE_AUDIO_ENABLE, 1); //是否存识别的音频 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_SAVE_AUDIO_PATH, "sdk_save_audio.d"); //存音频的路径 + + cfg_params.set_parameter(bds::ASR_PARAM_KEY_ENABLE_LONG_SPEECH, 1); // 强制固定值 + cfg_params.set_parameter(bds::ASR_PARAM_KEY_CHUNK_ENABLE, 1); // 强制固定值 + const std::string mfe_dnn_file_path = "../../resources/asr_resource/bds_easr_mfe_dnn.dat"; // bds_easr_mfe_dnn.dat文件路径 + const std::string mfe_cmvn_file_path = "../../resources/asr_resource/bds_easr_mfe_cmvn.dat"; // bds_easr_mfe_cmvn.dat文件路径 + cfg_params.set_parameter(bds::ASR_PARAM_KEY_MFE_DNN_DAT_FILE, mfe_dnn_file_path); // 强制固定值 + cfg_params.set_parameter(bds::ASR_PARAM_KEY_MFE_CMVN_DAT_FILE, mfe_cmvn_file_path); // 强制固定值 + cfg_params.set_parameter(bds::ASR_PARAM_KEY_COMPRESSION_TYPE, bds::EVR_AUDIO_COMPRESSION_PCM); + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_COMPRESSION_TYPE, bds::EVR_AUDIO_COMPRESSION_BV32); // 有损压缩, 可能遇见音频压缩问题 + +} + +// 设置启动参数 +void asr_set_start_params(bds::BDSSDKMessage &start_params) { + const std::string app = "YourOwnName"; + + start_params.name = bds::ASR_CMD_START; + start_params.set_parameter(bds::ASR_PARAM_KEY_APP, app); + start_params.set_parameter(bds::ASR_PARAM_KEY_PLATFORM, "linux"); //固定值 + start_params.set_parameter(bds::ASR_PARAM_KEY_SDK_VERSION, "LINUX TEST"); //固定值 +} + +/** + * @param file_path IN 文件路径 + * @param push_cmd IN 固定值 bds::ASR_CMD_PUSH_AUDIO + * @param sdk BDSpeechSDK + * @param thread_seq demo里的线程编号 用于获取如asr_finish_tags[thread_seq]线程相关的信息 + */ +int asr_online_pushaudio(const char *file_path, const std::string &push_cmd, bds::BDSpeechSDK *sdk, int thread_seq) { + const int audio_buf_len = 320; // 建议320字节一个包 + const double per_send_seconds = 0.01; //320字节 ,16000采样率 sleep 10ms, sleep时间与包大小成正比 + char audio_buf[audio_buf_len]; + + FILE *err_output_file = stderr; + + FILE *fp = fopen(file_path, "rb"); + + if (!fp) { + fprintf(err_output_file, "open audio data failed\n"); + return 1; + } + + std::string err_msg; + bds::BDSSDKMessage push_params; + push_params.name = push_cmd; + + bool asr_finished = false; + + while (!feof(fp) && !asr_finished) { + size_t read_cnt = fread(audio_buf, 1, audio_buf_len, fp); + if (read_cnt > 0) { + push_params.set_parameter(bds::DATA_CHUNK, audio_buf, (int) read_cnt); + // printf("[%s]push_audio data, size %ld\n", get_gmt_time().c_str(), read_cnt); + if (!sdk->post(push_params, err_msg)) { + fprintf(err_output_file, "push audio data failed for %s\n", err_msg.c_str()); + } + //考虑到解码器的识别速度,此处的sleep必不可少,否则影响识别 + usleep(static_cast(per_send_seconds * 1000 * 1000)); + } + + pthread_mutex_lock(&thread_mutexes[thread_seq]); + asr_finished = asr_finish_tags[thread_seq]; + pthread_mutex_unlock(&thread_mutexes[thread_seq]); + } + + //告诉sdk,后续不会再post音频数据 , 注意这个调用之后需要紧接着调用asr_online_stop + push_params.set_parameter(bds::DATA_CHUNK, audio_buf, 0); + printf("[%s]push_audio finish\n", get_gmt_time().c_str()); + if (!sdk->post(push_params, err_msg)) { + fprintf(err_output_file, "push audio data failed for %s\n", err_msg.c_str()); + } + + fclose(fp); + return 0; +} + +/** + * SDK 识别过程中的回调,注意回调产生在SDK内部的线程中,并非调用线程。 + * @param message IN SDK的回调信息 + * @param user_arg IN 用户设置set_event_listener的第二个参数 + * + */ +void asr_output_callback(bds::BDSSDKMessage &message, void *user_arg) { + int thread_seq = *(int *) user_arg; + FILE *err_output_file = stderr; + + if (message.name != bds::asr_callback_name) { + fprintf(err_output_file, "shouldn't call\n"); + return; + } + + int status = 0; + + if (!message.get_parameter(bds::CALLBACK_ASR_STATUS, status)) { + fprintf(err_output_file, "get status failed\n"); + return; + } + + FILE *result_output_file = stdout; + const char *time = get_gmt_time().c_str(); + switch (status) { + case bds::EVoiceRecognitionClientWorkStatusStartWorkIng: { + fprintf(result_output_file, "[%s]识别工作开始\n", time); + fflush(result_output_file); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusStart: { // 一句话开始 + fprintf(result_output_file, "[%s]检测到开始说话\n", time); + fflush(result_output_file); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusEnd: { // 一句话结束 + fprintf(result_output_file, "[%s]检测到说话结束\n", time); + fflush(result_output_file); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusFlushData: { // 连续上屏,中间结果 + std::string json_result; + message.get_parameter(bds::CALLBACK_ASR_RESULT, json_result); + fprintf(result_output_file, "[%s]patial result: %s\n", time, json_result.c_str()); + fflush(result_output_file); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusFinish: { //一句话的最终结果 + std::string json_result; + message.get_parameter(bds::CALLBACK_ASR_RESULT, json_result); + fprintf(result_output_file, "[%s]final result: %s\n", time, json_result.c_str()); + fflush(result_output_file); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusChunkNlu: { //语义解析 + const char *buf; + int len = 0; + message.get_parameter(bds::DATA_CHUNK, buf, len); + fprintf(result_output_file, "[%s]nlu result:", time); + for (int i = 0; i < len; ++i) { + fprintf(result_output_file, "%c", buf[i]); + } + fprintf(result_output_file, "\n"); + fflush(result_output_file); + break; + } + + //case bds::EVoiceRecognitionClientWorkStatusChunkThirdData: { //第三方结果 + // const char* buf; + // int len = 0; + // message.get_parameter(bds::DATA_CHUNK, buf, len); + // //第三方结果未必是文本字符串,所以以%s打印未必有意义 + // fprintf(result_output_file, "third final result len[%d]\n", len); + // //for (int i = 0; i < len; ++i) fprintf(result_output_file, "%c", buf[i]); + // fprintf(result_output_file, "\n"); + // fflush(result_output_file); + // break; + //} + + case bds::EVoiceRecognitionClientWorkStatusLongSpeechEnd: { + // 长语音结束状态 该实例处于空闲状态 + pthread_mutex_lock(&thread_mutexes[thread_seq]); + asr_finish_tags[thread_seq] = true; + fprintf(result_output_file, "识别完成\n"); + fflush(result_output_file); + pthread_mutex_unlock(&thread_mutexes[thread_seq]); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusError: { + // 产生错误 该实例处于空闲状态 + int err_code = 0; + int err_domain = 0; + std::string err_desc; + message.get_parameter(bds::CALLBACK_ERROR_CODE, err_code); + message.get_parameter(bds::CALLBACK_ERROR_DOMAIN, err_domain); + message.get_parameter(bds::CALLBACK_ERROR_DESC, err_desc); + + std::string sn; + message.get_parameter(bds::CALLBACK_ERROR_SERIAL_NUM, sn); + + pthread_mutex_lock(&thread_mutexes[thread_seq]); + asr_finish_tags[thread_seq] = true; + fprintf(result_output_file, + "[%s]识别出错, err_code: %d, err_domain: %d,\ + err_desc: %s, sn: %s\n", + get_gmt_time().c_str(), err_code, err_domain, err_desc.c_str(), sn.c_str()); + fflush(result_output_file); + pthread_mutex_unlock(&thread_mutexes[thread_seq]); + break; + } + + case bds::EVoiceRecognitionClientWorkStatusCancel: { + // 用户取消 该实例处于空闲状态 + pthread_mutex_lock(&thread_mutexes[thread_seq]); + asr_finish_tags[thread_seq] = true; + fprintf(result_output_file, "[%s]用户取消\n", get_gmt_time().c_str()); + fflush(result_output_file); + pthread_mutex_unlock(&thread_mutexes[thread_seq]); + break; + } + case bds::EVoiceRecognitionClientWorkStatusNewRecordData: + case bds::EVoiceRecognitionClientWorkStatusMeterLevel: + break; + default: { + fprintf(result_output_file, "其它状态%d\n", status); + break; + } + + //bds::EVoiceRecognitionClientWorkStatusChunkEnd : { // CHUNK: 识别过程结束 + //bds::EVoiceRecognitionClientWorkStatusChunkNlu, // CHUNK: 识别结果中的语义结果 + } +} + +/** + * 释放SDK + */ +void asr_online_release(bds::BDSpeechSDK *sdk) { + bds::BDSpeechSDK::release_instance(sdk); +} + +/** + * 发送停止命令 + */ +int asr_online_stop(bds::BDSpeechSDK *sdk) { + FILE *err_output_file = stderr; + std::string err_msg; + bds::BDSSDKMessage stop_params; + stop_params.name = bds::ASR_CMD_STOP; + + if (!sdk->post(stop_params, err_msg)) { + fprintf(err_output_file, "stop sdk failed for %s\n", err_msg.c_str()); + return 1; + } + + return 0; +} + +static void *asr_thread(void *arg) { + int thread_seq = *(int *) arg; + char *file_name = file_names[thread_seq]; + FILE *err_output_file = stderr; + fprintf(stdout, "Will recognize file: %s\n", file_name); + + fprintf(stdout, "sdk version :%s\n", bds::BDSpeechSDK::get_sdk_version().c_str()); + /* 0 设置日志文件路径,如不设置默认输出到stderr */ + bds::BDSpeechSDK::open_log_file("asr.log", 25); // 与BDSpeechSDK::close_log_file();配对使用。 + // 25 表示 日志文件大小约25*512k, 超过后SDK新建一个日志文件,旧日志文件覆盖到"asr.log.bak" + + /* 1 获取sdk实例 */ + std::string err_msg; + bds::BDSpeechSDK *sdk = bds::BDSpeechSDK::get_instance(bds::SDK_TYPE_ASR, err_msg); + + if (!sdk) { + fprintf(err_output_file, "thread %d, get sdk failed for %s\n", thread_seq, err_msg.c_str()); + return NULL; + } + + /* 2 设置输出回调 */ + sdk->set_event_listener(&asr_output_callback, (void *) &thread_seq); + + /* 3 设置并发送sdk配置参数 */ + bds::BDSSDKMessage cfg_params; + asr_set_config_params(cfg_params); + + if (!sdk->post(cfg_params, err_msg)) { + fprintf(err_output_file, "thread %d, init sdk failed for %s\n", thread_seq, err_msg.c_str()); + bds::BDSpeechSDK::release_instance(sdk); + return NULL; + } + + /* 4 设置并发送sdk启动参数 */ + bds::BDSSDKMessage start_params; + asr_set_start_params(start_params); + + if (!sdk->post(start_params, err_msg)) { + fprintf(err_output_file, "thread %d, start sdk failed for %s\n", thread_seq, err_msg.c_str()); + bds::BDSpeechSDK::release_instance(sdk); + return NULL; + } + + /* 5 传输音频数据 */ + int ret = asr_online_pushaudio(file_name, bds::ASR_CMD_PUSH_AUDIO, sdk, thread_seq); + + if (ret) { + fprintf(err_output_file, "thread %d, push audio failed\n", thread_seq); + asr_online_stop(sdk); + bds::BDSpeechSDK::release_instance(sdk); + return NULL; + } + + /* 6 发送停止传输音频数据标记 */ + asr_online_stop(sdk); + + /* 7 等待识别结束 */ + bool asr_finished = false; + + while (!asr_finished) { + usleep(10000); + pthread_mutex_lock(&thread_mutexes[thread_seq]); + asr_finished = asr_finish_tags[thread_seq]; + pthread_mutex_unlock(&thread_mutexes[thread_seq]); + } + + /* 8 关闭日志 ,如果之前调用过 open_log_file */ + bds::BDSpeechSDK::close_log_file(); + + /* 8 释放sdk */ + asr_online_release(sdk); + + fprintf(stdout, "[%s]thread[%d] finish\n", get_gmt_time().c_str(), thread_seq); + fflush(stdout); + return NULL; +} + +int main(int argc, char **argv) { + FILE *err_output_file = stderr; + + for (int i = 0; i < THREAD_NUM; ++i) { + thread_sequeces[i] = i; + pthread_mutex_init(&thread_mutexes[i], NULL); + snprintf(file_names[i], 256, "%s/16k-%d.pcm", audio_dir, i); + // 新线程中开启每个文件的识别 + int32_t ret = pthread_create(&thread_ids[i], NULL, asr_thread, + static_cast(thread_sequeces + i)); // thread_sequeces[i]的指针 + if (ret != 0) { + fprintf(err_output_file, "create thread failed[%d]\n", ret); + return 1; + } + } + + for (int i = 0; i < THREAD_NUM; ++i) { + int ret = pthread_join(thread_ids[i], NULL); // 等待所有线程结束 + + if (ret != 0) { + fprintf(err_output_file, "join thread failed[%d]\n", ret); + return 1; + } + } + + //所有任务结束,清理线程池 + bds::BDSpeechSDK::do_cleanup(); + return 0; +} diff --git a/sdk/sample/asr/valgrind.sh b/sdk/sample/asr/valgrind.sh new file mode 100644 index 0000000..bd7cc39 --- /dev/null +++ b/sdk/sample/asr/valgrind.sh @@ -0,0 +1,2 @@ +#~/bin/sh +valgrind diff --git a/sdk/sample/asrDemo-srt/.idea/.name b/sdk/sample/asrDemo-srt/.idea/.name new file mode 100644 index 0000000..fb52a5f --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/.name @@ -0,0 +1 @@ +asrDemo_srt \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/asrDemo-srt.iml b/sdk/sample/asrDemo-srt/.idea/asrDemo-srt.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/asrDemo-srt.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/codeStyles/Project.xml b/sdk/sample/asrDemo-srt/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/codeStyles/codeStyleConfig.xml b/sdk/sample/asrDemo-srt/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/misc.xml b/sdk/sample/asrDemo-srt/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/modules.xml b/sdk/sample/asrDemo-srt/.idea/modules.xml new file mode 100644 index 0000000..ca26241 --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/sdk/sample/asrDemo-srt/.idea/workspace.xml b/sdk/sample/asrDemo-srt/.idea/workspace.xml new file mode 100644 index 0000000..53fbea4 --- /dev/null +++ b/sdk/sample/asrDemo-srt/.idea/workspace.xml @@ -0,0 +1,543 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eof + sleep + asr_resource + SECRT + _controller + waiting_to + std::endl + RETURN_RECOG_FINISH_OK + _Predicate + _is_finished + mutex + + + $PROJECT_DIR$ + $PROJECT_DIR$/src + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1520928319000 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sdk/sample/asrDemo2/CMakeLists.txt b/sdk/sample/asrDemo2/CMakeLists.txt new file mode 100644 index 0000000..cb348d7 --- /dev/null +++ b/sdk/sample/asrDemo2/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 2.8) +set(CMAKE_VERBOSE_MAKEFILE on) +set(CMAKE_BUILD_TYPE DEBUG) +project(asrDemo2) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O0 -fPIC -D__LINUX__ -Wno-unknown-pragmas -D_GLIBCXX_USE_CXX11_ABI=0 -Wno-unused-function") +file(GLOB SOURCE_FILES "src/*.c*" "src/*/*.cpp") + +include_directories(../../extern/include ../../include ../../include/ASR) +link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../lib ${CMAKE_CURRENT_SOURCE_DIR}/../../extern/lib) +add_executable(asrDemo2 ${SOURCE_FILES}) +target_link_libraries(asrDemo2 BDSpeechSDK curl iconv z ssl crypto uuid rt dl pthread) \ No newline at end of file diff --git a/sdk/sample/asrDemo2/README.md b/sdk/sample/asrDemo2/README.md new file mode 100644 index 0000000..6c710bc --- /dev/null +++ b/sdk/sample/asrDemo2/README.md @@ -0,0 +1,5 @@ +# ASR demo 2 + +对sdk做出封装示例 + +测试成功后,可以修改 yours_main.cpp 内static void set_config(bds::BDSSDKMessage &cfg_params)内的设置参数 diff --git a/sdk/sample/asrDemo2/pcm/0.pcm b/sdk/sample/asrDemo2/pcm/0.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/0.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/1.pcm b/sdk/sample/asrDemo2/pcm/1.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/1.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/10.pcm b/sdk/sample/asrDemo2/pcm/10.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/10.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/11.pcm b/sdk/sample/asrDemo2/pcm/11.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/11.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/16k-0.pcm b/sdk/sample/asrDemo2/pcm/16k-0.pcm new file mode 100644 index 0000000..0501041 Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/16k-0.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/16k-1.pcm b/sdk/sample/asrDemo2/pcm/16k-1.pcm new file mode 100644 index 0000000..199d30e Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/16k-1.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/2.pcm b/sdk/sample/asrDemo2/pcm/2.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/2.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/3.pcm b/sdk/sample/asrDemo2/pcm/3.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/3.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/4.pcm b/sdk/sample/asrDemo2/pcm/4.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/4.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/5.pcm b/sdk/sample/asrDemo2/pcm/5.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/5.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/6.pcm b/sdk/sample/asrDemo2/pcm/6.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/6.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/7.pcm b/sdk/sample/asrDemo2/pcm/7.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/7.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/8.pcm b/sdk/sample/asrDemo2/pcm/8.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/8.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/8k-0.pcm b/sdk/sample/asrDemo2/pcm/8k-0.pcm new file mode 100644 index 0000000..01f9ad6 Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/8k-0.pcm differ diff --git a/sdk/sample/asrDemo2/pcm/9.pcm b/sdk/sample/asrDemo2/pcm/9.pcm new file mode 100644 index 0000000..4003d5f Binary files /dev/null and b/sdk/sample/asrDemo2/pcm/9.pcm differ diff --git a/sdk/sample/asrDemo2/run/Makefile b/sdk/sample/asrDemo2/run/Makefile new file mode 100644 index 0000000..9f7eae8 --- /dev/null +++ b/sdk/sample/asrDemo2/run/Makefile @@ -0,0 +1,58 @@ +CC=g++ +#gcc -shared -Wl,--allow-multiple-definition -Wl,--whole-archive libBDSpeechSDK.a -Wl,--no-whole-archive -o libBDSpeechSDK.so +INC_PATH = -I../../../include\ + -I../../../include/ASR\ + -I./include +# 程序名称 +TARGET = asrDemo +# cpp hpp源代码路径 +SRC_PATH = ../src + +# .o 临时目录 +OBJ_PATH = obj + +# 编译 libasrdemo.a +CFLAGS_LIB = $(INC_PATH) -std=c++11 -fPIC -g -O0 -Wall -Wno-unknown-pragmas -D__LINUX__ -D_GLIBCXX_USE_CXX11_ABI=0 +LIB_SOURCE = $(wildcard $(SRC_PATH)/asrdemo/*.cpp) +LIB_OBJECT = $(patsubst %.cpp,$(OBJ_PATH)/asrdemo/%.o,$(notdir $(LIB_SOURCE))) + +# 编译 demo +CFLAGS_DEMO = $(INC_PATH) -std=c++11 -fPIC -g -O0 -Wall -Wno-unknown-pragmas -Wno-unused-function -D__LINUX__ -D_GLIBCXX_USE_CXX11_ABI=0 +DEMO_SOURCE = $(wildcard $(SRC_PATH)/yours_*.cpp) +DEMO_OBJECT = $(patsubst %.cpp,$(OBJ_PATH)/%.o,$(notdir $(DEMO_SOURCE))) + +# .a 库 +COMMON_LIBS = -lrt -ldl -lpthread +LIBS = -lBDSpeechSDK -lcurl -liconv -lz -lssl -lcrypto -luuid $(COMMON_LIBS) +OPT_LIBS_EXT=-L../../../lib -L../../../extern/lib +LIB_P=lib + + +$(TARGET):init lib/libasrdemo.a lib/libasrdemoall.so $(DEMO_OBJECT) +#使用lib/libasrdemo.a +#$(CC) $(DEMO_OBJECT) -o $(TARGET) $(OPT_LIBS_EXT) -L$(LIB_P) $(LIBS) -lasrdemo +#使用lib/libasrdemo.so + $(CC) $(DEMO_OBJECT) -o $(TARGET) -L$(LIB_P) -Wl,-rpath,$(LIB_P) -lasrdemoall -lrt -ldl -lpthread + @echo + @echo "Build Success" + @echo "Type : ./$(TARGET)" + +init: + mkdir -p $(OBJ_PATH)/asrdemo +# lib 目录 libasrdemoall.so + mkdir -p lib +# 头文件 目录 + mkdir -p include/asrdemo + cp $(SRC_PATH)/asrdemo/*.h* include/asrdemo + +lib/libasrdemo.a:init $(LIB_OBJECT) + ar cr lib/libasrdemo.a $(LIB_OBJECT) +lib/libasrdemoall.so:init $(LIB_OBJECT) + $(CC) -shared -fPIC -g $(LIB_OBJECT) $(OPT_LIBS_EXT) $(LIBS) -o lib/libasrdemoall.so + +$(OBJ_PATH)/asrdemo/%.o:$(SRC_PATH)/asrdemo/%.cpp + $(CC) $(CFLAGS_LIB) -c $< -o $@ +$(OBJ_PATH)/yours_%.o:$(SRC_PATH)/yours_%.cpp + $(CC) $(CFLAGS_DEMO) -c $< -o $@ +clean: + -rm -rf lib include $(OBJ_PATH) $(TARGET) BDS_Ident.dat asrdemo.log *.so diff --git a/sdk/sample/asrDemo2/run/build_and_run.sh b/sdk/sample/asrDemo2/run/build_and_run.sh new file mode 100644 index 0000000..14b3c1d --- /dev/null +++ b/sdk/sample/asrDemo2/run/build_and_run.sh @@ -0,0 +1,4 @@ +#!/bin/bash +rm asrDemo core core.* +make clean && make && echo "build success, wait 3s to run" && sleep 3 && ./asrDemo + diff --git a/sdk/sample/asrDemo2/script/backup_log.sh b/sdk/sample/asrDemo2/script/backup_log.sh new file mode 100644 index 0000000..f693db7 --- /dev/null +++ b/sdk/sample/asrDemo2/script/backup_log.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# asrdemo.log超过大小后,自动重命名为asrdemo.log.bak。本脚本自动重命名asrdemo.log.bak。 +# crontab运行的间隔根据实际测试结果调整。注意不要两次运行的间隔过长,导致asrdemo.log.bak获取不到。 +# 按照open_log_file第二个参数,即 +LOG_NAME="asrdemo.log.bak" +base_path=$(cd `dirname $0`; pwd) +run_path="${base_path}/../run" +now=$(date "+%Y%m%d_%H%M%S") +file_name="${run_path}/${LOG_NAME}" +if [ -f "${file_name}" ]; then + set -x + mv "${file_name}" "${file_name}.$now" +else + echo skip; +fi diff --git a/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.cpp b/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.cpp new file mode 100644 index 0000000..6722b74 --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.cpp @@ -0,0 +1,190 @@ +/* + * BdSpeech.cpp + * + * Created on: Dec 12, 2017 + * Author: fu + */ +#include "AsrdemoController.hpp" +#include + +namespace asrdemo { + +#define CHECK_ERROR_THEN_RETURN(MSG) do{\ + if (_has_error) {\ + error_msg = MSG;\ + error_msg += ": ";\ + error_msg += _error_msg;\ + release();\ + return false;\ + }\ +}while(0); + +AsrdemoController::AsrdemoController(const std::string &app_name, const std::string &resource_path, + ResultListener &listener) : + _app_name(app_name), _resource_path(resource_path), _listener(listener), _is_configed(false), + _has_error(false) { + _sdk = bds::BDSpeechSDK::get_instance(bds::SDK_TYPE_ASR, _error_msg); + if (_sdk == nullptr) { + _has_error = true; + _error_msg = "BDSpeechSDK is NULL;"; + } + _push_params.name = bds::ASR_CMD_PUSH_AUDIO; +} + +AsrdemoController::~AsrdemoController() { + release(); +} + +bool AsrdemoController::config(bds::BDSSDKMessage &config_params, std::string &error_msg) { + if (_is_configed) { + _has_error = true; + _error_msg = "bd_speech_control_impl has already initialized"; + } + if (_is_finished || _sdk == nullptr){ + _has_error = true; + _error_msg = "bd_speech_control_impl has already initialized"; + } + CHECK_ERROR_THEN_RETURN("sdk is finished"); + _sdk->set_event_listener(&AsrdemoController::output_callback, (void *)this); + CHECK_ERROR_THEN_RETURN("BDSpeechSDK init ERROR"); + set_config_params(config_params); + CHECK_ERROR_THEN_RETURN("set_config_params ERROR"); + set_start_params(); + CHECK_ERROR_THEN_RETURN("set_start_params ERROR"); + + _is_configed = true; + return true; +} + +bool AsrdemoController::post_audio_data(const char *audio_buf, int audio_buf_len, std::string &error_msg) { + if (_is_finished){ + error_msg = " sdk is finished and released"; + return false; + } + _push_params.set_parameter(bds::DATA_CHUNK, audio_buf, audio_buf_len); + _has_error = !_sdk->post(_push_params, _error_msg); + CHECK_ERROR_THEN_RETURN("post_audio_data ERROR"); + return true; +} + +bool AsrdemoController::post_data_finish_and_stop(std::string &error_msg) { + if (!post_audio_data(NULL, 0, error_msg)) { + return false; + } + return stop(error_msg); +} + +bool AsrdemoController::cancel(std::string &error_msg) { + if (_is_finished){ + error_msg = " sdk is finished and released"; + return false; + } + bds::BDSSDKMessage cancel_params; + cancel_params.name = bds::ASR_CMD_CANCEL; + _has_error = !_sdk->post(cancel_params, _error_msg); + CHECK_ERROR_THEN_RETURN("post_audio_data ERROR"); + return true; +} + +bool AsrdemoController::stop(std::string &error_msg) { + if (_is_finished){ + error_msg = " sdk is finished and released"; + return false; + } + bds::BDSSDKMessage stop_params; + stop_params.name = bds::ASR_CMD_STOP; + _has_error = !_sdk->post(stop_params, _error_msg); + CHECK_ERROR_THEN_RETURN("post_audio_data ERROR"); + return true; +} + +void AsrdemoController::output_callback(bds::BDSSDKMessage &message, void *user_arg) { + if (message.name != bds::asr_callback_name) { + std::cerr << " message.name not correct:" + message.name << std::endl; + return; + } + int status = 0; + + if (!message.get_parameter(bds::CALLBACK_ASR_STATUS, status)) { + std::cerr << " message get status failed:" << std::endl; + return; + } + + AsrdemoController *controller = (AsrdemoController *) user_arg; + switch (status){ + case bds::EVoiceRecognitionClientWorkStatusCancel: + case bds::EVoiceRecognitionClientWorkStatusError: + case bds::EVoiceRecognitionClientWorkStatusLongSpeechEnd: + controller->release(); + break; + } + controller->_listener.output_callback(message, status); + +} + +void AsrdemoController::set_config_params(bds::BDSSDKMessage &config_params) { + config_params.name = bds::ASR_CMD_CONFIG; + std::string temp_str; + + // 校验必填字段 + const std::string required_string_fields[] = {bds::ASR_PARAM_KEY_APP_ID, + bds::ASR_PARAM_KEY_CHUNK_KEY, + bds::ASR_PARAM_KEY_SECRET_KEY, + bds::ASR_PARAM_KEY_PRODUCT_ID}; + std::string key; + for (int i = 0; i != sizeof(required_string_fields) / sizeof(std::string); ++i) { + key = required_string_fields[i]; + if (!config_params.get_parameter(key, temp_str)) { + _has_error = true; + _error_msg = key + " is not set "; + return; + } + } + + // 强制设置字段 + config_params.set_parameter(bds::ASR_PARAM_KEY_CHUNK_ENABLE, 1); + config_params.set_parameter(bds::ASR_PARAM_KEY_ENABLE_LONG_SPEECH, 1); + + // 补填字段 + if (!config_params.get_parameter(bds::ASR_PARAM_KEY_MFE_DNN_DAT_FILE, temp_str)) { + config_params.set_parameter(bds::ASR_PARAM_KEY_MFE_DNN_DAT_FILE, + _resource_path + "bds_easr_mfe_dnn.dat"); + } + if (!config_params.get_parameter(bds::ASR_PARAM_KEY_MFE_CMVN_DAT_FILE, temp_str)) { + config_params.set_parameter(bds::ASR_PARAM_KEY_MFE_CMVN_DAT_FILE, + _resource_path + "bds_easr_mfe_cmvn.dat"); + } + if (!config_params.get_parameter(bds::ASR_PARAM_KEY_COMPRESSION_TYPE, temp_str)) { + config_params.set_parameter(bds::ASR_PARAM_KEY_COMPRESSION_TYPE, + bds::EVR_AUDIO_COMPRESSION_PCM); + } + int tmp_int; + if (!config_params.get_parameter(bds::COMMON_PARAM_KEY_DEBUG_LOG_LEVEL, tmp_int)) { + config_params.set_parameter(bds::COMMON_PARAM_KEY_DEBUG_LOG_LEVEL, + bds::EVRDebugLogLevelTrace); + } + _has_error = !_sdk->post(config_params, _error_msg); +} + +void AsrdemoController::set_start_params() { + bds::BDSSDKMessage start_params; + start_params.name = bds::ASR_CMD_START; + start_params.set_parameter(bds::ASR_PARAM_KEY_APP, _app_name); + start_params.set_parameter(bds::ASR_PARAM_KEY_PLATFORM, "linux"); + start_params.set_parameter(bds::ASR_PARAM_KEY_SDK_VERSION, "LINUX TEST"); + _has_error = !_sdk->post(start_params, _error_msg); +} + +void AsrdemoController::release() { + std::lock_guard lk(_finish_mutex); + _is_finished = true; + if (_sdk != nullptr) { + bds::BDSpeechSDK::release_instance(_sdk); + _sdk = nullptr; // 注意 如果多次调用release方法,注意 _sdk的读取需要线程同步 + } +} + + +#undef CHECK_ERROR_THEN_RETURN + +} /* namespace asrdemo */ diff --git a/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.hpp b/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.hpp new file mode 100644 index 0000000..53cabca --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/AsrdemoController.hpp @@ -0,0 +1,131 @@ +/* + * BdSpeech.hpp + * + * Created on: Dec 12, 2017 + * Author: fu + */ + +#ifndef ASRDEMO_ASRDEMO_CONTROLLER_HPP_ +#define ASRDEMO_ASRDEMO_CONTROLLER_HPP_ + +#include +#include +#include + + +#include "BDSpeechSDK.hpp" +#include "BDSSDKMessage.hpp" +#include "bds_ASRDefines.hpp" +#include "bds_asr_key_definitions.hpp" + +#include "common.h" +#include "ResultListener.hpp" + +namespace asrdemo { + +class AsrdemoController { +public: + /** + * @param app_name IN 您自己的定义的程序名称,可能用于之后服务端的排查 + * @param resource_path IN 含有bds_easr_mfe_cmvn.dat和bds_easr_mfe_dnn.dat这两个文件的目录 + * @param listener IN ResultListener实例,含有您处理结果的业务逻辑 + */ + AsrdemoController(const std::string &app_name, const std::string &resource_path, ResultListener &listener); + + virtual ~AsrdemoController(); + + /** + * 配置参数。具体参数见在线文档 ,参数列表:输入参数列表:ASR_CMD_CONFIG一节 + * + * @param config_params IN + * @param error_msg OUT 错误消息 + * @return 是否成功 不成功时错误消息在error_msg输出 + */ + bool config(bds::BDSSDKMessage &config_params, std::string &error_msg); + + /** + * 推送音频数据,建议320字节(audio_buf_len = 320) + * + * @param audio_buf IN 二进制音频数据 + * @param audio_buf_len + */ + bool post_audio_data(const char *audio_buf, int audio_buf_len, std::string &error_msg); + + /** + * 音频流结束时调用 + */ + bool post_data_finish_and_stop(std::string &error_msg); + + /** + * 取消调用 + */ + bool cancel(std::string &error_msg); + + + /** + * 打开日志文件 + * + * logFileName IN 日志文件路径 + * fileSize IN 实际日志文件大小为 512K*fileSize, 超过后自动重命名并覆盖之前的“logFileName".bak文件 + */ + static void open_log_file(const char *logFileName, int fileSize = 0) { + bds::BDSpeechSDK::open_log_file(logFileName, fileSize); + } + + /** + * 关闭日志文件,与open_log_file配对使用 + */ + static void close_log_file() { + bds::BDSpeechSDK::close_log_file(); + } + + /** + * 不再需要SDK识别时,释放所有资源 + */ + static void do_cleanup() { + bds::BDSpeechSDK::do_cleanup(); + } + +protected: + /** + * 用于post_data_finish_and_stop + */ + bool stop(std::string &error_msg); + + /** + * 用于output_callback ,释放识别实例 + */ + void release(); + + /** + * 收到SDK的3个空闲状态返回,认为识别过程结束 + */ + static void output_callback(bds::BDSSDKMessage &message, void *user_arg); + +private: + const std::string _app_name; + const std::string _resource_path; + // 用户自己的回调逻辑 + ResultListener &_listener; + + bool _is_configed; + std::atomic_bool _is_finished = ATOMIC_VAR_INIT(false); + std::mutex _finish_mutex; + + // 调用SDK出错 + bool _has_error; + std::string _error_msg; + + bds::BDSpeechSDK *_sdk; + bds::BDSSDKMessage _push_params; + + // 设置配置参数 + void set_config_params(bds::BDSSDKMessage &config_params); + + // 设置启动参数 + void set_start_params(); + DISALLOW_COPY_AND_ASSIGN(AsrdemoController); +}; +}/* namespace asrdemo */ + +#endif /* ASRDEMO_ASRDEMO_BDSPEECHCONTROL_HPP_ */ diff --git a/sdk/sample/asrDemo2/src/asrdemo/ResultListener.cpp b/sdk/sample/asrDemo2/src/asrdemo/ResultListener.cpp new file mode 100644 index 0000000..b53acae --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/ResultListener.cpp @@ -0,0 +1,173 @@ +/* + * asrdemo_ResultListener.cpp + * + * Created on: Dec 13, 2017 + * Author: fu + */ + +#include "ResultListener.hpp" +#include + +namespace asrdemo { + +ResultListener::ResultListener() { + //std::cout<< "result_listener constructor"< +#include "BDSSDKMessage.hpp" +#include "bds_ASRDefines.hpp" +#include "bds_asr_key_definitions.hpp" + +namespace asrdemo { + +class ResultListener { +public: + ResultListener(); + + virtual ~ResultListener(); + + virtual void output_callback(bds::BDSSDKMessage &message, int status); + +protected: + + /** + * 识别开始 EVoiceRecognitionClientWorkStatusStartWorkIng + */ + virtual void on_start_working(); + + /** + * 检查到一句话开始 EVoiceRecognitionClientWorkStatusStart + */ + virtual void on_start(); + + /** + * 检查到一句话结束 EVoiceRecognitionClientWorkStatusEnd + */ + virtual void on_end(); + + /** + * 临时识别结果 + * + * json IN 含有临时结果的json EVoiceRecognitionClientWorkStatusFlushData + */ + virtual void on_flush_data(const std::string &json); + + /** + * 一句话识别结束。 EVoiceRecognitionClientWorkStatusFinish + * 未开启长语音时,表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * json IN 含有最终结果的json + */ + virtual void on_finish(const std::string &json) = 0; + + /** + * 语义理解结果 + * + * json IN 含有最终结果的json EVoiceRecognitionClientWorkStatusChunkNlu + */ + virtual void on_nlu(const std::string &json); + + /** + * 长语音识别结束,(长语音 = 多个“一句话”的识别过程) EVoiceRecognitionClientWorkStatusLongSpeechEnd + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ + virtual void on_long_speech_end(); + + /** + * 识别出错 EVoiceRecognitionClientWorkStatusError + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * err_domain IN 错误领域 + * err_code IN 具体错误码 + * err_desc IN 错误描述 + * sn IN 一句话的识别过程中的logId + */ + virtual void on_error(int err_domain, int err_code, const std::string &err_desc, const std::string &sn) = 0; + + /** + * 识别被用户取消 EVoiceRecognitionClientWorkStatusCancel + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ + virtual void on_cancel(); + + virtual void on_last_status(int status); +}; + +} /* namespace asrdemo */ + +#endif /* ASRDEMO_RESULTLISTENER_HPP_ */ diff --git a/sdk/sample/asrDemo2/src/asrdemo/StatusListener.cpp b/sdk/sample/asrDemo2/src/asrdemo/StatusListener.cpp new file mode 100644 index 0000000..9d108bb --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/StatusListener.cpp @@ -0,0 +1,14 @@ +// +// Created by fu on 3/13/18. +// + +#include "StatusListener.hpp" +namespace asrdemo { +void StatusListener::on_last_status(int status){ + finished = true; +} +bool StatusListener::is_finished() const{ + + return finished; +} +} \ No newline at end of file diff --git a/sdk/sample/asrDemo2/src/asrdemo/StatusListener.hpp b/sdk/sample/asrDemo2/src/asrdemo/StatusListener.hpp new file mode 100644 index 0000000..9c0d1e5 --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/StatusListener.hpp @@ -0,0 +1,24 @@ +// +// Created by fu on 3/13/18. +// + +#ifndef ASRDEMO_ASRDEMO_STATUSLISTENER_HPP +#define ASRDEMO_ASRDEMO_STATUSLISTENER_HPP + +#include +#include "ResultListener.hpp" + +namespace asrdemo { +/** + * @brief 在ResultListener基础上增加判断识别是否结束 + */ +class StatusListener : public ResultListener { +public: + virtual void on_last_status(int status); + virtual bool is_finished() const; +private: + std::atomic_bool finished = ATOMIC_VAR_INIT(false); +}; + +} +#endif diff --git a/sdk/sample/asrDemo2/src/asrdemo/Util.cpp b/sdk/sample/asrDemo2/src/asrdemo/Util.cpp new file mode 100644 index 0000000..860c9c2 --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/Util.cpp @@ -0,0 +1,97 @@ +/* + * common_Util.cpp + * + * Created on: Dec 13, 2017 + * Author: fu + */ + +#include "Util.hpp" +#include +#include +#include +#include + +namespace asrdemo { +template +static void print_values(bds::BDSSDKMessage ¶ms, std::vector &keys, + Type tmp_value, std::stringstream &stream) { + std::string k; + for (std::vector::iterator it = keys.begin(); it != keys.end(); ++it) { + k = (*it); + params.get_parameter(k, tmp_value); + + stream << k << "=" << tmp_value << "; "; + } + stream << std::endl; +} + +std::string Util::params_to_string(bds::BDSSDKMessage ¶ms) { + std::stringstream stream; + if (!params.name.empty()) { + stream << "name: " << params.name << std::endl; + } + std::vector param_keys = params.string_param_keys(); + if (param_keys.size() > 0) { + stream << "string: "; + std::string tmp_value; + print_values(params, param_keys, tmp_value, stream); + } + param_keys = params.int_param_keys(); + if (param_keys.size() > 0) { + stream << "int: "; + int tmp_value = 0; + print_values(params, param_keys, tmp_value, stream); + } + + param_keys = params.float_param_keys(); + if (param_keys.size() > 0) { + stream << "float: "; + float tmp_value = 0; + print_values(params, param_keys, tmp_value, stream); + } + + return stream.str(); +} + +uint64_t Util::current_timestamp() { + std::chrono::milliseconds ms = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch() + ); + return ms.count(); +} + +std::string Util::get_gmt_time() { + struct timeval tv; + gettimeofday(&tv, NULL); + + struct tm current_time; + localtime_r(&tv.tv_sec, ¤t_time); + + int year = (1900 + current_time.tm_year); + int month = (1 + current_time.tm_mon); + int day = current_time.tm_mday; + int hour = current_time.tm_hour; + int minute = current_time.tm_min; + int sec = current_time.tm_sec; + int msec = (int) (tv.tv_usec / 1000); + + char time_ch_buf[128] = {0}; + snprintf(time_ch_buf, sizeof(time_ch_buf) / sizeof(char), + "%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, sec, msec); + return std::string(time_ch_buf); +} + +uint64_t Util::cal_speech_duration_ms(int bytes, int sample_rate) { + const int sample_point_bytes = 2; + int duration_ms = bytes / (sample_point_bytes * sample_rate / 1000); + return duration_ms; +} + +uint64_t Util::cal_speech_8k_duration_ms(int bytes) { + return Util::cal_speech_duration_ms(bytes, 8000); +} + +uint64_t Util::cal_speech_16k_duration_ms(int bytes) { + return Util::cal_speech_duration_ms(bytes, 16000); +} +} /* namespace asrdemo */ diff --git a/sdk/sample/asrDemo2/src/asrdemo/Util.hpp b/sdk/sample/asrDemo2/src/asrdemo/Util.hpp new file mode 100644 index 0000000..7223f6f --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/Util.hpp @@ -0,0 +1,55 @@ +/* + * common_Util.hpp + * + * Created on: Dec 13, 2017 + * Author: fu + */ + +#ifndef ASRDEMO_UTIL_HPP_ +#define ASRDEMO_UTIL_HPP_ + +#include +#include +#include + +#include "BDSSDKMessage.hpp" + +namespace asrdemo { + +class Util { +public: + /** + * 转为 BDSSDKMessage 为可见的字符串 + */ + static std::string params_to_string(bds::BDSSDKMessage ¶ms); + + /** + * 当前的timestamp 毫秒 + */ + static uint64_t current_timestamp(); + + /** + * 8k采样率的原始音频对应的时长 毫秒 + */ + static uint64_t cal_speech_8k_duration_ms(int bytes); + + /** + * 16k采样率的原始音频对应的时长 毫秒 + */ + static uint64_t cal_speech_16k_duration_ms(int bytes); + + /** + * 当前时间字符串 + */ + static std::string get_gmt_time(); + +private: + /** + * 原始音频对应的时长 + */ + static uint64_t cal_speech_duration_ms(int bytes, int sample_rate); +}; + +} /* namespace asrdemo */ + +#endif /* ASRDEMO_UTIL_HPP_ */ diff --git a/sdk/sample/asrDemo2/src/asrdemo/common.h b/sdk/sample/asrDemo2/src/asrdemo/common.h new file mode 100644 index 0000000..3cc24b7 --- /dev/null +++ b/sdk/sample/asrDemo2/src/asrdemo/common.h @@ -0,0 +1,16 @@ +/* + * asrdemo_common.hpp + * + * Created on: Dec 12, 2017 + * Author: fu + */ + +#ifndef ASRDEMO_COMMON_ +#define ASRDEMO_COMMON_ + + +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + TypeName& operator=(const TypeName&) + +#endif /* ASRDEMO_COMMON_ */ diff --git a/sdk/sample/asrDemo2/src/yours_PrintResultListener.cpp b/sdk/sample/asrDemo2/src/yours_PrintResultListener.cpp new file mode 100644 index 0000000..1a1ae9a --- /dev/null +++ b/sdk/sample/asrDemo2/src/yours_PrintResultListener.cpp @@ -0,0 +1,108 @@ +/* + * asrdemo_PrintResultListener.cpp + * + * Created on: Dec 15, 2017 + * Author: fu + */ + +#include "yours_PrintResultListener.hpp" +#include +#include +#include "asrdemo/Util.hpp" + +namespace yours { + +PrintResultListener::PrintResultListener(const std::string &prefix) : + asrdemo::StatusListener(), _prefix(prefix) { + write_log("PrintResultListener constructor"); + +} + +PrintResultListener::~PrintResultListener() { + write_log("PrintResultListener DeConstructor"); +} + +/** + * 识别开始 + */ +void PrintResultListener::on_start_working() { + write_log("开始识别"); +} + +/** + * 检查到一句话开始 + */ +void PrintResultListener::on_start() { + write_log("检测到一句话开始"); +} + +/** + * 检查到一句话结束 + */ +void PrintResultListener::on_end() { + write_log("检测到一句话结束"); +} + +/** + * 临时识别结果 + * + * json IN 含有临时结果的json + */ +void PrintResultListener::on_flush_data(const std::string &json) { + write_log("临时结果:" + json); +} + +/** + * 一句话识别结束。 + * 未开启长语音时,表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * json IN 含有最终结果的json + */ +void PrintResultListener::on_finish(const std::string &json) { + write_log("识别结果:" + json); +} + +void PrintResultListener::on_nlu(const std::string &json) { + write_log("语义理解结果:" + json); +} + +/** + * 长语音识别结束,(长语音 = 多个“一句话”的识别过程) + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ +void PrintResultListener::on_long_speech_end() { + write_log("长语音结束"); +} + +/** + * 识别出错 + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * err_domain IN 错误领域 + * err_code IN 具体错误码 + * err_desc IN 错误描述 + * sn IN 一句话的识别过程中的logId + */ +void PrintResultListener::on_error(int err_domain, int err_code, const std::string &err_desc, const std::string &sn) { + std::ostringstream oss; + oss << "识别错误, err_domain=" << err_domain << " ;err_code=" << err_code << " ;err_desc=" << err_desc << " ;sn= " + << sn; + write_log(oss.str()); +} + +/** + * 识别被用户取消 + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ +void PrintResultListener::on_cancel() { + write_log("用户取消"); +} + +void PrintResultListener::write_log(const std::string str) const { + std::cout.flush(); + std::ostringstream os; + os << "[" << asrdemo::Util::get_gmt_time() << "][" << _prefix << "] :" << str << std::endl; + std::cout << os.str(); +} + +} /* namespace asrdemo */ diff --git a/sdk/sample/asrDemo2/src/yours_PrintResultListener.hpp b/sdk/sample/asrDemo2/src/yours_PrintResultListener.hpp new file mode 100644 index 0000000..92e4464 --- /dev/null +++ b/sdk/sample/asrDemo2/src/yours_PrintResultListener.hpp @@ -0,0 +1,96 @@ +/* + * asrdemo_PrintResultListener.h + * + * Created on: Dec 15, 2017 + * Author: fu + */ + +#ifndef YOURS_PRINTRESULTLISTENER_HPP_ +#define YOURS_PRINTRESULTLISTENER_HPP_ + +#include +#include "asrdemo/common.h" +#include "asrdemo/StatusListener.hpp" + +namespace yours { + +class PrintResultListener : public asrdemo::StatusListener { +public: + /** + * prefix IN 打印日志使用 + */ + PrintResultListener(const std::string &prefix); + + virtual ~PrintResultListener(); + +protected: + /** + * 识别开始 + */ + virtual void on_start_working(); + + /** + * 检查到一句话开始 + */ + virtual void on_start(); + + /** + * 检查到一句话结束 + */ + virtual void on_end(); + + /** + * 临时识别结果 + * + * json IN 含有临时结果的json + */ + virtual void on_flush_data(const std::string &json); + + /** + * 一句话识别结束。 + * 未开启长语音时,表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * json IN 含有最终结果的json + */ + virtual void on_finish(const std::string &json); + + /** + * 语义理解结果 + * + * json IN 含有最终结果的json + */ + virtual void on_nlu(const std::string &json); + + /** + * 长语音识别结束,(长语音 = 多个“一句话”的识别过程) + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ + virtual void on_long_speech_end(); + + /** + * 识别出错 + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + * + * err_domain IN 错误领域 + * err_code IN 具体错误码 + * err_desc IN 错误描述 + * sn IN 一句话的识别过程中的logId + */ + virtual void on_error(int err_domain, int err_code, const std::string &err_desc, const std::string &sn); + + /** + * 识别被用户取消 + * 表示整个识别过程结束,BDSpeechSDK实例处于空闲状态 + */ + virtual void on_cancel(); + +private: + const std::string _prefix; + + void write_log(const std::string str) const; + DISALLOW_COPY_AND_ASSIGN(PrintResultListener); +}; + +} /* namespace asrdemo */ + +#endif /* YOURS_PRINTRESULTLISTENER_HPP_ */ diff --git a/sdk/sample/asrDemo2/src/yours_main.cpp b/sdk/sample/asrDemo2/src/yours_main.cpp new file mode 100644 index 0000000..066e1b0 --- /dev/null +++ b/sdk/sample/asrDemo2/src/yours_main.cpp @@ -0,0 +1,293 @@ +//============================================================================ +// Name : asrdemo2.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +#include +#include +#include +#include "yours_PrintResultListener.hpp" +#include "asrdemo/AsrdemoController.hpp" +#include "asrdemo/Util.hpp" + +using namespace std; + +/** + * 0. demo不用修改任何代码,可以直接运行。测试成功后请进行以下步骤 + * 1. 请修改set_config 函数,配置您的参数。 + * 2. 在main函数中选择recog_one_file进行单文件识别,或者recog_multi_files同时识别多个文件。 + * + * 8k 采样率不再支持 + */ + + +static void set_config(bds::BDSSDKMessage &cfg_params) { + // app_id app_key app_secret 请测试成功后替换为您在网页上申请的appId appKey和appSecret + const std::string app_id = "10455099"; + const std::string app_key = "rKCHBLmYiFPuCQTS0HttLbUD"; + const std::string app_secret = "037dc446820ec143d1628c20146b9d34"; + + const std::string product_id = "1536"; // 普通话搜索模型(没有逗号)):1536,普通话搜索模型+语义理解 15361, 普通话输入法模型(有逗号) 1537 + + cfg_params.set_parameter(bds::ASR_PARAM_KEY_APP_ID, app_id); + cfg_params.set_parameter(bds::ASR_PARAM_KEY_CHUNK_KEY, app_key); + cfg_params.set_parameter(bds::ASR_PARAM_KEY_SECRET_KEY, app_secret); + cfg_params.set_parameter(bds::ASR_PARAM_KEY_PRODUCT_ID, product_id); + cfg_params.set_parameter(bds::COMMON_PARAM_KEY_DEBUG_LOG_LEVEL, bds::EVRDebugLogLevelOff); //关闭debug日志 ,上线时请注释此行 + float vad_pause_frame = 30; //设置vad语句静音切分门限, 30帧= 300ms。 即原始语音静音 300ms后,SDK认为一句话结束 + cfg_params.set_parameter(bds::ASR_PARAM_KEY_MAX_SPEECH_PAUSE, vad_pause_frame); + + //以下是不常用的参数 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_COMPRESSION_TYPE, bds::EVR_AUDIO_COMPRESSION_BV32); // 有损压缩 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_SAVE_AUDIO_ENABLE, 1); //是否存识别的音频 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_SAVE_AUDIO_PATH, "sdk_save_audio.d"); //存音频的路径 + // cfg_params.set_parameter(bds::ASR_PARAM_KEY_DISABLE_PUNCTUATION, 0); // 输出时没有逗号 搜索模型有效 + +} + +/**识别单个文件*/ +static void recog_one_file(); + +/**识别多个文件*/ +static void recog_multi_files(); + +// 调用set_config函数,写入sdk的配置参数 +static void _config(asrdemo::AsrdemoController &controller); + +// 按320字节大小,将数据post到sdk中。 +static bool _post_data(istream &io, asrdemo::AsrdemoController &controller, int &sleep_ms, string &error_msg); + +// 判断流是否结束或异常(io.eof()),结束的话,调用post_data_finish_and_stop,告知sdk音频结束 +static int _push_data_eof(istream &io, asrdemo::AsrdemoController &controller, string &error_msg); + +// flags 是否都是true +static bool _is_all_true(bool flags[], int size); + +int main(int argc, char **argv) { + cout << "BEGIN!" << endl; + //此处25 表示日志文件为 25*512k大小,asrdemo.log超过大小后,自动重命名为asrdemo.log.bak。asrdemo.log继续生成 + asrdemo::AsrdemoController::open_log_file("asrdemo.log", 25); + recog_one_file(); // 识别一个文件 + // recog_multi_files(); // 一个线程里识别多个文件 + asrdemo::AsrdemoController::close_log_file(); // 程序退出时或者不需要再写日志 固定用法, 引擎空闲时调用 + asrdemo::AsrdemoController::do_cleanup(); // 程序退出时或者不需要asr服务时 固定用法, 引擎空闲时调用 + return 0; +} + +/** + * 识别一个文件 + */ +static void recog_one_file() { + string filename = "../pcm/16k-0.pcm"; + ifstream io(filename.c_str()); + + if (!io || !io.is_open()) { + cerr << "read file error:" << filename << endl; + exit(1); + } + + yours::PrintResultListener listener(filename); + asrdemo::AsrdemoController controller("testYourApp", "../../../resources/asr_resource/", listener); + _config(controller); + + bool will_continue = true; + int sleep_ms = 0; + while (will_continue) { + string error_msg; + int status = _push_data_eof(io, controller, error_msg); // 判断是否是文件结尾,是的话,结束这个音频文件的SDK调用 + if (status < 0) { // 出错 + cerr << "stop audio data error:" << error_msg << " : " << status << endl; + will_continue = false; + } else if (status == 1) { // 文件读完了 + will_continue = false; + io.close(); + } else { // 正常 + if (sleep_ms > 0) { //上次_post_data欠的sleep + //cout << "sleep :"<is_open()) { + cerr << "read file error:" << filename << endl; + exit(1); + } + next_sleep_timestamps[i] = 0; + files_eof[i] = false; + sdk_finished[i] = false; + } + + for (int i = 0; i < num_files; i++) { + _config(*controllers[i]); + } + + bool is_success = true; + int status = 0; + int sleep_ms = 0; + std::string error_msg; + while (!_is_all_true(files_eof, num_files)) { + for (int i = 0; i < num_files; i++) { + if (files_eof[i]) { + // cout << "files finished:" << i << endl; + continue;// 文件已经结束 + } + status = _push_data_eof(*ifs[i], *controllers[i], error_msg); + if (status < 0) { // 出错 + cerr << "stop audio data error:" << error_msg << " : " << status << endl; + exit(3); + } else if (status == 1) { //文件结束 + // cout << "file ended :" << i << endl; + files_eof[i] = true; + ifs[i]->close(); + } else { + if (next_sleep_timestamps[i] > 0) { + sleep_ms = next_sleep_timestamps[i] - asrdemo::Util::current_timestamp(); // 需要sleep的耗时 + if (sleep_ms > 0) { + // cout << "sleep :" << sleep_ms << endl; + usleep(sleep_ms * 1000); + } + } + + is_success = _post_data(*ifs[i], *controllers[i], sleep_ms, error_msg); // 是否post成功,一般都是成功的。 + if (!is_success) { + cerr << "stop audio data error:" << error_msg << " : " << status << endl; + exit(3); + } + next_sleep_timestamps[i] = asrdemo::Util::current_timestamp() + sleep_ms; + } + } + } + cout << "ASR MULTIFILES BEGIN TO SLEEP" << endl; + while (!_is_all_true(sdk_finished, num_files)) { // 直到所有controllers都结束 + for (int i = 0; i < num_files; i++) { + if (sdk_finished[i]) { + continue; + } + if (listeners[i]->is_finished()) { + sdk_finished[i] = true; + } else { + //cout << "SLEEP 1s to wait" << endl; + sleep(1); + break; + } + } + } + for (int i = 0; i < num_files; i++) { + delete ifs[i]; + delete listeners[i]; + delete controllers[i]; + + } + + cout << "ASR MULTIFILES FINISHED" << endl; + +} + +static void _config(asrdemo::AsrdemoController &controller) { + bds::BDSSDKMessage cfg_params; + std::string error_msg; + set_config(cfg_params); + // cout << "Config is :\n " << asrdemo::Util::params_to_string(cfg_params); + if (controller.config(cfg_params, error_msg)) { + cout << "FOR Feedback : Config is filled :\n" << asrdemo::Util::params_to_string(cfg_params) << endl; + } else { + cerr << error_msg << " , END!" << endl; + exit(2); + } +} + +/** + * 判断流是否结束或异常(io.eof()),结束的话,调用post_data_finish_and_stop,告知sdk音频结束 + */ +static int _push_data_eof(istream &io, asrdemo::AsrdemoController &controller, string &error_msg) { + if (io.eof()) { + if (!controller.post_data_finish_and_stop(error_msg)) { + return -1; // 出错 + } + return 1; // 文件结束,并且告知了sdk音频结束 + } + return 0; // 文件未结束 +} + +/** + * 按320字节大小从io读取,将数据post到sdk中。 + */ +static bool _post_data(istream &io, asrdemo::AsrdemoController &controller, int &sleep_ms, string &error_msg) { + const int audio_buf_len = 320; + char audio_buf[audio_buf_len]; + int readed_len = 0; + + io.read(audio_buf, audio_buf_len); + readed_len = io.gcount(); + if (readed_len < 0) { + error_msg = " readed_len is <0"; + return false; + } + if (readed_len == 0) { // 读到0字节 + sleep_ms = 0; + return true; + } + if (!controller.post_audio_data(audio_buf, readed_len, error_msg)) { + return false; + } + + sleep_ms = asrdemo::Util::cal_speech_16k_duration_ms(readed_len); // 根据audio_buf_len 计算音频长度 16k采样率 + + return true; +} + +static bool _is_all_true(bool flags[], int size) { + for (int i = 0; i < size; i++) { + if (!flags[i]) { + return false; + } + } + return true; +} diff --git a/sdk/请先读README.md.txt b/sdk/请先读README.md.txt new file mode 100644 index 0000000..839b1fc --- /dev/null +++ b/sdk/请先读README.md.txt @@ -0,0 +1 @@ +请先读README.md