@lee-zg/melange 1.0.0 → 1.2.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.
Files changed (39) hide show
  1. package/dist/chunk-3RM45M64.js +1637 -0
  2. package/dist/chunk-3RM45M64.js.map +1 -0
  3. package/dist/{chunk-ALBD5XC5.js → chunk-GXFWPL5M.js} +4 -8
  4. package/dist/chunk-GXFWPL5M.js.map +1 -0
  5. package/dist/{chunk-Q73NOVWX.cjs → chunk-KUJARU3D.cjs} +7 -24
  6. package/dist/chunk-KUJARU3D.cjs.map +1 -0
  7. package/dist/{chunk-2PXWQDZC.js → chunk-MBBJMHTS.js} +3 -5
  8. package/dist/chunk-MBBJMHTS.js.map +1 -0
  9. package/dist/{chunk-ZT6HVG4G.cjs → chunk-UYJUSNDI.cjs} +4 -8
  10. package/dist/chunk-UYJUSNDI.cjs.map +1 -0
  11. package/dist/{chunk-Q7XG6YN6.cjs → chunk-V5THPEB2.cjs} +3 -5
  12. package/dist/chunk-V5THPEB2.cjs.map +1 -0
  13. package/dist/{chunk-352XNR3C.js → chunk-VGZMISJD.js} +7 -24
  14. package/dist/chunk-VGZMISJD.js.map +1 -0
  15. package/dist/chunk-YZVCK6VZ.cjs +1646 -0
  16. package/dist/chunk-YZVCK6VZ.cjs.map +1 -0
  17. package/dist/core/index.cjs +23 -23
  18. package/dist/core/index.js +1 -1
  19. package/dist/fp/index.cjs +45 -45
  20. package/dist/fp/index.js +1 -1
  21. package/dist/index.cjs +147 -147
  22. package/dist/index.js +4 -4
  23. package/dist/plugins/index.cjs +9 -9
  24. package/dist/plugins/index.d.cts +287 -133
  25. package/dist/plugins/index.d.ts +287 -133
  26. package/dist/plugins/index.js +1 -1
  27. package/dist/utils/index.cjs +73 -73
  28. package/dist/utils/index.js +1 -1
  29. package/package.json +5 -2
  30. package/dist/chunk-2PXWQDZC.js.map +0 -1
  31. package/dist/chunk-352XNR3C.js.map +0 -1
  32. package/dist/chunk-ALBD5XC5.js.map +0 -1
  33. package/dist/chunk-O7K662J5.cjs +0 -842
  34. package/dist/chunk-O7K662J5.cjs.map +0 -1
  35. package/dist/chunk-Q73NOVWX.cjs.map +0 -1
  36. package/dist/chunk-Q7XG6YN6.cjs.map +0 -1
  37. package/dist/chunk-YGMBCZJQ.js +0 -833
  38. package/dist/chunk-YGMBCZJQ.js.map +0 -1
  39. package/dist/chunk-ZT6HVG4G.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- export { allPass, alt, anyPass, apply, compose, constant, curry, err, filterOption, flatMapOption, flatMapResult, flip, flow, fromNullable, getOrElse, getOrElseL, identity, invert, isErr, isNone, isOk, isSome, mapOption, mapResult, matchOption, matchResult, memoize, none, noop, not, ok, once, partial, partialRight, pipe, some, tap, thunk, toNullable, tryCatch, tryCatchAsync, uncurry, unwrapOr, unwrapOrElse } from './chunk-ALBD5XC5.js';
