@amaster.ai/asr-client 1.1.0-beta.5 → 1.1.0-beta.51

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/README.md CHANGED
@@ -2,20 +2,38 @@
2
2
 
3
3
  基于 Web Audio + WebSocket 的 **实时语音识别(ASR)客户端 SDK**,用于将浏览器麦克风音频实时发送到 ASR 服务(如 Qwen ASR Realtime),并接收实时/最终转写结果。
4
4
 
5
+ 遵循 [Qwen-ASR Realtime API](https://help.aliyun.com/zh/model-studio/qwen-realtime-speech-recognition) 协议实现,完整支持会话管理、VAD 模式、手动模式等全部功能。
6
+
5
7
  ---
6
8
 
7
9
  ## 特性
8
10
 
9
11
  - 🎙 浏览器麦克风实时采集(16kHz / 单声道)
10
12
  - 🔁 实时音频流式发送(Base64 PCM16)
11
- - 🧠 支持语音开始 / 结束事件
13
+ - 🧠 支持 VAD 模式(自动检测语音开始/结束)和 Manual 模式(手动控制)
14
+ - 🌍 支持 25 种语言识别
12
15
  - ✍️ 支持中间结果与最终转写结果
13
- - 🔌 简单的 WebSocket 生命周期管理
16
+ - 📡 完整的会话生命周期管理(session.update、session.finish)
17
+ - 🔌 事件 ID 自动生成,符合协议规范
18
+ - 🌐 同时支持 WebSocket 实时识别和 HTTP 按压识别
14
19
 
15
20
  ---
16
21
 
17
22
  ## 安装与环境要求
18
23
 
24
+ ### 安装
25
+
26
+ ```bash
27
+ # npm
28
+ npm install @amaster.ai/asr-client
29
+
30
+ # pnpm
31
+ pnpm add @amaster.ai/asr-client
32
+
33
+ # yarn
34
+ yarn add @amaster.ai/asr-client
35
+ ```
36
+
19
37
  ### 浏览器要求
20
38
 
21
39
  - 支持 `getUserMedia`
@@ -28,143 +46,400 @@
28
46
 
29
47
  ## 快速开始
30
48
 
31
- ### 1️⃣ 创建 ASR Client
49
+ ### 方式一:WebSocket 实时 ASR(流式识别)
50
+
51
+ 适合需要实时看到识别结果的场景,如语音输入、实时字幕等。
52
+
53
+ #### VAD 模式(推荐)
54
+
55
+ 自动检测语音开始和结束:
32
56
 
33
57
  ```ts
34
- import { createASRClient } from "./asr-client";
58
+ import { createASRClient } from "@amaster.ai/asr-client";
35
59
 
36
60
  const client = createASRClient({
61
+ // 语言配置
62
+ language: "zh", // 支持 zh、yue、en、ja 等 25 种语言
63
+
64
+ // VAD 配置(自动检测语音)
65
+ enableVAD: true,
66
+ vadThreshold: 0.0, // 推荐设为 0.0,默认 0.2
67
+ vadSilenceDurationMs: 400, // 推荐设为 400ms,默认 800ms
68
+
69
+ // 回调函数
37
70
  onReady() {
38
- console.log("ASR 连接成功");
71
+ console.log("ASR 连接成功,会话已配置");
39
72
  },
40
73
 
41
74
  onSpeechStart() {
42
- console.log("检测到说话开始");
75
+ console.log("🎤 检测到说话开始");
43
76
  },
44
77
 
45
78
  onSpeechEnd() {
46
- console.log("检测到说话结束");
79
+ console.log("🛑 检测到说话结束");
47
80
  },
48
81
 
49
82
  onTranscript(text, isFinal) {
50
- console.log(isFinal ? "最终结果:" : "实时结果:", text);
83
+ console.log(isFinal ? "最终结果:" : "📝 实时结果:", text);
84
+ },
85
+
86
+ onSessionFinished() {
87
+ console.log("👋 会话已结束");
51
88
  },
52
89
 
53
90
  onError(err) {
54
- console.error("ASR 错误", err);
91
+ console.error("ASR 错误", err);
55
92
  },
56
93
 
57
94
  onClose() {
58
- console.log("连接已关闭");
95
+ console.log("🔌 连接已关闭");
59
96
  },
60
97
  });
98
+
99
+ // 1. 建立连接(自动发送 session.update)
100
+ await client.connect();
101
+
102
+ // 2. 开始录音
103
+ await client.startRecording();
104
+
105
+ // 3. 停止录音(VAD 模式下可选,会自动检测结束)
106
+ await client.stopRecording();
107
+
108
+ // 4. 关闭连接(自动发送 session.finish)
109
+ await client.close();
61
110
  ```
62
111
 
63
- ---
112
+ #### Manual 模式(非 VAD)
64
113
 
65
- ### 2️⃣ 建立 WebSocket 连接
114
+ 手动控制识别时机,适合按住说话、松开识别的场景:
66
115
 
67
116
  ```ts
68
- await client.connect();
69
- ```
117
+ const client = createASRClient({
118
+ language: "zh",
119
+ enableVAD: false, // 关闭 VAD,使用手动模式
70
120
 
71
- 连接成功后会触发 `onReady` 回调。
121
+ onReady() {
122
+ console.log("准备就绪");
123
+ },
72
124
 
73
- ---
125
+ onTranscript(text, isFinal) {
126
+ if (isFinal) {
127
+ console.log("识别结果:", text);
128
+ }
129
+ },
74
130
 
75
- ### 3️⃣ 开始录音并实时识别
131
+ onAudioBufferCommitted() {
132
+ console.log("音频已提交,等待识别结果...");
133
+ },
134
+ });
76
135
 
77
- ```ts
136
+ await client.connect();
78
137
  await client.startRecording();
138
+
139
+ // 用户说话中...
140
+
141
+ await client.stopRecording(); // 触发 input_audio_buffer.commit
79
142
  ```
80
143
 
81
- * 自动请求麦克风权限
82
- * 自动开始推送音频流
83
- * 服务端会持续返回实时转写结果
144
+ ---
145
+
146
+ ### 方式二:HTTP 按压式 ASR(一句话识别)
147
+
148
+ 适合按住说话、松开识别的场景,如语音消息、语音搜索等。
149
+
150
+ ```tsx
151
+ import { useRef, useState } from "react";
152
+ import { createASRHttpClient } from "@amaster.ai/asr-client";
153
+
154
+ function VoiceButton() {
155
+ const [recording, setRecording] = useState(false);
156
+ const [text, setText] = useState("");
157
+ const [error, setError] = useState<string | null>(null);
158
+
159
+ // 创建 HTTP ASR 客户端
160
+ const asrHttpClient = useRef(
161
+ createASRHttpClient({
162
+ onRecordingStart() {
163
+ setRecording(true);
164
+ setText("");
165
+ setError(null);
166
+ },
167
+ onRecordingStop() {
168
+ setRecording(false);
169
+ },
170
+ onResult(result) {
171
+ setText(result);
172
+ },
173
+ onError(err) {
174
+ setError(err.message);
175
+ },
176
+ })
177
+ ).current;
178
+
179
+ return (
180
+ <div>
181
+ <button
182
+ onMouseDown={() => asrHttpClient.startRecording()}
183
+ onMouseUp={() => asrHttpClient.stopRecording()}
184
+ onTouchStart={() => asrHttpClient.startRecording()}
185
+ onTouchEnd={() => asrHttpClient.stopRecording()}
186
+ style={{
187
+ padding: "12px 24px",
188
+ background: recording ? "#f87171" : "#4ade80",
189
+ }}
190
+ >
191
+ {recording ? "松开识别" : "按住说话"}
192
+ </button>
193
+
194
+ <div>
195
+ <strong>识别结果:</strong>
196
+ <div>{text || "(暂无)"}</div>
197
+ </div>
198
+
199
+ {error && <div style={{ color: "red" }}>错误:{error}</div>}
200
+ </div>
201
+ );
202
+ }
203
+ ```
84
204
 
85
205
  ---
86
206
 
87
- ### 4️⃣ 停止录音
207
+ ## API 说明
208
+
209
+ ### `createASRClient(config): ASRClient`
210
+
211
+ WebSocket 实时 ASR 客户端。
212
+
213
+ #### `ASRClientConfig`
214
+
215
+ | 参数 | 类型 | 默认值 | 说明 |
216
+ | ------------------------ | ------------------------------------------ | ------- | -------------------------------------------------------- |
217
+ | `audioFormat` | `"pcm" \| "opus"` | `"pcm"` | 音频格式 |
218
+ | `sampleRate` | `16000 \| 8000` | `16000` | 采样率。8000 会先升采样到 16000,可能引入微小延迟 |
219
+ | `language` | `ASRLanguage` | `"zh"` | 音频源语言,支持 25 种语言(见下方语言列表) |
220
+ | `enableVAD` | `boolean` | `true` | 是否启用 VAD 模式。`true`=自动检测,`false`=手动控制 |
221
+ | `vadThreshold` | `number` | `0.2` | VAD 检测阈值,推荐设为 `0.0`,范围 `[-1, 1]` |
222
+ | `vadSilenceDurationMs` | `number` | `800` | VAD 断句检测阈值(ms),推荐设为 `400`,范围 `[200, 6000]` |
223
+ | `getAccessToken` | `() => string \| null` | - | 获取访问令牌(用于 WebSocket 认证) |
224
+ | `onReady` | `() => void` | - | 会话创建并配置完成(收到 session.updated) |
225
+ | `onSpeechStart` | `() => void` | - | 检测到语音开始(VAD 模式) |
226
+ | `onSpeechEnd` | `() => void` | - | 检测到语音结束(VAD 模式) |
227
+ | `onTranscript` | `(text: string, isFinal: boolean) => void` | - | 转写回调,`isFinal` 表示是否为最终结果 |
228
+ | `onAudioBufferCommitted` | `() => void` | - | 音频缓冲区已提交(非 VAD 模式) |
229
+ | `onSessionFinished` | `() => void` | - | 会话已结束(收到 session.finished) |
230
+ | `onError` | `(error: Error) => void` | - | 错误回调 |
231
+ | `onClose` | `() => void` | - | 连接关闭回调 |
232
+
233
+ #### 支持的语言 (ASRLanguage)
234
+
235
+ | 代码 | 语言 | 代码 | 语言 |
236
+ | ----- | ------------------------------------ | ---- | -------- |
237
+ | `zh` | 中文(普通话、四川话、闽南语、吴语) | `it` | 意大利语 |
238
+ | `yue` | 粤语 | `es` | 西班牙语 |
239
+ | `en` | 英文 | `hi` | 印地语 |
240
+ | `ja` | 日语 | `id` | 印尼语 |
241
+ | `de` | 德语 | `th` | 泰语 |
242
+ | `ko` | 韩语 | `tr` | 土耳其语 |
243
+ | `ru` | 俄语 | `uk` | 乌克兰语 |
244
+ | `fr` | 法语 | `vi` | 越南语 |
245
+ | `pt` | 葡萄牙语 | `cs` | 捷克语 |
246
+ | `ar` | 阿拉伯语 | `da` | 丹麦语 |
247
+ | `fil` | 菲律宾语 | `fi` | 芬兰语 |
248
+ | `is` | 冰岛语 | `ms` | 马来语 |
249
+ | `no` | 挪威语 | `pl` | 波兰语 |
250
+ | `sv` | 瑞典语 | | |
251
+
252
+ #### `ASRClient`
88
253
 
89
254
  ```ts
90
- client.stopRecording();
91
- ```
255
+ interface ASRClient {
256
+ /** 建立 WebSocket 连接并发送 session.update */
257
+ connect(): Promise<void>;
92
258
 
93
- * 停止麦克风采集
94
- * 向服务端发送 `input_audio_buffer.commit`
95
- * 触发最终转写结果
259
+ /** 开始录音并流式发送音频 */
260
+ startRecording(): Promise<void>;
261
+
262
+ /**
263
+ * 停止录音
264
+ * - VAD 模式:停止发送音频
265
+ * - 非 VAD 模式:停止发送音频并触发识别(发送 input_audio_buffer.commit)
266
+ */
267
+ stopRecording(): Promise<void>;
268
+
269
+ /**
270
+ * 关闭连接
271
+ * - 发送 session.finish
272
+ * - 等待 session.finished
273
+ * - 关闭 WebSocket
274
+ */
275
+ close(): Promise<void>;
276
+
277
+ /** 是否正在录音 */
278
+ isRecording(): boolean;
279
+
280
+ /** 是否已连接到服务器 */
281
+ isConnected(): boolean;
282
+ }
283
+ ```
96
284
 
97
285
  ---
98
286
 
99
- ### 5️⃣ 关闭连接
287
+ ### `createASRHttpClient(config): ASRHttpClient`
288
+
289
+ HTTP 按压式 ASR 客户端。
290
+
291
+ #### `ASRHttpClientConfig`
292
+
293
+ | 参数 | 类型 | 说明 |
294
+ | ------------------ | ------------------------ | -------------------- |
295
+ | `getAccessToken` | `() => string \| null` | 获取访问令牌 |
296
+ | `language` | `string` | 语言,默认 `'zh'` |
297
+ | `sampleRate` | `number` | 采样率,默认 `16000` |
298
+ | `onRecordingStart` | `() => void` | 录音开始回调 |
299
+ | `onRecordingStop` | `() => void` | 录音停止回调 |
300
+ | `onResult` | `(text: string) => void` | 识别结果回调 |
301
+ | `onError` | `(error: Error) => void` | 错误回调 |
302
+
303
+ #### `ASRHttpClient`
100
304
 
101
305
  ```ts
102
- client.close();
306
+ interface ASRHttpClient {
307
+ startRecording(): Promise<void>; // 开始录音
308
+ stopRecording(): Promise<string>; // 停止录音并返回识别结果
309
+ recordAndRecognize(durationMs: number): Promise<string>; // 录音指定时长
310
+ recognizeFile(file: File | Blob): Promise<string>; // 识别音频文件
311
+ recognizeUrl(audioUrl: string): Promise<string>; // 识别音频 URL
312
+ }
103
313
  ```
104
314
 
105
315
  ---
106
316
 
107
- ## API 说明
317
+ ## 配合统一客户端使用
108
318
 
109
- ### `createASRClient(config): ASRClient`
319
+ 推荐与 `@amaster.ai/client` 统一客户端一起使用,自动处理认证:
110
320
 
111
- #### `ASRClientConfig`
321
+ ```tsx
322
+ import { createClient } from "@amaster.ai/client";
323
+
324
+ const client = createClient({
325
+ baseURL: "https://api.amaster.ai",
326
+ });
327
+
328
+ // WebSocket ASR
329
+ const asrClient = client.asr({
330
+ onTranscript(text, isFinal) {
331
+ console.log(isFinal ? "[最终]" : "[实时]", text);
332
+ },
333
+ });
334
+
335
+ await asrClient.connect();
336
+ await asrClient.startRecording();
112
337
 
113
- | 参数 | 类型 | 说明 |
114
- | --------------- | ------------------------------------------ | -------------- |
115
- | `onReady` | `() => void` | 会话创建完成 |
116
- | `onSpeechStart` | `() => void` | 检测到语音开始 |
117
- | `onSpeechEnd` | `() => void` | 检测到语音结束 |
118
- | `onTranscript` | `(text: string, isFinal: boolean) => void` | 转写回调 |
119
- | `onError` | `(error: Error) => void` | 错误回调 |
120
- | `onClose` | `() => void` | 连接关闭回调 |
338
+ // HTTP ASR
339
+ const asrHttpClient = client.asrHttp({
340
+ onResult(text) {
341
+ console.log("识别结果:", text);
342
+ },
343
+ });
344
+
345
+ await asrHttpClient.startRecording();
346
+ // ... 用户说话 ...
347
+ const result = await asrHttpClient.stopRecording();
348
+ ```
121
349
 
122
350
  ---
123
351
 
124
- ### `ASRClient`
352
+ ## VAD 模式 vs Manual 模式
353
+
354
+ | 特性 | VAD 模式(enableVAD: true) | Manual 模式(enableVAD: false) |
355
+ | -------- | -------------------------------------- | ------------------------------- |
356
+ | 语音检测 | 服务端自动检测 | 客户端手动控制 |
357
+ | 适用场景 | 连续对话、实时字幕 | 按住说话、语音消息 |
358
+ | 开始录音 | `startRecording()` | `startRecording()` |
359
+ | 停止录音 | 自动检测 / `stopRecording()` | `stopRecording()` 触发识别 |
360
+ | 事件触发 | `onSpeechStart` / `onSpeechEnd` | `onAudioBufferCommitted` |
361
+ | 配置参数 | `vadThreshold`, `vadSilenceDurationMs` | 无需 VAD 配置 |
362
+
363
+ ### VAD 配置建议
125
364
 
126
365
  ```ts
127
- interface ASRClient {
128
- connect(): Promise<void>;
129
- startRecording(): Promise<void>;
130
- stopRecording(): void;
131
- close(): void;
132
- }
366
+ // 快速响应场景(如实时字幕)
367
+ const client = createASRClient({
368
+ enableVAD: true,
369
+ vadThreshold: 0.0, // 最灵敏
370
+ vadSilenceDurationMs: 400, // 较短停顿即断句
371
+ });
372
+
373
+ // 长句输入场景(如文章朗读)
374
+ const client = createASRClient({
375
+ enableVAD: true,
376
+ vadThreshold: 0.2, // 默认灵敏度
377
+ vadSilenceDurationMs: 1200, // 较长停顿才断句
378
+ });
133
379
  ```
134
380
 
135
381
  ---
136
382
 
137
383
  ## 音频参数说明
138
384
 
139
- * **采样率**:16000 Hz
140
- * **声道**:单声道
141
- * **格式**:PCM16 Base64
142
- * **缓冲大小**:4096 frames
385
+ ### WebSocket ASR
386
+
387
+ - **采样率**:16000 Hz(推荐)或 8000 Hz
388
+ - **声道**:单声道
389
+ - **格式**:PCM16 → Base64
390
+ - **缓冲大小**:4096 frames
391
+
392
+ ### HTTP ASR
393
+
394
+ - **采样率**:自动检测(通常为 16000 Hz)
395
+ - **声道**:单声道
396
+ - **格式**:WAV(内部将 PCM 转为 WAV)
143
397
 
144
398
  ---
145
399
 
146
400
  ## 常见问题
147
401
 
402
+ ### Q: WebSocket 和 HTTP ASR 有什么区别?
403
+
404
+ | 特性 | WebSocket ASR | HTTP ASR |
405
+ | -------- | ---------------------- | ------------------- |
406
+ | 实时性 | 实时流式返回 | 说完后一次性返回 |
407
+ | 适用场景 | 语音输入、实时字幕 | 语音消息、语音搜索 |
408
+ | 交互方式 | 开始 → 持续识别 → 停止 | 按住说话 → 松开识别 |
409
+ | 网络开销 | 持续连接 | 每次请求独立 |
410
+
148
411
  ### Q: 为什么必须是 16kHz?
149
412
 
150
413
  ASR 服务通常要求 16kHz PCM 输入,否则会影响识别效果或直接报错。
151
414
 
152
- ---
153
-
154
415
  ### Q: 支持移动端吗?
155
416
 
156
417
  支持,但需注意:
157
418
 
158
- * iOS Safari 需用户手势触发录音
159
- * 后台会自动暂停音频采集
419
+ - iOS Safari 需用户手势触发录音
420
+ - 后台会自动暂停音频采集
421
+
422
+ ### Q: VAD 模式下为什么要等一段时间才返回最终结果?
423
+
424
+ VAD 需要检测到静音(silence)才会认为一句话结束,然后返回最终结果。`vadSilenceDurationMs` 配置了这个静音时长阈值。
425
+
426
+ ### Q: 如何切换语言?
427
+
428
+ ```ts
429
+ const client = createASRClient({
430
+ language: "en", // 英文识别
431
+ });
432
+ ```
160
433
 
161
434
  ---
162
435
 
163
436
  ## 注意事项
164
437
 
165
- * WebSocket 必须在 HTTPS 页面下使用麦克风
166
- * 页面关闭前建议调用 `client.close()`
167
- * 不建议在多个 ASR Client 实例中共享麦克风
438
+ - WebSocket 必须在 HTTPS 页面下使用麦克风
439
+ - 页面关闭前建议调用 `await client.close()` 优雅关闭
440
+ - 不建议在多个 ASR Client 实例中共享麦克风
441
+ - HTTP ASR 会自动将录音转为 WAV 格式上传
442
+ - 每个事件都会自动生成唯一的 `event_id`,符合协议规范
168
443
 
169
444
  ---
170
445