@creativeorange/azure-text-to-speech 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export type { SpeechAuthorization, SpeechAuthorizationProvider, SpeechTokenRequestOptions, SpeechAuthenticationOptions, SpeechSafeError, } from './authentication';
2
+ export { DEFAULT_TOKEN_LIFETIME_MS, SpeechAuthorizationManager, createSpeechAuthorizationProvider, validateSpeechAuthorization, toSafeErrorDetail, sanitizeErrorText, isLikelyAuthorizationError, } from './authentication';
3
+ export * from './SpeechToText';
4
+ export * from './TextToSpeech';
package/package.json CHANGED
@@ -1,38 +1,42 @@
1
1
  {
2
2
  "name": "@creativeorange/azure-text-to-speech",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "main": "dist/co-azure-tts.umd.js",
5
5
  "browser": "dist/co-azure-tts.es.js",
6
6
  "module": "dist/co-azure-tts.es.js",
7
+ "types": "./dist/main.d.ts",
7
8
  "exports": {
8
9
  ".": {
10
+ "types": "./dist/main.d.ts",
9
11
  "import": "./dist/co-azure-tts.es.js",
10
12
  "require": "./dist/co-azure-tts.umd.js"
11
13
  }
12
14
  },
13
15
  "files": [
14
16
  "dist",
15
- "src"
17
+ "README.md",
18
+ "LICENSE"
16
19
  ],
17
20
  "scripts": {
18
- "build": "vite build",
21
+ "build": "vite build && tsc -p tsconfig.build.json",
19
22
  "build:watch": "vite build --watch",
20
23
  "test": "vitest run",
21
24
  "test:watch": "vitest",
22
- "lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\""
25
+ "lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
26
+ "typecheck": "tsc --noEmit"
23
27
  },
24
28
  "license": "MIT",
25
29
  "author": "Edsardio",
26
30
  "devDependencies": {
27
31
  "@originjs/vite-plugin-commonjs": "^1.0.1",
28
- "@typescript-eslint/eslint-plugin": "^5.33.0",
29
- "@typescript-eslint/parser": "^5.33.0",
32
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
33
+ "@typescript-eslint/parser": "^6.21.0",
30
34
  "build-esm": "^4.2.2",
31
35
  "eslint": "^8.21.0",
32
36
  "eslint-config-google": "^0.14.0",
33
37
  "jsdom": "^24.1.0",
34
38
  "microsoft-cognitiveservices-speech-sdk": "^1.22.0",
35
- "typescript": "^4.7.4",
39
+ "typescript": "~5.4.5",
36
40
  "vite": "^2.7.2",
37
41
  "vite-plugin-env-compatible": "^1.1.1",
38
42
  "vite-plugin-eslint": "^1.7.0",
@@ -1,172 +0,0 @@
1
- import {
2
- SpeechTranslationConfig,
3
- AudioConfig,
4
- TranslationRecognizer,
5
- ResultReason,
6
- } from 'microsoft-cognitiveservices-speech-sdk';
7
- import {
8
- SpeechAuthenticationOptions,
9
- SpeechAuthorizationManager,
10
- toSafeErrorDetail,
11
- } from './authentication';
12
-
13
- export type SpeechToTextOptions = SpeechAuthenticationOptions & {
14
- region?: string;
15
- sourceLanguage: string;
16
- targetLanguage?: string;
17
- };
18
-
19
- export class SpeechToText {
20
- region: string;
21
- sourceLanguage: string;
22
- targetLanguage: string;
23
- recognizer: TranslationRecognizer | undefined;
24
-
25
- private readonly authorizationManager: SpeechAuthorizationManager;
26
-
27
- constructor(options: SpeechToTextOptions) {
28
- if (!options || typeof options.sourceLanguage !== 'string' ||
29
- options.sourceLanguage.trim() === '') {
30
- throw new Error('A sourceLanguage is required.');
31
- }
32
-
33
- this.authorizationManager = new SpeechAuthorizationManager(options);
34
- this.region = options.region?.trim() ?? '';
35
- this.sourceLanguage = options.sourceLanguage;
36
- this.targetLanguage = options.targetLanguage ?? options.sourceLanguage;
37
- }
38
-
39
- async start() {
40
- await this.registerBindings(document);
41
- }
42
-
43
- async registerBindings(node: any) {
44
- const nodes = node.childNodes;
45
- for (let i = 0; i < nodes.length; i++) {
46
- if (!nodes[i]) {
47
- continue;
48
- }
49
-
50
- const currentNode = nodes[i];
51
-
52
- if (currentNode.attributes) {
53
- if (currentNode.attributes.getNamedItem('co-stt.start')) {
54
- await this.handleStartModifier(currentNode, currentNode.attributes.getNamedItem('co-stt.start'));
55
- } else if (currentNode.attributes.getNamedItem('co-stt.stop')) {
56
- await this.handleStopModifier(currentNode, currentNode.attributes.getNamedItem('co-stt.stop'));
57
- }
58
- }
59
-
60
- if (currentNode.childNodes.length > 0) {
61
- await this.registerBindings(currentNode);
62
- }
63
- }
64
- }
65
-
66
- private async createSpeechTranslationConfig():
67
- Promise<SpeechTranslationConfig> {
68
- const authorization =
69
- await this.authorizationManager.getAuthorization();
70
-
71
- const speechConfig =
72
- SpeechTranslationConfig.fromAuthorizationToken(
73
- authorization.token,
74
- authorization.region,
75
- );
76
-
77
- speechConfig.speechRecognitionLanguage =
78
- this.sourceLanguage;
79
-
80
- speechConfig.addTargetLanguage(
81
- this.targetLanguage,
82
- );
83
-
84
- this.region = authorization.region;
85
-
86
- return speechConfig;
87
- }
88
-
89
- async handleStartModifier(node: any, attr: Attr) {
90
- node.addEventListener('click', async (_: any) => {
91
- try {
92
- const speechConfig = await this.createSpeechTranslationConfig();
93
- const audioConfig = AudioConfig.fromDefaultMicrophoneInput();
94
-
95
- this.recognizer = new TranslationRecognizer(speechConfig, audioConfig);
96
-
97
- document.dispatchEvent(new CustomEvent('COAzureSTTStartedRecording', {}));
98
-
99
- const prevResults = [];
100
- this.recognizer.recognizing = (sender, event) => {
101
- const result = event.result;
102
-
103
- if (result && result.reason === ResultReason.TranslatingSpeech) {
104
- const translation = result.translations.get(this.targetLanguage);
105
- prevResults['result_' +result.privOffset.toString()] = translation;
106
- const totalResult = Object.values(prevResults).join('. ');
107
-
108
- const inputElement = document.getElementById(attr.value);
109
- if (inputElement !== null) {
110
- if (inputElement instanceof HTMLInputElement) {
111
- inputElement.value = `${totalResult} `;
112
- } else {
113
- inputElement.innerHTML = `${totalResult} `;
114
- }
115
- }
116
- }
117
- };
118
-
119
- this.recognizer.startContinuousRecognitionAsync(
120
- () => {},
121
- (err) => {
122
- this.dispatchError(err, 'RECOGNITION_ERROR');
123
- this.stop();
124
- },
125
- );
126
- } catch (error) {
127
- this.dispatchError(error);
128
- await this.stop();
129
- }
130
- });
131
- }
132
-
133
- async handleStopModifier(node: any, attr: Attr) {
134
- node.addEventListener('click', async (_: any) => {
135
- await this.stop();
136
- });
137
- }
138
-
139
- async stop() {
140
- if (this.recognizer !== undefined) {
141
- try {
142
- this.recognizer.stopContinuousRecognitionAsync();
143
- } catch {
144
- // Recognizer may already be stopping.
145
- }
146
-
147
- try {
148
- this.recognizer.close();
149
- } catch {
150
- // Recognizer may already be closed.
151
- }
152
-
153
- this.recognizer = undefined;
154
- }
155
- document.dispatchEvent(new CustomEvent('COAzureSTTStoppedRecording', {}));
156
- }
157
-
158
- private dispatchError(error: unknown, code?: string) {
159
- const detail = toSafeErrorDetail(error);
160
- if (code && !detail.code) {
161
- detail.code = code;
162
- }
163
-
164
- document.dispatchEvent(
165
- new CustomEvent('COAzureSTTError', {
166
- detail: {
167
- error: detail,
168
- },
169
- }),
170
- );
171
- }
172
- }