@lee-zg/melange 1.0.0
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/LICENSE +21 -0
- package/README.md +256 -0
- package/dist/chunk-2PXWQDZC.js +659 -0
- package/dist/chunk-2PXWQDZC.js.map +1 -0
- package/dist/chunk-352XNR3C.js +716 -0
- package/dist/chunk-352XNR3C.js.map +1 -0
- package/dist/chunk-7QVYU63E.js +6 -0
- package/dist/chunk-7QVYU63E.js.map +1 -0
- package/dist/chunk-ALBD5XC5.js +285 -0
- package/dist/chunk-ALBD5XC5.js.map +1 -0
- package/dist/chunk-O7K662J5.cjs +842 -0
- package/dist/chunk-O7K662J5.cjs.map +1 -0
- package/dist/chunk-PK6SKIKE.cjs +8 -0
- package/dist/chunk-PK6SKIKE.cjs.map +1 -0
- package/dist/chunk-Q73NOVWX.cjs +789 -0
- package/dist/chunk-Q73NOVWX.cjs.map +1 -0
- package/dist/chunk-Q7XG6YN6.cjs +682 -0
- package/dist/chunk-Q7XG6YN6.cjs.map +1 -0
- package/dist/chunk-YGMBCZJQ.js +833 -0
- package/dist/chunk-YGMBCZJQ.js.map +1 -0
- package/dist/chunk-ZT6HVG4G.cjs +330 -0
- package/dist/chunk-ZT6HVG4G.cjs.map +1 -0
- package/dist/core/index.cjs +97 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +718 -0
- package/dist/core/index.d.ts +718 -0
- package/dist/core/index.js +4 -0
- package/dist/core/index.js.map +1 -0
- package/dist/fp/index.cjs +185 -0
- package/dist/fp/index.cjs.map +1 -0
- package/dist/fp/index.d.cts +913 -0
- package/dist/fp/index.d.ts +913 -0
- package/dist/fp/index.js +4 -0
- package/dist/fp/index.js.map +1 -0
- package/dist/index.cjs +608 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/index.cjs +41 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.cts +643 -0
- package/dist/plugins/index.d.ts +643 -0
- package/dist/plugins/index.js +4 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/types-BtOUCLB-.d.cts +293 -0
- package/dist/types-BtOUCLB-.d.ts +293 -0
- package/dist/utils/index.cjs +297 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +1179 -0
- package/dist/utils/index.d.ts +1179 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +132 -0
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview 语音模块类型定义
|
|
3
|
+
* @module melange/plugins/speech/types
|
|
4
|
+
* @description 语音合成和语音识别的类型定义
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 语音服务提供商类型
|
|
8
|
+
*/
|
|
9
|
+
type SpeechProviderType = 'browser' | 'azure' | 'google' | 'aws' | 'custom';
|
|
10
|
+
/**
|
|
11
|
+
* 语音服务状态
|
|
12
|
+
*/
|
|
13
|
+
type SpeechServiceStatus = 'idle' | 'loading' | 'ready' | 'error';
|
|
14
|
+
/**
|
|
15
|
+
* 语音服务错误
|
|
16
|
+
*/
|
|
17
|
+
interface SpeechError {
|
|
18
|
+
/** 错误代码 */
|
|
19
|
+
code: string;
|
|
20
|
+
/** 错误信息 */
|
|
21
|
+
message: string;
|
|
22
|
+
/** 原始错误对象 */
|
|
23
|
+
originalError?: Error;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 语音服务基础配置
|
|
27
|
+
*/
|
|
28
|
+
interface BaseSpeechConfig {
|
|
29
|
+
/** 语言代码 (如 'zh-CN', 'en-US') */
|
|
30
|
+
lang?: string;
|
|
31
|
+
/** 首选提供商 */
|
|
32
|
+
preferredProvider?: SpeechProviderType;
|
|
33
|
+
/** 是否自动降级 */
|
|
34
|
+
autoFallback?: boolean;
|
|
35
|
+
/** 降级提供商列表 */
|
|
36
|
+
fallbackProviders?: SpeechProviderType[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 语音信息
|
|
40
|
+
*/
|
|
41
|
+
interface VoiceInfo {
|
|
42
|
+
/** 语音唯一标识 */
|
|
43
|
+
id: string;
|
|
44
|
+
/** 语音名称 */
|
|
45
|
+
name: string;
|
|
46
|
+
/** 语言代码 */
|
|
47
|
+
lang: string;
|
|
48
|
+
/** 是否为本地语音 */
|
|
49
|
+
localService: boolean;
|
|
50
|
+
/** 是否为默认语音 */
|
|
51
|
+
default: boolean;
|
|
52
|
+
/** 提供商类型 */
|
|
53
|
+
provider: SpeechProviderType;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 语音合成配置
|
|
57
|
+
*/
|
|
58
|
+
interface SynthesisConfig extends BaseSpeechConfig {
|
|
59
|
+
/** 语音对象或语音名称 */
|
|
60
|
+
voice?: VoiceInfo | string;
|
|
61
|
+
/** 音量 (0-1) */
|
|
62
|
+
volume?: number;
|
|
63
|
+
/** 语速 (0.1-10) */
|
|
64
|
+
rate?: number;
|
|
65
|
+
/** 音调 (0-2) */
|
|
66
|
+
pitch?: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 语音合成事件类型
|
|
70
|
+
*/
|
|
71
|
+
type SynthesisEventType = 'start' | 'end' | 'pause' | 'resume' | 'boundary' | 'mark' | 'error';
|
|
72
|
+
/**
|
|
73
|
+
* 语音合成事件数据
|
|
74
|
+
*/
|
|
75
|
+
interface SynthesisEvent {
|
|
76
|
+
/** 事件类型 */
|
|
77
|
+
type: SynthesisEventType;
|
|
78
|
+
/** 当前字符索引 */
|
|
79
|
+
charIndex?: number;
|
|
80
|
+
/** 当前字符长度 */
|
|
81
|
+
charLength?: number;
|
|
82
|
+
/** 经过的时间(毫秒) */
|
|
83
|
+
elapsedTime?: number;
|
|
84
|
+
/** 边界名称 */
|
|
85
|
+
name?: string;
|
|
86
|
+
/** 错误信息 */
|
|
87
|
+
error?: SpeechError;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 语音合成事件处理器
|
|
91
|
+
*/
|
|
92
|
+
type SynthesisEventHandler = (event: SynthesisEvent) => void;
|
|
93
|
+
/**
|
|
94
|
+
* 语音合成提供商接口
|
|
95
|
+
*/
|
|
96
|
+
interface SynthesisProvider {
|
|
97
|
+
/** 提供商类型 */
|
|
98
|
+
readonly type: SpeechProviderType;
|
|
99
|
+
/** 检查是否可用 */
|
|
100
|
+
isAvailable(): boolean;
|
|
101
|
+
/** 获取可用语音列表 */
|
|
102
|
+
getVoices(): Promise<VoiceInfo[]>;
|
|
103
|
+
/** 朗读文本 */
|
|
104
|
+
speak(text: string, config?: SynthesisConfig): Promise<void>;
|
|
105
|
+
/** 暂停 */
|
|
106
|
+
pause(): void;
|
|
107
|
+
/** 继续 */
|
|
108
|
+
resume(): void;
|
|
109
|
+
/** 取消 */
|
|
110
|
+
cancel(): void;
|
|
111
|
+
/** 是否正在朗读 */
|
|
112
|
+
isSpeaking(): boolean;
|
|
113
|
+
/** 是否已暂停 */
|
|
114
|
+
isPaused(): boolean;
|
|
115
|
+
/** 添加事件监听 */
|
|
116
|
+
on(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
117
|
+
/** 移除事件监听 */
|
|
118
|
+
off(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 语音合成器接口
|
|
122
|
+
*/
|
|
123
|
+
interface SpeechSynthesizer {
|
|
124
|
+
/** 当前使用的提供商 */
|
|
125
|
+
readonly currentProvider: SpeechProviderType;
|
|
126
|
+
/** 服务状态 */
|
|
127
|
+
readonly status: SpeechServiceStatus;
|
|
128
|
+
/** 初始化 */
|
|
129
|
+
initialize(config?: SynthesisConfig): Promise<void>;
|
|
130
|
+
/** 获取可用语音列表 */
|
|
131
|
+
getVoices(): Promise<VoiceInfo[]>;
|
|
132
|
+
/** 朗读文本 */
|
|
133
|
+
speak(text: string, config?: SynthesisConfig): Promise<void>;
|
|
134
|
+
/** 暂停 */
|
|
135
|
+
pause(): void;
|
|
136
|
+
/** 继续 */
|
|
137
|
+
resume(): void;
|
|
138
|
+
/** 取消 */
|
|
139
|
+
cancel(): void;
|
|
140
|
+
/** 是否正在朗读 */
|
|
141
|
+
isSpeaking(): boolean;
|
|
142
|
+
/** 是否已暂停 */
|
|
143
|
+
isPaused(): boolean;
|
|
144
|
+
/** 添加事件监听 */
|
|
145
|
+
on(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
146
|
+
/** 移除事件监听 */
|
|
147
|
+
off(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
148
|
+
/** 销毁实例 */
|
|
149
|
+
dispose(): void;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 识别结果项
|
|
153
|
+
*/
|
|
154
|
+
interface RecognitionResultItem {
|
|
155
|
+
/** 识别的文本 */
|
|
156
|
+
transcript: string;
|
|
157
|
+
/** 置信度 (0-1) */
|
|
158
|
+
confidence: number;
|
|
159
|
+
/** 是否为最终结果 */
|
|
160
|
+
isFinal: boolean;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 识别结果
|
|
164
|
+
*/
|
|
165
|
+
interface RecognitionResult {
|
|
166
|
+
/** 结果列表 */
|
|
167
|
+
results: RecognitionResultItem[];
|
|
168
|
+
/** 最佳结果 */
|
|
169
|
+
bestTranscript: string;
|
|
170
|
+
/** 最佳置信度 */
|
|
171
|
+
bestConfidence: number;
|
|
172
|
+
/** 是否为最终结果 */
|
|
173
|
+
isFinal: boolean;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 语音识别配置
|
|
177
|
+
*/
|
|
178
|
+
interface RecognitionConfig extends BaseSpeechConfig {
|
|
179
|
+
/** 是否连续识别 */
|
|
180
|
+
continuous?: boolean;
|
|
181
|
+
/** 是否返回中间结果 */
|
|
182
|
+
interimResults?: boolean;
|
|
183
|
+
/** 最大备选结果数 */
|
|
184
|
+
maxAlternatives?: number;
|
|
185
|
+
/** 语法列表 (仅部分浏览器支持) */
|
|
186
|
+
grammars?: string[];
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 语音识别事件类型
|
|
190
|
+
*/
|
|
191
|
+
type RecognitionEventType = 'start' | 'end' | 'result' | 'error' | 'soundstart' | 'soundend' | 'speechstart' | 'speechend' | 'audiostart' | 'audioend' | 'nomatch';
|
|
192
|
+
/**
|
|
193
|
+
* 语音识别事件数据
|
|
194
|
+
*/
|
|
195
|
+
interface RecognitionEvent {
|
|
196
|
+
/** 事件类型 */
|
|
197
|
+
type: RecognitionEventType;
|
|
198
|
+
/** 识别结果 (仅 result 事件) */
|
|
199
|
+
result?: RecognitionResult;
|
|
200
|
+
/** 错误信息 (仅 error 事件) */
|
|
201
|
+
error?: SpeechError;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 语音识别事件处理器
|
|
205
|
+
*/
|
|
206
|
+
type RecognitionEventHandler = (event: RecognitionEvent) => void;
|
|
207
|
+
/**
|
|
208
|
+
* 语音识别提供商接口
|
|
209
|
+
*/
|
|
210
|
+
interface RecognitionProvider {
|
|
211
|
+
/** 提供商类型 */
|
|
212
|
+
readonly type: SpeechProviderType;
|
|
213
|
+
/** 检查是否可用 */
|
|
214
|
+
isAvailable(): boolean;
|
|
215
|
+
/** 开始识别 */
|
|
216
|
+
start(config?: RecognitionConfig): Promise<void>;
|
|
217
|
+
/** 停止识别 */
|
|
218
|
+
stop(): void;
|
|
219
|
+
/** 中止识别 */
|
|
220
|
+
abort(): void;
|
|
221
|
+
/** 是否正在识别 */
|
|
222
|
+
isListening(): boolean;
|
|
223
|
+
/** 添加事件监听 */
|
|
224
|
+
on(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
225
|
+
/** 移除事件监听 */
|
|
226
|
+
off(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* 语音识别器接口
|
|
230
|
+
*/
|
|
231
|
+
interface SpeechRecognizer {
|
|
232
|
+
/** 当前使用的提供商 */
|
|
233
|
+
readonly currentProvider: SpeechProviderType;
|
|
234
|
+
/** 服务状态 */
|
|
235
|
+
readonly status: SpeechServiceStatus;
|
|
236
|
+
/** 初始化 */
|
|
237
|
+
initialize(config?: RecognitionConfig): Promise<void>;
|
|
238
|
+
/** 开始识别 */
|
|
239
|
+
start(config?: RecognitionConfig): Promise<void>;
|
|
240
|
+
/** 停止识别 */
|
|
241
|
+
stop(): void;
|
|
242
|
+
/** 中止识别 */
|
|
243
|
+
abort(): void;
|
|
244
|
+
/** 是否正在识别 */
|
|
245
|
+
isListening(): boolean;
|
|
246
|
+
/** 添加事件监听 */
|
|
247
|
+
on(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
248
|
+
/** 移除事件监听 */
|
|
249
|
+
off(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
250
|
+
/** 销毁实例 */
|
|
251
|
+
dispose(): void;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Azure 语音服务配置
|
|
255
|
+
*/
|
|
256
|
+
interface AzureSpeechConfig {
|
|
257
|
+
/** 订阅密钥 */
|
|
258
|
+
subscriptionKey: string;
|
|
259
|
+
/** 服务区域 */
|
|
260
|
+
region: string;
|
|
261
|
+
/** 自定义端点 */
|
|
262
|
+
endpoint?: string;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Google Cloud Speech 配置
|
|
266
|
+
*/
|
|
267
|
+
interface GoogleSpeechConfig {
|
|
268
|
+
/** API 密钥 */
|
|
269
|
+
apiKey: string;
|
|
270
|
+
/** 项目 ID */
|
|
271
|
+
projectId?: string;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* AWS Polly/Transcribe 配置
|
|
275
|
+
*/
|
|
276
|
+
interface AWSSpeechConfig {
|
|
277
|
+
/** 访问密钥 ID */
|
|
278
|
+
accessKeyId: string;
|
|
279
|
+
/** 秘密访问密钥 */
|
|
280
|
+
secretAccessKey: string;
|
|
281
|
+
/** 区域 */
|
|
282
|
+
region: string;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 自定义提供商配置
|
|
286
|
+
*/
|
|
287
|
+
interface CustomProviderConfig {
|
|
288
|
+
/** 合成 API 端点 */
|
|
289
|
+
synthesisEndpoint?: string;
|
|
290
|
+
/** 识别 API 端点 */
|
|
291
|
+
recognitionEndpoint?: string;
|
|
292
|
+
/** 自定义请求头 */
|
|
293
|
+
headers?: Record<string, string>;
|
|
294
|
+
/** 认证令牌 */
|
|
295
|
+
authToken?: string;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* 提供商配置联合类型
|
|
299
|
+
*/
|
|
300
|
+
type ProviderConfig = AzureSpeechConfig | GoogleSpeechConfig | AWSSpeechConfig | CustomProviderConfig;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* @fileoverview 语音合成 (TTS) 实现
|
|
304
|
+
* @module melange/plugins/speech/synthesis
|
|
305
|
+
* @description 提供语音合成功能,支持浏览器原生 API 和第三方服务降级
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 语音合成器
|
|
310
|
+
* 管理多个提供商,支持自动降级
|
|
311
|
+
*/
|
|
312
|
+
declare class SpeechSynthesizerImpl implements SpeechSynthesizer {
|
|
313
|
+
private _currentProvider;
|
|
314
|
+
private _status;
|
|
315
|
+
private provider;
|
|
316
|
+
private config;
|
|
317
|
+
private eventHandlers;
|
|
318
|
+
private providers;
|
|
319
|
+
/**
|
|
320
|
+
* 当前使用的提供商类型
|
|
321
|
+
*/
|
|
322
|
+
get currentProvider(): SpeechProviderType;
|
|
323
|
+
/**
|
|
324
|
+
* 服务状态
|
|
325
|
+
*/
|
|
326
|
+
get status(): SpeechServiceStatus;
|
|
327
|
+
/**
|
|
328
|
+
* 初始化语音合成器
|
|
329
|
+
* @param config - 合成配置
|
|
330
|
+
*/
|
|
331
|
+
initialize(config?: SynthesisConfig): Promise<void>;
|
|
332
|
+
/**
|
|
333
|
+
* 尝试使用指定提供商
|
|
334
|
+
*/
|
|
335
|
+
private tryProvider;
|
|
336
|
+
/**
|
|
337
|
+
* 转发提供商事件到合成器
|
|
338
|
+
*/
|
|
339
|
+
private forwardProviderEvents;
|
|
340
|
+
/**
|
|
341
|
+
* 获取可用语音列表
|
|
342
|
+
*/
|
|
343
|
+
getVoices(): Promise<VoiceInfo[]>;
|
|
344
|
+
/**
|
|
345
|
+
* 朗读文本
|
|
346
|
+
* @param text - 要朗读的文本
|
|
347
|
+
* @param config - 可选的合成配置
|
|
348
|
+
*/
|
|
349
|
+
speak(text: string, config?: SynthesisConfig): Promise<void>;
|
|
350
|
+
/**
|
|
351
|
+
* 暂停朗读
|
|
352
|
+
*/
|
|
353
|
+
pause(): void;
|
|
354
|
+
/**
|
|
355
|
+
* 继续朗读
|
|
356
|
+
*/
|
|
357
|
+
resume(): void;
|
|
358
|
+
/**
|
|
359
|
+
* 取消朗读
|
|
360
|
+
*/
|
|
361
|
+
cancel(): void;
|
|
362
|
+
/**
|
|
363
|
+
* 是否正在朗读
|
|
364
|
+
*/
|
|
365
|
+
isSpeaking(): boolean;
|
|
366
|
+
/**
|
|
367
|
+
* 是否已暂停
|
|
368
|
+
*/
|
|
369
|
+
isPaused(): boolean;
|
|
370
|
+
/**
|
|
371
|
+
* 添加事件监听
|
|
372
|
+
*/
|
|
373
|
+
on(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
374
|
+
/**
|
|
375
|
+
* 移除事件监听
|
|
376
|
+
*/
|
|
377
|
+
off(event: SynthesisEventType, handler: SynthesisEventHandler): void;
|
|
378
|
+
/**
|
|
379
|
+
* 触发事件
|
|
380
|
+
*/
|
|
381
|
+
private emit;
|
|
382
|
+
/**
|
|
383
|
+
* 销毁实例
|
|
384
|
+
*/
|
|
385
|
+
dispose(): void;
|
|
386
|
+
/**
|
|
387
|
+
* 注册自定义提供商
|
|
388
|
+
* @param type - 提供商类型
|
|
389
|
+
* @param provider - 提供商实例
|
|
390
|
+
*/
|
|
391
|
+
registerProvider(type: SpeechProviderType, provider: SynthesisProvider): void;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* 创建语音合成器实例
|
|
395
|
+
* @param config - 可选的合成配置
|
|
396
|
+
* @returns 语音合成器实例
|
|
397
|
+
*
|
|
398
|
+
* @example
|
|
399
|
+
* ```typescript
|
|
400
|
+
* // 创建并初始化语音合成器
|
|
401
|
+
* const synthesizer = await createSpeechSynthesizer({
|
|
402
|
+
* lang: 'zh-CN',
|
|
403
|
+
* rate: 1.0,
|
|
404
|
+
* pitch: 1.0,
|
|
405
|
+
* });
|
|
406
|
+
*
|
|
407
|
+
* // 朗读文本
|
|
408
|
+
* await synthesizer.speak('你好,世界!');
|
|
409
|
+
*
|
|
410
|
+
* // 监听事件
|
|
411
|
+
* synthesizer.on('end', () => {
|
|
412
|
+
* console.log('朗读完成');
|
|
413
|
+
* });
|
|
414
|
+
*
|
|
415
|
+
* // 销毁
|
|
416
|
+
* synthesizer.dispose();
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
declare function createSpeechSynthesizer(config?: SynthesisConfig): Promise<SpeechSynthesizer>;
|
|
420
|
+
/**
|
|
421
|
+
* 检查当前环境是否支持语音合成
|
|
422
|
+
* @returns 是否支持语音合成
|
|
423
|
+
*/
|
|
424
|
+
declare function isSpeechSynthesisSupported(): boolean;
|
|
425
|
+
/**
|
|
426
|
+
* 快速朗读文本(一次性使用)
|
|
427
|
+
* @param text - 要朗读的文本
|
|
428
|
+
* @param config - 可选的合成配置
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```typescript
|
|
432
|
+
* // 快速朗读
|
|
433
|
+
* await speak('你好,世界!');
|
|
434
|
+
*
|
|
435
|
+
* // 带配置的朗读
|
|
436
|
+
* await speak('Hello World', { lang: 'en-US', rate: 0.8 });
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
declare function speak(text: string, config?: SynthesisConfig): Promise<void>;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* @fileoverview 语音识别 (STT) 实现
|
|
443
|
+
* @module melange/plugins/speech/recognition
|
|
444
|
+
* @description 提供语音识别功能,支持浏览器原生 API 和第三方服务降级
|
|
445
|
+
*/
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* SpeechRecognition 结果列表接口
|
|
449
|
+
*/
|
|
450
|
+
interface BrowserSpeechRecognitionResultList {
|
|
451
|
+
readonly length: number;
|
|
452
|
+
item(index: number): BrowserSpeechRecognitionResult | null;
|
|
453
|
+
[index: number]: BrowserSpeechRecognitionResult;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* SpeechRecognition 结果接口
|
|
457
|
+
*/
|
|
458
|
+
interface BrowserSpeechRecognitionResult {
|
|
459
|
+
readonly length: number;
|
|
460
|
+
readonly isFinal: boolean;
|
|
461
|
+
item(index: number): BrowserSpeechRecognitionAlternative | null;
|
|
462
|
+
[index: number]: BrowserSpeechRecognitionAlternative;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* SpeechRecognition 备选结果接口
|
|
466
|
+
*/
|
|
467
|
+
interface BrowserSpeechRecognitionAlternative {
|
|
468
|
+
readonly transcript: string;
|
|
469
|
+
readonly confidence: number;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* SpeechRecognition 事件接口
|
|
473
|
+
*/
|
|
474
|
+
interface BrowserSpeechRecognitionEvent {
|
|
475
|
+
readonly results: BrowserSpeechRecognitionResultList;
|
|
476
|
+
readonly resultIndex: number;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* SpeechRecognition 错误事件接口
|
|
480
|
+
*/
|
|
481
|
+
interface BrowserSpeechRecognitionErrorEvent {
|
|
482
|
+
readonly error: string;
|
|
483
|
+
readonly message: string;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* 浏览器原生 SpeechRecognition 接口
|
|
487
|
+
*/
|
|
488
|
+
interface BrowserSpeechRecognition extends EventTarget {
|
|
489
|
+
lang: string;
|
|
490
|
+
continuous: boolean;
|
|
491
|
+
interimResults: boolean;
|
|
492
|
+
maxAlternatives: number;
|
|
493
|
+
start(): void;
|
|
494
|
+
stop(): void;
|
|
495
|
+
abort(): void;
|
|
496
|
+
onstart: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
497
|
+
onend: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
498
|
+
onresult: ((this: BrowserSpeechRecognition, ev: BrowserSpeechRecognitionEvent) => void) | null;
|
|
499
|
+
onerror: ((this: BrowserSpeechRecognition, ev: BrowserSpeechRecognitionErrorEvent) => void) | null;
|
|
500
|
+
onsoundstart: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
501
|
+
onsoundend: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
502
|
+
onspeechstart: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
503
|
+
onspeechend: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
504
|
+
onaudiostart: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
505
|
+
onaudioend: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
506
|
+
onnomatch: ((this: BrowserSpeechRecognition, ev: Event) => void) | null;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* SpeechRecognition 构造函数类型
|
|
510
|
+
*/
|
|
511
|
+
interface BrowserSpeechRecognitionConstructor {
|
|
512
|
+
new (): BrowserSpeechRecognition;
|
|
513
|
+
}
|
|
514
|
+
declare global {
|
|
515
|
+
interface Window {
|
|
516
|
+
SpeechRecognition?: BrowserSpeechRecognitionConstructor;
|
|
517
|
+
webkitSpeechRecognition?: BrowserSpeechRecognitionConstructor;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* 语音识别器
|
|
522
|
+
* 管理多个提供商,支持自动降级
|
|
523
|
+
*/
|
|
524
|
+
declare class SpeechRecognizerImpl implements SpeechRecognizer {
|
|
525
|
+
private _currentProvider;
|
|
526
|
+
private _status;
|
|
527
|
+
private provider;
|
|
528
|
+
private config;
|
|
529
|
+
private eventHandlers;
|
|
530
|
+
private providers;
|
|
531
|
+
/**
|
|
532
|
+
* 当前使用的提供商类型
|
|
533
|
+
*/
|
|
534
|
+
get currentProvider(): SpeechProviderType;
|
|
535
|
+
/**
|
|
536
|
+
* 服务状态
|
|
537
|
+
*/
|
|
538
|
+
get status(): SpeechServiceStatus;
|
|
539
|
+
/**
|
|
540
|
+
* 初始化语音识别器
|
|
541
|
+
* @param config - 识别配置
|
|
542
|
+
*/
|
|
543
|
+
initialize(config?: RecognitionConfig): Promise<void>;
|
|
544
|
+
/**
|
|
545
|
+
* 尝试使用指定提供商
|
|
546
|
+
*/
|
|
547
|
+
private tryProvider;
|
|
548
|
+
/**
|
|
549
|
+
* 转发提供商事件到识别器
|
|
550
|
+
*/
|
|
551
|
+
private forwardProviderEvents;
|
|
552
|
+
/**
|
|
553
|
+
* 开始语音识别
|
|
554
|
+
* @param config - 可选的识别配置
|
|
555
|
+
*/
|
|
556
|
+
start(config?: RecognitionConfig): Promise<void>;
|
|
557
|
+
/**
|
|
558
|
+
* 停止语音识别
|
|
559
|
+
*/
|
|
560
|
+
stop(): void;
|
|
561
|
+
/**
|
|
562
|
+
* 中止语音识别
|
|
563
|
+
*/
|
|
564
|
+
abort(): void;
|
|
565
|
+
/**
|
|
566
|
+
* 是否正在监听
|
|
567
|
+
*/
|
|
568
|
+
isListening(): boolean;
|
|
569
|
+
/**
|
|
570
|
+
* 添加事件监听
|
|
571
|
+
*/
|
|
572
|
+
on(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
573
|
+
/**
|
|
574
|
+
* 移除事件监听
|
|
575
|
+
*/
|
|
576
|
+
off(event: RecognitionEventType, handler: RecognitionEventHandler): void;
|
|
577
|
+
/**
|
|
578
|
+
* 触发事件
|
|
579
|
+
*/
|
|
580
|
+
private emit;
|
|
581
|
+
/**
|
|
582
|
+
* 销毁实例
|
|
583
|
+
*/
|
|
584
|
+
dispose(): void;
|
|
585
|
+
/**
|
|
586
|
+
* 注册自定义提供商
|
|
587
|
+
* @param type - 提供商类型
|
|
588
|
+
* @param provider - 提供商实例
|
|
589
|
+
*/
|
|
590
|
+
registerProvider(type: SpeechProviderType, provider: RecognitionProvider): void;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* 创建语音识别器实例
|
|
594
|
+
* @param config - 可选的识别配置
|
|
595
|
+
* @returns 语音识别器实例
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```typescript
|
|
599
|
+
* // 创建并初始化语音识别器
|
|
600
|
+
* const recognizer = await createSpeechRecognizer({
|
|
601
|
+
* lang: 'zh-CN',
|
|
602
|
+
* continuous: true,
|
|
603
|
+
* interimResults: true,
|
|
604
|
+
* });
|
|
605
|
+
*
|
|
606
|
+
* // 监听识别结果
|
|
607
|
+
* recognizer.on('result', (event) => {
|
|
608
|
+
* if (event.result) {
|
|
609
|
+
* console.log('识别结果:', event.result.bestTranscript);
|
|
610
|
+
* }
|
|
611
|
+
* });
|
|
612
|
+
*
|
|
613
|
+
* // 开始识别
|
|
614
|
+
* await recognizer.start();
|
|
615
|
+
*
|
|
616
|
+
* // 停止识别
|
|
617
|
+
* recognizer.stop();
|
|
618
|
+
*
|
|
619
|
+
* // 销毁
|
|
620
|
+
* recognizer.dispose();
|
|
621
|
+
* ```
|
|
622
|
+
*/
|
|
623
|
+
declare function createSpeechRecognizer(config?: RecognitionConfig): Promise<SpeechRecognizer>;
|
|
624
|
+
/**
|
|
625
|
+
* 检查当前环境是否支持语音识别
|
|
626
|
+
* @returns 是否支持语音识别
|
|
627
|
+
*/
|
|
628
|
+
declare function isSpeechRecognitionSupported(): boolean;
|
|
629
|
+
/**
|
|
630
|
+
* 快速进行一次语音识别
|
|
631
|
+
* @param config - 可选的识别配置
|
|
632
|
+
* @returns 识别结果
|
|
633
|
+
*
|
|
634
|
+
* @example
|
|
635
|
+
* ```typescript
|
|
636
|
+
* // 进行一次语音识别
|
|
637
|
+
* const result = await listen({ lang: 'zh-CN' });
|
|
638
|
+
* console.log('识别结果:', result.bestTranscript);
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
declare function listen(config?: RecognitionConfig): Promise<RecognitionResult>;
|
|
642
|
+
|
|
643
|
+
export { type AWSSpeechConfig, type AzureSpeechConfig, type BaseSpeechConfig, type CustomProviderConfig, type GoogleSpeechConfig, type ProviderConfig, type RecognitionConfig, type RecognitionEvent, type RecognitionEventHandler, type RecognitionEventType, type RecognitionProvider, type RecognitionResult, type RecognitionResultItem, type SpeechError, type SpeechProviderType, type SpeechRecognizer, SpeechRecognizerImpl, type SpeechServiceStatus, type SpeechSynthesizer, SpeechSynthesizerImpl, type SynthesisConfig, type SynthesisEvent, type SynthesisEventHandler, type SynthesisEventType, type SynthesisProvider, type VoiceInfo, createSpeechRecognizer, createSpeechSynthesizer, isSpeechRecognitionSupported, isSpeechSynthesisSupported, listen, speak };
|