-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversalSpeechWrapper.cs
More file actions
180 lines (160 loc) · 5.67 KB
/
UniversalSpeechWrapper.cs
File metadata and controls
180 lines (160 loc) · 5.67 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
using System;
using System.Runtime.InteropServices;
namespace UnityAccessibilityLib
{
/// <summary>
/// Low-level P/Invoke wrapper for the UniversalSpeech library.
/// Provides direct access to screen reader output with SAPI fallback.
///
/// Requires UniversalSpeech.dll (32-bit) to be in the game directory.
/// Download from: https://github.com/accessibleapps/UniversalSpeech
/// </summary>
public static class UniversalSpeechWrapper
{
private const string DLL_NAME = "UniversalSpeech.dll";
// P/Invoke declarations for UniversalSpeech
[DllImport(
DLL_NAME,
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode
)]
private static extern int speechSay(
[MarshalAs(UnmanagedType.LPWStr)] string str,
int interrupt
);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
private static extern int speechStop();
[DllImport(
DLL_NAME,
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode
)]
private static extern int brailleDisplay(
[MarshalAs(UnmanagedType.LPWStr)] string str
);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
private static extern int speechSetValue(int what, int value);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
private static extern int speechGetValue(int what);
// Constants from UniversalSpeech.h
private const int SP_ENABLE_NATIVE_SPEECH = 0xFFFF;
private const int SP_DETECT_SCREEN_READER = 0;
private static bool _initialized = false;
private static bool _dllAvailable = true;
/// <summary>
/// Whether the speech system has been initialized successfully.
/// </summary>
public static bool IsInitialized => _initialized;
/// <summary>
/// Initialize the speech system. Enables SAPI fallback if no screen reader is available.
/// </summary>
/// <returns>True if initialization succeeded, false otherwise.</returns>
public static bool Initialize()
{
if (_initialized)
return true;
if (!_dllAvailable)
return false;
try
{
// Enable native speech engines (SAPI) as fallback
speechSetValue(SP_ENABLE_NATIVE_SPEECH, 1);
_initialized = true;
AccessibilityLog.Msg("UniversalSpeech initialized");
return true;
}
catch (DllNotFoundException ex)
{
_dllAvailable = false;
AccessibilityLog.Error($"UniversalSpeech.dll not found: {ex.Message}");
AccessibilityLog.Error("Ensure UniversalSpeech.dll is in the game directory");
return false;
}
catch (Exception ex)
{
AccessibilityLog.Error($"Failed to initialize UniversalSpeech: {ex.Message}");
return false;
}
}
/// <summary>
/// Speak the given text directly.
/// </summary>
/// <param name="text">The text to speak</param>
/// <param name="interrupt">Whether to interrupt current speech</param>
public static void Speak(string text, bool interrupt = false)
{
if (!_initialized || !_dllAvailable || Net35Extensions.IsNullOrWhiteSpace(text))
return;
try
{
speechSay(text, interrupt ? 1 : 0);
}
catch (DllNotFoundException)
{
_dllAvailable = false;
}
catch (Exception ex)
{
AccessibilityLog.Error($"Speech error: {ex.Message}");
}
}
/// <summary>
/// Display the given text on a braille display via the current screen reader.
/// </summary>
/// <param name="text">The text to display on braille</param>
public static void DisplayBraille(string text)
{
if (!_initialized || !_dllAvailable || Net35Extensions.IsNullOrWhiteSpace(text))
return;
try
{
brailleDisplay(text);
}
catch (DllNotFoundException)
{
_dllAvailable = false;
}
catch (Exception ex)
{
AccessibilityLog.Error($"Braille display error: {ex.Message}");
}
}
/// <summary>
/// Stop any currently playing speech.
/// </summary>
public static void Stop()
{
if (!_dllAvailable)
return;
try
{
speechStop();
}
catch (DllNotFoundException)
{
_dllAvailable = false;
}
catch (Exception ex)
{
AccessibilityLog.Error($"Failed to stop speech: {ex.Message}");
}
}
/// <summary>
/// Check if a screen reader is currently active (not using SAPI fallback).
/// </summary>
/// <returns>True if a screen reader is detected, false otherwise.</returns>
public static bool IsScreenReaderActive()
{
if (!_initialized || !_dllAvailable)
return false;
try
{
return speechGetValue(SP_DETECT_SCREEN_READER) != 0;
}
catch
{
return false;
}
}
}
}