Repository navigation
Expand file tree
/
Copy pathScreenReader.cs
More file actions
226 lines (193 loc) · 7.33 KB
/
Copy pathScreenReader.cs
File metadata and controls
226 lines (193 loc) · 7.33 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
using System;
namespace BeforeTheStormAccess
{
/// <summary>
/// Screen reader announcements: duplicate suppression, coalesced interrupts and
/// repeat-last on top of the Prism speech backend (PrismSpeech).
/// Ported from Date Everything Access; net35-safe.
/// </summary>
public static class ScreenReader
{
private static readonly object _speechLock = new object();
private static bool _available;
private static bool _initialized;
private static string _lastSpokenText;
private static string _lastRepeatableText;
// Duplicate suppression: multiple announcers (focus reader, window label
// tracker, patches) can legitimately observe the same text change on the
// same tick or across adjacent ticks. Speaking it once is correct; the
// repeat-last key bypasses this via RepeatLastSpoken.
private const int DuplicateWindowMs = 800;
private static int _lastSpokenTick;
// Coalesced-cycle interrupt. Ambient announcers run as a chain every poll tick.
// They must NOT cut each other off WITHIN a tick, but the FIRST of them in a tick
// SHOULD interrupt stale speech from earlier ticks. The watcher calls
// BeginCoalescedCycle() once at the top of the ambient chain; the first
// SayCoalesced() that actually emits consumes the flag and interrupts, the rest in
// that tick append. Menus don't use this — they call Say(interrupt:true) directly.
private static bool _coalescedInterruptPending;
/// <summary>
/// Loads Prism and selects the running screen reader, or the OS voice when none
/// is running. Prism logs which backend it picked.
/// </summary>
public static void Initialize()
{
if (_initialized)
return;
_available = PrismSpeech.Initialize();
if (!_available)
Main.Log.LogWarning("Speech output disabled; announcements are only logged.");
_initialized = true;
}
/// <summary>
/// Speaks text through the screen reader and optionally remembers it for replay.
/// </summary>
public static void Say(string text, bool interrupt = true, bool remember = true, bool rememberAsRepeatable = false)
{
if (string.IsNullOrEmpty(text) || text.Trim().Length == 0)
return;
lock (_speechLock)
{
int now = Environment.TickCount;
if (text == _lastSpokenText && unchecked(now - _lastSpokenTick) < DuplicateWindowMs)
{
DebugLogger.Log(LogCategory.State, "ScreenReader", "duplicate suppressed: " + text);
return;
}
_lastSpokenTick = now;
}
if (remember || rememberAsRepeatable)
{
lock (_speechLock)
{
if (remember)
{
_lastSpokenText = text;
}
if (rememberAsRepeatable)
{
_lastRepeatableText = text;
}
}
}
DebugLogger.LogScreenReader(text);
if (!_available)
return;
try
{
Output(text, interrupt);
}
catch (Exception ex)
{
Main.Log.LogWarning("ScreenReader.Say failed: " + ex.Message);
}
}
/// <summary>
/// Arms the next coalesced announcement of this poll tick to interrupt. Call once
/// at the start of the ambient announcer chain.
/// </summary>
public static void BeginCoalescedCycle()
{
_coalescedInterruptPending = true;
}
/// <summary>
/// Speaks an ambient announcement that coalesces within a poll tick: the FIRST one
/// to emit after BeginCoalescedCycle() interrupts stale speech from earlier ticks;
/// later ones in the same tick append so co-occurring announcements all play.
/// </summary>
public static void SayCoalesced(string text, bool remember = true, bool rememberAsRepeatable = false)
{
if (string.IsNullOrEmpty(text) || text.Trim().Length == 0)
return;
bool interrupt = _coalescedInterruptPending;
_coalescedInterruptPending = false;
Say(text, interrupt, remember, rememberAsRepeatable);
}
/// <summary>
/// Sends text to Prism, issuing an explicit stop first when interrupting.
///
/// With Tolk, the bundled cancel in an interrupting output did not clear NVDA's
/// speech queue while NVDA was in SLEEP MODE; a separate, ordered stop before the
/// output did. Kept for Prism's NVDA backend, which uses the same controller
/// client. Harmless when NVDA is awake.
/// </summary>
private static void Output(string text, bool interrupt)
{
if (interrupt)
{
try
{
PrismSpeech.Stop();
}
catch
{
// A failed pre-silence must not block the speak that follows.
}
}
PrismSpeech.Output(text, interrupt);
}
/// <summary>
/// Repeats the most recently spoken text when one is available.
/// </summary>
public static bool RepeatLastSpoken(bool interrupt = true)
{
string lastRepeatableText;
string lastSpokenText;
lock (_speechLock)
{
lastRepeatableText = _lastRepeatableText;
lastSpokenText = _lastSpokenText;
}
if (!string.IsNullOrEmpty(lastRepeatableText))
{
lastSpokenText = lastRepeatableText;
}
if (string.IsNullOrEmpty(lastSpokenText))
return false;
DebugLogger.LogScreenReader(lastSpokenText);
if (!_available)
return true;
try
{
Output(lastSpokenText, interrupt);
}
catch (Exception ex)
{
Main.Log.LogWarning("ScreenReader.RepeatLastSpoken failed: " + ex.Message);
}
return true;
}
/// <summary>
/// Stops any current speech output.
/// </summary>
public static void Stop()
{
if (!_available)
return;
try
{
PrismSpeech.Stop();
}
catch (Exception ex)
{
Main.Log.LogWarning("ScreenReader.Stop failed: " + ex.Message);
}
}
/// <summary>
/// Releases the speech backend and clears cached speech state.
/// </summary>
public static void Shutdown()
{
if (!_initialized)
return;
PrismSpeech.Shutdown();
_initialized = false;
_available = false;
lock (_speechLock)
{
_lastSpokenText = null;
_lastRepeatableText = null;
}
}
}
}