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

Implementing captions in audio augmentation #2132

Open
wants to merge 1 commit into
base: develop
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
82 changes: 80 additions & 2 deletions Assets/MirageXR/ContentTypes/Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections;
using UnityEngine;

using TMPro;
using System;
using System.Linq;
namespace MirageXR
{
public class AudioPlayer : MirageXRPrefab
Expand All @@ -21,6 +23,9 @@ public class AudioPlayer : MirageXRPrefab

[SerializeField] private GameObject icon;
[SerializeField] private Sprite iconSprite;

[SerializeField] private TMP_Text _captionText;
[SerializeField] private GameObject _captionObj;
public Sprite IconSprite => iconSprite;

[SerializeField] private Sprite pauseIcon;
Expand All @@ -38,6 +43,8 @@ public DialogRecorder DialogRecorderPanel
private bool isReady = false;
private bool isPlaying = false;

private bool _hasCaption = false;

private LearningExperienceEngine.ToggleObject _obj;

public LearningExperienceEngine.ToggleObject MyAnnotation => _obj;
Expand Down Expand Up @@ -110,11 +117,71 @@ public override bool Init(LearningExperienceEngine.ToggleObject obj)
audioName = obj.url;
CreateAudioPlayer(true, audio3dMode, radius, Loop);
}

var caption = obj.caption;
//Checking if caption exists then it should be displayed otherwise deactivate the caption object
_hasCaption = !string.IsNullOrEmpty(caption);
if (_hasCaption)
{
StartCaptionDisplay(caption);
}
else
{
_captionObj.SetActive(false);
}
// 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 @@ -160,6 +227,11 @@ public void PlayAudio()
}
audioSource.mute = false;
audioSource.volume = 1.0f;
//To display caption when audio plays if there are captions
if (_hasCaption)
{
_captionObj.SetActive(true);
}
audioSource.Play();
isPlaying = true;

Expand Down Expand Up @@ -242,11 +314,17 @@ public void StopAudio()
{
audioSource.Stop();
isPlaying = false;
//When audio stops, caption disappears
if (_hasCaption)
{
_captionObj.SetActive(false);
}
}
}
}



/// <summary>
/// Set the volume of the audio
/// </summary>
Expand Down
Loading