This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathSpeechConnectionFactory.ts
More file actions
90 lines (76 loc) · 3.04 KB
/
SpeechConnectionFactory.ts
File metadata and controls
90 lines (76 loc) · 3.04 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
import { WebsocketConnection } from "../../common.browser/Exports";
import {
IConnection,
IStringDictionary,
Promise,
Storage,
} from "../../common/Exports";
import {
AuthInfo,
IAuthentication,
IConnectionFactory,
RecognitionAPI,
RecognitionMode,
RecognizerConfig,
SpeechResultFormat,
WebsocketMessageFormatter,
} from "../speech/Exports";
const TestHooksParamName: string = "testhooks";
const ConnectionIdHeader: string = "X-ConnectionId";
export class SpeechConnectionFactory implements IConnectionFactory {
public Create = (
config: RecognizerConfig,
authInfo: AuthInfo,
connectionId?: string): IConnection => {
switch (config.RecognitionAPI) {
case RecognitionAPI.BingSpeech:
Storage.Local.Set("Host", "wss://speech.platform.bing.com");
break;
case RecognitionAPI.CustomSpeech:
Storage.Local.Set("Host", "wss://westus.stt.speech.microsoft.com");
break;
default:
Storage.Local.Set("Host", "wss://speech.platform.bing.com");
break;
}
let endpoint = "";
switch (config.RecognitionMode) {
case RecognitionMode.Conversation:
endpoint = this.Host + this.ConversationRelativeUri;
break;
case RecognitionMode.Dictation:
endpoint = this.Host + this.DictationRelativeUri;
break;
default:
endpoint = this.Host + this.InteractiveRelativeUri; // default is interactive
break;
}
const queryParams: IStringDictionary<string> = {
format: SpeechResultFormat[config.Format].toString().toLowerCase(),
language: config.Language,
};
if (this.IsDebugModeEnabled) {
queryParams[TestHooksParamName] = "1";
}
const headers: IStringDictionary<string> = {};
headers[authInfo.HeaderName] = authInfo.Token;
headers[ConnectionIdHeader] = connectionId;
return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), config.RecognitionAPI, config.EndpointId, connectionId);
}
private get Host(): string {
return Storage.Local.GetOrAdd("Host", "wss://speech.platform.bing.com");
}
private get InteractiveRelativeUri(): string {
return Storage.Local.GetOrAdd("InteractiveRelativeUri", "/speech/recognition/interactive/cognitiveservices/v1");
}
private get ConversationRelativeUri(): string {
return Storage.Local.GetOrAdd("ConversationRelativeUri", "/speech/recognition/conversation/cognitiveservices/v1");
}
private get DictationRelativeUri(): string {
return Storage.Local.GetOrAdd("DictationRelativeUri", "/speech/recognition/dictation/cognitiveservices/v1");
}
private get IsDebugModeEnabled(): boolean {
const value = Storage.Local.GetOrAdd("IsDebugModeEnabled", "false");
return value.toLowerCase() === "true";
}
}