|
| 1 | +using Microsoft.MixedReality.Toolkit; |
| 2 | +using Microsoft.MixedReality.Toolkit.Input; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.Events; |
| 7 | +using UnityEngine.UI; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Script used to start and stop recording sessions in the current dictation system and report the transcribed text via UnityEvents. |
| 11 | +/// For this script to work, a dictation system like 'Windows Dictation Input Provider' must be added to the Data Providers in the Input System profile. |
| 12 | +/// </summary> |
| 13 | +public class DictationHandler : BaseInputHandler, IMixedRealityDictationHandler |
| 14 | +{ |
| 15 | + [SerializeField] |
| 16 | + [Tooltip("Time length in seconds before the dictation session ends due to lack of audio input in case there was no audio heard in the current session")] |
| 17 | + private float initialSilenceTimeout = 5f; |
| 18 | + |
| 19 | + [SerializeField] |
| 20 | + [Tooltip("Time length in seconds before the dictation session ends due to lack of audio input.")] |
| 21 | + private float autoSilenceTimeout = 20f; |
| 22 | + |
| 23 | + [SerializeField] |
| 24 | + [Tooltip("Length in seconds for the dictation service to listen")] |
| 25 | + private int recordingTime = 10; |
| 26 | + |
| 27 | + [SerializeField] |
| 28 | + [Tooltip("Whether recording should start automatically on start")] |
| 29 | + private bool startRecordingOnStart = false; |
| 30 | + |
| 31 | + [System.Serializable] |
| 32 | + public class StringUnityEvent : UnityEvent<string> { } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Event raised while the user is talking. As the recognizer listens, it provides text of what it's heard so far. |
| 36 | + /// </summary> |
| 37 | + public StringUnityEvent OnDictationHypothesis; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Event raised after the user pauses, typically at the end of a sentence. Contains the full recognized string so far. |
| 41 | + /// </summary> |
| 42 | + public StringUnityEvent OnDictationResult; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Event raised when the recognizer stops. Contains the final recognized string. |
| 46 | + /// </summary> |
| 47 | + public StringUnityEvent OnDictationComplete; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Event raised when an error occurs. Contains the string representation of the error reason. |
| 51 | + /// </summary> |
| 52 | + public StringUnityEvent OnDictationError; |
| 53 | + |
| 54 | + private bool isRecording = false; |
| 55 | + private IMixedRealityDictationSystem dictationSystem; |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Start a recording session in the dictation system. |
| 59 | + /// </summary> |
| 60 | + public void StartRecording() |
| 61 | + { |
| 62 | + if (dictationSystem != null) |
| 63 | + { |
| 64 | + isRecording = true; |
| 65 | + dictationSystem.StartRecording(gameObject, initialSilenceTimeout, autoSilenceTimeout, recordingTime); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Stop a recording session in the dictation system. |
| 71 | + /// </summary> |
| 72 | + public void StopRecording() |
| 73 | + { |
| 74 | + if (dictationSystem != null) |
| 75 | + { |
| 76 | + dictationSystem.StopRecording(); |
| 77 | + isRecording = false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #region IMixedRealityDictationHandler implementation |
| 82 | + |
| 83 | + void IMixedRealityDictationHandler.OnDictationHypothesis(DictationEventData eventData) |
| 84 | + { |
| 85 | + OnDictationHypothesis.Invoke(eventData.DictationResult); |
| 86 | + } |
| 87 | + |
| 88 | + void IMixedRealityDictationHandler.OnDictationResult(DictationEventData eventData) |
| 89 | + { |
| 90 | + OnDictationResult.Invoke(eventData.DictationResult); |
| 91 | + } |
| 92 | + |
| 93 | + void IMixedRealityDictationHandler.OnDictationComplete(DictationEventData eventData) |
| 94 | + { |
| 95 | + OnDictationComplete.Invoke(eventData.DictationResult); |
| 96 | + } |
| 97 | + |
| 98 | + void IMixedRealityDictationHandler.OnDictationError(DictationEventData eventData) |
| 99 | + { |
| 100 | + OnDictationError.Invoke(eventData.DictationResult); |
| 101 | + } |
| 102 | + |
| 103 | + #endregion IMixedRealityDictationHandler implementation |
| 104 | + |
| 105 | + #region MonoBehaviour implementation |
| 106 | + |
| 107 | + protected override void Start() |
| 108 | + { |
| 109 | + base.Start(); |
| 110 | + |
| 111 | + dictationSystem = MixedRealityToolkit.Instance.GetService<IMixedRealityDictationSystem>(); |
| 112 | + Debug.Assert(dictationSystem != null, "No dictation system found. In order to use dictation, add a dictation system like 'Windows Dictation Input Provider' to the Data Providers in the Input System profile"); |
| 113 | + |
| 114 | + if (startRecordingOnStart) |
| 115 | + { |
| 116 | + StartRecording(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + protected override void OnDisable() |
| 121 | + { |
| 122 | + StopRecording(); |
| 123 | + |
| 124 | + base.OnDisable(); |
| 125 | + } |
| 126 | + |
| 127 | + #endregion MonoBehaviour implementation |
| 128 | +} |
0 commit comments