Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text audio response #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mevaterse_Classroom_2/Assets/Resources/Student.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -2187,7 +2187,7 @@ MonoBehaviour:
personality: 0
intelligence: 0
interest: 0
happyness: 0
happiness: 0
--- !u!82 &-4635132013586665566
AudioSource:
m_ObjectHideFlags: 0
Expand Down
4 changes: 4 additions & 0 deletions Mevaterse_Classroom_2/Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ private void OnTriggerExit(Collider collision)

private void InteractionInfoUpdate()
{
if (interactionInfo == null){
interactionInfo = GameObject.Find("InteractionInfo").GetComponent<TMP_Text>();
}

if (chair != null && !isSitting && !chair.GetComponent<ChairController>().IsBusy())
interactionInfo.text = "Press C to sit";

Expand Down
143 changes: 81 additions & 62 deletions Mevaterse_Classroom_2/Assets/Scripts/QuestionDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class RequestData
public int personality;
public int intelligence;
public int interest;
public int happyness;
public int happiness;
public string audio;

public RequestData(string subject, int personality, int intelligence, int interest, int happyness, string audio)
public RequestData(string subject, int personality, int intelligence, int interest, int happiness, string audio)
{
this.subject = subject;
this.personality = personality;
this.intelligence = intelligence;
this.interest = interest;
this.happyness = happyness;
this.happiness = happiness;
this.audio = audio;
}
}
Expand All @@ -37,12 +37,12 @@ void Start()
studentHandler = GameObject.Find("StudentHandler").GetComponent<StudentHandler>();
}

public void AddQuestionRequest(AudioClip audioClip, SmartStudentController studentController, Personality personality, Intelligence intelligence, Interest interest, Happyness happyness)
public void AddQuestionRequest(AudioClip audioClip, SmartStudentController studentController, Personality personality, Intelligence intelligence, Interest interest, Happiness happiness)
{
StartCoroutine(SendAudioToServer(audioClip, studentController, personality, intelligence, interest, happyness));
StartCoroutine(SendAudioToServer(audioClip, studentController, personality, intelligence, interest, happiness));
}

private IEnumerator SendAudioToServer(AudioClip audioClip, SmartStudentController studentController, Personality personality, Intelligence intelligence, Interest interest, Happyness happyness)
private IEnumerator SendAudioToServer(AudioClip audioClip, SmartStudentController studentController, Personality personality, Intelligence intelligence, Interest interest, Happiness happiness)
{
byte[] audioData = ConvertAudioClipToWav(audioClip);

Expand All @@ -51,7 +51,7 @@ private IEnumerator SendAudioToServer(AudioClip audioClip, SmartStudentControlle
(int)personality,
(int)intelligence,
(int)interest,
(int)happyness,
(int)happiness,
Convert.ToBase64String(audioData)
);

Expand All @@ -65,80 +65,88 @@ private IEnumerator SendAudioToServer(AudioClip audioClip, SmartStudentControlle
};
www.SetRequestHeader("Content-Type", "application/json");

yield return www.SendWebRequest();
using(www){
yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Errore nella richiesta al server: {www.error}");
yield break;
}

string jsonResponse = www.downloadHandler.text;
Debug.Log("Risposta JSON ricevuta (task ID): " + jsonResponse);
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Errore nella richiesta al server: {www.error}");
yield break;
}

var taskResponse = JsonUtility.FromJson<TaskResponse>(jsonResponse);
string taskId = taskResponse.task_id;
string jsonResponse = www.downloadHandler.text;
Debug.Log("Risposta JSON ricevuta (task ID): " + jsonResponse);

bool isCompleted = false;
string audioBase64 = null;
string answerText = "prova";
var taskResponse = JsonUtility.FromJson<TaskResponse>(jsonResponse);
string taskId = taskResponse.task_id;

while (!isCompleted)
{
var statusRequest = UnityWebRequest.Get($"http://127.0.0.1:5000/result/{taskId}");
yield return statusRequest.SendWebRequest();
bool isCompleted = false;
string audioBase64 = null;
string answerText = null;

if (statusRequest.result == UnityWebRequest.Result.Success)
while (!isCompleted)
{
var statusResponse = JsonUtility.FromJson<TaskStatusResponse>(statusRequest.downloadHandler.text);
var statusRequest = UnityWebRequest.Get($"http://127.0.0.1:5000/result/{taskId}");
yield return statusRequest.SendWebRequest();

if (statusResponse.status == "completed")
if (statusRequest.result == UnityWebRequest.Result.Success)
{
audioBase64 = statusResponse.audio;
//answerText = statusResponse.text;
isCompleted = true;
var statusResponse = JsonUtility.FromJson<TaskStatusResponse>(statusRequest.downloadHandler.text);

if (statusResponse.status == "completed")
{
audioBase64 = statusResponse.audio;
answerText = statusResponse.text;
isCompleted = true;
}
else
{
Debug.Log("Task ancora in corso, attendo...");
yield return new WaitForSeconds(1);
}
}
else
{
Debug.Log("Task ancora in corso, attendo...");
yield return new WaitForSeconds(1);
Debug.LogError($"Errore nella richiesta di stato del task: {statusRequest.error}");
yield break;
}
}
else
{
Debug.LogError($"Errore nella richiesta di stato del task: {statusRequest.error}");
yield break;
}
}

