// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; using UnityEngine.Events; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Script used to start and stop recording sessions in the current dictation system and report the transcribed text via UnityEvents. /// 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. /// [AddComponentMenu("Scripts/MRTK/SDK/DictationHandler")] public class DictationHandler : BaseInputHandler, IMixedRealityDictationHandler { [SerializeField] [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")] private float initialSilenceTimeout = 5f; [SerializeField] [Tooltip("Time length in seconds before the dictation session ends due to lack of audio input.")] private float autoSilenceTimeout = 20f; [SerializeField] [Tooltip("Length in seconds for the dictation service to listen")] private int recordingTime = 10; [SerializeField] [Tooltip("Whether recording should start automatically on start")] private bool startRecordingOnStart = false; [System.Serializable] public class StringUnityEvent : UnityEvent { } /// /// Event raised while the user is talking. As the recognizer listens, it provides text of what it's heard so far. /// public StringUnityEvent OnDictationHypothesis; /// /// Event raised after the user pauses, typically at the end of a sentence. Contains the full recognized string so far. /// public StringUnityEvent OnDictationResult; /// /// Event raised when the recognizer stops. Contains the final recognized string. /// public StringUnityEvent OnDictationComplete; /// /// Event raised when an error occurs. Contains the string representation of the error reason. /// public StringUnityEvent OnDictationError; private IMixedRealityDictationSystem dictationSystem; /// /// Start a recording session in the dictation system. /// public void StartRecording() { if (dictationSystem != null) { dictationSystem.StartRecording(gameObject, initialSilenceTimeout, autoSilenceTimeout, recordingTime); } } /// /// Stop a recording session in the dictation system. /// public void StopRecording() { if (dictationSystem != null) { dictationSystem.StopRecording(); } } /// /// Get the listening state /// public bool IsListening { get { return dictationSystem.IsListening; } } /// /// Get the last audio clip from the dictation session /// public AudioClip AudioClip { get { return dictationSystem.AudioClip; } } #region InputSystemGlobalHandlerListener Implementation /// protected override void RegisterHandlers() { CoreServices.InputSystem?.RegisterHandler(this); } /// protected override void UnregisterHandlers() { CoreServices.InputSystem?.UnregisterHandler(this); } #endregion InputSystemGlobalHandlerListener Implementation #region IMixedRealityDictationHandler implementation void IMixedRealityDictationHandler.OnDictationHypothesis(DictationEventData eventData) { OnDictationHypothesis.Invoke(eventData.DictationResult); } void IMixedRealityDictationHandler.OnDictationResult(DictationEventData eventData) { OnDictationResult.Invoke(eventData.DictationResult); } void IMixedRealityDictationHandler.OnDictationComplete(DictationEventData eventData) { OnDictationComplete.Invoke(eventData.DictationResult); } void IMixedRealityDictationHandler.OnDictationError(DictationEventData eventData) { OnDictationError.Invoke(eventData.DictationResult); } #endregion IMixedRealityDictationHandler implementation #region MonoBehaviour implementation protected override void Start() { base.Start(); dictationSystem = CoreServices.GetInputSystemDataProvider(); 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"); if (startRecordingOnStart) { StartRecording(); } } protected override void OnDisable() { StopRecording(); base.OnDisable(); } #endregion MonoBehaviour implementation } }