Skip to content

Commit 97b2441

Browse files
authored
Merge pull request #3764 from luis-valverde-ms/dictation_handler
Create DictationHandler script for easy access to dictation service
2 parents c900345 + dec6b26 commit 97b2441

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using UnityEngine;
5+
using UnityEngine.Events;
6+
7+
namespace Microsoft.MixedReality.Toolkit.Input
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 IMixedRealityDictationSystem dictationSystem;
55+
56+
/// <summary>
57+
/// Start a recording session in the dictation system.
58+
/// </summary>
59+
public void StartRecording()
60+
{
61+
if (dictationSystem != null)
62+
{
63+
dictationSystem.StartRecording(gameObject, initialSilenceTimeout, autoSilenceTimeout, recordingTime);
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Stop a recording session in the dictation system.
69+
/// </summary>
70+
public void StopRecording()
71+
{
72+
if (dictationSystem != null)
73+
{
74+
dictationSystem.StopRecording();
75+
}
76+
}
77+
78+
#region IMixedRealityDictationHandler implementation
79+
80+
void IMixedRealityDictationHandler.OnDictationHypothesis(DictationEventData eventData)
81+
{
82+
OnDictationHypothesis.Invoke(eventData.DictationResult);
83+
}
84+
85+
void IMixedRealityDictationHandler.OnDictationResult(DictationEventData eventData)
86+
{
87+
OnDictationResult.Invoke(eventData.DictationResult);
88+
}
89+
90+
void IMixedRealityDictationHandler.OnDictationComplete(DictationEventData eventData)
91+
{
92+
OnDictationComplete.Invoke(eventData.DictationResult);
93+
}
94+
95+
void IMixedRealityDictationHandler.OnDictationError(DictationEventData eventData)
96+
{
97+
OnDictationError.Invoke(eventData.DictationResult);
98+
}
99+
100+
#endregion IMixedRealityDictationHandler implementation
101+
102+
#region MonoBehaviour implementation
103+
104+
protected override void Start()
105+
{
106+
base.Start();
107+
108+
dictationSystem = MixedRealityToolkit.Instance.GetService<IMixedRealityDictationSystem>();
109+
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");
110+
111+
if (startRecordingOnStart)
112+
{
113+
StartRecording();
114+
}
115+
}
116+
117+
protected override void OnDisable()
118+
{
119+
StopRecording();
120+
121+
base.OnDisable();
122+
}
123+
124+
#endregion MonoBehaviour implementation
125+
}
126+
}

Assets/MixedRealityToolkit.SDK/Features/Input/Handlers/DictationHandler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)