http://2vr.jp/2014/12/23/unity-assets/
@hecomi san
http://2vr.jp/2014/12/23/unity-assets/
@hecomi san
| using UnityEngine; | |
| using WebSocketSharp; | |
| using System.Collections; | |
| public class AI : MonoBehaviour { | |
| private WebSocket ws_; | |
| public string docomoAPIkey = ""; | |
| public string oreSay = ""; | |
| public string yomeSay = ""; | |
| public string nickname = ""; | |
| public string nickname_y = ""; | |
| public string context = ""; | |
| public string t = ""; | |
| private bool isSay = false; | |
| public string voicetextAPIkey = ""; | |
| public string v_speaker = ""; | |
| public string v_emotion = ""; | |
| public string v_emotion_level = ""; | |
| public string v_pitch = ""; | |
| public string v_speed = ""; | |
| public string v_volume = ""; | |
| private MMD4M_LipSync lipSync; | |
| void Initialize() { | |
| TextAsset conf = Instantiate(Resources.Load("Config")) as TextAsset; | |
| JSONObject cnf = new JSONObject(conf.text); | |
| // Docomo API | |
| var cnf_docomo = cnf.GetField("DOCOMO"); | |
| docomoAPIkey = cnf_docomo.GetField("APIkey").str; | |
| nickname = cnf_docomo.GetField("nickname").str; | |
| nickname_y = cnf_docomo.GetField("nickname_y").str; | |
| if (t == "") { | |
| t = cnf_docomo.GetField("t").str; | |
| } | |
| if (!PlayerPrefs.HasKey("context")) { | |
| PlayerPrefs.SetString("context", ""); | |
| PlayerPrefs.Save(); | |
| } | |
| context = PlayerPrefs.GetString("context"); | |
| // VoiceTextAPI | |
| var cnf_voicetext = cnf.GetField("VoiceText"); | |
| voicetextAPIkey = cnf_voicetext.GetField("APIkey").str; | |
| v_speaker = cnf_voicetext.GetField("Speaker").str; | |
| v_emotion = cnf_voicetext.GetField("emotion").str; | |
| v_emotion_level = cnf_voicetext.GetField("emotion_level").str; | |
| v_pitch = cnf_voicetext.GetField("pitch").str; | |
| v_speed = cnf_voicetext.GetField("speed").str; | |
| v_volume = cnf_voicetext.GetField("volume").str; | |
| } | |
| void Start() { | |
| Initialize(); | |
| lipSync = GetComponent<MMD4M_LipSync>(); | |
| ws_ = new WebSocket("ws://127.0.0.1:12002"); | |
| ws_.OnMessage += (sender, e) => { | |
| oreSay = e.Data; | |
| Debug.Log("俺: " + oreSay); | |
| isSay = true; | |
| }; | |
| ws_.Connect(); | |
| } | |
| void OnApplicationQuit() { | |
| ws_.Close(); | |
| } | |
| void Update() { | |
| if (isSay) { | |
| isSay = false; | |
| StartCoroutine(dialogue()); | |
| } | |
| } | |
| IEnumerator dialogue() { | |
| var url = "https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=" + docomoAPIkey; | |
| var data = | |
| "{\"utt\":\"" + oreSay + "\"," + | |
| "\"context\":\"" + context + "\"," + | |
| "\"nickname\":\"" + nickname + "\"," + | |
| "\"nickname_y\":\"" + nickname_y + "\"," + | |
| "\"mode\":\"dialog\"," + | |
| "\"t\":\"" + t + "\"}"; | |
| var postData = System.Text.Encoding.UTF8.GetBytes(data); | |
| var headers = new Hashtable(); | |
| headers["Content-Type"] = "application/json;charset=UTF-8"; | |
| WWW www = new WWW(url, postData, headers); | |
| yield return www; | |
| if (www.error != null) { | |
| Debug.Log("Error: " + www.error); | |
| } else { | |
| JSONObject json = new JSONObject(www.text); | |
| yomeSay = json.GetField("yomi").str; | |
| Debug.Log("嫁: " + yomeSay); | |
| if (lipSync != null) { | |
| StartCoroutine(voiceText(yomeSay)); | |
| } | |
| context = json.GetField("context").str; | |
| PlayerPrefs.SetString("context", json.GetField("context").str); | |
| PlayerPrefs.Save(); | |
| } | |
| } | |
| IEnumerator voiceText(string text) { | |
| var url = "https://api.voicetext.jp/v1/tts"; | |
| WWWForm form = new WWWForm (); | |
| form.AddField("speaker", v_speaker); | |
| form.AddField("emotion", v_emotion); | |
| form.AddField("emotion_level", v_emotion_level); | |
| form.AddField("pitch", v_pitch); | |
| form.AddField("speed", v_speed); | |
| form.AddField("volume", v_volume); | |
| form.AddField("text", text); | |
| Hashtable headers = form.headers; | |
| headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(voicetextAPIkey + ":")); | |
| WWW www = new WWW (url, form.data, headers); | |
| yield return www; | |
| if (www.error != null) { | |
| Debug.Log(www.error); | |
| } else { | |
| lipSync.audioClip = www.GetAudioClip(false, false, AudioType.WAV); | |
| lipSync.Play(); | |
| } | |
| } | |
| } |
| { | |
| "DOCOMO": { | |
| "//": "https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=5", | |
| "APIkey": "", | |
| "//": "なし: デフォルト, 20: 関西弁, 30: 赤ちゃん", | |
| "t": "", | |
| "//": "自分の名前", | |
| "nickname": "ゆーじ", | |
| "//": "自分の名前をカタカナで", | |
| "nickname_y": "ユージ" | |
| }, | |
| "VoiceText": { | |
| "//": "https://cloud.voicetext.jp/webapi/api_keys/new", | |
| "APIkey": "", | |
| "//": "show (男性), haruka (女性), hikari (女性), takeru (男性), santa (サンタクロース), bear (凶暴なクマ)", | |
| "Speaker": "hikari", | |
| "//": "happiness 喜, anger 怒, sadness 悲(話者 haruka、hikari、takeru、santa、bear 指定時のみ", | |
| "emotion": "happiness", | |
| "//": "感情レベル 1 2 (2: 感情大 デフォ1)", | |
| "emotion_level": "2", | |
| "//": "声の高さ%(50から200 デフォ100)", | |
| "pitch": "100", | |
| "//": "話すスピード%(50から400 デフォ100)", | |
| "speed": "100", | |
| "//": "声の大きさ%(50から200 デフォ100)", | |
| "volume": "100" | |
| } | |
| } | |