if (!string.IsNullOrEmpty(audioBase64))
{
Debug.Log("Audio Base64 ricevuto: " + audioBase64);
AudioClip responseAudioClip = ConvertBase64ToAudioClip(audioBase64);
if (responseAudioClip != null)
if (!string.IsNullOrEmpty(audioBase64))
{
studentHandler.AddAudioToQueue(responseAudioClip, studentController);
studentHandler.AddTextToQueue(answerText, studentController);
Debug.Log("Audio Base64 ricevuto: " + audioBase64);
StartCoroutine(ConvertBase64ToAudioClip(audioBase64, audioClip =>
{
if (audioClip != null)
{
studentHandler.AddAudioToQueue(audioClip, studentController);
studentHandler.AddTextToQueue(answerText, studentController);
}
else
{
Debug.LogError("Errore nella conversione dell'audio in AudioClip.");
}
}));

}
else
{
Debug.LogError("Errore nella conversione dell'audio in AudioClip.");
Debug.LogError("Risposta audio non valida o mancante.");
}
}
else
{
Debug.LogError("Risposta audio non valida o mancante.");
}
}

private AudioClip ConvertBase64ToAudioClip(string audioBase64)
// Converted to coroutine to handle the asynchronous conversion with UnityWebRequest
private IEnumerator ConvertBase64ToAudioClip(string audioBase64, Action<AudioClip> callback)
{
if (string.IsNullOrEmpty(audioBase64))
{
Debug.LogError("Base64 audio data is null or empty.");
return null;
callback(null);
yield break;
}

UnityWebRequest www = null;

try
{
byte[] audioData = Convert.FromBase64String(audioBase64);
Expand All @@ -149,20 +157,31 @@ private AudioClip ConvertBase64ToAudioClip(string audioBase64)
File.WriteAllBytes(tempFilePath, audioData);

// Load the audio file as an AudioClip
WWW www = new WWW("file://" + tempFilePath);
while (!www.isDone) { }

AudioClip audioClip = www.GetAudioClip(false, false);
return audioClip;
www = UnityWebRequestMultimedia.GetAudioClip("file://" + tempFilePath, AudioType.WAV);
}
catch (Exception ex)
{
Debug.LogError("Errore nella conversione dell'audio Base64 in AudioClip: " + ex.Message);
return null;
callback(null);
}

using(www)
{
yield return www.SendWebRequest();

if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError(www.error);
callback(null);
yield break;
}

AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
callback(audioClip);
}
}

// Funzione per convertire l�audio in formato WAV
// Funzione per convertire l�audio in formato WAV
private byte[] ConvertAudioClipToWav(AudioClip clip)
{
using (MemoryStream stream = new MemoryStream())
Expand Down Expand Up @@ -215,6 +234,6 @@ private class TaskStatusResponse
{
public string status;
public string audio;
//public string text;
public string text;
}
}
15 changes: 10 additions & 5 deletions Mevaterse_Classroom_2/Assets/Scripts/SmartStudentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class SmartStudentController : MonoBehaviourPun
private GameObject volumeIcon;
private PhotonView textChatView;

// Tratti di personalit� per ogni studente
// Tratti di personalit� per ogni studente
public Personality personality;
public Intelligence intelligence;
public Interest interest;
public Happyness happyness;
public Happiness happiness;

private float questionTriggerProbability;
private QuestionDispatcher questionDispatcher;
Expand Down Expand Up @@ -60,7 +60,7 @@ private float CalculateQuestionProbability()
{
float baseProbability = 0.1f;
baseProbability += (float)interest / 10f; // Aumenta in base all'interesse
baseProbability += (float)personality / 20f; // Aumenta in base alla personalit�
baseProbability += (float)personality / 20f; // Aumenta in base alla personalit�
return Mathf.Clamp01(baseProbability); // Limita tra 0 e 1
}

Expand All @@ -75,7 +75,7 @@ public void EvaluateAudioForQuestion(AudioClip audioClip)
{
if (Roll())
{
questionDispatcher.AddQuestionRequest(audioClip, this, personality, intelligence, interest, happyness);
questionDispatcher.AddQuestionRequest(audioClip, this, personality, intelligence, interest, happiness);
}
}