2
- export { camelCase, capitalize, chunk, collapseWhitespace, constantCase, countOccurrences, createDeferred, debounce, deepClone, deepMerge, delay, difference, escapeHtml, filterObject, first, flatten, flattenDeep, get, groupBy, has, intersection, isArray, isBoolean, isDate, isEmail, isEmpty, isFunction, isInRange, isInteger, isNegative, isNil, isNotEmpty, isNotNil, isNumber, isObject, isPlainObject, isPositive, isPromise, isString, isURL, isUUID, kebabCase, last, mapValues, omit, padEnd, padStart, parallel, partition, pascalCase, pick, randomString, range, retry, reverse, sample, sequence, set, shuffle, snakeCase, sortBy, throttle, timeout, titleCase, truncate, unescapeHtml, unique, words, zip } from './chunk-352XNR3C.js';
3
- export { Bind, Container, Debounce, Deprecated, Disposable, DisposableStore, EventEmitter, Frozen, Inject, Injectable, Lifecycle, Log, Memoize, Retry, Sealed, Singleton, Throttle, Timeout, Validate, combineDisposables, globalContainer, toDisposable } from './chunk-2PXWQDZC.js';
4
- export { SpeechRecognizerImpl, SpeechSynthesizerImpl, createSpeechRecognizer, createSpeechSynthesizer, isSpeechRecognitionSupported, isSpeechSynthesisSupported, listen, speak } from './chunk-YGMBCZJQ.js';
1
+ export { allPass, alt, anyPass, apply, compose, constant, curry, err, filterOption, flatMapOption, flatMapResult, flip, flow, fromNullable, getOrElse, getOrElseL, identity, invert, isErr, isNone, isOk, isSome, mapOption, mapResult, matchOption, matchResult, memoize, none, noop, not, ok, once, partial, partialRight, pipe, some, tap, thunk, toNullable, tryCatch, tryCatchAsync, uncurry, unwrapOr, unwrapOrElse } from './chunk-GXFWPL5M.js';
2
+ export { camelCase, capitalize, chunk, collapseWhitespace, constantCase, countOccurrences, createDeferred, debounce, deepClone, deepMerge, delay, difference, escapeHtml, filterObject, first, flatten, flattenDeep, get, groupBy, has, intersection, isArray, isBoolean, isDate, isEmail, isEmpty, isFunction, isInRange, isInteger, isNegative, isNil, isNotEmpty, isNotNil, isNumber, isObject, isPlainObject, isPositive, isPromise, isString, isURL, isUUID, kebabCase, last, mapValues, omit, padEnd, padStart, parallel, partition, pascalCase, pick, randomString, range, retry, reverse, sample, sequence, set, shuffle, snakeCase, sortBy, throttle, timeout, titleCase, truncate, unescapeHtml, unique, words, zip } from './chunk-VGZMISJD.js';
3
+ export { Bind, Container, Debounce, Deprecated, Disposable, DisposableStore, EventEmitter, Frozen, Inject, Injectable, Lifecycle, Log, Memoize, Retry, Sealed, Singleton, Throttle, Timeout, Validate, combineDisposables, globalContainer, toDisposable } from './chunk-MBBJMHTS.js';
4
+ export { SpeechRecognizerImpl, SpeechSynthesizerImpl, createSpeechRecognizer, createSpeechSynthesizer, isSpeechRecognitionSupported, isSpeechSynthesisSupported, listen, speak } from './chunk-3RM45M64.js';
5
5
  import './chunk-7QVYU63E.js';
6
6
 
7
7
  // src/index.ts
@@ -1,41 +1,41 @@
1
1
  'use strict';
2
2
 
3
- var chunkO7K662J5_cjs = require('../chunk-O7K662J5.cjs');
3
+ var chunkYZVCK6VZ_cjs = require('../chunk-YZVCK6VZ.cjs');
4
4
  require('../chunk-PK6SKIKE.cjs');
5
5
 
6
6
 
7
7
 
8
8
  Object.defineProperty(exports, "SpeechRecognizerImpl", {
9
9
  enumerable: true,
10
- get: function () { return chunkO7K662J5_cjs.SpeechRecognizerImpl; }
10
+ get: function () { return chunkYZVCK6VZ_cjs.SpeechRecognizerImpl; }
11
11
  });
12
12
  Object.defineProperty(exports, "SpeechSynthesizerImpl", {
13
13
  enumerable: true,
14
- get: function () { return chunkO7K662J5_cjs.SpeechSynthesizerImpl; }
14
+ get: function () { return chunkYZVCK6VZ_cjs.SpeechSynthesizerImpl; }
15
15
  });
