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

Generate audio caption #1935

Merged
merged 15 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 72 additions & 13 deletions Assets/MirageXR/ContentTypes/Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections;
using System;
using System.Collections;
using System.Linq;
using TMPro;
using UnityEngine;

namespace MirageXR
Expand All @@ -18,22 +21,17 @@ public class AudioPlayer : MirageXRPrefab
private bool audio3dMode;
public bool Loop { get; private set; }


[SerializeField] private GameObject icon;
[SerializeField] private Sprite iconSprite;
public Sprite IconSprite => iconSprite;

[SerializeField] private TMP_Text _captionText;
[SerializeField] private GameObject _captionObj;
[SerializeField] private Sprite pauseIcon;

[SerializeField] private SpriteRenderer iconImage;

public Sprite IconSprite => iconSprite;
public SpriteRenderer IconImage => iconImage;

public string AudioSpatialType { get; private set; }

public DialogRecorder DialogRecorderPanel
{
get; set;
}
public DialogRecorder DialogRecorderPanel { get; set; }

private bool isReady = false;
private bool isPlaying = false;
Expand Down Expand Up @@ -118,10 +116,69 @@ public override bool Init(ToggleObject obj)
CreateAudioPlayer(true, audio3dMode, radius, Loop);
}

var caption = obj.caption;
if (caption != string.Empty)
{
StartCaptionDisplay(caption);
}

// If all went well, return true.
return true;
}

private void StartCaptionDisplay(string caption)
{
StartCoroutine(DisplayCaptionWithDelay(caption));
}

private IEnumerator DisplayCaptionWithDelay(string fullCaption)
{
// Split the full caption into words
string[] words = fullCaption.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// number of words after split
int check = words.Length;

// Determine the number of words to display per section
int numberOfWords = 12;

if (check <= numberOfWords)
{
// If the total number of words is less than or equal to numberOfWords, display all at once
string allWords = string.Join(" ", words);
_captionText.text = allWords.Trim();
_captionObj.SetActive(true);

// Wait for a time before hiding the caption object
yield return new WaitForSeconds(4);
_captionObj.SetActive(false);
}
else
{
// Calculate the number of sections
int numberOfSections = (int)Math.Ceiling((double)check / numberOfWords);

for (int i = 0; i < numberOfSections; i++)
{
// Get the words for the current section
string[] sectionWords = words.Skip(i * numberOfWords).Take(numberOfWords).ToArray();

// Join the words back into a string
string sectionText = string.Join(" ", sectionWords);

// Display the text section
_captionText.text = sectionText.Trim();
_captionObj.SetActive(true);

// Wait for 4 seconds before moving to the next section
yield return new WaitForSeconds(4);
}

// hide the caption object after all sections have been displayed
_captionObj.SetActive(false);
}
}



private void Update()
{
Expand Down Expand Up @@ -167,9 +224,10 @@ public void PlayAudio()
}
audioSource.mute = false;
audioSource.volume = 1.0f;
_captionObj.SetActive(true);
audioSource.Play();
isPlaying = true;

audioLength = audioSource.clip.length;
var myTrigger = activityManager.ActiveAction.triggers.Find(t => t.id == _obj.poi);
if (myTrigger != null)
Expand Down Expand Up @@ -451,5 +509,6 @@ public float getCurrenttime()
AudioSource audioSource = gameObject.GetComponent<AudioSource>();
return audioSource.time;
}

}
}
}