欧拉密SDK 下载
欧拉密SDK 下载 地址 : 欧拉密SDK
欧拉密SDK 下载 并解压 会得到这四个文件夹 根据需求使用相应的 .dll 库
欧拉密基础教学
欧拉密官方网站
注册账号之后点击应用管理模块
点击创建新应用
根据提示创建你的应用
配置对话模块 默认全部勾选 注意保存设置
可以查看 欧拉密 Key 和 Secret 值
欧拉密 NLI管理系统
自定义模块
打开 欧拉密 NLI管理系统
点击我的语法
新建自定义语法
注意编写格式 回答模块包括 语音回答和自定义回答两种模式
编写格式
编写完成后注意保存
点击发布 发布之后才可以使用
如果不满意 可以在再次编辑
编辑完成注意保存和发布
返回应用管理 并点击配置模块
勾选自定义回答模块 保存设置 自定义回答模块就完成了
Unity 模块
文件导入
新建一个项目工程 并在 Assets 路径下新建一个 Plugins的文件夹
并把 AipSdk.dll Newtonsoft.Json.dll 拖入
项目创建
基础创建
音源创建
脚本搭载
ASR 代码模块
特别说明:如果想要使用欧拉密的相关模块,需要去欧拉密官方网站进行项目配置,默认创建的项目是全部勾选的
如果想要自定义配置的话需要自己手动配置并发布,才可以正常使用。
特别注意:百度的文字转语音API是免费的,但是语音识别转文字是需要付费使用的哟。
ASR 语音合成
using Baidu.Aip.Speech;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using UnityEngine;
using UnityEngine.Networking;
public class VoiceSynthesis_ZH : MonoBehaviour
{
[Header("音源")]
public AudioSource _Audio;
[Header("AI 发声器")]
public Pronouncer _Pronouncer = Pronouncer.Duyaya;
public static VoiceSynthesis_ZH _World;
private string _Url;
private Asr _AipClient;
private void Start()
{
_World = this;
}
public IEnumerator GetAudioClip(string AudioText)
{
_Url = "http://tsn.baidu.com/text2audio?tex=" + AudioText +
"&tok=25.3141e5ae3aa109abb6fc9a8179131181.315360000.1886566986.282335-17539441" +
"&cuid=a7a0e3326da873c6fb0609e6385a82b934c9cb11" +
"&ctp=1" +
"&lan=zh" +
"&spd=5" +
"&pit=5" +
"&vol=10" +
"&per=" + (((int)_Pronouncer).ToString()) +
"&aue=6";
using (UnityWebRequest _AudioWeb = UnityWebRequestMultimedia.GetAudioClip(_Url, AudioType.WAV))
{
yield return _AudioWeb.SendWebRequest();
if (_AudioWeb.isNetworkError)
{
yield break;
}
AudioClip _Cli = DownloadHandlerAudioClip.GetContent(_AudioWeb);
_Audio.clip = _Cli;
_Audio.Play();
}
}
private IEnumerator Recognition(AudioClip _Clip)
{
float[] _Sample = new float[_Clip.samples];
_Clip.GetData(_Sample, 0);
short[] _IntData = new short[_Sample.Length];
byte[] _ByteData = new byte[_IntData.Length * 2];
for (int i = 0; i < _Sample.Length; i++)
{
_IntData[i] = (short)(_Sample[i] * short.MaxValue);
}
Buffer.BlockCopy(_IntData, 0, _ByteData, 0, _ByteData.Length);
var _Result = _AipClient.Recognize(_ByteData, "pcm", 16000);
var _Speaking = _Result.GetValue("result");
if (_Speaking == null)
{
StopAllCoroutines();
yield return null;
}
string _UsefulText = _Speaking.First.ToString();
print(_UsefulText);
yield return 0;
}
private static class AccessToken
{
public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
private static String clientId = "百度云应用的AK";
private static String clientSecret = "百度云应用的SK";
public static String GetAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List<KeyValuePair<string, string>> paraList = new List<KeyValuePair<string, string>>();
paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
return result;
}
}
public enum Pronouncer
{
Female,
Male,
Teshunan,
Duxiaoyao,
Duyaya
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
人工智能语音播放
using Ai.Olami.Example;
using Baidu.Aip.Speech;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using static Json_ZH;
public class ASR_ZH : MonoBehaviour
{
[Header("文字显示")]
public Text _ASRText;
[Header("ASR Toggle")]
public Toggle _Toggle;
[Header("ASR Json")]
private Root _JsonDataWeb;
[Header("ASR Json 故事")]
private Root02 _JsonDataWeb02;
[Header("ASR Json 百度百科")]
private Root03 _JsonDataWeb03;
[Header("ASR Json 选择")]
private Root04 _JsonDataWeb04;
[Header("ASR Json 菜肴")]
private Root05 _JsonDataWeb05;
[Header("ASR Json 搜索")]
private Root06 _JsonDataWeb06;
[Header("ASR Json 股票")]
private Root07 _JsonDataWeb07;
[Header("ASR Json 电视节目")]
private Root08 _JsonDataWeb08;
[Header("ASR Json 天气")]
private Root09 _JsonDataWeb09;
[Header("ASR Json 诗集选择")]
private Root10 _JsonDataWeb10;
[Header("ASR Json 菜肴选择")]
private Root11 _JsonDataWeb11;
[Header("ASR Json 酒店选择")]
private Root12 _JsonDataWeb12;
[Header("ASR Json 诗集")]
private Root13 _JsonDataWeb13;
private string _Str;
private string _Key= "需要自己去欧拉密 官方网站申请";
private string _Secret = "需要自己去欧拉密 官方网站申请";
private string _ApiBaseUrl = "http://cn.olami.ai/cloudservice/api";
private AudioClip _RecordClip;
private string _AccessToken;
private string _ASR_ID = "需要自己去百度 官方网站申请";
private string _API_Key = "需要自己去百度 官方网站申请";
private string _Secret_Key = "需要自己去百度 官方网站申请";
private string _AuthHost = "https://aip.baidubce.com/oauth/2.0/token";
private Asr _AipClient;
void Start()
{
_AipClient = new Asr(_ASR_ID, _API_Key, _Secret_Key);
_AipClient.Timeout = 6000;
_AccessToken = GetAccessToken();
_Toggle.onValueChanged.AddListener((bool ison) => { ToggleIs(_Toggle); });
}
public void StartRecord()
{
if (Microphone.devices.Length > 0)
{
string _Device = Microphone.devices[0];
print(_Device);
_RecordClip = Microphone.Start(_Device, true, 60, 16000);
}
else
{
_ASRText.text = "没有音频输入模块。";
print("没有音频输入模块。");
}
}
private void StopRecord()
{
Microphone.End(Microphone.devices[0]);
StartCoroutine(Recognition(_RecordClip));
}
private void SetRecognizeText(string _Result)
{
_ASRText.text = _Result;
}
private string GetAccessToken()
{
HttpClient _Client = new HttpClient();
List<KeyValuePair<string, string>> _ParaList = new List<KeyValuePair<string, string>>();
_ParaList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
_ParaList.Add(new KeyValuePair<string, string>("client_id", _API_Key));
_ParaList.Add(new KeyValuePair<string, string>("client_secret", _Secret_Key));
HttpResponseMessage _Response = _Client.PostAsync(_AuthHost, new FormUrlEncodedContent(_ParaList)).Result;
string _Result = _Response.Content.ReadAsStringAsync().Result;
Debug.Log("result is " + _Result);
return _Result;
}
private IEnumerator Recognition(AudioClip _Clip)
{
float[] _Sample = new float[_Clip.samples];
_Clip.GetData(_Sample, 0);
short[] _IntData = new short[_Sample.Length];
byte[] _ByteData = new byte[_IntData.Length * 2];
for (int i = 0; i < _Sample.Length; i++)
{
_IntData[i] = (short)(_Sample[i] * short.MaxValue);
}
Buffer.BlockCopy(_IntData, 0, _ByteData, 0, _ByteData.Length);
var _Result = _AipClient.Recognize(_ByteData, "pcm", 16000);
var _Speaking = _Result.GetValue("result");
if (_Speaking == null)
{
SetRecognizeText("没有录到声音");
StopAllCoroutines();
yield return null;
}
string _UsefulText = _Speaking.First.ToString();
print(_UsefulText);
StartCoroutine(OLAMIStr(_UsefulText));
yield return 0;
}
public void ToggleIs(Toggle _ToggleASR)
{
if (_ToggleASR.isOn)
{
StartRecord();
}
else
{
StopRecord();
}
}
private IEnumerator OLAMIStr(string _ASRText)
{
string _OLAMIStr = GetRecognitionResult("nli", _ASRText);
_JsonDataWeb = new Root();
_JsonDataWeb = JsonUtility.FromJson<Root>(_OLAMIStr);
if (_JsonDataWeb != null)
{
if (_JsonDataWeb.status == "error")
{
SetRecognizeText("解析失败,请重试。");
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip("解析失败,请重试。"));
}
else
{
for (int i = 0; i < _JsonDataWeb.data.nli.Count; i++)
{
switch (_JsonDataWeb.data.nli[i].type)
{
#region 故事模块
case "joke":
print("故事模块");
_JsonDataWeb02 = new Root02();
_JsonDataWeb02 = JsonUtility.FromJson<Root02>(_OLAMIStr);
StringBuilder _SpeciesJoke = new StringBuilder();
if (_JsonDataWeb02.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb02.data.nli.Count; j++)
{
_SpeciesJoke.Append(_JsonDataWeb02.data.nli[j].desc_obj.result + "\n");
_SpeciesJoke.Append(_JsonDataWeb02.data.nli[j].data_obj[j].content);
SetRecognizeText(_SpeciesJoke.ToString());
}
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesJoke.ToString()));
}
else
{
_SpeciesJoke.Append("你可以换个说法哟。");
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesJoke.ToString()));
}
break;
#endregion
#region 百度百科模块
case "baike":
print("百度百科模块");
_JsonDataWeb03 = new Root03();
_JsonDataWeb03 = JsonUtility.FromJson<Root03>(_OLAMIStr);
for (int k = 0; k < _JsonDataWeb03.data.nli.Count; k++)
{
StringBuilder _SpeciesBaike = new StringBuilder();
if (_JsonDataWeb03.data.nli[k].data_obj[k].type == "species")
{
_SpeciesBaike.Append(_JsonDataWeb03.data.nli[k].desc_obj.result + "\n" + "\n");
if (_JsonDataWeb03.data.nli[k].data_obj[k].field_value.Count>0)
{
for (int j = 0; j < _JsonDataWeb03.data.nli[k].data_obj[k].field_value.Count; j++)
{
_SpeciesBaike.Append(_JsonDataWeb03.data.nli[k].data_obj[k].field_name[j] + ":" + _JsonDataWeb03.data.nli[k].data_obj[k].field_value[j] + "\n");
}
}
else
{
_SpeciesBaike.Append("没有找到具体数据");
}
_SpeciesBaike.Append("\n" + "详细信息:" + _JsonDataWeb03.data.nli[k].data_obj[k].description);
SetRecognizeText(_SpeciesBaike.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesBaike.ToString()));
}
else
{
_SpeciesBaike.Append(_JsonDataWeb03.data.nli[k].desc_obj.result + "\n");
if (_JsonDataWeb03.data.nli[k].data_obj[k].field_value.Count > 0)
{
for (int j = 0; j < _JsonDataWeb03.data.nli[k].data_obj[k].field_value.Count; j++)
{
_SpeciesBaike.Append(_JsonDataWeb03.data.nli[k].data_obj[k].field_name[j] + ":" + _JsonDataWeb03.data.nli[k].data_obj[k].field_value[j] + "\n");
}
}
else
{
_SpeciesBaike.Append("没有找到具体数据");
}
_SpeciesBaike.Append(_JsonDataWeb03.data.nli[k].data_obj[k].description);
SetRecognizeText(_SpeciesBaike.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesBaike.ToString()));
}
}
break;
#endregion
#region 选择模块
case "selection":
print("选择模块");
#region 多轮对话选择模块
switch (_JsonDataWeb.data.nli[i].desc_obj.type)
{
case "poem":
print("诗集模块");
_JsonDataWeb10 = new Root10();
_JsonDataWeb10 = JsonUtility.FromJson<Root10>(_OLAMIStr);
StringBuilder _SpeciesSelectionPoem = new StringBuilder();
_SpeciesSelectionPoem.Append(_JsonDataWeb10.data.nli[i].desc_obj.result + "\n\n");
if (_JsonDataWeb10.data.nli[i].data_obj.Count > 0)
{
for (int j = 0; j < _JsonDataWeb10.data.nli[i].data_obj.Count; j++)
{
_SpeciesSelectionPoem.Append("《" + _JsonDataWeb10.data.nli[i].data_obj[j].poem_name + "》" + "\n");
_SpeciesSelectionPoem.Append("作者:" + _JsonDataWeb10.data.nli[i].data_obj[j].author + "\n\n");
}
}
else
{
_SpeciesSelectionPoem.Append("没有找到哟");
}
SetRecognizeText(_SpeciesSelectionPoem.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesSelectionPoem.ToString()));
break;
case "cooking":
print("菜肴选择模块");
_JsonDataWeb11 = new Root11();
_JsonDataWeb11 = JsonUtility.FromJson<Root11>(_OLAMIStr);
StringBuilder _SpeciesSelectionCooking = new StringBuilder();
_SpeciesSelectionCooking.Append(_JsonDataWeb11.data.nli[i].desc_obj.result + "\n\n");
if (_JsonDataWeb11.data.nli[i].data_obj.Count > 0)
{
for (int j = 0; j < _JsonDataWeb11.data.nli[i].data_obj.Count; j++)
{
_SpeciesSelectionCooking.Append("名称:" + _JsonDataWeb11.data.nli[i].data_obj[j].name + "\n\n");
}
}
else
{
_SpeciesSelectionCooking.Append("没有找到这道菜,可以换一个说法。");
}
SetRecognizeText(_SpeciesSelectionCooking.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesSelectionCooking.ToString()));
break;
case "hotel":
print("酒店选择模块");
_JsonDataWeb12 = new Root12();
_JsonDataWeb12 = JsonUtility.FromJson<Root12>(_OLAMIStr);
StringBuilder _SpeciesHotel = new StringBuilder();
_SpeciesHotel.Append(_JsonDataWeb12.data.nli[i].desc_obj.result + "\n\n");
if (_JsonDataWeb12.data.nli[i].data_obj.Count > 0)
{
for (int j = 0; j < _JsonDataWeb12.data.nli[i].data_obj.Count; j++)
{
_SpeciesHotel.Append("酒店名字:" + _JsonDataWeb12.data.nli[i].data_obj[j].hotel_name + "\n");
_SpeciesHotel.Append("酒店描述:" + _JsonDataWeb12.data.nli[i].data_obj[j].description + "\n");
_SpeciesHotel.Append("酒店地址:" + _JsonDataWeb12.data.nli[i].data_obj[j].hotel_address + "\n");
_SpeciesHotel.Append("酒店形象:" + _JsonDataWeb12.data.nli[i].data_obj[j].hotel_image + "\n");
_SpeciesHotel.Append("平均价格:" + _JsonDataWeb12.data.nli[i].data_obj[j].max_price + "\n");
_SpeciesHotel.Append("最低价格:" + _JsonDataWeb12.data.nli[i].data_obj[j].floor_price + "\n");
_SpeciesHotel.Append("酒店网站:" + _JsonDataWeb12.data.nli[i].data_obj[j].description_url + "\n");
_SpeciesHotel.Append("携程评级:" + _JsonDataWeb12.data.nli[i].data_obj[j].ctrip_rating + "\n");
_SpeciesHotel.Append("用户评级:" + _JsonDataWeb12.data.nli[i].data_obj[j].user_rating + "\n\n");
}
}
else
{
_SpeciesHotel.Append("可以尝试再说一遍。");
}
SetRecognizeText(_SpeciesHotel.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesHotel.ToString()));
break;
default:
SetRecognizeText("对不起,我没有明白你说的是什么意思?");
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip("对不起,我没有明白你说的是什么意思?"));
break;
}
#endregion
break;
#endregion
#region 菜肴模块
case "cooking":
print("菜肴模块");
_JsonDataWeb05 = new Root05();
_JsonDataWeb05 = JsonUtility.FromJson<Root05>(_OLAMIStr);
StringBuilder _SpeciesCooking = new StringBuilder();
if (_JsonDataWeb05.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb05.data.nli.Count; j++)
{
_SpeciesCooking.Append(_JsonDataWeb05.data.nli[j].desc_obj.result + "\n");
_SpeciesCooking.Append(_JsonDataWeb05.data.nli[j].data_obj[j].content);
}
}
else
{
_SpeciesCooking.Append("没有听清你在说什么。");
}
SetRecognizeText(_SpeciesCooking.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesCooking.ToString()));
break;
#endregion
#region 搜索模块
case "openweb":
print("搜索模块");
_JsonDataWeb06 = new Root06();
_JsonDataWeb06 = JsonUtility.FromJson<Root06>(_OLAMIStr);
StringBuilder _SpeciesOpenweb = new StringBuilder();
if (_JsonDataWeb06.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb06.data.nli.Count; j++)
{
_SpeciesOpenweb.Append(_JsonDataWeb06.data.nli[j].desc_obj.result + "\n");
_SpeciesOpenweb.Append(_JsonDataWeb06.data.nli[j].data_obj[j].url);
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_JsonDataWeb06.data.nli[j].desc_obj.result));
}
}
else
{
_SpeciesOpenweb.Append("可以描述的再具体一点。");
}
SetRecognizeText(_SpeciesOpenweb.ToString());
break;
#endregion
#region 股票模块
case "stock":
print("股票模块");
_JsonDataWeb07 = new Root07();
_JsonDataWeb07 = JsonUtility.FromJson<Root07>(_OLAMIStr);
StringBuilder _SpeciesStock = new StringBuilder();
if (_JsonDataWeb07.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb07.data.nli.Count; j++)
{
if (_JsonDataWeb07.data.nli[j].data_obj.Count > 0)
{
_SpeciesStock.Append(_JsonDataWeb07.data.nli[j].desc_obj.result + "\n");
_SpeciesStock.Append("股票名称:" + _JsonDataWeb07.data.nli[j].data_obj[j].name + "\n");
_SpeciesStock.Append("股票代码:" + _JsonDataWeb07.data.nli[j].data_obj[j].id + "\n");
_SpeciesStock.Append("最高点:" + _JsonDataWeb07.data.nli[j].data_obj[j].price_high + "\n");
_SpeciesStock.Append("最低点:" + _JsonDataWeb07.data.nli[j].data_obj[j].price_low + "\n");
_SpeciesStock.Append("今开:" + _JsonDataWeb07.data.nli[j].data_obj[j].price_start + "\n");
_SpeciesStock.Append("昨收:" + _JsonDataWeb07.data.nli[j].data_obj[j].price_end + "\n");
_SpeciesStock.Append("变化率:" + _JsonDataWeb07.data.nli[j].data_obj[j].change_rate + "\n");
_SpeciesStock.Append("最低交易:" + _JsonDataWeb07.data.nli[j].data_obj[j].cur_price + "\n");
_SpeciesStock.Append("成交量:" + _JsonDataWeb07.data.nli[j].data_obj[j].volume + "\n");
}
else
{
_SpeciesStock.Append(_JsonDataWeb07.data.nli[j].desc_obj.result + "\n");
}
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_JsonDataWeb07.data.nli[0].desc_obj.result));
}
SetRecognizeText(_SpeciesStock.ToString());
}
break;
#endregion
#region 电视模块
case "tvprogram":
print("电视模块");
_JsonDataWeb08 = new Root08();
_JsonDataWeb08 = JsonUtility.FromJson<Root08>(_OLAMIStr);
StringBuilder _SpeciesTvprogram = new StringBuilder();
if (_JsonDataWeb08.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb08.data.nli.Count; j++)
{
_SpeciesTvprogram.Append(_JsonDataWeb08.data.nli[j].desc_obj.result + "\n");
for (int k = 0; k < _JsonDataWeb08.data.nli[j].data_obj.Count; k++)
{
_SpeciesTvprogram.Append(_JsonDataWeb08.data.nli[j].data_obj[k].name + "\n");
_SpeciesTvprogram.Append("播出时间是:" + _JsonDataWeb08.data.nli[j].data_obj[k].time + "\n" + "\n");
}
}
}
SetRecognizeText(_SpeciesTvprogram.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesTvprogram.ToString()));
break;
#endregion
#region 天气模块
case "weather":
print("天气模块");
_JsonDataWeb09 = new Root09();
_JsonDataWeb09 = JsonUtility.FromJson<Root09>(_OLAMIStr);
StringBuilder _SpeciesWeather = new StringBuilder();
if (_JsonDataWeb09.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb09.data.nli.Count; j++)
{
_SpeciesWeather.Append(_JsonDataWeb09.data.nli[j].desc_obj.result + "\n");
for (int k = 0; k < _JsonDataWeb09.data.nli[j].data_obj.Count; k++)
{
_SpeciesWeather.Append(_JsonDataWeb09.data.nli[j].data_obj[k].description + "\n");
}
}
}
else
{
_SpeciesWeather.Append("你的声音太小了,我没听清楚。");
}
SetRecognizeText(_SpeciesWeather.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesWeather.ToString()));
break;
#endregion
#region 诗集模块
case "poem":
print("诗集模块");
_JsonDataWeb13 = new Root13();
_JsonDataWeb13 = JsonUtility.FromJson<Root13>(_OLAMIStr);
StringBuilder _SpeciesPoem = new StringBuilder();
if (_JsonDataWeb13.data.nli.Count > 0)
{
for (int j = 0; j < _JsonDataWeb13.data.nli.Count; j++)
{
if (_JsonDataWeb13.data.nli[j].data_obj.Count > 0)
{
for (int k = 0; k < _JsonDataWeb13.data.nli[j].data_obj.Count; k++)
{
_SpeciesPoem.Append("《" + _JsonDataWeb13.data.nli[j].data_obj[k].title + "》" + "\n");
_SpeciesPoem.Append("作者:" + _JsonDataWeb13.data.nli[j].data_obj[k].author + "\n\n");
_SpeciesPoem.Append(_JsonDataWeb13.data.nli[j].data_obj[k].content + "\n");
}
}
else
{
_SpeciesPoem.Append("我没有找到哦,你可以再说一遍。");
}
}
}
else
{
_SpeciesPoem.Append("你的声音太小了,我没听清楚。");
}
SetRecognizeText(_SpeciesPoem.ToString());
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_SpeciesPoem.ToString()));
break;
#endregion
default:
print("基础模块");
SetRecognizeText(_JsonDataWeb.data.nli[i].desc_obj.result);
StartCoroutine(VoiceSynthesis_ZH._World.GetAudioClip(_JsonDataWeb.data.nli[i].desc_obj.result));
break;
}
}
}
}
yield return 0;
}
private void Update()
{
}
private string GetRecognitionResult(string _ApiName, string _InputText)
{
string _Result;
double _Timestamp = Convert.ToInt64((DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds);
StringBuilder _SignMsg = new StringBuilder()
.Append(_Secret)
.Append("api=")
.Append(_ApiName)
.Append("appkey=")
.Append(_Key)
.Append("timestamp=")
.Append(_Timestamp)
.Append(_Secret);
MD5 _Md5 = MD5.Create();
StringBuilder _Sign = new StringBuilder();
byte[] hash = _Md5.ComputeHash(Encoding.Default.GetBytes(_SignMsg.ToString()));
for (int i = 0; i < hash.Length; i++)
{
_Sign.Append(hash[i].ToString("X2"));
}
using (WebClient _Client = new WebClient())
{
print(("发送'POST'请求到URL" + _ApiBaseUrl));
NameValueCollection _ApiParam = null;
if (_ApiName == NluApiSample.API_NAME_SEG)
{
_ApiParam = new NameValueCollection()
{
{ "appkey", _Key },
{ "api", NluApiSample.API_NAME_SEG },
{ "timestamp", _Timestamp.ToString() },
{ "sign", _Sign.ToString() },
{ "rq", _InputText }
};
}
else if (_ApiName == NluApiSample.API_NAME_NLI)
{
_ApiParam = new NameValueCollection()
{
{ "appkey", _Key },
{ "api", NluApiSample.API_NAME_NLI },
{ "timestamp", _Timestamp.ToString() },
{ "sign", _Sign.ToString() },
{ "rq", "{\"data_type\":\"stt\", \"data\":{\"input_type\":1,\"text\":\"" + _InputText + "\"}}" }
};
}
byte[] _Response = _Client.UploadValues(_ApiBaseUrl, _ApiParam);
_Result = Encoding.UTF8.GetString(_Response);
}
print(_Result);
return _Result;
}
#region 百度令牌访问弃用
private IEnumerator GetAccessToken02()
{
WWWForm wWWForm = new WWWForm();
wWWForm.AddField("grant_type", "client_credentials");
wWWForm.AddField("client_id", _API_Key);
wWWForm.AddField("client_secret", _Secret_Key);
UnityWebRequest unityWebRequest = UnityWebRequest.Post(_AuthHost, wWWForm);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isNetworkError)
{
print("网络错误");
}
else
{
if (unityWebRequest.responseCode == 200)
{
string re = unityWebRequest.downloadHandler.text;
print("获取:" + re);
}
}
}
#endregion
}
人工智能语音 Json 数据支持
using System;
using System.Collections.Generic;
[Serializable]
public static class Json_ZH
{
#region 基础模块返回数据
[Serializable]
public class Desc_obj
{
public string result;
public int status;
public string type;
}
[Serializable]
public class NliItem
{
public Desc_obj desc_obj;
public string type;
}
[Serializable]
public class Data
{
public List<NliItem> nli;
}
[Serializable]
public class Root
{
public string status;
public Data data;
}
#endregion
#region 故事模块返回数据 joke story 讲个故事吧
[Serializable]
public class Desc_obj02
{
public string result;
public string name;
public string type;
public int status;
}
[Serializable]
public class Data_objItem02
{
public string content;
}
[Serializable]
public class NliItem02
{
public Desc_obj02 desc_obj;
public List<Data_objItem02> data_obj;
public string type;
}
[Serializable]
public class Data02
{
public List<NliItem02> nli;
}
[Serializable]
public class Root02
{
public string status;
public Data02 data;
}
#endregion
#region 百度百科模块返回数据 baike 什么是海豹
[Serializable]
public class Desc_obj03
{
public string result;
public int status;
}
[Serializable]
public class Data_objItem03
{
public List<int> highlight;
public List<string> field_value;
public string description;
public string photo_url;
public string type;
public List<string> categorylist;
public List<string> field_name;
}
[Serializable]
public class NliItem03
{
public Desc_obj03 desc_obj;
public List<Data_objItem03> data_obj;
public string type;
}
[Serializable]
public class Data03
{
public List<NliItem03> nli;
}
[Serializable]
public class Root03
{
public string status;
public Data03 data;
}
#endregion
#region 选择模块 返回数据 selection 新闻联播在哪
[Serializable]
public class Desc_obj04
{
public string type;
public int status;
}
[Serializable]
public class Data_objItem04
{
public string name;
public string desc;
}
[Serializable]
public class NliItem04
{
public Desc_obj04 desc_obj;
public List<Data_objItem04> data_obj;
public string type;
}
[Serializable]
public class Data04
{
public List<NliItem04> nli;
}
[Serializable]
public class Root04
{
public string status;
public Data04 data;
}
#endregion
#region 菜肴模块返回数据 cooking 糖醋排骨怎么做
[Serializable]
public class Desc_obj05
{
public string result;
public int status;
}
[Serializable]
public class Data_objItem05
{
public string name;
public string content;
}
[Serializable]
public class NliItem05
{
public Desc_obj05 desc_obj;
public List<Data_objItem05> data_obj;
public string type;
}
[Serializable]
public class Data05
{
public List<NliItem05> nli;
}
[Serializable]
public class Root05
{
public string status;
public Data05 data;
}
#endregion
#region 搜索模块返回数据 openweb 搜索西瓜
[Serializable]
public class Desc_obj06
{
public string result;
public string type;
public int status;
}
[Serializable]
public class Data_objItem06
{
public string url;
}
[Serializable]
public class NliItem06
{
public Desc_obj06 desc_obj;
public List<Data_objItem06> data_obj;
public string type;
}
[Serializable]
public class Data06
{
public List<NliItem06> nli;
}
[Serializable]
public class Root06
{
public string status;
public Data06 data;
}
#endregion
#region 股票模块返回数据 stock 中国石油股票
[Serializable]
public class Desc_obj07
{
public string result;
public int status;
}
[Serializable]
public class Data_objItem07
{
public string amount;
public string cur_price;
public string price_start;
public string intent;
public string price_low;
public string change_rate;
public string volume;
public string change_amount;
public int is_history;
public string price_end;
public string name;
public string id;
public string time;
public string price_high;
public string favorite;
}
[Serializable]
public class NliItem07
{
public Desc_obj07 desc_obj;
public List<Data_objItem07> data_obj;
public string type;
}
[Serializable]
public class Data07
{
public List<NliItem07> nli;
}
[Serializable]
public class Root07
{
public string status;
public Data07 data;
}
#endregion
#region 电视节目模块返回数据 tvprogram 电视节目 CCTV1
[Serializable]
public class Data_objItem08
{
public int highlight;
public string name;
public string time;
}
[Serializable]
public class Desc_obj08
{
public string result;
public string type;
public int status;
}
[Serializable]
public class NliItem08
{
public Desc_obj08 desc_obj;
public List<Data_objItem08> data_obj;
public string type;
}
[Serializable]
public class Data08
{
public List<NliItem08> nli;
}
[Serializable]
public class Root08
{
public string status;
public Data08 data;
}
#endregion
#region 天气模块返回数据 weather 上海的天气
[Serializable]
public class Desc_obj09
{
public string result;
public int status;
}
[Serializable]
public class Data_objItem09
{
public int date;
public int weather_end;
public int pm25;
public string city;
public string temperature_low;
public int weather_start;
public string temperature_high;
public string description;
public int is_querying;
public int real_date;
public string wind;
}
[Serializable]
public class NliItem09
{
public Desc_obj09 desc_obj;
public List<Data_objItem09> data_obj;
public string type;
}
[Serializable]
public class Data09
{
public List<NliItem09> nli;
}
[Serializable]
public class Root09
{
public string status;
public Data09 data;
}
#endregion
#region 诗集选择模块 poem selection 水调歌头 苏轼
[Serializable]
public class Desc_obj10
{
public string result;
public string type;
public int status;
}
[Serializable]
public class Data_objItem10
{
public string poem_name;
public string author;
}
[Serializable]
public class NliItem10
{
public Desc_obj10 desc_obj;
public List<Data_objItem10> data_obj;
public string type;
}
[Serializable]
public class Data10
{
public List<NliItem10> nli;
}
[Serializable]
public class Root10
{
public string status;
public Data10 data;
}
#endregion
#region 菜肴选择模块 cooking selection 蛋糕怎么做
[Serializable]
public class Desc_obj11
{
public string result;
public string type;
public int status;
}
[Serializable]
public class Data_objItem11
{
public string name;
}
[Serializable]
public class NliItem11
{
public Desc_obj11 desc_obj;
public List<Data_objItem11> data_obj;
public string type;
}
[Serializable]
public class Data11
{
public List<NliItem11> nli;
}
[Serializable]
public class Root11
{
public string status;
public Data11 data;
}
#endregion
#region 酒店选择模块 hotel 预定酒店
[Serializable]
public class Desc_obj12
{
public string result;
public string type;
public int status;
}
[Serializable]
public class Data_objItem12
{
public string hotel_image;
public string max_price;
public string description_url;
public string ctrip_rating;
public string description;
public string hotel_address;
public string floor_price;
public string hotel_name;
public string user_rating;
}
[Serializable]
public class NliItem12
{
public Desc_obj12 desc_obj;
public List<Data_objItem12> data_obj;
public string type;
}
[Serializable]
public class Data12
{
public List<NliItem12> nli;
}
[Serializable]
public class Root12
{
public string status;
public Data12 data;
}
#endregion
#region 诗集模块 poem 我要听长恨歌
[Serializable]
public class Desc_obj13
{
public string result;
public int status;
}
[Serializable]
public class Data_objItem13
{
public string author;
public string title;
public string content;
}
[Serializable]
public class NliItem13
{
public Desc_obj13 desc_obj;
public List<Data_objItem13> data_obj;
public string type;
}
[Serializable]
public class Data13
{
public List<NliItem13> nli;
}
[Serializable]
public class Root13
{
public string status;
public Data13 data;
}
#endregion
}
搭载运行
基础运行
支持多轮对话
实时传输语音数据 并播放
暂时先这样吧,如果有时间的话就会更新,实在看不明白就留言,看到我会回复的。 路漫漫其修远兮,与君共勉。
|