在我们的项目里,我负责的功能是实现在场景中通过麦克风输入语音命令进行交互,这首先就要求工程链接一个语音识别库。
常用的比较好的语音识别库有讯飞平台、百度云、腾讯云等等。
这里我们选择百度云,因为我发现百度云除了使用SDK以外,它可以选择一种不用下载配置SDK的方式,即REST API:
链接:http://ai.baidu.com/docs#/ASR-API/top
private string token; //access_token
private string cuid = "; //用户标识
private string format = "wav"; //语音格式
private int rate = 8000; //采样率
private int channel = 1; //声道数
private string speech; //语音数据,进行base64编码
private int len; //原始语音长度
private string lan = "zh"; //语种
private string grant_Type = "client_credentials";
private string client_ID = "; //百度appkey
private string client_Secret = "; //百度Secret Key
private string baiduAPI = "http://vop.baidu.com/server_api";
private string getTokenAPIPath = "https://openapi.baidu.com/oauth/2.0/token";
private Byte[] clipByte;
///
/// 转换出来的TEXT
///
public static string audioToString;
private AudioSource aud;
private int audioLength;//录音的长度
继续代码:
///
/// 获取百度用户令牌
///
/// 获取的url
///
private IEnumerator GetToken(string url)
{
WWWForm getTForm = new WWWForm();
getTForm.AddField("grant_type", grant_Type);
getTForm.AddField("client_id", client_ID);
getTForm.AddField("client_secret", client_Secret);
WWW getTW = new WWW(url, getTForm);
yield return getTW;
if (getTW.isDone)
{
if (getTW.error == null)
{
token = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
StartCoroutine(GetAudioString(baiduAPI));
}
else
Debug.LogError(getTW.error);
}
}
///
/// 把语音转换为文字
///
///
///
private IEnumerator GetAudioString(string url)
{
JsonWriter jw = new JsonWriter();
jw.WriteObjectStart();
jw.WritePropertyName("format");
jw.Write(format);
jw.WritePropertyName("rate");
jw.Write(rate);
jw.WritePropertyName("channel");
jw.Write(channel);
jw.WritePropertyName("token");
jw.Write(token);
jw.WritePropertyName("cuid");
jw.Write(cuid);
jw.WritePropertyName("len");
jw.Write(len);
jw.WritePropertyName("speech");
jw.Write(speech);
jw.WriteObjectEnd();
WWWForm w = new WWWForm();
WWW getASW = new WWW(url, Encoding.Default.GetBytes(jw.ToString()));
yield return getASW;
if (getASW.isDone)
{
if (getASW.error == null)
{
JsonData getASWJson = JsonMapper.ToObject(getASW.text);
if (getASWJson["err_msg"].ToString() == "success.")
{
audioToString = getASWJson["result"][0].ToString();
if (audioToString.Substring(audioToString.Length - 1) == ",")
audioToString = audioToString.Substring(0, audioToString.Length - 1);
Debug.Log(audioToString);
}
}
else
{
Debug.LogError(getASW.error);
}
}
}
到目前为止,链接百度云语音就完成了,之后我们再继续实现unity麦克风录入音源以及具体的识别过程。