-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdictation_device_manager.ts
329 lines (278 loc) · 10.9 KB
/
dictation_device_manager.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {DictationDevice} from './dictation_device';
import {ButtonEventListener, ImplementationType} from './dictation_device_base';
import {FootControlDevice} from './foot_control_device';
import {PowerMic3Device} from './powermic_3_device';
import {SpeechMikeGamepadDevice} from './speechmike_gamepad_device';
import {MotionEventListener, SpeechMikeHidDevice,} from './speechmike_hid_device';
type DeviceEventListener = (device: DictationDevice) => void|Promise<void>;
const DEVICE_FILTERS: Readonly<
Record<ImplementationType, ReadonlyArray<HIDDeviceFilter>>> =
Object.freeze({
[ImplementationType.SPEECHMIKE_HID]: Object.freeze([
// Wired SpeechMikes (LFH35xx, LFH36xx, SMP37xx, SMP38xx) in HID mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0c1c, usagePage: 65440, usage: 1}),
// SpeechMike Premium Air (SMP40xx) in HID mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0c1d, usagePage: 65440, usage: 1}),
// SpeechOne (PSM6000) in HID or Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0c1e, usagePage: 65440, usage: 1}),
// All SpeechMikes in Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0fa0, usagePage: 65440, usage: 1}),
// PowerMic IV in HID or Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0554, productId: 0x0064, usagePage: 65440, usage: 1}),
]),
[ImplementationType.SPEECHMIKE_GAMEPAD]: Object.freeze([
// All SpeechMikes in Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0fa0, usagePage: 1, usage: 4}),
// SpeechOne (PSM6000) in Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0911, productId: 0x0c1e, usagePage: 1, usage: 4}),
// PowerMic IV in Browser/Gamepad mode
Object.freeze(
{vendorId: 0x0554, productId: 0x0064, usagePage: 1, usage: 4}),
]),
[ImplementationType.FOOT_CONTROL]: Object.freeze([
// 3-pedal Foot control ACC2310/2320
Object.freeze(
{vendorId: 0x0911, productId: 0x1844, usagePage: 1, usage: 4}),
// 4-pedal Foot control ACC2330
Object.freeze(
{vendorId: 0x0911, productId: 0x091a, usagePage: 1, usage: 4}),
]),
[ImplementationType.POWERMIC_3]: Object.freeze([
// PowerMic III
Object.freeze(
{vendorId: 0x0554, productId: 0x1001, usagePage: 1, usage: 0}),
]),
});
export class DictationDeviceManager {
protected readonly buttonEventListeners = new Set<ButtonEventListener>();
protected readonly deviceConnectEventListeners =
new Set<DeviceEventListener>();
protected readonly deviceDisconnectEventListeners =
new Set<DeviceEventListener>();
protected readonly motionEventListeners = new Set<MotionEventListener>();
protected readonly devices = new Map<HIDDevice, DictationDevice>();
protected readonly pendingProxyDevices =
new Map<HIDDevice, SpeechMikeGamepadDevice>();
protected readonly onConnectHandler = (event: HIDConnectionEvent) =>
this.onHidDeviceConnected(event);
protected readonly onDisconectHandler = (event: HIDConnectionEvent) =>
this.onHidDeviceDisconnected(event);
protected isInitialized = false;
constructor(protected readonly hidApi = navigator.hid) {
if (this.hidApi === undefined) {
throw new Error('WebHID is not available');
}
}
getDevices(): DictationDevice[] {
this.failIfNotInitialized();
return [...this.devices.values()];
}
async init() {
if (this.isInitialized) {
throw new Error('DictationDeviceManager already initialized');
}
this.hidApi.addEventListener('connect', this.onConnectHandler);
this.hidApi.addEventListener('disconnect', this.onDisconectHandler);
const hidDevices = await this.hidApi.getDevices();
await this.createAndAddInitializedDevices(hidDevices);
this.isInitialized = true;
}
async shutdown() {
this.failIfNotInitialized();
this.hidApi.removeEventListener('connect', this.onConnectHandler);
this.hidApi.removeEventListener('disconnect', this.onDisconectHandler);
await Promise.all([
[...this.devices.values()].map(
device => device.shutdown(/*closeDevice=*/ true)),
]);
this.devices.clear();
this.isInitialized = false;
}
async requestDevice(): Promise<Array<DictationDevice>> {
this.failIfNotInitialized();
const hidDevices = await this.hidApi.requestDevice({
filters: getFilters(),
});
const devices = await this.createAndAddInitializedDevices(hidDevices);
return devices;
}
addButtonEventListener(listener: ButtonEventListener) {
this.buttonEventListeners.add(listener);
}
addDeviceConnectedEventListener(listener: DeviceEventListener) {
this.deviceConnectEventListeners.add(listener);
}
addDeviceDisconnectedEventListener(listener: DeviceEventListener) {
this.deviceDisconnectEventListeners.add(listener);
}
addMotionEventListener(listener: MotionEventListener) {
this.motionEventListeners.add(listener);
}
protected failIfNotInitialized() {
if (!this.isInitialized) {
throw new Error('DictationDeviceManager not yet initialized');
}
}
protected async createAndAddInitializedDevices(hidDevices: HIDDevice[]):
Promise<Array<DictationDevice>> {
const devices = await Promise.all(
hidDevices.map(hidDevice => this.createDevice(hidDevice)));
const result: DictationDevice[] = [];
for (const device of devices) {
if (device === undefined) continue;
try {
await device.init();
} catch (e: unknown) {
await device.shutdown();
console.error('failed to initialize device', e);
continue;
}
// Handle proxy device
if (device.implType === ImplementationType.SPEECHMIKE_GAMEPAD) {
this.pendingProxyDevices.set(device.hidDevice, device);
continue;
}
// Handle host device
this.addListeners(device);
this.devices.set(device.hidDevice, device);
result.push(device);
}
this.assignPendingProxyDevices();
return result;
}
protected async createDevice(hidDevice: HIDDevice):
Promise<DictationDevice|undefined> {
// Don't recreate devices for known devices.
if (this.devices.has(hidDevice)) return undefined;
const implType = getImplType(hidDevice);
if (implType === undefined) return undefined;
switch (implType) {
case ImplementationType.SPEECHMIKE_HID:
return SpeechMikeHidDevice.create(hidDevice);
case ImplementationType.POWERMIC_3:
return PowerMic3Device.create(hidDevice);
case ImplementationType.SPEECHMIKE_GAMEPAD:
return SpeechMikeGamepadDevice.create(hidDevice);
case ImplementationType.FOOT_CONTROL:
return FootControlDevice.create(hidDevice);
default:
checkExhaustive(implType);
}
}
protected assignPendingProxyDevices() {
for (const [proxyHidDevice, proxyDevice] of this.pendingProxyDevices) {
// Find matching host and assign
for (const hostDevice of this.devices.values()) {
if (hostDevice.implType !== ImplementationType.SPEECHMIKE_HID) {
continue;
}
const hostHidDevice = hostDevice.hidDevice;
if (proxyHidDevice.vendorId !== hostHidDevice.vendorId ||
proxyHidDevice.productId !== hostHidDevice.productId) {
continue;
}
hostDevice.assignProxyDevice(proxyDevice);
this.pendingProxyDevices.delete(proxyHidDevice);
break;
}
}
}
protected addListeners(device: DictationDevice) {
for (const listener of this.buttonEventListeners) {
device.addButtonEventListener(listener);
}
if (device.implType === ImplementationType.SPEECHMIKE_HID) {
for (const listener of this.motionEventListeners) {
device.addMotionEventListener(listener);
}
}
}
protected async onHidDeviceConnected(event: HIDConnectionEvent) {
const hidDevice = event.device;
const devices = await this.createAndAddInitializedDevices([hidDevice]);
const device = devices[0];
if (device === undefined) return;
await Promise.all([...this.deviceConnectEventListeners].map(
listener => listener(device)));
}
protected async onHidDeviceDisconnected(event: HIDConnectionEvent) {
const hidDevice = event.device;
this.pendingProxyDevices.delete(hidDevice);
const device = this.devices.get(hidDevice);
if (device === undefined) return;
await device.shutdown(/*closeDevice=*/ false);
await Promise.all([...this.deviceDisconnectEventListeners].map(
listener => listener(device)));
this.devices.delete(hidDevice);
}
}
function getFilters(): HIDDeviceFilter[] {
const filters: HIDDeviceFilter[] = [];
for (const implType of Object.values(ImplementationType)) {
if (typeof implType === 'string') continue;
const filtersForImplType = DEVICE_FILTERS[implType];
filters.push(...filtersForImplType);
}
return filters;
}
function getImplType(hidDevice: HIDDevice): ImplementationType|undefined {
for (const implType of Object.values(ImplementationType)) {
if (typeof implType === 'string') continue;
const filtersForImplType = DEVICE_FILTERS[implType];
if (deviceMatchesFilters(hidDevice, filtersForImplType)) return implType;
}
return undefined;
}
function deviceMatchesFilters(
hidDevice: HIDDevice, filters: ReadonlyArray<HIDDeviceFilter>): boolean {
return filters.some(filter => deviceMatchesFilter(hidDevice, filter));
}
function deviceMatchesFilter(
hidDevice: HIDDevice, filter: Readonly<HIDDeviceFilter>): boolean {
if (filter.vendorId !== undefined && hidDevice.vendorId !== filter.vendorId)
return false;
if (filter.productId !== undefined &&
hidDevice.productId !== filter.productId)
return false;
if (filter.usagePage !== undefined) {
if (hidDevice.collections === undefined ||
hidDevice.collections.every(
collection => collection.usagePage !== filter.usagePage)) {
return false;
}
}
if (filter.usage !== undefined) {
if (hidDevice.collections === undefined ||
hidDevice.collections.every(
collection => collection.usage !== filter.usage)) {
return false;
}
}
return true;
}
function checkExhaustive(arg: never): never {
throw new Error(`Unexpected input: ${arg}`);
}