888 lines
27 KiB
C#
888 lines
27 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 enum TipoConversacao
|
|
{
|
|
ConversacaoLivre,
|
|
ConversacaoDirigida,
|
|
ModoConfiguracao,
|
|
ExercicioPronuncia,
|
|
ModoRepeticao
|
|
}
|
|
|
|
public class BodyReq
|
|
{
|
|
public int Modo;
|
|
public int Seq = 0;
|
|
public int Vezes = 0;
|
|
public int ChatTimeID;
|
|
public int Ordem = 0;
|
|
|
|
public int Matricula = Convert.ToInt32(ReadingFile.idmatricula);
|
|
public int IDUnidade = Convert.ToInt32(ReadingFile.idUnit);
|
|
public int IDAula;
|
|
public int IDExercicio;
|
|
public byte[] Arquivo;
|
|
public string Mensagem = "";
|
|
}
|
|
|
|
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 Legenda;
|
|
public int Modo;
|
|
public int Seq;
|
|
public string img;
|
|
}
|
|
public class BypassCertificate : CertificateHandler
|
|
{
|
|
protected override bool ValidateCertificate(byte[] certificateData)
|
|
{
|
|
//Simply return true no matter what
|
|
return true;
|
|
}
|
|
}
|
|
|
|
#region Intet
|
|
[Serializable]
|
|
public class Intent {
|
|
public int status;
|
|
public ResponseIntent[] response;
|
|
}
|
|
[Serializable]
|
|
public class ResponseIntent {
|
|
public string reqIntent;
|
|
}
|
|
#endregion
|
|
|
|
public Answer respostaZoe = new Answer();
|
|
public AudioSource source;
|
|
public AudioClip clip;
|
|
GameObject outObj; Animator faceAnime;
|
|
//Text txt, txt1;
|
|
|
|
WriteFile writeFile; public bool end; public bool noInternet = false;
|
|
Animator microAnime;
|
|
|
|
public string token;
|
|
|
|
public int conversationtree; string intent = ""; bool firstTime = true;
|
|
public int ultimoIntent = 0, fixedUltimoIntent = 0, idclasses, idexercises;
|
|
public List<string> exercisePhrase; int actualExercise = 0;
|
|
|
|
public bool inAnime = false, endConversation = false;
|
|
|
|
bool inExercise = false, cansendAudio = true, clickedrecord = false;
|
|
bool exerciseFirst = true;
|
|
|
|
public Text exerciseExample;
|
|
public Button BotaoPularEtapa;
|
|
public Image BotaoMicrofone;
|
|
|
|
public BodyReq CorpoRequisicao = new BodyReq();
|
|
|
|
string exemploResposta; GameObject txtExemploResposta;
|
|
|
|
public bool Gravou = false;
|
|
WaitForEndOfFrame waitFrame = new WaitForEndOfFrame();
|
|
WaitForSecondsRealtime waitASecond = new WaitForSecondsRealtime(1);
|
|
|
|
int mode() {
|
|
if (inExercise)
|
|
{
|
|
return Convert.ToInt32(TipoConversacao.ExercicioPronuncia);
|
|
}
|
|
else
|
|
{
|
|
return Convert.ToInt32(TipoConversacao.ConversacaoDirigida);
|
|
}
|
|
}
|
|
|
|
int getIntentLocal() {
|
|
if (!inExercise)
|
|
{
|
|
return ultimoIntent;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int getConversationTree() {
|
|
if (!inExercise)
|
|
{
|
|
return conversationtree;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
string route = "18.228.91.135:3337";
|
|
|
|
private void Start()
|
|
{
|
|
route = ApiClass.instance.route;
|
|
|
|
GeneralScript.FaceObjects faceObjects = GeneralScript.instance.faceObjects;
|
|
|
|
transform.Find("Rosto").Find("Left_Eye").GetComponent<Image>().sprite = faceObjects.eyes;
|
|
transform.Find("Rosto").Find("Left_Eye").Find("Right_Eye").GetComponent<Image>().sprite = faceObjects.eyes;
|
|
transform.Find("Rosto").Find("Left_Eye")
|
|
.Find("Right_Eye").Find("Bochecha_Right").GetComponent<Image>().sprite = faceObjects.cheek;
|
|
transform.Find("Rosto").Find("Left_Eye").Find("Bochecha").GetComponent<Image>().sprite = faceObjects.cheek;
|
|
|
|
if (faceObjects.face != null)
|
|
{
|
|
Image imgface_01 = transform.Find("Rosto").Find("Mouth").Find("FeatRight").GetComponent<Image>();
|
|
imgface_01.sprite = faceObjects.face;
|
|
imgface_01.color = new Color(1, 1, 1, 1);
|
|
|
|
Image imgface_02 = transform.Find("Rosto").Find("Mouth").Find("FeatLeft").GetComponent<Image>();
|
|
imgface_02.sprite = faceObjects.face;
|
|
imgface_02.color = new Color(1, 1, 1, 1);
|
|
}
|
|
|
|
if (faceObjects.glasses != null)
|
|
{
|
|
Image imgGlasses = transform.Find("Eyeglass").GetComponent<Image>();
|
|
imgGlasses.sprite = faceObjects.glasses;
|
|
imgGlasses.color = new Color(1, 1, 1, 1);
|
|
}
|
|
}
|
|
|
|
public void fakeStart(bool teacher, bool inAnime = false, bool exercise = false)
|
|
{
|
|
inExercise = exercise;
|
|
|
|
token = PlayerPrefs.GetString("token");
|
|
|
|
faceAnime = GetComponentInParent<Animator>();
|
|
microAnime = GameObject.Find("Micro").GetComponent<Animator>();
|
|
|
|
BotaoMicrofone = GameObject.Find("Micro").GetComponent<Image>();
|
|
|
|
if (inExercise)
|
|
{
|
|
GameObject noInternetObj = GameObject.Find("txtError");
|
|
exerciseExample = noInternetObj.GetComponent<Text>();
|
|
exerciseExample.color = Color.black;
|
|
setExerciseExample();
|
|
}
|
|
else
|
|
{
|
|
GameObject noInternetObj = GameObject.Find("txtError");
|
|
exerciseExample = noInternetObj.GetComponent<Text>();
|
|
exerciseExample.color = Color.black;
|
|
exerciseExample.text = "";
|
|
txtExemploResposta = noInternetObj;
|
|
txtExemploResposta.SetActive(false);
|
|
}
|
|
|
|
GameObject btnAvancar = GameObject.Find("btnAvancar");
|
|
BotaoPularEtapa = btnAvancar.GetComponent<Button>();
|
|
BotaoPularEtapa.gameObject.SetActive(false);
|
|
BotaoPularEtapa.onClick.AddListener(ProximaFrase);
|
|
|
|
writeFile = GetComponent<WriteFile>();
|
|
|
|
//source = GameObject.Find("Audio Source").GetComponent<AudioSource>();
|
|
|
|
StartCoroutine(firstRequest());
|
|
}
|
|
|
|
void ProximaFrase()
|
|
{
|
|
actualExercise++;
|
|
endConversation = (inExercise) ? (actualExercise > (exercisePhrase.Count - 1)) : endConversation;
|
|
|
|
if (inExercise && !endConversation)
|
|
{
|
|
QuestionsController.QTotal++;
|
|
exerciseFirst = true;
|
|
setExerciseExample();
|
|
}
|
|
if (!endConversation)
|
|
{
|
|
CorpoRequisicao.Vezes = 0;
|
|
CorpoRequisicao.Ordem++;
|
|
StartCoroutine(firstRequest());
|
|
}
|
|
else
|
|
{
|
|
ExitConersation();
|
|
}
|
|
}
|
|
|
|
void setExerciseExample() {
|
|
if (actualExercise < exercisePhrase.Count)
|
|
{
|
|
try
|
|
{
|
|
if (CorpoRequisicao.Vezes > 4)
|
|
{
|
|
exerciseExample.text = "You said: " + respostaZoe.ZoeEntendeu + " - " + respostaZoe.TextoAudio;
|
|
}
|
|
else
|
|
{
|
|
exerciseExample.text = exercisePhrase[actualExercise].Split('<')[0];
|
|
}
|
|
exerciseExample.gameObject.SetActive(true);
|
|
}
|
|
catch
|
|
{
|
|
exerciseExample.text = exercisePhrase[actualExercise];
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator firstRequest()
|
|
{
|
|
StartRecording();
|
|
yield return new WaitForSecondsRealtime(.5f);
|
|
StopRecording(true);
|
|
}
|
|
|
|
public void GoBack(int i) {
|
|
if (i == 1)
|
|
{
|
|
SceneManager.LoadSceneAsync("SampleScene");
|
|
SceneManager.UnloadSceneAsync("FaceConversation");
|
|
}
|
|
else {
|
|
outObj.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void OpenBack() {
|
|
if (noInternet)
|
|
{
|
|
GoBack(1);
|
|
}
|
|
else
|
|
{
|
|
outObj.SetActive(true);
|
|
}
|
|
}
|
|
|
|
IEnumerator LoadAudioCor() {
|
|
yield return StartCoroutine(writeFile.loadAudio());
|
|
|
|
source.clip = writeFile.audioLoaded;
|
|
source.Play();
|
|
source.clip = null;
|
|
}
|
|
|
|
public void StartRecording() {
|
|
Debug.Log("Aqui!!!");
|
|
if (cansendAudio)
|
|
{
|
|
Debug.Log("PODE ENVIAR O AUDIO");
|
|
clickedrecord = true; // Debug.Log("Testefef");
|
|
StartCoroutine(syncCoroutineForRecord());
|
|
}
|
|
}
|
|
|
|
public void StopRecording(bool First = false) {
|
|
if (cansendAudio && clickedrecord)
|
|
{
|
|
clickedrecord = false;
|
|
ResetaLegenda();
|
|
VezesRepetiu = 0;
|
|
StartCoroutine(stopNGoBack());
|
|
if (!First)
|
|
Gravou = true;
|
|
}
|
|
}
|
|
|
|
IEnumerator stopNGoBack(float Tempo = 2f) {
|
|
end = true;
|
|
cansendAudio = false;
|
|
yield return new WaitForSecondsRealtime(Tempo);
|
|
end = false;
|
|
}
|
|
|
|
public IEnumerator syncCoroutineForRecord()
|
|
{
|
|
//source = GameObject.Find("Audio Source").GetComponent<AudioSource>();
|
|
if (source.clip == null)
|
|
{
|
|
yield return StartCoroutine(record());
|
|
Debug.Log("GRAVOU");
|
|
microAnime.SetBool("Loading", true);
|
|
yield return waitFrame;
|
|
yield return StartCoroutine(SendAudio());
|
|
Debug.Log("ENVIOU!");
|
|
|
|
|
|
microAnime.SetBool("Loading", false);
|
|
}
|
|
|
|
while (Application.internetReachability == NetworkReachability.NotReachable)
|
|
{
|
|
exerciseExample.text = "No access from internet, seacrching network...";
|
|
|
|
yield return waitFrame;
|
|
}
|
|
|
|
exerciseExample.text = "";
|
|
|
|
if (VezesErro == 0)
|
|
{
|
|
yield return new WaitForSecondsRealtime(0.3f);
|
|
BotaoMicrofone.gameObject.SetActive(false);
|
|
|
|
if (inExercise && exerciseFirst)
|
|
{
|
|
cansendAudio = true;
|
|
|
|
StartCoroutine(firstRequest());
|
|
}
|
|
else
|
|
{
|
|
if (!inExercise && !LegendaSetada)
|
|
{
|
|
if (respostaZoe.Legenda == 0)
|
|
ResetaLegenda();
|
|
else if (respostaZoe.Legenda == 1)
|
|
SetarLegenda(respostaZoe.ExemploResposta);
|
|
else if (respostaZoe.Legenda == 2)
|
|
SetarLegenda(respostaZoe.TextoAudio);
|
|
}
|
|
|
|
source.Play();
|
|
|
|
faceAnime.SetBool("Talking", true);
|
|
yield return new WaitForSecondsRealtime(clip.length - ((respostaZoe.ProximoZoeTalking == 1) ? 0 : .5f));
|
|
faceAnime.SetBool("Talking", false);
|
|
yield return new WaitForSecondsRealtime((respostaZoe.ProximoZoeTalking == 1) ? 0 : 1);
|
|
source.clip = null;
|
|
cansendAudio = true;
|
|
}
|
|
|
|
if (endConversation)
|
|
{
|
|
ExitConersation();
|
|
Gravou = false;
|
|
}
|
|
else
|
|
{
|
|
if (respostaZoe.ProximoZoeTalking == 1)
|
|
{
|
|
StartCoroutine(stopNGoBack());
|
|
StartCoroutine(syncCoroutineForRecord());
|
|
}
|
|
else
|
|
{
|
|
// destrava botão que grava
|
|
BotaoMicrofone.gameObject.SetActive(true);
|
|
//BotaoMicrofone.color = Color.white;
|
|
|
|
StartCoroutine(ContadorDeTempo(7));
|
|
}
|
|
|
|
Gravou = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (VezesErro < 2)
|
|
{
|
|
StartCoroutine(stopNGoBack());
|
|
StartCoroutine(syncCoroutineForRecord());
|
|
}
|
|
else
|
|
{
|
|
//ErrorFind(Erro);
|
|
SetarLegenda("Reconnecting to server...");
|
|
//ExitConersation();
|
|
ResetaErro();
|
|
|
|
yield return StartCoroutine(syncCoroutineForRecord());
|
|
//cansendAudio = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
int VezesRepetiu = 0;
|
|
public IEnumerator ContadorDeTempo(int Tempo)
|
|
{
|
|
int Seconds = 0;
|
|
while (Seconds < Tempo && !clickedrecord)
|
|
{
|
|
yield return waitASecond;
|
|
Seconds++;
|
|
}
|
|
if (!clickedrecord)
|
|
{
|
|
if (VezesRepetiu < 2)
|
|
{
|
|
VezesRepetiu++;
|
|
source.clip = clip;
|
|
source.Play();
|
|
faceAnime.SetBool("Talking", true);
|
|
yield return new WaitForSecondsRealtime(clip.length - ((respostaZoe.ProximoZoeTalking == 1) ? 0 : .5f));
|
|
faceAnime.SetBool("Talking", false);
|
|
yield return new WaitForSecondsRealtime((respostaZoe.ProximoZoeTalking == 1) ? 0 : 1);
|
|
source.clip = null;
|
|
cansendAudio = true;
|
|
StartCoroutine(ContadorDeTempo(7));
|
|
}
|
|
else
|
|
{
|
|
exerciseExample.text = respostaZoe.ExemploResposta;
|
|
exerciseExample.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetarLegenda(string Texto)
|
|
{
|
|
exerciseExample.text = Texto;
|
|
exerciseExample.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void ResetaLegenda()
|
|
{
|
|
exerciseExample.text = "";
|
|
exerciseExample.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void ResetaErro()
|
|
{
|
|
VezesErro = 0;
|
|
Erro = "";
|
|
}
|
|
|
|
IEnumerator fakeAudioSend(string texto) {
|
|
string path = Application.persistentDataPath + "/tempAudio.wav";
|
|
|
|
WWWForm form = new WWWForm();
|
|
CorpoRequisicao.Modo = (inExercise) ? Convert.ToInt32(TipoConversacao.ExercicioPronuncia) : Convert.ToInt32(TipoConversacao.ConversacaoDirigida);
|
|
CorpoRequisicao.Vezes = 0;
|
|
CorpoRequisicao.Ordem = 0;
|
|
CorpoRequisicao.Matricula = Convert.ToInt32(ReadingFile.idmatricula);
|
|
CorpoRequisicao.IDUnidade = Convert.ToInt32(ReadingFile.idUnit);
|
|
CorpoRequisicao.Arquivo = writeFile.getAudioFile("file://" + path).bytes;
|
|
CorpoRequisicao.Mensagem = texto;
|
|
|
|
form.AddField("Modo", CorpoRequisicao.Modo);
|
|
form.AddField("Vezes", CorpoRequisicao.Vezes);
|
|
form.AddField("ChatTimeID", CorpoRequisicao.ChatTimeID);
|
|
form.AddField("Ordem", CorpoRequisicao.Ordem);
|
|
form.AddField("Matricula", CorpoRequisicao.Matricula);
|
|
form.AddField("IDUnidade", CorpoRequisicao.IDUnidade);
|
|
form.AddField("IDAula", CorpoRequisicao.IDAula);
|
|
form.AddField("IDExercicio", CorpoRequisicao.IDExercicio);
|
|
form.AddBinaryData("Arquivo", CorpoRequisicao.Arquivo);
|
|
form.AddField("Mensagem", CorpoRequisicao.Mensagem);
|
|
|
|
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);
|
|
|
|
ErrorFind(www.error);
|
|
|
|
}
|
|
else
|
|
{
|
|
if (source == null)
|
|
source = GameObject.Find("Audio Source").GetComponent<AudioSource>();
|
|
|
|
string Json = www.downloadHandler.text;
|
|
Answer answer = JsonUtility.FromJson<Answer>(Json);
|
|
Debug.Log(Json);
|
|
|
|
if (answer.status == 200)
|
|
{
|
|
StartCoroutine(SetImage(answer.img));
|
|
|
|
using (var wwwclip = new WWW(FormatAudio(answer.url_audio)))
|
|
{
|
|
yield return wwwclip;
|
|
clip = wwwclip.GetAudioClip();
|
|
clip.name = "Answer";
|
|
source.clip = clip;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int VezesErro = 0;
|
|
string Erro = "";
|
|
bool LegendaSetada = false;
|
|
IEnumerator SendAudio()
|
|
{
|
|
Debug.Log("Send Audio Called");
|
|
string path = Application.persistentDataPath + "/tempAudio.wav";
|
|
bool existPath = false;
|
|
while (!existPath)
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
yield return waitFrame;
|
|
}
|
|
else
|
|
{
|
|
existPath = true;
|
|
}
|
|
}
|
|
|
|
WWWForm form = new WWWForm();
|
|
CorpoRequisicao.Modo = (inExercise) ? Convert.ToInt32(TipoConversacao.ExercicioPronuncia) : Convert.ToInt32(TipoConversacao.ConversacaoDirigida);
|
|
CorpoRequisicao.Arquivo = writeFile.getAudioFile("file://" + path).bytes;
|
|
|
|
if (inExercise)
|
|
{
|
|
if (exerciseFirst)
|
|
{
|
|
string Frase = exercisePhrase[actualExercise];
|
|
try { Frase = exercisePhrase[actualExercise].Split('<')[0]; } catch { }
|
|
CorpoRequisicao.Mensagem = Frase;
|
|
}
|
|
else
|
|
{
|
|
CorpoRequisicao.Mensagem = "";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (firstTime)
|
|
{
|
|
CorpoRequisicao.Mensagem = intent;
|
|
}
|
|
else
|
|
{
|
|
CorpoRequisicao.Mensagem = "";
|
|
}
|
|
}
|
|
|
|
form.AddField("Modo", CorpoRequisicao.Modo);
|
|
form.AddField("Seq", CorpoRequisicao.Seq);
|
|
form.AddField("Vezes", CorpoRequisicao.Vezes);
|
|
form.AddField("ChatTimeID", CorpoRequisicao.ChatTimeID);
|
|
form.AddField("Ordem", CorpoRequisicao.Ordem);
|
|
form.AddField("Matricula", CorpoRequisicao.Matricula);
|
|
form.AddField("IDUnidade", CorpoRequisicao.IDUnidade);
|
|
form.AddField("IDAula", CorpoRequisicao.IDAula);
|
|
form.AddField("IDExercicio", CorpoRequisicao.IDExercicio);
|
|
form.AddBinaryData("Arquivo", CorpoRequisicao.Arquivo);
|
|
form.AddField("Mensagem", CorpoRequisicao.Mensagem);
|
|
|
|
string URL = route + "/api/chattime/recognizeAudio/asd123dsa654w789";
|
|
using (UnityWebRequest www = UnityWebRequest.Post(URL, form))
|
|
{
|
|
www.timeout = 10;
|
|
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);
|
|
VezesErro++;
|
|
Erro = www.error;
|
|
}
|
|
else
|
|
{
|
|
ResetaErro();
|
|
|
|
if (source == null)
|
|
source = GameObject.Find("Audio Source").GetComponent<AudioSource>();
|
|
|
|
string Json = www.downloadHandler.text;
|
|
|
|
Answer answer = JsonUtility.FromJson<Answer>(Json);
|
|
respostaZoe = answer;
|
|
|
|
if (answer.status == 200)
|
|
{
|
|
CorpoRequisicao.Vezes = answer.Vezes;
|
|
CorpoRequisicao.Ordem = answer.Ordem;
|
|
CorpoRequisicao.Seq = answer.Seq;
|
|
exemploResposta = answer.ExemploResposta;
|
|
endConversation = answer.Final;
|
|
|
|
exerciseExample.text = exemploResposta;
|
|
|
|
if (CorpoRequisicao.Vezes > 4)
|
|
{
|
|
BotaoPularEtapa.gameObject.SetActive(true);
|
|
if (!inExercise) {
|
|
txtExemploResposta.SetActive(true);
|
|
}
|
|
exerciseExample.text = "You said: " + answer.ZoeEntendeu + " - " + answer.TextoAudio;
|
|
LegendaSetada = true;
|
|
}
|
|
else if (CorpoRequisicao.Vezes > 2 && !inExercise)
|
|
{
|
|
exemploResposta = answer.TextoAudio;
|
|
exerciseExample.gameObject.SetActive(true);
|
|
LegendaSetada = true;
|
|
}
|
|
else if (CorpoRequisicao.Vezes == 0 || endConversation)
|
|
{
|
|
if (!inExercise)
|
|
{
|
|
exerciseExample.gameObject.SetActive(false);
|
|
LegendaSetada = false;
|
|
}
|
|
BotaoPularEtapa.gameObject.SetActive(false);
|
|
|
|
StartCoroutine(SetImage(answer.img));
|
|
}
|
|
|
|
if (firstTime)
|
|
{
|
|
fixedUltimoIntent = ultimoIntent;
|
|
firstTime = false;
|
|
}
|
|
|
|
using (var wwwclip = new WWW(FormatAudio(answer.url_audio)))
|
|
{
|
|
yield return wwwclip;
|
|
clip = wwwclip.GetAudioClip(false, false, AudioType.WAV);
|
|
clip.name = "Answer";
|
|
source.clip = clip;
|
|
}
|
|
|
|
if (inExercise)
|
|
{
|
|
if (answer.Validou && !exerciseFirst)
|
|
{
|
|
actualExercise++;
|
|
QuestionsController.QAcertos++;
|
|
QuestionsController.QTotal++;
|
|
|
|
StartCoroutine(SetImage(answer.img));
|
|
}
|
|
else if (exerciseFirst)
|
|
{
|
|
exerciseFirst = false;
|
|
}
|
|
setExerciseExample();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
string FormatAudio(string urlAudio)
|
|
{
|
|
Debug.Log("Reformated Audio");
|
|
string urlDoAudios = urlAudio;
|
|
//urlDoAudios = urlDoAudios.Replace("?", "%3f");
|
|
|
|
Debug.Log(urlDoAudios);
|
|
|
|
return urlDoAudios;
|
|
}
|
|
|
|
void ExitConersation()
|
|
{
|
|
if (inAnime)
|
|
{
|
|
AnimeControl thisAnime = GetComponentInParent<AnimeControl>();
|
|
thisAnime.endConversation();
|
|
}
|
|
else
|
|
{
|
|
Destroy(transform.Find("ImgShow").gameObject);
|
|
GeneralScript general = GeneralScript.instance;
|
|
general.LiberarAvanco(true);
|
|
general.Avancar();
|
|
}
|
|
}
|
|
|
|
IEnumerator SetImage(string url)
|
|
{
|
|
if (url.Length > 0)
|
|
{
|
|
faceAnime.SetBool("img", true);
|
|
|
|
Image imgShow = GameObject.Find("ImgQuestion").GetComponent<Image>();
|
|
WWW wwwImg = new WWW(url);
|
|
|
|
yield return wwwImg;
|
|
|
|
imgShow.sprite = Sprite.Create(wwwImg.texture, new Rect(0, 0,
|
|
wwwImg.texture.width, wwwImg.texture.height), new Vector2(0, 0));
|
|
}
|
|
else
|
|
{
|
|
faceAnime.SetBool("img", false);
|
|
}
|
|
}
|
|
|
|
IEnumerator getIntent() {
|
|
string url = route + "/api/alexa/getFirstIntentofTree/" + conversationtree;
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
www.certificateHandler = new BypassCertificate();
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("token", Uri.EscapeDataString(token));
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
Debug.Log(www.downloadHandler.text);
|
|
|
|
if (www.isHttpError || www.isNetworkError)
|
|
{
|
|
Debug.LogError(www.error);
|
|
}
|
|
else
|
|
{
|
|
string Json = www.downloadHandler.text;
|
|
Intent thisIntent = JsonUtility.FromJson<Intent>(Json);
|
|
|
|
if (thisIntent.status == 200)
|
|
{
|
|
intent = thisIntent.response[0].reqIntent;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ErrorFind(string textToPass)
|
|
{
|
|
Text txt = GameObject.Find("txtError").GetComponent<Text>();
|
|
txt.gameObject.SetActive(true);
|
|
txt.text = textToPass;
|
|
}
|
|
|
|
IEnumerator record()
|
|
{
|
|
end = false;
|
|
|
|
int micToUse = ReadingFile.instance.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 && clickedrecord)
|
|
{
|
|
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 waitFrame;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
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);
|
|
}
|
|
|
|
/*AudioSource audio = gameObject.AddComponent<AudioSource>();
|
|
audio.clip = clipMicro;
|
|
audio.Play();
|
|
|
|
yield return new WaitForSecondsRealtime(5);
|
|
|
|
Destroy(audio);*/
|
|
|
|
if (File.Exists(Application.persistentDataPath + "/tempAudio.wav"))
|
|
{
|
|
File.Delete(Application.persistentDataPath + "/tempAudio.wav");
|
|
}
|
|
|
|
writeFile.CreateAudio(clipMicro);
|
|
//yield return new WaitForSecondsRealtime(5);
|
|
yield break;
|
|
}
|
|
|
|
}
|