common_config.cpp
Go to the documentation of this file.
1 #include "common_config.h"
2 #include <iostream>
4 #include "asr/xunfei/msp_cmn.h"
6 // MSP_login讯飞登录模块
7 void CommonConfig::loginToXunfei(const std::string base_path)
8 {
9  // 讯飞 sdk 中注册的 appid 需要与下载的文件对应,否则报错
10  std::string login_parameters = "appid = 5ade9569, work_dir = " + base_path + "/defaultconfig";
11  int error_code = MSPLogin(NULL, NULL, login_parameters.c_str());
12  if (MSP_SUCCESS != error_code)
13  {
14  std::cout << "MSPLogin failed :" << handleErrorcode(error_code) << "errorcode:" << error_code << std::endl;
16  }
17 }
18 
19 // 读取 config,存入 UserData 中
20 static void readConfig(const string file_path, UserData* info)
21 {
22  std::ifstream infile(file_path);
23  if (infile.is_open())
24  {
25  infile >> info->build_fini;
26  infile >> info->update_fini;
27  infile >> info->errcode;
28  infile >> info->grammar_id;
29  infile.close();
30  }
31  else
32  {
33  std::cout << "Fail to open expected Json file" << file_path << std::endl;
35  }
36 }
37 
38 // 设置离线语法参数
39 std::string CommonConfig::configGramParas(const std::string base_path, const std::string grammar_file)
40 {
41  UserData asr_data;
42  //讯飞语音sdk参数配置
43  const std::string ASR_RES_PATH = "fo|res/asr/common.jet";
44  const std::string GRAMMAR_FILE = grammar_file; // 到/config的绝对路径
45  const std::string GRAMMAR_BUILD_PATH = GRAMMAR_FILE + "/grammar";
46 
47  // 读入语法构建数据,存入 Userdata 中
48  std::string filepath = GRAMMAR_BUILD_PATH + "/info.txt";
49  readConfig(filepath, &asr_data);
50  // 离线语法识别参数设置
51  return std::string("engine_type = local, "
52  "asr_denoise=1,vad_bos=10000,vad_eos=10000,asr_res_path =" +
53  ASR_RES_PATH + ", sample_rate = " + to_string(SAMPLE_RATE_16K) + ", grm_build_path = " +
54  GRAMMAR_BUILD_PATH + ", local_grammar = " + asr_data.grammar_id +
55  ", result_type = json, result_encoding = UTF-8 ");
56 };
57 
58 // 科大讯飞API返回值的处理:
59 // 错误码参考msp_errors.h。将几个用到的API中常见的错误码转换成了文字描述。
60 std::string CommonConfig::handleErrorcode(const int errorcode)
61 {
62  while ((errorcode > MSP_SUCCESS) && (errorcode <= SPEECH_ERROR_LFASR_BASE))
63  {
64  switch (errorcode)
65  {
67  return "Invalid parameters!";
68 
70  return "Invalid parameter value!";
71 
73  return "Invalid username!";
74 
76  return "Invalid password!";
77 
79  return "Invalid app ID!";
80 
81  case MSP_ERROR_NOT_INIT:
82  return "Not initialized!";
83 
85  return "Fail to create session instance!";
86 
88  return "Invalid session ID!";
89 
90  case MSP_ERROR_NO_DATA:
91  return "NO data!";
92 
94  return "Null handle!";
95 
97  return "Buffer overflow!";
98 
99  default:
100  return "Please refer to msp_errors.h or official development "
101  "documentation for error codes.";
102  }
103  }
104 };
std::string handleErrorcode(const int errorcode)
static const int SAMPLE_RATE_16K
Definition: BuildGrammar.h:20
int MSPAPI MSPLogin(const char *usr, const char *pwd, const char *params)
int update_fini
Definition: BuildGrammar.h:32
void loginToXunfei(const std::string base_path)
Mobile Speech Platform Common Interface Header File.
char grammar_id[MAX_GRAMMARID_LEN]
Definition: BuildGrammar.h:34
const std::string base_path
int build_fini
Definition: BuildGrammar.h:31
std::string configGramParas(const std::string base_path, const std::string grammar_file)
static void readConfig(const string file_path, UserData *info)


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53