Expand All @@ -93,8 +93,10 @@ public void EnqueueTextResponse(string responseText)
// Metodo modificato per riprodurre solo quando chiamato da StudentHandler
public void TriggerPlayNextAudio()
{
PlayNextAudio();
// This order is important to ensure the text will be sent first
// Because it checks if the audio is playing before sending the next question
SendNextQuestion();
PlayNextAudio();
}

private void PlayNextAudio()
Expand All @@ -112,9 +114,12 @@ private void PlayNextAudio()

private void SendNextQuestion()
{

if (!question.isPlaying && stringQueue.Count > 0)
{
Debug.Log("Sending next question");
string questionText = stringQueue.Dequeue();
Debug.Log("Question: " + questionText);
photonView.RPC("WriteQuestionInChat", RpcTarget.All, questionText);
}
}
Expand Down
22 changes: 11 additions & 11 deletions Mevaterse_Classroom_2/Assets/Scripts/SpawnStudents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public void InstantiateStudent(int studentNumber)
Personality randomPersonality = (Personality)UnityEngine.Random.Range(1, System.Enum.GetValues(typeof(Personality)).Length + 1);
Intelligence randomIntelligence = (Intelligence)UnityEngine.Random.Range(1, System.Enum.GetValues(typeof(Intelligence)).Length + 1);
Interest randomInterest = (Interest)UnityEngine.Random.Range(1, System.Enum.GetValues(typeof(Interest)).Length + 1);
Happyness randomHappyness = (Happyness)UnityEngine.Random.Range(1, System.Enum.GetValues(typeof(Happyness)).Length + 1);
Happiness randomHappiness = (Happiness)UnityEngine.Random.Range(1, System.Enum.GetValues(typeof(Happiness)).Length + 1);

//Logger.Instance.LogInfo($"Spawned a {randomPersonality}, {randomIntelligence}, {randomInterest}, {randomHappyness} student!");
//Logger.Instance.LogInfo($"Spawned a {randomPersonality}, {randomIntelligence}, {randomInterest}, {randomHappiness} student!");

// Instanzia lo studente nella posizione specificata e con tratti casuali
GameObject student = PhotonNetwork.Instantiate("Student", spawnPosition, Quaternion.identity);
studentList.Add(new Student(spawnPosition, randomPersonality, randomIntelligence, randomInterest, randomHappyness));
studentList.Add(new Student(spawnPosition, randomPersonality, randomIntelligence, randomInterest, randomHappiness));

if (student == null)
{
Expand All @@ -97,14 +97,14 @@ public void InstantiateStudent(int studentNumber)
Animator studentAnimator = student.GetComponent<Animator>();
studentAnimator.Play("Idle");

// Imposta i tratti di personalit� sul componente SmartStudentController
// Imposta i tratti di personalit� sul componente SmartStudentController
SmartStudentController studentController = student.GetComponent<SmartStudentController>();
if (studentController != null)
{
studentController.personality = randomPersonality;
studentController.intelligence = randomIntelligence;
studentController.interest = randomInterest;
studentController.happyness = randomHappyness;
studentController.happiness = randomHappiness;
}

// Aggiorna la lista di studenti condivisa in rete
Expand All @@ -128,35 +128,35 @@ public void SetStudentData(string studentString)
{
GameObject studentObject = PhotonNetwork.Instantiate("Student", student.studentPosition, Quaternion.identity);

// Imposta i tratti di personalit� su SmartStudentController
// Imposta i tratti di personalit� su SmartStudentController
SmartStudentController studentController = studentObject.GetComponent<SmartStudentController>();
if (studentController != null)
{
studentController.personality = student.personality;
studentController.intelligence = student.intelligence;
studentController.interest = student.interest;
studentController.happyness = student.happyness;
studentController.happiness = student.happiness;
}
}
}
}

// Classe per rappresentare uno studente con posizione e tratti di personalit�
// Classe per rappresentare uno studente con posizione e tratti di personalit�
[System.Serializable]
class Student
{
public Vector3 studentPosition;
public Personality personality;
public Intelligence intelligence;
public Interest interest;
public Happyness happyness;
public Happiness happiness;

public Student(Vector3 position, Personality personalityType, Intelligence intelligenceLevel, Interest interestLevel, Happyness happynessLevel)
public Student(Vector3 position, Personality personalityType, Intelligence intelligenceLevel, Interest interestLevel, Happiness happinessLevel)
{
studentPosition = position;
personality = personalityType;
intelligence = intelligenceLevel;
interest = interestLevel;
happyness = happynessLevel;
happiness = happinessLevel;
}
}
Loading