16
16
  Object.defineProperty(exports, "createSpeechRecognizer", {
17
17
  enumerable: true,
18
- get: function () { return chunkO7K662J5_cjs.createSpeechRecognizer; }
18
+ get: function () { return chunkYZVCK6VZ_cjs.createSpeechRecognizer; }
19
19
  });
20
20
  Object.defineProperty(exports, "createSpeechSynthesizer", {
21
21
  enumerable: true,
22
- get: function () { return chunkO7K662J5_cjs.createSpeechSynthesizer; }
22
+ get: function () { return chunkYZVCK6VZ_cjs.createSpeechSynthesizer; }
23
23
  });
24
24
  Object.defineProperty(exports, "isSpeechRecognitionSupported", {
25
25
  enumerable: true,
26
- get: function () { return chunkO7K662J5_cjs.isSpeechRecognitionSupported; }
26
+ get: function () { return chunkYZVCK6VZ_cjs.isSpeechRecognitionSupported; }
27
27
  });
28
28
  Object.defineProperty(exports, "isSpeechSynthesisSupported", {
29
29
  enumerable: true,
30
- get: function () { return chunkO7K662J5_cjs.isSpeechSynthesisSupported; }
30
+ get: function () { return chunkYZVCK6VZ_cjs.isSpeechSynthesisSupported; }
31
31
  });
32
32
  Object.defineProperty(exports, "listen", {
33
33
  enumerable: true,
34
- get: function () { return chunkO7K662J5_cjs.listen; }
34
+ get: function () { return chunkYZVCK6VZ_cjs.listen; }
35
35
  });
36
36
  Object.defineProperty(exports, "speak", {
37
37
  enumerable: true,
38
- get: function () { return chunkO7K662J5_cjs.speak; }
38
+ get: function () { return chunkYZVCK6VZ_cjs.speak; }
39
39
  });
40
40
  //# sourceMappingURL=index.cjs.map
41
41
  //# sourceMappingURL=index.cjs.map
