-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechManager.cs
More file actions
234 lines (198 loc) · 8.32 KB
/
SpeechManager.cs
File metadata and controls
234 lines (198 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
namespace UnityAccessibilityLib
{
/// <summary>
/// High-level speech output manager.
/// Handles formatting, duplicate prevention, and repeat functionality.
/// </summary>
public static class SpeechManager
{
// Repeat functionality
private static string _currentSpeaker = "";
private static string _currentText = "";
private static int _currentType = TextType.Dialogue;
// Duplicate prevention
private static string _lastOutputMessage = "";
private static DateTime _lastOutputTime = DateTime.MinValue;
/// <summary>
/// Time window in seconds during which duplicate messages are suppressed.
/// Default is 0.5 seconds.
/// </summary>
public static double DuplicateWindowSeconds { get; set; } = 0.5;
/// <summary>
/// Whether to log all speech output. Default is true.
/// </summary>
public static bool EnableLogging { get; set; } = true;
/// <summary>
/// Whether to send output to braille displays. Default is true.
/// When enabled, all speech output is also sent to the braille display
/// via the screen reader's braille support.
/// </summary>
public static bool EnableBraille { get; set; } = true;
/// <summary>
/// Initialize the speech system.
/// </summary>
/// <returns>True if initialization succeeded.</returns>
public static bool Initialize()
{
return UniversalSpeechWrapper.Initialize();
}
/// <summary>
/// Output text with optional speaker name. Handles formatting, duplicate prevention, and repeat storage.
/// </summary>
/// <param name="speaker">The speaker name (can be null or empty)</param>
/// <param name="text">The text to speak</param>
/// <param name="textType">The type of text being spoken (use TextType constants or custom values)</param>
public static void Output(string speaker, string text, int textType = TextType.Dialogue)
{
if (Net35Extensions.IsNullOrWhiteSpace(text))
return;
string formattedText = FormatText(speaker, text, textType);
// Duplicate prevention - skip if same text within window
DateTime now = DateTime.UtcNow;
if (
formattedText == _lastOutputMessage
&& (now - _lastOutputTime).TotalSeconds < DuplicateWindowSeconds
)
{
return;
}
_lastOutputMessage = formattedText;
_lastOutputTime = now;
// Store for repeat functionality (only for narrative content)
if (ShouldStoreForRepeat(textType))
{
_currentSpeaker = speaker ?? "";
_currentText = text;
_currentType = textType;
}
// Output via speech
UniversalSpeechWrapper.Speak(formattedText);
// Output via braille if enabled
if (EnableBraille)
{
UniversalSpeechWrapper.DisplayBraille(formattedText);
}
if (EnableLogging)
{
string typeName =
TextTypeNames != null && TextTypeNames.ContainsKey(textType)
? TextTypeNames[textType]
: textType.ToString();
AccessibilityLog.Msg($"[{typeName}] {formattedText}");
}
}
/// <summary>
/// Announce text without a speaker name.
/// </summary>
/// <param name="text">The text to announce</param>
/// <param name="textType">The type of text (use TextType constants or custom values)</param>
public static void Announce(string text, int textType = TextType.System)
{
Output(null, text, textType);
}
/// <summary>
/// Repeat the last dialogue or narrator text.
/// </summary>
public static void RepeatLast()
{
if (!Net35Extensions.IsNullOrWhiteSpace(_currentText))
{
string formattedText = FormatText(_currentSpeaker, _currentText, _currentType);
UniversalSpeechWrapper.Speak(formattedText);
if (EnableBraille)
{
UniversalSpeechWrapper.DisplayBraille(formattedText);
}
if (EnableLogging)
{
AccessibilityLog.Msg($"Repeating: '{formattedText}'");
}
}
else
{
AccessibilityLog.Msg("Nothing to repeat");
}
}
/// <summary>
/// Stop any currently playing speech.
/// </summary>
public static void Stop()
{
UniversalSpeechWrapper.Stop();
}
/// <summary>
/// Clear the stored repeat text.
/// </summary>
public static void ClearRepeatBuffer()
{
_currentSpeaker = "";
_currentText = "";
_currentType = TextType.Dialogue;
}
/// <summary>
/// Optional dictionary mapping text type IDs to names for logging.
/// Set this to provide readable names for custom text types.
/// </summary>
public static System.Collections.Generic.Dictionary<int, string> TextTypeNames { get; set; }
/// <summary>
/// Custom predicate to determine if a text type should be stored for repeat.
/// If null, defaults to storing Dialogue and Narrator types.
/// </summary>
public static Func<int, bool> ShouldStoreForRepeatPredicate { get; set; }
private static bool ShouldStoreForRepeat(int textType)
{
if (ShouldStoreForRepeatPredicate != null)
return ShouldStoreForRepeatPredicate(textType);
// Default: store dialogue and narrator text
return textType == TextType.Dialogue || textType == TextType.Narrator;
}
/// <summary>
/// Custom formatter for text output. If null, uses default formatting.
/// Parameters: speaker, text, textType. Returns formatted string.
/// </summary>
public static Func<string, string, int, string> FormatTextOverride { get; set; }
private static string FormatText(string speaker, string text, int textType)
{
text = TextCleaner.Clean(text);
if (FormatTextOverride != null)
return FormatTextOverride(speaker, text, textType);
return BuildSpeakerPrefixedText(speaker, text, textType);
}
/// <summary>
/// Builds formatted text with optional speaker prefix based on text type.
/// </summary>
/// <param name="speaker">The speaker name (can be null or empty)</param>
/// <param name="text">The text content</param>
/// <param name="textType">The type of text being formatted</param>
/// <returns>Formatted text with speaker prefix for Dialogue type, or plain text otherwise</returns>
public static string BuildSpeakerPrefixedText(string speaker, string text, int textType)
{
// Default: only Dialogue type gets speaker prefix
if (textType == TextType.Dialogue && !Net35Extensions.IsNullOrWhiteSpace(speaker))
return $"{speaker}: {text}";
return text;
}
}
/// <summary>
/// Base text type constants. Define custom types starting from CustomBase (100).
/// </summary>
public static class TextType
{
/// <summary>Character dialogue with speaker name</summary>
public const int Dialogue = 0;
/// <summary>Narrator or descriptive text</summary>
public const int Narrator = 1;
/// <summary>Menu item text</summary>
public const int Menu = 2;
/// <summary>Menu choice or selection</summary>
public const int MenuChoice = 3;
/// <summary>System message or notification</summary>
public const int System = 4;
/// <summary>
/// Base value for custom text types. Define your own types starting from this value.
/// Example: public const int MyCustomType = TextType.CustomBase + 1;
/// </summary>
public const int CustomBase = 100;
}
}