482 lines
14 KiB
C#
482 lines
14 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using System.IO;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class Conversation : MonoBehaviour
|
|
{
|
|
[System.Serializable]
|
|
public class Answer {
|
|
public int status;
|
|
public string url_audio;
|
|
public string TextoAudio;
|
|
public int Vezes;
|
|
public string ExemploResposta;
|
|
public bool Final;
|
|
public double Coerencia;
|
|
public bool Validou;
|
|
public string ZoeEntendeu;
|
|
public int Ordem;
|
|
public int ZoeTalking;
|
|
public int ProximoZoeTalking;
|
|
public int Modo = 0;
|
|
}
|
|
|
|
public Answer respostaZoe = new Answer();
|
|
|
|
public class BypassCertificate : CertificateHandler
|
|
{
|
|
protected override bool ValidateCertificate(byte[] certificateData)
|
|
{
|
|
//Simply return true no matter what
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
AudioSource source; AudioClip clip; GameObject outObj; Animator faceAnime;
|
|
//Text txt, txt1;
|
|
|
|
WriteFile writeFile; public bool end; public bool noInternet = false;
|
|
GameObject noInternetObj; Animator microAnime;
|
|
string sessionID;
|
|
|
|
Text legTxt; bool cooldownLeg = false;
|
|
|
|
public GameObject BotaoMicrofone;
|
|
public Image Imagem;
|
|
|
|
Animator animeCamera;
|
|
|
|
string route = "https://zoe.myrobotlab.com.br";
|
|
// Start is called before the first frame update
|
|
void Awake()
|
|
{
|
|
GameObject mainCamera = GameObject.Find("MainMenu Camera");
|
|
animeCamera = mainCamera.GetComponent<Animator>();
|
|
animeCamera.enabled = false;
|
|
|
|
mainCamera.GetComponent<Camera>().backgroundColor = new Color(0.9686275f, 1, 0.2235294f);
|
|
|
|
faceAnime = GameObject.Find("Canvas - Conversation").GetComponent<Animator>();
|
|
microAnime = GameObject.Find("Micro").GetComponent<Animator>();
|
|
|
|
BotaoMicrofone = GameObject.Find("Micro");
|
|
Imagem = GameObject.Find("Imagem").GetComponent<Image>();
|
|
Imagem.gameObject.SetActive(false);
|
|
|
|
outObj = GameObject.Find("Sair");
|
|
outObj.SetActive(false);
|
|
|
|
legTxt = GameObject.Find("txtLeg").GetComponent<Text>();
|
|
|
|
noInternetObj = GameObject.Find("txtError");
|
|
noInternetObj.SetActive(false);
|
|
|
|
writeFile = GetComponent<WriteFile>();
|
|
|
|
source = GameObject.Find("Audio Source").GetComponent<AudioSource>();
|
|
|
|
StartCoroutine(CoroutineToSync());
|
|
}
|
|
|
|
public IEnumerator CoroutineToSync()
|
|
{
|
|
yield return new WaitForFixedUpdate();
|
|
|
|
APICustomize aPICustomize = APICustomize.instance;
|
|
|
|
while (!aPICustomize.checkdItens)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
string[] itens = aPICustomize.actualItens.Split(';');
|
|
|
|
yield return StartCoroutine(DownloadPhoto(itens[0], new List<Image>(2)
|
|
{
|
|
GameObject.Find("Left_Eye").GetComponent<Image>(), GameObject.Find("Right_Eye").GetComponent<Image>()
|
|
}));
|
|
|
|
yield return StartCoroutine(DownloadPhoto(itens[1], new List<Image>(2)
|
|
{
|
|
GameObject.Find("Bochecha_Right").GetComponent<Image>(), GameObject.Find("Bochecha").GetComponent<Image>()
|
|
}));
|
|
|
|
yield return StartCoroutine(DownloadPhoto(itens[2], new List<Image>(2)
|
|
{
|
|
GameObject.Find("FeatRight").GetComponent<Image>(), GameObject.Find("FeatLeft").GetComponent<Image>()
|
|
}));
|
|
|
|
yield return StartCoroutine(DownloadPhoto(itens[3], new List<Image>(1)
|
|
{
|
|
GameObject.Find("Eyeglass").GetComponent<Image>()
|
|
}));
|
|
|
|
StartCoroutine(getSessionID());
|
|
StartCoroutine(checkInternet());
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
Destroy(GameObject.Find("Load"));
|
|
}
|
|
|
|
string StrRemove(string str)
|
|
{
|
|
string[] split = str.Split('_');
|
|
|
|
return split[split.Length - 1];
|
|
}
|
|
|
|
IEnumerator DownloadPhoto(string name, List<Image> imgs)
|
|
{
|
|
if (StrRemove(name) == "0")
|
|
{
|
|
foreach (Image image in imgs)
|
|
{
|
|
image.sprite = null;
|
|
image.color = new Color(1, 1, 1, 0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string url = "http://" + ReadingFile.iploc + "/zoeGamesResources/CustomizeZoe/" +
|
|
name + ".png";
|
|
|
|
using (WWW www = new WWW(url))
|
|
{
|
|
yield return www;
|
|
|
|
Sprite spr = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height),
|
|
new Vector2(0, 0));
|
|
foreach (Image image in imgs)
|
|
{
|
|
image.sprite = spr;
|
|
image.color = new Color(1, 1, 1, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void showSubtitle(LegBtn leg) {
|
|
if (!cooldownLeg)
|
|
{
|
|
bool actualstate = faceAnime.GetBool("leg");
|
|
faceAnime.SetBool("leg", !actualstate);
|
|
leg.changeSprite(actualstate);
|
|
|
|
StartCoroutine(CooldownLegend());
|
|
}
|
|
}
|
|
|
|
IEnumerator CooldownLegend() {
|
|
cooldownLeg = true;
|
|
yield return new WaitForSecondsRealtime(1);
|
|
cooldownLeg = false;
|
|
}
|
|
|
|
public IEnumerator checkInternet() {
|
|
while (true)
|
|
{
|
|
if (Application.internetReachability == NetworkReachability.NotReachable)
|
|
{
|
|
noInternet = true; noInternetObj.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
noInternet = false; noInternetObj.SetActive(false);
|
|
yield return new WaitForSecondsRealtime(5);
|
|
}
|
|
yield return new WaitForSecondsRealtime(5);
|
|
}
|
|
|
|
}
|
|
|
|
public void GoBack(int i) {
|
|
if (i == 1)
|
|
{
|
|
AsyncOperation obj = SceneManager.LoadSceneAsync("Transition", LoadSceneMode.Additive);
|
|
|
|
obj.completed += (asyncOperation) =>
|
|
{
|
|
//Do stuff here with returned value asyncOperation
|
|
GameObject.Find("Canvas - Transition").GetComponent<Transition_Controller>().StartLoadingScene("SampleScene");
|
|
|
|
StartCoroutine(WaitToDeactivateScene());
|
|
};
|
|
}
|
|
else {
|
|
outObj.SetActive(false);
|
|
}
|
|
}
|
|
|
|
IEnumerator WaitToDeactivateScene()
|
|
{
|
|
yield return new WaitForSecondsRealtime(.6f);
|
|
|
|
animeCamera.enabled = true;
|
|
|
|
SceneManager.UnloadSceneAsync("FaceConversation", UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
|
|
}
|
|
|
|
public void OpenBack() {
|
|
if (noInternet)
|
|
{
|
|
GoBack(1);
|
|
}
|
|
else
|
|
{
|
|
outObj.SetActive(true);
|
|
}
|
|
}
|
|
|
|
IEnumerator LoadAudioCor() {
|
|
yield return StartCoroutine(writeFile.loadAudio());
|
|
|
|
source.clip = writeFile.audioLoaded;
|
|
source.Play();
|
|
}
|
|
|
|
public void StartRecording() {
|
|
StartCoroutine(syncCoroutineForRecord());
|
|
}
|
|
|
|
public void StopRecording() {
|
|
Debug.Log("Out");
|
|
StartCoroutine(stopNGoBack());
|
|
}
|
|
|
|
IEnumerator stopNGoBack() {
|
|
end = true;
|
|
yield return new WaitForSecondsRealtime(2f);
|
|
end = false;
|
|
}
|
|
|
|
public IEnumerator syncCoroutineForRecord() {
|
|
Imagem.gameObject.SetActive(false);
|
|
yield return StartCoroutine(record());
|
|
BotaoMicrofone.SetActive(false);
|
|
microAnime.SetBool("Loading", true);
|
|
yield return new WaitForEndOfFrame();
|
|
yield return StartCoroutine(SendAudio());
|
|
microAnime.SetBool("Loading", false);
|
|
source.Play();
|
|
yield return new WaitForSecondsRealtime(0.3f);
|
|
BotaoMicrofone.SetActive(false);
|
|
faceAnime.SetBool("Talking", true);
|
|
yield return new WaitForSecondsRealtime(clip.length - .5f);
|
|
faceAnime.SetBool("Talking", false);
|
|
BotaoMicrofone.SetActive(true);
|
|
}
|
|
|
|
IEnumerator intent() {
|
|
|
|
|
|
yield break;
|
|
}
|
|
|
|
IEnumerator getSessionID() {
|
|
string url = "https://kakko.pandorabots.com/pandora/talk-xml";
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("input", "anything");
|
|
form.AddField("botid", "9fa364f2fe345a10");
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Post(url, form)) {
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.certificateHandler = new BypassCertificate();
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.LogWarning(www.error);
|
|
}
|
|
else
|
|
{
|
|
string response = www.downloadHandler.text;
|
|
int start = response.IndexOf("custid=\"");
|
|
string newresponse = response;
|
|
newresponse = newresponse.Remove(0, start + 8);
|
|
int end = newresponse.IndexOf("\"><");
|
|
newresponse = newresponse.Remove(end);
|
|
Debug.Log(newresponse);
|
|
|
|
sessionID = newresponse;
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator SendAudio() {
|
|
|
|
string path = Application.persistentDataPath + "/tempAudio.wav";
|
|
bool foundWay = false;
|
|
while (!foundWay)
|
|
{
|
|
if (File.Exists(path)) { foundWay = true; }
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddBinaryData("Arquivo", writeFile.getAudioFile("file://" + Application.persistentDataPath + "/tempAudio.wav").bytes);
|
|
form.AddField("Modo", respostaZoe.Modo);
|
|
form.AddField("Matricula", PlayerPrefs.GetInt("IDMat"));
|
|
form.AddField("IDUnidade", PlayerPrefs.GetInt("IDUnit"));
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Post(route + "/api/chattime/recognizeAudio/asd123dsa654w789", form))
|
|
{
|
|
www.certificateHandler = new BypassCertificate();
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.Log(www.error);
|
|
Debug.Log(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
string Json = www.downloadHandler.text;
|
|
Answer answer = JsonUtility.FromJson<Answer>(Json);
|
|
if (answer.status == 200)
|
|
{
|
|
respostaZoe = answer;
|
|
legTxt.text = answer.TextoAudio;
|
|
Debug.Log(answer.TextoAudio);
|
|
if (answer.TextoAudio.Contains("<image>"))
|
|
{
|
|
string Link = "";
|
|
Link = answer.TextoAudio.Split('>')[1].Split('<')[0];
|
|
Debug.Log(Link);
|
|
StartCoroutine(LoadImage(Link));
|
|
}
|
|
using (var wwwclip = new WWW(answer.url_audio))
|
|
{
|
|
yield return wwwclip;
|
|
clip = wwwclip.GetAudioClip();
|
|
clip.name = "Answer";
|
|
source.clip = clip;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator record()
|
|
{
|
|
end = false;
|
|
|
|
int micToUse = writeFile.CheckMicro();
|
|
int frequency = 24000;
|
|
|
|
yield return new WaitForSecondsRealtime(.1f);
|
|
|
|
List<AudioClip> clips = new List<AudioClip>();
|
|
|
|
clips.Add(Microphone.Start(Microphone.devices[micToUse], false, 10, frequency));
|
|
|
|
float timeRecording = 0;
|
|
while (!end)
|
|
{
|
|
timeRecording += Time.deltaTime;
|
|
|
|
if (timeRecording >= 10)
|
|
{
|
|
Microphone.End(Microphone.devices[micToUse]);
|
|
clips.Add(Microphone.Start(Microphone.devices[micToUse], false, 10, frequency));
|
|
timeRecording = 0;
|
|
}
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
Microphone.End(Microphone.devices[micToUse]);
|
|
int samplerate = 24000;
|
|
AudioClip clipMicro;
|
|
float startIndex = timeRecording * 24000;
|
|
|
|
if (clips.Count > 0)
|
|
{
|
|
int lenghtAudio = (samplerate * 10 * (clips.Count - 1)) + (int)startIndex;
|
|
if (lenghtAudio > 0)
|
|
{
|
|
clipMicro = AudioClip.Create("Merge", lenghtAudio, 1, samplerate, false);
|
|
}
|
|
else
|
|
{
|
|
clipMicro = AudioClip.Create("Merge", samplerate, 1, samplerate, false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
clipMicro = AudioClip.Create("Merge", samplerate, 1, samplerate, false);
|
|
}
|
|
|
|
if (clips.Count > 0)
|
|
{
|
|
List<float> clipsMerged = new List<float>();
|
|
clipsMerged.Add(0);
|
|
|
|
for (int i = 0; i < clips.Count; i++)
|
|
{
|
|
if (i == (clips.Count - 1))
|
|
{
|
|
float[] samples = new float[clips[i].samples * clips[i].channels];
|
|
clips[i].GetData(samples, 0);
|
|
|
|
List<float> sampList = new List<float>();
|
|
sampList.AddRange(samples);
|
|
|
|
int countIndex = (24000 * 10) - (int)startIndex;
|
|
sampList.RemoveRange((int)startIndex, countIndex);
|
|
|
|
Debug.Log(sampList.Count);
|
|
|
|
clipsMerged.AddRange(sampList);
|
|
}
|
|
else
|
|
{
|
|
float[] samples = new float[clips[i].samples * clips[i].channels];
|
|
clips[i].GetData(samples, 0);
|
|
|
|
clipsMerged.AddRange(samples);
|
|
}
|
|
}
|
|
|
|
clipMicro.SetData(clipsMerged.ToArray(), 0);
|
|
}
|
|
|
|
if (File.Exists(Application.persistentDataPath + "/tempAudio.wav"))
|
|
{
|
|
File.Delete(Application.persistentDataPath + "/tempAudio.wav");
|
|
}
|
|
|
|
writeFile.CreateAudio(clipMicro);
|
|
//yield return new WaitForSecondsRealtime(5);
|
|
yield break;
|
|
}
|
|
|
|
|
|
|
|
IEnumerator LoadImage(string MediaUrl)
|
|
{
|
|
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
|
|
yield return request.SendWebRequest();
|
|
if (request.isNetworkError || request.isHttpError)
|
|
Debug.Log(request.error);
|
|
else
|
|
{
|
|
Imagem.gameObject.SetActive(true);
|
|
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
|
|
Imagem.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
|
|
}
|
|
}
|
|
|
|
|
|
}
|