InteractionHandler.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using Meta.WitAi;
  21. using Meta.WitAi.Json;
  22. using Meta.WitAi.Requests;
  23. using UnityEngine;
  24. using UnityEngine.UI;
  25. namespace Oculus.Voice.Demo
  26. {
  27. public class InteractionHandler : MonoBehaviour
  28. {
  29. [Header("Default States"), Multiline]
  30. [SerializeField] private string freshStateText = "Try pressing the Activate button and saying \"Make the cube red\"";
  31. [Header("UI")]
  32. [SerializeField] private Text textArea;
  33. [SerializeField] private bool showJson;
  34. [Header("Voice")]
  35. [SerializeField] private AppVoiceExperience appVoiceExperience;
  36. // Whether voice is activated
  37. public bool IsActive => _active;
  38. private bool _active = false;
  39. // Add delegates
  40. private void OnEnable()
  41. {
  42. textArea.text = freshStateText;
  43. appVoiceExperience.VoiceEvents.OnSend.AddListener(OnSend);
  44. appVoiceExperience.VoiceEvents.OnPartialTranscription.AddListener(OnRequestTranscript);
  45. appVoiceExperience.VoiceEvents.OnFullTranscription.AddListener(OnRequestTranscript);
  46. appVoiceExperience.VoiceEvents.OnStartListening.AddListener(OnListenStart);
  47. appVoiceExperience.VoiceEvents.OnStoppedListening.AddListener(OnListenStop);
  48. appVoiceExperience.VoiceEvents.OnStoppedListeningDueToDeactivation.AddListener(OnListenForcedStop);
  49. appVoiceExperience.VoiceEvents.OnStoppedListeningDueToInactivity.AddListener(OnListenForcedStop);
  50. appVoiceExperience.VoiceEvents.OnResponse.AddListener(OnRequestResponse);
  51. appVoiceExperience.VoiceEvents.OnError.AddListener(OnRequestError);
  52. }
  53. // Remove delegates
  54. private void OnDisable()
  55. {
  56. appVoiceExperience.VoiceEvents.OnSend.RemoveListener(OnSend);
  57. appVoiceExperience.VoiceEvents.OnPartialTranscription.RemoveListener(OnRequestTranscript);
  58. appVoiceExperience.VoiceEvents.OnFullTranscription.RemoveListener(OnRequestTranscript);
  59. appVoiceExperience.VoiceEvents.OnStartListening.RemoveListener(OnListenStart);
  60. appVoiceExperience.VoiceEvents.OnStoppedListening.RemoveListener(OnListenStop);
  61. appVoiceExperience.VoiceEvents.OnStoppedListeningDueToDeactivation.RemoveListener(OnListenForcedStop);
  62. appVoiceExperience.VoiceEvents.OnStoppedListeningDueToInactivity.RemoveListener(OnListenForcedStop);
  63. appVoiceExperience.VoiceEvents.OnResponse.RemoveListener(OnRequestResponse);
  64. appVoiceExperience.VoiceEvents.OnError.RemoveListener(OnRequestError);
  65. }
  66. // Request began
  67. private void OnSend(VoiceServiceRequest request)
  68. {
  69. // Store json on completion
  70. if (showJson && request is WitRequest witRequest) witRequest.onRawResponse = (response) => textArea.text = response;
  71. // Begin
  72. _active = true;
  73. }
  74. // Request transcript
  75. private void OnRequestTranscript(string transcript)
  76. {
  77. textArea.text = transcript;
  78. }
  79. // Listen start
  80. private void OnListenStart()
  81. {
  82. textArea.text = "Listening...";
  83. }
  84. // Listen stop
  85. private void OnListenStop()
  86. {
  87. textArea.text = "Processing...";
  88. }
  89. // Listen stop
  90. private void OnListenForcedStop()
  91. {
  92. if (!showJson)
  93. {
  94. textArea.text = freshStateText;
  95. }
  96. OnRequestComplete();
  97. }
  98. // Request response
  99. private void OnRequestResponse(WitResponseNode response)
  100. {
  101. if (!showJson)
  102. {
  103. if (!string.IsNullOrEmpty(response["text"]))
  104. {
  105. textArea.text = "I heard: " + response["text"];
  106. }
  107. else
  108. {
  109. textArea.text = freshStateText;
  110. }
  111. }
  112. OnRequestComplete();
  113. }
  114. // Request error
  115. private void OnRequestError(string error, string message)
  116. {
  117. if (!showJson)
  118. {
  119. textArea.text = $"<color=\"red\">Error: {error}\n\n{message}</color>";
  120. }
  121. OnRequestComplete();
  122. }
  123. // Deactivate
  124. private void OnRequestComplete()
  125. {
  126. _active = false;
  127. }
  128. // Toggle activation
  129. public void ToggleActivation()
  130. {
  131. SetActivation(!_active);
  132. }
  133. // Set activation
  134. public void SetActivation(bool toActivated)
  135. {
  136. if (_active != toActivated)
  137. {
  138. _active = toActivated;
  139. if (_active)
  140. {
  141. appVoiceExperience.Activate();
  142. }
  143. else
  144. {
  145. appVoiceExperience.Deactivate();
  146. }
  147. }
  148. }
  149. }
  150. }