@@ -300,51 +300,150 @@ interface CustomProviderConfig {
300
300
  type ProviderConfig = AzureSpeechConfig | GoogleSpeechConfig | AWSSpeechConfig | CustomProviderConfig;
301
301
 
302
302
  /**
303
- * @fileoverview 语音合成 (TTS) 实现
303
+ * @fileoverview 语音合成 (TTS) 实现 - 商业级版本 v2.0
304
304
  * @module melange/plugins/speech/synthesis
305
- * @description 提供语音合成功能,支持浏览器原生 API 和第三方服务降级
305
+ * @description 生产级 Web 语音合成插件
306
+ *
307
+ * 架构:
308
+ * [UI层] -> [SpeechSynthesizerImpl] -> [NativeStrategy / CloudStrategy]
309
+ * |
310
+ * +-> [音频核心]: AudioContext, 流式播放
311
+ * +-> [适配器]: AzureAdapter, GoogleAdapter, TencentAdapter...
312
+ *
313
+ * 功能特性:
314
+ * 1. 多模式: 支持原生 Web Speech API & 云端 TTS 服务
315
+ * 2. 状态机: IDLE -> LOADING -> SPEAKING -> PAUSED
316
+ * 3. 插件化: 内置 Azure/Google/AWS/讯飞/腾讯/百度/阿里 适配器
317
+ * 4. 核心: AudioContext 流式播放 + 音频队列管理
318
+ * 5. 兼容性: 多浏览器支持 + 自动降级处理
306
319
  */
307
320
 
308
321
  /**
309
- * 语音合成器
310
- * 管理多个提供商,支持自动降级
322
+ * 合成器状态枚举
323
+ */
324
+ declare enum SynthesisStatus {
325
+ /** 空闲状态 */
326
+ IDLE = "IDLE",
327
+ /** 加载中(获取语音/准备中) */
328
+ LOADING = "LOADING",
329
+ /** 正在播放 */
330
+ SPEAKING = "SPEAKING",
331
+ /** 已暂停 */
332
+ PAUSED = "PAUSED"
333
+ }
334
+ /**
335
+ * 合成引擎模式
336
+ */
337
+ type SynthesisEngineMode = 'native' | 'cloud' | 'auto';
338
+ /**
339
+ * 云端 TTS 音频格式
340
+ */
341
+ type CloudAudioFormat = 'mp3' | 'wav' | 'ogg' | 'pcm';
342
+ /**
343
+ * 云端合成结果接口
344
+ */
345
+ interface ISynthesisResult {
346
+ /** 音频数据 */
347
+ audioData: ArrayBuffer;
348
+ /** 音频格式 */
349
+ format: CloudAudioFormat;
350
+ /** 音频时长 (ms) */
351
+ duration?: number;
352
+ /** 原始响应数据 */
353
+ original?: unknown;
354
+ }
355
+ /**
356
+ * 云端语音信息
357
+ */
358
+ interface ICloudVoice {
359
+ /** 语音 ID */
360
+ id: string;
361
+ /** 语音名称 */
362
+ name: string;
363
+ /** 语言代码 */
364
+ lang: string;
365
+ /** 性别 */
366
+ gender?: 'male' | 'female' | 'neutral';
367
+ /** 提供商名称 */
368
+ provider: string;
369
+ }
370
+ /**
371
+ * 高级合成配置
372
+ */
373
+ interface IAdvancedSynthesisConfig extends SynthesisConfig {
374
+ /** 合成引擎模式 */
375
+ mode?: SynthesisEngineMode;
376
+ /** 云端适配器 */
377
+ cloudAdapter?: ICloudSynthesisAdapter;
378
+ /** 音频格式偏好 */
379
+ audioFormat?: CloudAudioFormat;
380
+ /** 是否启用 SSML */
381
+ enableSSML?: boolean;
382
+ /** 是否预加载音频 */
383
+ preload?: boolean;
384
+ /** 音频缓存大小 */
385
+ cacheSize?: number;
386
+ }
387
+ /**
388
+ * 云端合成适配器接口
389
+ * 提供统一的第三方语音合成服务集成接口
390
+ */
391
+ interface ICloudSynthesisAdapter {
392
+ /** 适配器名称 */
393
+ readonly name: string;
394
+ /**
395
+ * 合成语音
396
+ * @param text 要合成的文本
397
+ * @param config 合成配置
398
+ * @returns 合成结果(音频数据)
399
+ */
400
+ synthesize(text: string, config?: IAdvancedSynthesisConfig): Promise<ISynthesisResult>;
401
+ /**
402
+ * 获取可用语音列表
403
+ * @returns 语音列表
404
+ */
405
+ getVoices?(): Promise<ICloudVoice[]>;
406
+ /**
407
+ * 检查适配器是否可用
408
+ * @returns 是否可用
409
+ */
410
+ isAvailable?(): boolean;
411
+ }
412
+ /**
413
+ * 语音合成器实现
414
+ * 统一封装原生合成和云端合成
311
415
  */
312
416
  declare class SpeechSynthesizerImpl implements SpeechSynthesizer {
417
+ private strategy;
418
+ private config;
313
419
  private _currentProvider;
314
420
  private _status;
315
- private provider;
316
- private config;
317
421
  private eventHandlers;
318
- private providers;
319
- /**
320
- * 当前使用的提供商类型
321
- */
422
+ private customProviders;
322
423
  get currentProvider(): SpeechProviderType;
424
+ get status(): SpeechServiceStatus;
323
425
  /**
324
- * 服务状态
426
+ * 获取当前合成状态
325
427
  */
326
- get status(): SpeechServiceStatus;
428
+ get synthesisStatus(): SynthesisStatus;
327
429
  /**
328
430
  * 初始化语音合成器
329
- * @param config - 合成配置
330
431
  */
331
432
  initialize(config?: SynthesisConfig): Promise<void>;
332
433
  /**
333
- * 尝试使用指定提供商
434
+ * 初始化合成策略
334
435
  */
335
- private tryProvider;
436
+ private initializeStrategy;
336
437
  /**
337
- * 转发提供商事件到合成器
438
+ * 转发策略事件
338
439
  */
339
- private forwardProviderEvents;
440
+ private forwardStrategyEvents;
340
441
  /**
341
442
  * 获取可用语音列表
342
443
  */
343
444
  getVoices(): Promise<VoiceInfo[]>;
344
445
  /**
345
446
  * 朗读文本
346
- * @param text - 要朗读的文本
347
- * @param config - 可选的合成配置
348
447
  */
349
448
  speak(text: string, config?: SynthesisConfig): Promise<void>;
350
449
  /**
@@ -385,10 +484,12 @@ declare class SpeechSynthesizerImpl implements SpeechSynthesizer {
385
484
  dispose(): void;
386
485
  /**
387
486
  * 注册自定义提供商
388
- * @param type - 提供商类型
389
- * @param provider - 提供商实例
390
487
  */
391
488
  registerProvider(type: SpeechProviderType, provider: SynthesisProvider): void;
489
+ /**
490
+ * 使用云端适配器
491
+ */
492
+ useCloudAdapter(adapter: ICloudSynthesisAdapter): void;
392
493
  }
393
494
  /**
394
495
  * 创建语音合成器实例
@@ -397,26 +498,23 @@ declare class SpeechSynthesizerImpl implements SpeechSynthesizer {
397
498
  *
398
499
  * @example
399
500
  * ```typescript
400
- * // 创建并初始化语音合成器
501
+ * // 使用原生合成
401
502
  * const synthesizer = await createSpeechSynthesizer({
402
503
  * lang: 'zh-CN',
403
504
  * rate: 1.0,
404
- * pitch: 1.0,
405
505
  * });
406
506
  *
407
- * // 朗读文本
408
- * await synthesizer.speak('你好,世界!');
409
- *
410
- * // 监听事件
411
- * synthesizer.on('end', () => {
412
- * console.log('朗读完成');
507
+ * // 使用云端合成 (Azure)
508
+ * const azureAdapter = new AzureSynthesisAdapter('key', 'eastasia');
509
+ * const cloudSynthesizer = await createSpeechSynthesizer({
510
+ * mode: 'cloud',
511
+ * cloudAdapter: azureAdapter,
413
512
  * });
414
513
  *
415
- * // 销毁
416
- * synthesizer.dispose();
514
+ * await synthesizer.speak('你好,世界!');
417
515
  * ```
418
516
  */
419
- declare function createSpeechSynthesizer(config?: SynthesisConfig): Promise<SpeechSynthesizer>;
517
+ declare function createSpeechSynthesizer(config?: SynthesisConfig | IAdvancedSynthesisConfig): Promise<SpeechSynthesizer>;
420
518
  /**
421
519
  * 检查当前环境是否支持语音合成
422
520
  * @returns 是否支持语音合成
@@ -436,134 +534,192 @@ declare function isSpeechSynthesisSupported(): boolean;
436
534
  * await speak('Hello World', { lang: 'en-US', rate: 0.8 });
437
535
  * ```
438
536
  */
439
- declare function speak(text: string, config?: SynthesisConfig): Promise<void>;
537
+ declare function speak(text: string, config?: SynthesisConfig | IAdvancedSynthesisConfig): Promise<void>;
440
538
 
441
539
  /**
442
- * @fileoverview 语音识别 (STT) 实现
540
+ * @fileoverview 语音识别 (STT) 实现 - 商业级版本 v2.0
443
541
  * @module melange/plugins/speech/recognition
444
- * @description 提供语音识别功能,支持浏览器原生 API 和第三方服务降级
542
+ * @description 生产级 Web 语音识别插件
543
+ *
544
+ * 架构:
545
+ * [UI层] -> [SpeechRecognizerImpl] -> [NativeStrategy / CloudStrategy]
546
+ * |
547
+ * +-> [音频核心]: Worklet, VAD, Resample
548
+ * +-> [适配器]: BaiduAdapter, XunfeiAdapter, TencentAdapter...
549
+ *
550
+ * 功能特性:
551
+ * 1. 多模式: 支持 WebSocket 流式识别 & HTTP 短语音识别
552
+ * 2. 状态机: IDLE -> CONNECTING -> RECORDING -> PROCESSING
553
+ * 3. 插件化: 内置 讯飞/腾讯/百度/阿里/Google/Azure 适配器
554
+ * 4. 核心: AudioWorklet + VAD + 自动重采样 + WAV编码
555
+ * 5. 兼容性: 自动降级处理 (ScriptProcessor) + 断网缓冲队列
445
556
  */
446
557
 
558
+ declare global {
559
+ interface Window {
560
+ SpeechRecognition?: unknown;
561
+ webkitSpeechRecognition?: unknown;
562
+ webkitAudioContext?: typeof AudioContext;
563
+ }
564
+ }
447
565
  /**
448
- * SpeechRecognition 结果列表接口
449
- */
450
- interface BrowserSpeechRecognitionResultList {
451
- readonly length: number;
452
- item(index: number): BrowserSpeechRecognitionResult | null;
453
- [index: number]: BrowserSpeechRecognitionResult;
566
+ * 识别器状态枚举
567
+ */
568
+ declare enum RecognitionStatus {
569
+ /** 空闲状态 */
570
+ IDLE = "IDLE",
571
+ /** 连接中 */
572
+ CONNECTING = "CONNECTING",
573
+ /** 录音中 */
574
+ RECORDING = "RECORDING",
575
+ /** 处理中/上传中 */
576
+ PROCESSING = "PROCESSING"
454
577
  }
455
578
  /**
456
- * SpeechRecognition 结果接口
579
+ * 识别引擎模式
457
580
  */
458
- interface BrowserSpeechRecognitionResult {
459
- readonly length: number;
460
- readonly isFinal: boolean;
461
- item(index: number): BrowserSpeechRecognitionAlternative | null;
462
- [index: number]: BrowserSpeechRecognitionAlternative;
463
- }
581
+ type RecognitionEngineMode = 'native' | 'cloud' | 'auto';
464
582
  /**
465
- * SpeechRecognition 备选结果接口
583
+ * 云端传输协议类型
466
584
  */
467
- interface BrowserSpeechRecognitionAlternative {
468
- readonly transcript: string;
469
- readonly confidence: number;
470
- }
585
+ type CloudTransportType = 'websocket' | 'http';
471
586
  /**
472
- * SpeechRecognition 事件接口
587
+ * 识别结果接口
473
588
  */
474
- interface BrowserSpeechRecognitionEvent {
475
- readonly results: BrowserSpeechRecognitionResultList;
476
- readonly resultIndex: number;
589
+ interface IRecognitionResult {
590
+ /** 识别文本 */
591
+ transcript: string;
592
+ /** 是否为最终结果 */
593
+ isFinal: boolean;
594
+ /** 置信度 (0-1) */
595
+ confidence: number;
596
+ /** 原始响应数据 */
597
+ original?: unknown;
477
598
  }
478
599
  /**
479
- * SpeechRecognition 错误事件接口
480
- */
481
- interface BrowserSpeechRecognitionErrorEvent {
482
- readonly error: string;
483
- readonly message: string;
600
+ * 音频配置接口
601
+ */
602
+ interface IAudioConfig {
603
+ /** 目标采样率 (默认 16000) */
604
+ sampleRate?: number;
605
+ /** VAD 阈值 (0.01 ~ 0.5) */
606
+ vadThreshold?: number;
607
+ /** VAD 静音超时 (ms) */
608
+ vadDuration?: number;
609
+ /** 是否启用回声消除 */
610
+ echoCancellation?: boolean;
611
+ /** 是否启用噪声抑制 */
612
+ noiseSuppression?: boolean;
613
+ /** 是否启用自动增益控制 */
614
+ autoGainControl?: boolean;
484
615
  }
485
616
  /**
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;
617
+ * 高级识别配置
618
+ */
619
+ interface IAdvancedRecognitionConfig extends RecognitionConfig {
620
+ /** 识别引擎模式 */
621
+ mode?: RecognitionEngineMode;
622
+ /** 云端适配器 */
623
+ cloudAdapter?: ICloudRecognitionAdapter;
624
+ /** 传输协议 */
625
+ transport?: CloudTransportType;
626
+ /** 音频配置 */
627
+ audioConfig?: IAudioConfig;
628
+ /** 是否启用自动重连 */
629
+ autoReconnect?: boolean;
630
+ /** 最大重连次数 */
631
+ maxReconnectAttempts?: number;
632
+ /** 重连间隔 (ms) */
633
+ reconnectInterval?: number;
507
634
  }
508
635
  /**
509
- * SpeechRecognition 构造函数类型
636
+ * 云端识别适配器接口
637
+ * 提供统一的第三方语音识别服务集成接口
510
638
  */
511
- interface BrowserSpeechRecognitionConstructor {
512
- new (): BrowserSpeechRecognition;
513
- }
514
- declare global {
515
- interface Window {
516
- SpeechRecognition?: BrowserSpeechRecognitionConstructor;
517
- webkitSpeechRecognition?: BrowserSpeechRecognitionConstructor;
518
- }
639
+ interface ICloudRecognitionAdapter {
640
+ /** 适配器名称 */
641
+ readonly name: string;
642
+ /**
643
+ * 获取 WebSocket 连接地址
644
+ * @returns WebSocket URL
645
+ */
646
+ getConnectUrl?(): Promise<string> | string;
647
+ /**
648
+ * 获取握手参数
649
+ * @returns 握手消息
650
+ */
651
+ getHandshakeParams?(): unknown;
652
+ /**
653
+ * HTTP 短语音识别
654
+ * @param audioData WAV/PCM 二进制数据
655
+ * @returns 识别结果
656
+ */
657
+ recognizeShortAudio?(audioData: ArrayBuffer): Promise<IRecognitionResult>;
658
+ /**
659
+ * 转换音频数据格式
660
+ * @param pcmData PCM 原始数据
661
+ * @returns 转换后的数据
662
+ */
663
+ transformAudioData?(pcmData: ArrayBuffer): ArrayBuffer | string;
664
+ /**
665
+ * 解析识别结果
666
+ * @param data 原始响应数据
667
+ * @returns 识别结果
668
+ */
669
+ parseResult(data: unknown): IRecognitionResult | null;
670
+ /**
671
+ * 检查适配器是否可用
672
+ * @returns 是否可用
673
+ */
674
+ isAvailable?(): boolean;
519
675
  }
520
676
  /**
521
- * 语音识别器
522
- * 管理多个提供商,支持自动降级
677
+ * 语音识别器实现
678
+ * 统一封装原生识别和云端识别
523
679
  */
524
680
  declare class SpeechRecognizerImpl implements SpeechRecognizer {
681
+ private strategy;
682
+ private config;
525
683
  private _currentProvider;
526
684
  private _status;
527
- private provider;
528
- private config;
529
685
  private eventHandlers;
530
- private providers;
531
- /**
532
- * 当前使用的提供商类型
533
- */
686
+ private customProviders;
534
687
  get currentProvider(): SpeechProviderType;
688
+ get status(): SpeechServiceStatus;
535
689
  /**
536
- * 服务状态
690
+ * 获取当前识别状态
537
691
  */
538
- get status(): SpeechServiceStatus;
692
+ get recognitionStatus(): RecognitionStatus;
539
693
  /**
540
694
  * 初始化语音识别器
541
- * @param config - 识别配置
542
695
  */
543
696
  initialize(config?: RecognitionConfig): Promise<void>;
544
697
  /**
545
- * 尝试使用指定提供商
698
+ * 初始化识别策略
546
699
  */
547
- private tryProvider;
700
+ private initializeStrategy;
548
701
  /**
549
- * 转发提供商事件到识别器
702
+ * 转发策略事件
550
703
  */
551
- private forwardProviderEvents;
704
+ private forwardStrategyEvents;
552
705
  /**
553
- * 开始语音识别
554
- * @param config - 可选的识别配置
706
+ * 转换识别结果格式
707
+ */
708
+ private convertResult;
709
+ /**
710
+ * 开始识别
555
711
  */
556
712
  start(config?: RecognitionConfig): Promise<void>;
557
713
  /**
558
- * 停止语音识别
714
+ * 停止识别
559
715
  */
560
716
  stop(): void;
561
717
  /**
562
- * 中止语音识别
718
+ * 中止识别
563
719
  */
564
720
  abort(): void;
565
721
  /**
566
- * 是否正在监听
722
+ * 是否正在识别
567
723
  */
568
724
  isListening(): boolean;
569
725
  /**
@@ -584,60 +740,58 @@ declare class SpeechRecognizerImpl implements SpeechRecognizer {
584
740
  dispose(): void;
585
741
  /**
586
742
  * 注册自定义提供商
587
- * @param type - 提供商类型
588
- * @param provider - 提供商实例
589
743
  */
590
744
  registerProvider(type: SpeechProviderType, provider: RecognitionProvider): void;
745
+ /**
746
+ * 使用云端适配器
747
+ */
748
+ useCloudAdapter(adapter: ICloudRecognitionAdapter): void;
591
749
  }
592
750
  /**
593
751
  * 创建语音识别器实例
594
- * @param config - 可选的识别配置
752
+ * @param config 识别配置
595
753
  * @returns 语音识别器实例
596
754
  *
597
755
  * @example
598
756
  * ```typescript
599
- * // 创建并初始化语音识别器
757
+ * // 使用原生识别
600
758
  * const recognizer = await createSpeechRecognizer({
601
759
  * lang: 'zh-CN',
602
760
  * continuous: true,
603
- * interimResults: true,
604
761
  * });
605
762
  *
606
- * // 监听识别结果
763
+ * // 使用云端识别 (百度)
764
+ * const baiduAdapter = new BaiduAdapter('your-access-token');
765
+ * const cloudRecognizer = await createSpeechRecognizer({
766
+ * mode: 'cloud',
767
+ * cloudAdapter: baiduAdapter,
768
+ * transport: 'http',
769
+ * });
770
+ *
607
771
  * recognizer.on('result', (event) => {
608
- * if (event.result) {
609
- * console.log('识别结果:', event.result.bestTranscript);
610
- * }
772
+ * console.log('识别结果:', event.result?.bestTranscript);
611
773
  * });
612
774
  *
613
- * // 开始识别
614
775
  * await recognizer.start();
615
- *
616
- * // 停止识别
617
- * recognizer.stop();
618
- *
619
- * // 销毁
620
- * recognizer.dispose();
621
776
  * ```
622
777
  */
623
- declare function createSpeechRecognizer(config?: RecognitionConfig): Promise<SpeechRecognizer>;
778
+ declare function createSpeechRecognizer(config?: RecognitionConfig | IAdvancedRecognitionConfig): Promise<SpeechRecognizer>;
624
779
  /**
625
780
  * 检查当前环境是否支持语音识别
626
- * @returns 是否支持语音识别
781
+ * @returns 是否支持
627
782
  */
628
783
  declare function isSpeechRecognitionSupported(): boolean;
629
784
  /**
630
785
  * 快速进行一次语音识别
631
- * @param config - 可选的识别配置
786
+ * @param config 识别配置
632
787
  * @returns 识别结果
633
788
  *
634
789
  * @example
635
790
  * ```typescript
636
- * // 进行一次语音识别
637
791
  * const result = await listen({ lang: 'zh-CN' });
638
792
  * console.log('识别结果:', result.bestTranscript);
639
793
  * ```
640
794
  */
641
- declare function listen(config?: RecognitionConfig): Promise<RecognitionResult>;
795
+ declare function listen(config?: RecognitionConfig | IAdvancedRecognitionConfig): Promise<RecognitionResult>;
642
796
 
643
797
  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 };