@wq-hook/volcano-react 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/README.md +112 -0
- package/dist/index.d.mts +358 -0
- package/dist/index.d.ts +358 -0
- package/dist/index.js +1406 -0
- package/dist/index.mjs +1364 -0
- package/package.json +40 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AudioProgressBar: () => AudioProgressBar_default,
|
|
34
|
+
AudioWaveVisualizer: () => AudioWaveVisualizer_default,
|
|
35
|
+
splitTextByDelimiters: () => splitTextByDelimiters,
|
|
36
|
+
useMessageTTS: () => useMessageTTS,
|
|
37
|
+
useVolcanoASR: () => useVolcanoASR,
|
|
38
|
+
useVolcanoTTS: () => useVolcanoTTS
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
|
|
42
|
+
// src/asr/useVolcanoASR.ts
|
|
43
|
+
var import_react = require("react");
|
|
44
|
+
var import_asr = require("@wq-hook/volcano-sdk/asr");
|
|
45
|
+
var DEFAULT_URL = "wss://openspeech.bytedance.com/api/v3/sauc/bigmodel_async";
|
|
46
|
+
function useVolcanoASR(params) {
|
|
47
|
+
const [status, setStatus] = (0, import_react.useState)("idle");
|
|
48
|
+
const [text, setText] = (0, import_react.useState)("");
|
|
49
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
50
|
+
const asrInstanceRef = (0, import_react.useRef)(null);
|
|
51
|
+
(0, import_react.useEffect)(() => {
|
|
52
|
+
asrInstanceRef.current = (0, import_asr.LabASR)({
|
|
53
|
+
onStart: () => {
|
|
54
|
+
setStatus("recording");
|
|
55
|
+
setError(null);
|
|
56
|
+
},
|
|
57
|
+
onMessage: (msg, fullData) => {
|
|
58
|
+
if (msg) {
|
|
59
|
+
setText(msg);
|
|
60
|
+
}
|
|
61
|
+
console.log("fullData", fullData);
|
|
62
|
+
},
|
|
63
|
+
onClose: () => {
|
|
64
|
+
setStatus("idle");
|
|
65
|
+
},
|
|
66
|
+
onError: () => {
|
|
67
|
+
setStatus("error");
|
|
68
|
+
setError("Connection error");
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return () => {
|
|
72
|
+
asrInstanceRef.current?.stopRecord();
|
|
73
|
+
};
|
|
74
|
+
}, []);
|
|
75
|
+
const start = (0, import_react.useCallback)(async () => {
|
|
76
|
+
if (!asrInstanceRef.current) return;
|
|
77
|
+
try {
|
|
78
|
+
setStatus("connecting");
|
|
79
|
+
setText("");
|
|
80
|
+
setError(null);
|
|
81
|
+
const { appId, token } = params;
|
|
82
|
+
const auth = {
|
|
83
|
+
api_app_key: appId,
|
|
84
|
+
api_access_key: `Jwt; ${token}`,
|
|
85
|
+
api_resource_id: "volc.seedasr.sauc.duration"
|
|
86
|
+
};
|
|
87
|
+
const authUrl = `${DEFAULT_URL}?${new URLSearchParams(auth).toString()}`;
|
|
88
|
+
const config = {
|
|
89
|
+
user: {
|
|
90
|
+
uid: (0, import_asr.uuid)()
|
|
91
|
+
},
|
|
92
|
+
audio: {
|
|
93
|
+
format: "pcm",
|
|
94
|
+
rate: 16e3,
|
|
95
|
+
bits: 16,
|
|
96
|
+
channel: 1
|
|
97
|
+
},
|
|
98
|
+
request: {
|
|
99
|
+
model_name: "bigmodel",
|
|
100
|
+
enable_punc: true,
|
|
101
|
+
result_type: "full"
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
asrInstanceRef.current.connect({
|
|
105
|
+
url: authUrl,
|
|
106
|
+
config
|
|
107
|
+
});
|
|
108
|
+
await asrInstanceRef.current.startRecord({}, () => {
|
|
109
|
+
});
|
|
110
|
+
} catch (err) {
|
|
111
|
+
const error2 = err;
|
|
112
|
+
setError(error2.message || "Failed to start ASR");
|
|
113
|
+
setStatus("error");
|
|
114
|
+
}
|
|
115
|
+
}, [params]);
|
|
116
|
+
const stop = (0, import_react.useCallback)(() => {
|
|
117
|
+
if (!asrInstanceRef.current) return;
|
|
118
|
+
asrInstanceRef.current.stopRecord();
|
|
119
|
+
}, []);
|
|
120
|
+
return {
|
|
121
|
+
status,
|
|
122
|
+
text,
|
|
123
|
+
error,
|
|
124
|
+
start,
|
|
125
|
+
stop
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/tts/useVolcanoTTS.ts
|
|
130
|
+
var import_tts = require("@wq-hook/volcano-sdk/tts");
|
|
131
|
+
var import_volcano_sdk = require("@wq-hook/volcano-sdk");
|
|
132
|
+
var import_react2 = require("react");
|
|
133
|
+
var import_emoji_regex = __toESM(require("emoji-regex"));
|
|
134
|
+
|
|
135
|
+
// src/tts/TTSCache.ts
|
|
136
|
+
var DB_NAME = "VolcanoTTSCache";
|
|
137
|
+
var STORE_NAME = "audio_segments";
|
|
138
|
+
var DB_VERSION = 1;
|
|
139
|
+
var memoryCache = /* @__PURE__ */ new Map();
|
|
140
|
+
var dbPromise = null;
|
|
141
|
+
function getDB() {
|
|
142
|
+
if (dbPromise) return dbPromise;
|
|
143
|
+
dbPromise = new Promise((resolve, reject) => {
|
|
144
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
145
|
+
request.onerror = () => {
|
|
146
|
+
console.error("IndexedDB error:", request.error);
|
|
147
|
+
reject(request.error);
|
|
148
|
+
};
|
|
149
|
+
request.onsuccess = () => {
|
|
150
|
+
resolve(request.result);
|
|
151
|
+
};
|
|
152
|
+
request.onupgradeneeded = (event) => {
|
|
153
|
+
const db = event.target.result;
|
|
154
|
+
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
155
|
+
db.createObjectStore(STORE_NAME, { keyPath: "key" });
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
return dbPromise;
|
|
160
|
+
}
|
|
161
|
+
var TTSCache = {
|
|
162
|
+
get: async (key) => {
|
|
163
|
+
if (memoryCache.has(key)) {
|
|
164
|
+
console.log("[TTSCache] L1 Hit:", key);
|
|
165
|
+
return memoryCache.get(key);
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const db = await getDB();
|
|
169
|
+
return new Promise((resolve) => {
|
|
170
|
+
const transaction = db.transaction([STORE_NAME], "readonly");
|
|
171
|
+
const store = transaction.objectStore(STORE_NAME);
|
|
172
|
+
const request = store.get(key);
|
|
173
|
+
request.onsuccess = async () => {
|
|
174
|
+
const result = request.result;
|
|
175
|
+
if (result) {
|
|
176
|
+
console.log("[TTSCache] L2 Hit:", key);
|
|
177
|
+
memoryCache.set(key, result.data);
|
|
178
|
+
resolve(result.data);
|
|
179
|
+
} else {
|
|
180
|
+
resolve(void 0);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
request.onerror = () => resolve(void 0);
|
|
184
|
+
});
|
|
185
|
+
} catch (e) {
|
|
186
|
+
console.warn("TTSCache L2 read failed", e);
|
|
187
|
+
return void 0;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
set: async (key, data) => {
|
|
191
|
+
memoryCache.set(key, data);
|
|
192
|
+
if (memoryCache.size > 50) {
|
|
193
|
+
const firstKey = memoryCache.keys().next().value;
|
|
194
|
+
if (firstKey) memoryCache.delete(firstKey);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const db = await getDB();
|
|
198
|
+
const transaction = db.transaction([STORE_NAME], "readwrite");
|
|
199
|
+
const store = transaction.objectStore(STORE_NAME);
|
|
200
|
+
store.put({
|
|
201
|
+
key,
|
|
202
|
+
data,
|
|
203
|
+
timestamp: Date.now()
|
|
204
|
+
});
|
|
205
|
+
} catch (e) {
|
|
206
|
+
console.warn("TTSCache L2 write failed", e);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
generateKey: (text, voice, speed) => {
|
|
210
|
+
const textHash = simpleHash(text);
|
|
211
|
+
return `tts_${voice}_${speed}_${textHash}`;
|
|
212
|
+
},
|
|
213
|
+
delete: async (key) => {
|
|
214
|
+
memoryCache.delete(key);
|
|
215
|
+
try {
|
|
216
|
+
const db = await getDB();
|
|
217
|
+
const transaction = db.transaction([STORE_NAME], "readwrite");
|
|
218
|
+
const store = transaction.objectStore(STORE_NAME);
|
|
219
|
+
store.delete(key);
|
|
220
|
+
} catch (e) {
|
|
221
|
+
console.warn("TTSCache L2 delete failed", e);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
clear: async () => {
|
|
225
|
+
memoryCache.clear();
|
|
226
|
+
try {
|
|
227
|
+
const db = await getDB();
|
|
228
|
+
const transaction = db.transaction([STORE_NAME], "readwrite");
|
|
229
|
+
transaction.objectStore(STORE_NAME).clear();
|
|
230
|
+
} catch (e) {
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function simpleHash(str) {
|
|
235
|
+
let hash = 0;
|
|
236
|
+
for (let i = 0; i < str.length; i++) {
|
|
237
|
+
const char = str.charCodeAt(i);
|
|
238
|
+
hash = (hash << 5) - hash + char;
|
|
239
|
+
hash = hash & hash;
|
|
240
|
+
}
|
|
241
|
+
return Math.abs(hash).toString(36);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/tts/useVolcanoTTS.ts
|
|
245
|
+
var URL2 = "wss://openspeech.bytedance.com/api/v3/tts/bidirection";
|
|
246
|
+
function buildFullUrl(url, auth) {
|
|
247
|
+
const arr = [];
|
|
248
|
+
for (const key in auth) {
|
|
249
|
+
if (auth[key]) {
|
|
250
|
+
arr.push(
|
|
251
|
+
`${key}=${encodeURIComponent(auth[key])}`
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return `${url}?${arr.join("&")}`;
|
|
256
|
+
}
|
|
257
|
+
function useVolcanoTTS({
|
|
258
|
+
ttsConfig,
|
|
259
|
+
audioParams
|
|
260
|
+
}) {
|
|
261
|
+
const [status, setStatus] = (0, import_react2.useState)("idle");
|
|
262
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
263
|
+
const client = (0, import_react2.useRef)(null);
|
|
264
|
+
const currentAudioBuffersRef = (0, import_react2.useRef)([]);
|
|
265
|
+
const cacheKeyRef = (0, import_react2.useRef)("");
|
|
266
|
+
const cleanupAll = (0, import_react2.useCallback)(() => {
|
|
267
|
+
if (client.current) {
|
|
268
|
+
try {
|
|
269
|
+
client.current.finishConnection();
|
|
270
|
+
client.current = null;
|
|
271
|
+
setTimeout(() => {
|
|
272
|
+
setStatus("idle");
|
|
273
|
+
setError(null);
|
|
274
|
+
}, 0);
|
|
275
|
+
} catch (err) {
|
|
276
|
+
console.error("[useTTS] cleanupAll error", err);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}, []);
|
|
280
|
+
const connect = (0, import_react2.useCallback)(async () => {
|
|
281
|
+
try {
|
|
282
|
+
setStatus("connecting");
|
|
283
|
+
setError(null);
|
|
284
|
+
const clientInstance = (0, import_tts.WebsocketMSE)({ autoStartSession: false });
|
|
285
|
+
client.current = clientInstance;
|
|
286
|
+
const auth = {};
|
|
287
|
+
if (ttsConfig.token) {
|
|
288
|
+
auth.api_app_key = ttsConfig.appid;
|
|
289
|
+
auth.api_access_key = `Jwt; ${ttsConfig.token}`;
|
|
290
|
+
auth.api_resource_id = ttsConfig.resourceId || "seed-tts-2.0";
|
|
291
|
+
}
|
|
292
|
+
const fullUrl = buildFullUrl(URL2, auth);
|
|
293
|
+
const audioUrl = clientInstance.start({
|
|
294
|
+
url: fullUrl,
|
|
295
|
+
config: {
|
|
296
|
+
user: {
|
|
297
|
+
uid: `feewee-tts-user-${Date.now()}`
|
|
298
|
+
},
|
|
299
|
+
namespace: ttsConfig.namespace || "BidirectionalTTS",
|
|
300
|
+
req_params: {
|
|
301
|
+
speaker: audioParams?.speaker || "zh_female_vv_uranus_bigtts",
|
|
302
|
+
audio_params: {
|
|
303
|
+
...audioParams
|
|
304
|
+
},
|
|
305
|
+
additions: JSON.stringify({
|
|
306
|
+
enable_language_detector: true,
|
|
307
|
+
disable_markdown_filter: true,
|
|
308
|
+
enable_latex_tn: true
|
|
309
|
+
// max_length_to_filter_parenthesis: 100,
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
onWSError(errMsg) {
|
|
314
|
+
setStatus("error");
|
|
315
|
+
setError("\u8FDE\u63A5\u9519\u8BEF");
|
|
316
|
+
console.error("[useTTS] onWSError", errMsg);
|
|
317
|
+
},
|
|
318
|
+
onStart() {
|
|
319
|
+
setStatus("connected");
|
|
320
|
+
console.log("[useTTS] onStart");
|
|
321
|
+
},
|
|
322
|
+
onSessionStarted() {
|
|
323
|
+
setStatus("session-started");
|
|
324
|
+
console.log("[useTTS] onSessionStarted");
|
|
325
|
+
},
|
|
326
|
+
onSessionFinished() {
|
|
327
|
+
setStatus("connected");
|
|
328
|
+
console.log("[useTTS] onSessionFinished");
|
|
329
|
+
if (currentAudioBuffersRef.current.length > 0 && cacheKeyRef.current) {
|
|
330
|
+
const totalSize = currentAudioBuffersRef.current.reduce((acc, cur) => acc + cur.byteLength, 0);
|
|
331
|
+
if (totalSize > 512) {
|
|
332
|
+
TTSCache.set(cacheKeyRef.current, [...currentAudioBuffersRef.current]);
|
|
333
|
+
} else {
|
|
334
|
+
console.warn("[useTTS] Audio data too small, skip caching:", totalSize);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
onTTSSentenceStart(val) {
|
|
339
|
+
console.log("[useTTS] onTTSSentenceStart", val);
|
|
340
|
+
},
|
|
341
|
+
onTTSSentenceEnd(val) {
|
|
342
|
+
console.log("[useTTS] onTTSSentenceEnd", val);
|
|
343
|
+
},
|
|
344
|
+
onError(err) {
|
|
345
|
+
setStatus("error");
|
|
346
|
+
setError(err.msg || "\u8FDE\u63A5\u9519\u8BEF");
|
|
347
|
+
console.error("[useTTS] onError", err);
|
|
348
|
+
},
|
|
349
|
+
onClose() {
|
|
350
|
+
cleanupAll();
|
|
351
|
+
},
|
|
352
|
+
onMessage(audioBuffer) {
|
|
353
|
+
console.log("[useTTS] onMessage", audioBuffer);
|
|
354
|
+
currentAudioBuffersRef.current.push(audioBuffer);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
return audioUrl;
|
|
358
|
+
} catch (err) {
|
|
359
|
+
setStatus("error");
|
|
360
|
+
setError(err instanceof Error ? err.message : "Unknown error");
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
}, [ttsConfig, audioParams, cleanupAll]);
|
|
364
|
+
const disconnect = (0, import_react2.useCallback)(() => {
|
|
365
|
+
cleanupAll();
|
|
366
|
+
setStatus("idle");
|
|
367
|
+
setError(null);
|
|
368
|
+
}, [cleanupAll]);
|
|
369
|
+
const finishSession = (0, import_react2.useCallback)(() => {
|
|
370
|
+
if (!client.current) {
|
|
371
|
+
throw new Error("Client not connected");
|
|
372
|
+
}
|
|
373
|
+
client.current.finishSession();
|
|
374
|
+
}, []);
|
|
375
|
+
const startSession = (0, import_react2.useCallback)(() => {
|
|
376
|
+
if (!client.current) {
|
|
377
|
+
throw new Error("Client not connected");
|
|
378
|
+
}
|
|
379
|
+
client.current?.startSession();
|
|
380
|
+
}, []);
|
|
381
|
+
const sendText = (0, import_react2.useCallback)(async (text) => {
|
|
382
|
+
if (!client.current) {
|
|
383
|
+
throw new Error("Client not connected");
|
|
384
|
+
}
|
|
385
|
+
const speed = audioParams?.speech_rate || 0;
|
|
386
|
+
const voice = audioParams?.speaker || "zh_female_vv_uranus_bigtts";
|
|
387
|
+
const cacheKey = TTSCache.generateKey(text, voice, speed);
|
|
388
|
+
cacheKeyRef.current = cacheKey;
|
|
389
|
+
currentAudioBuffersRef.current = [];
|
|
390
|
+
const cached = await TTSCache.get(cacheKey);
|
|
391
|
+
if (cached && cached.length > 0) {
|
|
392
|
+
const totalSize = cached.reduce((acc, cur) => acc + cur.byteLength, 0);
|
|
393
|
+
if (totalSize < 1024) {
|
|
394
|
+
console.warn("[useTTS] Cached data too small, invalidating cache:", totalSize);
|
|
395
|
+
await TTSCache.delete(cacheKey);
|
|
396
|
+
} else {
|
|
397
|
+
console.log(
|
|
398
|
+
"[useTTS] Cache hit",
|
|
399
|
+
cacheKey,
|
|
400
|
+
"chunks:",
|
|
401
|
+
cached.length,
|
|
402
|
+
"totalSize:",
|
|
403
|
+
totalSize
|
|
404
|
+
);
|
|
405
|
+
if (client.current && typeof client.current.appendBuffer === "function") {
|
|
406
|
+
cached.forEach((buf) => client.current?.appendBuffer(buf));
|
|
407
|
+
if (typeof client.current.closeStream === "function") {
|
|
408
|
+
client.current.closeStream();
|
|
409
|
+
}
|
|
410
|
+
if (typeof client.current.finishConnection === "function") {
|
|
411
|
+
client.current.finishConnection();
|
|
412
|
+
}
|
|
413
|
+
return;
|
|
414
|
+
} else {
|
|
415
|
+
console.warn(
|
|
416
|
+
"[useTTS] Cache hit but client.appendBuffer is missing. Falling back to network."
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const formatText = import_volcano_sdk.MarkdownFormatter.format(text).replace((0, import_emoji_regex.default)(), "");
|
|
422
|
+
console.log("[useTTS] sendText", formatText);
|
|
423
|
+
client.current.sendText(formatText);
|
|
424
|
+
setTimeout(() => {
|
|
425
|
+
client.current?.finishSession();
|
|
426
|
+
}, 50);
|
|
427
|
+
}, [audioParams]);
|
|
428
|
+
return {
|
|
429
|
+
status,
|
|
430
|
+
error,
|
|
431
|
+
connect,
|
|
432
|
+
disconnect,
|
|
433
|
+
startSession,
|
|
434
|
+
finishSession,
|
|
435
|
+
sendText
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// src/tts/useMessageTTS.ts
|
|
440
|
+
var import_tts2 = require("@wq-hook/volcano-sdk/tts");
|
|
441
|
+
var import_volcano_sdk2 = require("@wq-hook/volcano-sdk");
|
|
442
|
+
var import_react3 = require("react");
|
|
443
|
+
var import_emoji_regex2 = __toESM(require("emoji-regex"));
|
|
444
|
+
|
|
445
|
+
// src/tts/TextSplitter.ts
|
|
446
|
+
function splitTextByDelimiters(text, minLength = 10, maxLength = 150) {
|
|
447
|
+
const segments = [];
|
|
448
|
+
let cleanText = text.replace(/[\u200B-\u200D\uFEFF]/g, "");
|
|
449
|
+
const sentenceEndRegex = /([。!?.!?]+[”"']?)/g;
|
|
450
|
+
const rawSegments = cleanText.split(sentenceEndRegex);
|
|
451
|
+
let currentBuffer = "";
|
|
452
|
+
for (let i = 0; i < rawSegments.length; i++) {
|
|
453
|
+
const part = rawSegments[i];
|
|
454
|
+
if (sentenceEndRegex.test(part)) {
|
|
455
|
+
currentBuffer += part;
|
|
456
|
+
} else {
|
|
457
|
+
if (part.trim().length === 0) continue;
|
|
458
|
+
if (currentBuffer.length + part.length > maxLength && currentBuffer.length >= minLength) {
|
|
459
|
+
segments.push({
|
|
460
|
+
index: segments.length,
|
|
461
|
+
content: currentBuffer.trim(),
|
|
462
|
+
length: currentBuffer.trim().length
|
|
463
|
+
});
|
|
464
|
+
currentBuffer = part;
|
|
465
|
+
} else {
|
|
466
|
+
currentBuffer += part;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
const mergedSentences = [];
|
|
471
|
+
let temp = "";
|
|
472
|
+
for (let i = 0; i < rawSegments.length; i++) {
|
|
473
|
+
const part = rawSegments[i];
|
|
474
|
+
if (sentenceEndRegex.test(part)) {
|
|
475
|
+
temp += part;
|
|
476
|
+
mergedSentences.push(temp);
|
|
477
|
+
temp = "";
|
|
478
|
+
} else {
|
|
479
|
+
if (temp.length > 0) {
|
|
480
|
+
}
|
|
481
|
+
temp += part;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (temp.trim().length > 0) {
|
|
485
|
+
mergedSentences.push(temp);
|
|
486
|
+
}
|
|
487
|
+
segments.length = 0;
|
|
488
|
+
currentBuffer = "";
|
|
489
|
+
for (const sent of mergedSentences) {
|
|
490
|
+
if (currentBuffer.length + sent.length > maxLength) {
|
|
491
|
+
if (currentBuffer.trim().length > 0) {
|
|
492
|
+
segments.push({
|
|
493
|
+
index: segments.length,
|
|
494
|
+
content: currentBuffer.trim(),
|
|
495
|
+
length: currentBuffer.trim().length
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
currentBuffer = sent;
|
|
499
|
+
} else {
|
|
500
|
+
currentBuffer += sent;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (currentBuffer.trim().length > 0) {
|
|
504
|
+
segments.push({
|
|
505
|
+
index: segments.length,
|
|
506
|
+
content: currentBuffer.trim(),
|
|
507
|
+
length: currentBuffer.trim().length
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
return segments;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// src/tts/Metrics.ts
|
|
514
|
+
var NoopMetricsCollector = class {
|
|
515
|
+
record(_metric) {
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/tts/useMessageTTS.ts
|
|
520
|
+
var WS_URL = "wss://openspeech.bytedance.com/api/v3/tts/bidirection";
|
|
521
|
+
var activeInstances = /* @__PURE__ */ new Map();
|
|
522
|
+
function buildFullUrl2(url, params) {
|
|
523
|
+
const { ...auth } = params;
|
|
524
|
+
const arr = [];
|
|
525
|
+
for (const key in auth) {
|
|
526
|
+
if (Object.prototype.hasOwnProperty.call(auth, key)) {
|
|
527
|
+
arr.push(
|
|
528
|
+
`${key}=${encodeURIComponent(auth[key])}`
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return `${url}?${arr.join("&")}`;
|
|
533
|
+
}
|
|
534
|
+
function useMessageTTS({
|
|
535
|
+
ttsConfig,
|
|
536
|
+
audioParams,
|
|
537
|
+
autoPlay = true,
|
|
538
|
+
metricsCollector = new NoopMetricsCollector(),
|
|
539
|
+
onPlayStart,
|
|
540
|
+
onPlayPause,
|
|
541
|
+
onPlayResume,
|
|
542
|
+
onPlayEnd,
|
|
543
|
+
onError,
|
|
544
|
+
exclusive = true,
|
|
545
|
+
fallbackVoice,
|
|
546
|
+
visualization
|
|
547
|
+
}) {
|
|
548
|
+
const [isPlaying, setIsPlaying] = (0, import_react3.useState)(false);
|
|
549
|
+
const [isPaused, setIsPaused] = (0, import_react3.useState)(false);
|
|
550
|
+
const [isSynthesizing, setIsSynthesizing] = (0, import_react3.useState)(false);
|
|
551
|
+
const [error, setErrorState] = (0, import_react3.useState)(null);
|
|
552
|
+
const [progress, setProgress] = (0, import_react3.useState)(0);
|
|
553
|
+
const [visualizationData, setVisualizationData] = (0, import_react3.useState)({
|
|
554
|
+
frequencyData: new Uint8Array(0),
|
|
555
|
+
timeDomainData: new Uint8Array(0)
|
|
556
|
+
});
|
|
557
|
+
const instanceId = (0, import_react3.useRef)(
|
|
558
|
+
`tts-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
559
|
+
).current;
|
|
560
|
+
const clientRef = (0, import_react3.useRef)(null);
|
|
561
|
+
const audioRef = (0, import_react3.useRef)(null);
|
|
562
|
+
const audioContextRef = (0, import_react3.useRef)(null);
|
|
563
|
+
const analyserRef = (0, import_react3.useRef)(null);
|
|
564
|
+
const sourceRef = (0, import_react3.useRef)(null);
|
|
565
|
+
const audioUrlRef = (0, import_react3.useRef)(null);
|
|
566
|
+
const cacheKeyRef = (0, import_react3.useRef)("");
|
|
567
|
+
const audioBuffersRef = (0, import_react3.useRef)([]);
|
|
568
|
+
const isFallbackRef = (0, import_react3.useRef)(false);
|
|
569
|
+
const fallbackUtteranceRef = (0, import_react3.useRef)(null);
|
|
570
|
+
const stopOthers = (0, import_react3.useCallback)(() => {
|
|
571
|
+
if (!exclusive) return;
|
|
572
|
+
activeInstances.forEach((instance, id) => {
|
|
573
|
+
if (id !== instanceId) {
|
|
574
|
+
instance.pause();
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
}, [exclusive, instanceId]);
|
|
578
|
+
const initAudioContext = (0, import_react3.useCallback)(() => {
|
|
579
|
+
if (!audioRef.current) return;
|
|
580
|
+
if (!audioContextRef.current) {
|
|
581
|
+
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
|
|
582
|
+
audioContextRef.current = new AudioContextClass();
|
|
583
|
+
}
|
|
584
|
+
if (audioContextRef.current.state === "suspended") {
|
|
585
|
+
audioContextRef.current.resume();
|
|
586
|
+
}
|
|
587
|
+
if (!analyserRef.current) {
|
|
588
|
+
analyserRef.current = audioContextRef.current.createAnalyser();
|
|
589
|
+
analyserRef.current.fftSize = visualization?.fftSize || 256;
|
|
590
|
+
}
|
|
591
|
+
if (!sourceRef.current) {
|
|
592
|
+
try {
|
|
593
|
+
sourceRef.current = audioContextRef.current.createMediaElementSource(
|
|
594
|
+
audioRef.current
|
|
595
|
+
);
|
|
596
|
+
sourceRef.current.connect(analyserRef.current);
|
|
597
|
+
analyserRef.current.connect(audioContextRef.current.destination);
|
|
598
|
+
} catch (e) {
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}, []);
|
|
602
|
+
const cleanupAudio = (0, import_react3.useCallback)(() => {
|
|
603
|
+
if (audioUrlRef.current) {
|
|
604
|
+
URL.revokeObjectURL(audioUrlRef.current);
|
|
605
|
+
audioUrlRef.current = null;
|
|
606
|
+
}
|
|
607
|
+
if (audioRef.current) {
|
|
608
|
+
audioRef.current.onerror = null;
|
|
609
|
+
audioRef.current.onended = null;
|
|
610
|
+
audioRef.current.onpause = null;
|
|
611
|
+
audioRef.current.onplay = null;
|
|
612
|
+
audioRef.current.ontimeupdate = null;
|
|
613
|
+
audioRef.current.pause();
|
|
614
|
+
audioRef.current.src = "";
|
|
615
|
+
audioRef.current = null;
|
|
616
|
+
}
|
|
617
|
+
if (sourceRef.current) {
|
|
618
|
+
try {
|
|
619
|
+
sourceRef.current.disconnect();
|
|
620
|
+
} catch (e) {
|
|
621
|
+
}
|
|
622
|
+
sourceRef.current = null;
|
|
623
|
+
}
|
|
624
|
+
if (fallbackUtteranceRef.current) {
|
|
625
|
+
window.speechSynthesis.cancel();
|
|
626
|
+
fallbackUtteranceRef.current = null;
|
|
627
|
+
}
|
|
628
|
+
isFallbackRef.current = false;
|
|
629
|
+
}, []);
|
|
630
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
631
|
+
if (clientRef.current) {
|
|
632
|
+
clientRef.current.close();
|
|
633
|
+
clientRef.current = null;
|
|
634
|
+
}
|
|
635
|
+
cleanupAudio();
|
|
636
|
+
setIsPlaying(false);
|
|
637
|
+
setIsPaused(false);
|
|
638
|
+
setIsSynthesizing(false);
|
|
639
|
+
setProgress(0);
|
|
640
|
+
activeInstances.delete(instanceId);
|
|
641
|
+
}, [cleanupAudio, instanceId]);
|
|
642
|
+
const pause = (0, import_react3.useCallback)(() => {
|
|
643
|
+
if (isFallbackRef.current) {
|
|
644
|
+
window.speechSynthesis.pause();
|
|
645
|
+
} else if (audioRef.current) {
|
|
646
|
+
audioRef.current.pause();
|
|
647
|
+
}
|
|
648
|
+
setIsPaused(true);
|
|
649
|
+
setIsPlaying(false);
|
|
650
|
+
onPlayPause?.();
|
|
651
|
+
}, [onPlayPause]);
|
|
652
|
+
const resume = (0, import_react3.useCallback)(() => {
|
|
653
|
+
stopOthers();
|
|
654
|
+
if (isFallbackRef.current) {
|
|
655
|
+
window.speechSynthesis.resume();
|
|
656
|
+
} else if (audioRef.current) {
|
|
657
|
+
audioRef.current.play();
|
|
658
|
+
}
|
|
659
|
+
setIsPaused(false);
|
|
660
|
+
setIsPlaying(true);
|
|
661
|
+
onPlayResume?.();
|
|
662
|
+
activeInstances.set(instanceId, { pause });
|
|
663
|
+
}, [stopOthers, instanceId, pause, onPlayResume]);
|
|
664
|
+
const togglePlay = (0, import_react3.useCallback)(() => {
|
|
665
|
+
if (isPlaying) {
|
|
666
|
+
pause();
|
|
667
|
+
} else {
|
|
668
|
+
resume();
|
|
669
|
+
}
|
|
670
|
+
}, [isPlaying, pause, resume]);
|
|
671
|
+
const playFallback = (0, import_react3.useCallback)(
|
|
672
|
+
(text) => {
|
|
673
|
+
console.warn("[useMessageTTS] Switching to fallback TTS");
|
|
674
|
+
isFallbackRef.current = true;
|
|
675
|
+
if (clientRef.current) {
|
|
676
|
+
clientRef.current.close();
|
|
677
|
+
clientRef.current = null;
|
|
678
|
+
}
|
|
679
|
+
if (audioRef.current) {
|
|
680
|
+
audioRef.current.pause();
|
|
681
|
+
audioRef.current = null;
|
|
682
|
+
}
|
|
683
|
+
const utterance = new SpeechSynthesisUtterance(text);
|
|
684
|
+
utterance.rate = audioParams?.speech_rate || 1;
|
|
685
|
+
const voices = window.speechSynthesis.getVoices();
|
|
686
|
+
const zhVoice = voices.find((v) => v.lang.includes("zh"));
|
|
687
|
+
if (zhVoice) utterance.voice = zhVoice;
|
|
688
|
+
utterance.onstart = () => {
|
|
689
|
+
setIsPlaying(true);
|
|
690
|
+
setIsPaused(false);
|
|
691
|
+
setIsSynthesizing(false);
|
|
692
|
+
onPlayStart?.();
|
|
693
|
+
activeInstances.set(instanceId, { pause });
|
|
694
|
+
};
|
|
695
|
+
utterance.onend = () => {
|
|
696
|
+
setIsPlaying(false);
|
|
697
|
+
setIsPaused(false);
|
|
698
|
+
activeInstances.delete(instanceId);
|
|
699
|
+
onPlayEnd?.();
|
|
700
|
+
};
|
|
701
|
+
utterance.onerror = (e) => {
|
|
702
|
+
console.error("[useMessageTTS] Fallback TTS failed", e);
|
|
703
|
+
setErrorState("Fallback TTS failed");
|
|
704
|
+
onError?.(new Error("Fallback TTS failed"));
|
|
705
|
+
setIsPlaying(false);
|
|
706
|
+
};
|
|
707
|
+
fallbackUtteranceRef.current = utterance;
|
|
708
|
+
window.speechSynthesis.speak(utterance);
|
|
709
|
+
},
|
|
710
|
+
[audioParams, instanceId, onError, onPlayEnd, onPlayStart, pause]
|
|
711
|
+
);
|
|
712
|
+
const executeTTS = (0, import_react3.useCallback)(
|
|
713
|
+
async (text, targetVoice) => {
|
|
714
|
+
stop();
|
|
715
|
+
stopOthers();
|
|
716
|
+
setErrorState(null);
|
|
717
|
+
setIsSynthesizing(true);
|
|
718
|
+
setProgress(0);
|
|
719
|
+
audioBuffersRef.current = [];
|
|
720
|
+
isFallbackRef.current = false;
|
|
721
|
+
const speed = audioParams?.speech_rate || 0;
|
|
722
|
+
const voice = targetVoice;
|
|
723
|
+
const cacheKey = TTSCache.generateKey(text, voice, speed);
|
|
724
|
+
cacheKeyRef.current = cacheKey;
|
|
725
|
+
const startTime = Date.now();
|
|
726
|
+
metricsCollector.record({
|
|
727
|
+
name: "tts_request",
|
|
728
|
+
labels: { voice, speed, text_length: text.length },
|
|
729
|
+
value: 1,
|
|
730
|
+
timestamp: startTime
|
|
731
|
+
});
|
|
732
|
+
try {
|
|
733
|
+
const cachedData = await TTSCache.get(cacheKey);
|
|
734
|
+
const audio = new Audio();
|
|
735
|
+
audio.crossOrigin = "anonymous";
|
|
736
|
+
audioRef.current = audio;
|
|
737
|
+
audio.onplay = () => {
|
|
738
|
+
setIsPlaying(true);
|
|
739
|
+
setIsPaused(false);
|
|
740
|
+
onPlayStart?.();
|
|
741
|
+
initAudioContext();
|
|
742
|
+
activeInstances.set(instanceId, { pause });
|
|
743
|
+
metricsCollector.record({
|
|
744
|
+
name: "tts_latency",
|
|
745
|
+
labels: { stage: "playback", voice, speed },
|
|
746
|
+
value: Date.now() - startTime,
|
|
747
|
+
timestamp: Date.now()
|
|
748
|
+
});
|
|
749
|
+
};
|
|
750
|
+
audio.onpause = () => {
|
|
751
|
+
if (!audio.ended) {
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
audio.onended = () => {
|
|
755
|
+
setIsPlaying(false);
|
|
756
|
+
setIsPaused(false);
|
|
757
|
+
onPlayEnd?.();
|
|
758
|
+
activeInstances.delete(instanceId);
|
|
759
|
+
};
|
|
760
|
+
audio.onerror = (e) => {
|
|
761
|
+
console.error("Audio playback error:", e, audio.error);
|
|
762
|
+
metricsCollector.record({
|
|
763
|
+
name: "tts_error",
|
|
764
|
+
labels: { error_code: "playback_error", voice, detail: audio.error?.message || String(audio.error?.code) },
|
|
765
|
+
value: 1,
|
|
766
|
+
timestamp: Date.now()
|
|
767
|
+
});
|
|
768
|
+
handleError(text, voice);
|
|
769
|
+
};
|
|
770
|
+
audio.ontimeupdate = () => {
|
|
771
|
+
let duration = audio.duration;
|
|
772
|
+
if (!isFinite(duration)) {
|
|
773
|
+
if (audio.buffered.length > 0) {
|
|
774
|
+
duration = audio.buffered.end(audio.buffered.length - 1);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
if (isFinite(duration) && duration > 0) {
|
|
778
|
+
setProgress(audio.currentTime / duration * 100);
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
if (cachedData) {
|
|
782
|
+
const totalSize = cachedData.reduce((acc, buf) => acc + buf.byteLength, 0);
|
|
783
|
+
metricsCollector.record({
|
|
784
|
+
name: "tts_cache_hit",
|
|
785
|
+
labels: { voice, speed },
|
|
786
|
+
value: 1,
|
|
787
|
+
timestamp: Date.now()
|
|
788
|
+
});
|
|
789
|
+
console.log(
|
|
790
|
+
JSON.stringify({
|
|
791
|
+
event: "tts_cache_hit",
|
|
792
|
+
cache_hit: true,
|
|
793
|
+
text_len: text.length,
|
|
794
|
+
voice,
|
|
795
|
+
speed,
|
|
796
|
+
data_size: totalSize
|
|
797
|
+
})
|
|
798
|
+
);
|
|
799
|
+
if (totalSize === 0) {
|
|
800
|
+
console.warn("[useMessageTTS] Cached data is empty, falling back to stream");
|
|
801
|
+
} else {
|
|
802
|
+
const blob = new Blob(cachedData, { type: "audio/mpeg" });
|
|
803
|
+
const url2 = URL.createObjectURL(blob);
|
|
804
|
+
audioUrlRef.current = url2;
|
|
805
|
+
audio.src = url2;
|
|
806
|
+
setIsSynthesizing(false);
|
|
807
|
+
if (autoPlay) {
|
|
808
|
+
try {
|
|
809
|
+
await audio.play();
|
|
810
|
+
} catch (err) {
|
|
811
|
+
console.warn("AutoPlay blocked", err);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
console.log("[useMessageTTS] Cache miss, starting stream");
|
|
818
|
+
clientRef.current = (0, import_tts2.WebsocketMSE)({ autoStartSession: true });
|
|
819
|
+
const formattedText = import_volcano_sdk2.MarkdownFormatter.format(text).replace((0, import_emoji_regex2.default)(), "");
|
|
820
|
+
const segments = splitTextByDelimiters(formattedText);
|
|
821
|
+
const url = clientRef.current.start({
|
|
822
|
+
url: buildFullUrl2(WS_URL, {
|
|
823
|
+
api_access_key: `Jwt; ${ttsConfig.token}`,
|
|
824
|
+
api_app_key: ttsConfig.appid,
|
|
825
|
+
api_resource_id: ttsConfig.resourceId || "seed-tts-2.0"
|
|
826
|
+
}),
|
|
827
|
+
config: {
|
|
828
|
+
user: {
|
|
829
|
+
uid: `req-${Date.now()}`
|
|
830
|
+
},
|
|
831
|
+
namespace: ttsConfig.namespace || "BidirectionalTTS",
|
|
832
|
+
req_params: {
|
|
833
|
+
speaker: voice,
|
|
834
|
+
audio_params: {
|
|
835
|
+
sample_rate: audioParams?.sample_rate || 24e3,
|
|
836
|
+
format: audioParams?.format || "mp3",
|
|
837
|
+
speech_rate: audioParams?.speech_rate,
|
|
838
|
+
pitch_rate: audioParams?.pitch_rate,
|
|
839
|
+
loudness_rate: audioParams?.loudness_rate
|
|
840
|
+
},
|
|
841
|
+
additions: JSON.stringify({
|
|
842
|
+
enable_language_detector: true,
|
|
843
|
+
disable_markdown_filter: true,
|
|
844
|
+
enable_latex_tn: true
|
|
845
|
+
// max_length_to_filter_parenthesis: 100,
|
|
846
|
+
})
|
|
847
|
+
}
|
|
848
|
+
},
|
|
849
|
+
onSessionStarted: () => {
|
|
850
|
+
segments.forEach((seg) => {
|
|
851
|
+
clientRef.current?.sendText(seg.content);
|
|
852
|
+
});
|
|
853
|
+
clientRef.current?.finishSession();
|
|
854
|
+
},
|
|
855
|
+
onMessage: (data) => {
|
|
856
|
+
if (audioBuffersRef.current.length === 0) {
|
|
857
|
+
console.log(
|
|
858
|
+
JSON.stringify({
|
|
859
|
+
event: "tts_first_packet",
|
|
860
|
+
latency_ms: Date.now() - startTime,
|
|
861
|
+
voice
|
|
862
|
+
})
|
|
863
|
+
);
|
|
864
|
+
}
|
|
865
|
+
const buffer = data instanceof ArrayBuffer ? data.slice(0) : new Uint8Array(data).buffer;
|
|
866
|
+
audioBuffersRef.current.push(buffer);
|
|
867
|
+
},
|
|
868
|
+
onSessionFinished: () => {
|
|
869
|
+
setIsSynthesizing(false);
|
|
870
|
+
if (audioBuffersRef.current.length > 0) {
|
|
871
|
+
TTSCache.set(cacheKey, [...audioBuffersRef.current]);
|
|
872
|
+
}
|
|
873
|
+
console.log(
|
|
874
|
+
JSON.stringify({
|
|
875
|
+
event: "tts_synthesis_finished",
|
|
876
|
+
cache_hit: false,
|
|
877
|
+
text_len: text.length,
|
|
878
|
+
duration_ms: Date.now() - startTime,
|
|
879
|
+
voice,
|
|
880
|
+
speed
|
|
881
|
+
})
|
|
882
|
+
);
|
|
883
|
+
},
|
|
884
|
+
onError: (err) => {
|
|
885
|
+
console.error("TTS Synthesis error:", err);
|
|
886
|
+
metricsCollector.record({
|
|
887
|
+
name: "tts_error",
|
|
888
|
+
labels: { error_code: "synthesis_error", voice },
|
|
889
|
+
value: 1,
|
|
890
|
+
timestamp: Date.now()
|
|
891
|
+
});
|
|
892
|
+
handleError(text, voice);
|
|
893
|
+
setIsSynthesizing(false);
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
audioUrlRef.current = url;
|
|
897
|
+
audio.src = url;
|
|
898
|
+
if (autoPlay) {
|
|
899
|
+
try {
|
|
900
|
+
await audio.play();
|
|
901
|
+
} catch (e) {
|
|
902
|
+
console.warn("Autoplay blocked/pending", e);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
} catch (err) {
|
|
906
|
+
console.error("Unexpected error in executeTTS:", err);
|
|
907
|
+
metricsCollector.record({
|
|
908
|
+
name: "tts_error",
|
|
909
|
+
labels: { error_code: "unexpected_error", voice },
|
|
910
|
+
value: 1,
|
|
911
|
+
timestamp: Date.now()
|
|
912
|
+
});
|
|
913
|
+
handleError(text, voice);
|
|
914
|
+
}
|
|
915
|
+
},
|
|
916
|
+
[
|
|
917
|
+
ttsConfig,
|
|
918
|
+
audioParams,
|
|
919
|
+
autoPlay,
|
|
920
|
+
stop,
|
|
921
|
+
stopOthers,
|
|
922
|
+
instanceId,
|
|
923
|
+
onPlayStart,
|
|
924
|
+
onPlayEnd,
|
|
925
|
+
initAudioContext,
|
|
926
|
+
pause,
|
|
927
|
+
fallbackVoice,
|
|
928
|
+
metricsCollector
|
|
929
|
+
]
|
|
930
|
+
);
|
|
931
|
+
const handleError = (0, import_react3.useCallback)(
|
|
932
|
+
(text, failedVoice) => {
|
|
933
|
+
if (fallbackVoice && failedVoice !== fallbackVoice) {
|
|
934
|
+
console.warn(
|
|
935
|
+
`[useMessageTTS] Voice ${failedVoice} failed, switching to fallback voice ${fallbackVoice}`
|
|
936
|
+
);
|
|
937
|
+
executeTTS(text, fallbackVoice);
|
|
938
|
+
} else {
|
|
939
|
+
playFallback(text);
|
|
940
|
+
}
|
|
941
|
+
},
|
|
942
|
+
[fallbackVoice, executeTTS, playFallback]
|
|
943
|
+
);
|
|
944
|
+
const play = (0, import_react3.useCallback)(
|
|
945
|
+
(text) => {
|
|
946
|
+
const voice = audioParams?.speaker || "default";
|
|
947
|
+
return executeTTS(text, voice);
|
|
948
|
+
},
|
|
949
|
+
[audioParams, executeTTS]
|
|
950
|
+
);
|
|
951
|
+
const getFrequencyData = (0, import_react3.useCallback)(() => {
|
|
952
|
+
if (!analyserRef.current) return new Uint8Array(0);
|
|
953
|
+
const dataArray = new Uint8Array(analyserRef.current.frequencyBinCount);
|
|
954
|
+
analyserRef.current.getByteFrequencyData(dataArray);
|
|
955
|
+
return dataArray;
|
|
956
|
+
}, []);
|
|
957
|
+
const getTimeDomainData = (0, import_react3.useCallback)(() => {
|
|
958
|
+
if (!analyserRef.current) return new Uint8Array(0);
|
|
959
|
+
const dataArray = new Uint8Array(analyserRef.current.frequencyBinCount);
|
|
960
|
+
analyserRef.current.getByteTimeDomainData(dataArray);
|
|
961
|
+
return dataArray;
|
|
962
|
+
}, []);
|
|
963
|
+
(0, import_react3.useEffect)(() => {
|
|
964
|
+
if (!visualization?.enabled) return;
|
|
965
|
+
let animId;
|
|
966
|
+
let lastUpdate = 0;
|
|
967
|
+
const interval = visualization.refreshInterval || 0;
|
|
968
|
+
const update = (timestamp) => {
|
|
969
|
+
if (isPlaying && !isPaused) {
|
|
970
|
+
if (timestamp - lastUpdate >= interval) {
|
|
971
|
+
setVisualizationData({
|
|
972
|
+
frequencyData: getFrequencyData(),
|
|
973
|
+
timeDomainData: getTimeDomainData()
|
|
974
|
+
});
|
|
975
|
+
lastUpdate = timestamp;
|
|
976
|
+
}
|
|
977
|
+
animId = requestAnimationFrame(update);
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
if (isPlaying && !isPaused) {
|
|
981
|
+
animId = requestAnimationFrame(update);
|
|
982
|
+
}
|
|
983
|
+
return () => {
|
|
984
|
+
if (animId) cancelAnimationFrame(animId);
|
|
985
|
+
};
|
|
986
|
+
}, [isPlaying, isPaused, visualization, getFrequencyData, getTimeDomainData]);
|
|
987
|
+
(0, import_react3.useEffect)(() => {
|
|
988
|
+
return () => {
|
|
989
|
+
stop();
|
|
990
|
+
if (audioContextRef.current) {
|
|
991
|
+
audioContextRef.current.close();
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
}, [stop]);
|
|
995
|
+
const seek = (0, import_react3.useCallback)((percentage) => {
|
|
996
|
+
if (audioRef.current) {
|
|
997
|
+
let duration = audioRef.current.duration;
|
|
998
|
+
if (!isFinite(duration) && audioRef.current.buffered.length > 0) {
|
|
999
|
+
duration = audioRef.current.buffered.end(audioRef.current.buffered.length - 1);
|
|
1000
|
+
}
|
|
1001
|
+
if (isFinite(duration) && duration > 0) {
|
|
1002
|
+
const time = percentage / 100 * duration;
|
|
1003
|
+
if (isFinite(time)) {
|
|
1004
|
+
audioRef.current.currentTime = time;
|
|
1005
|
+
setProgress(percentage);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}, []);
|
|
1010
|
+
return {
|
|
1011
|
+
isPlaying,
|
|
1012
|
+
isPaused,
|
|
1013
|
+
isSynthesizing,
|
|
1014
|
+
error,
|
|
1015
|
+
play,
|
|
1016
|
+
pause,
|
|
1017
|
+
resume,
|
|
1018
|
+
stop,
|
|
1019
|
+
togglePlay,
|
|
1020
|
+
seek,
|
|
1021
|
+
progress,
|
|
1022
|
+
getFrequencyData,
|
|
1023
|
+
getTimeDomainData,
|
|
1024
|
+
visualizationData
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// src/components/AudioWaveVisualizer.tsx
|
|
1029
|
+
var import_react4 = require("react");
|
|
1030
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
1031
|
+
var AudioWaveVisualizer = ({
|
|
1032
|
+
isPlaying,
|
|
1033
|
+
isPaused,
|
|
1034
|
+
frequencyData,
|
|
1035
|
+
timeDomainData,
|
|
1036
|
+
style = "bar",
|
|
1037
|
+
color = "#8b5cf6",
|
|
1038
|
+
bars = 50,
|
|
1039
|
+
height = 60,
|
|
1040
|
+
width = 200,
|
|
1041
|
+
className,
|
|
1042
|
+
styleObj
|
|
1043
|
+
}) => {
|
|
1044
|
+
const canvasRef = (0, import_react4.useRef)(null);
|
|
1045
|
+
const requestRef = (0, import_react4.useRef)(null);
|
|
1046
|
+
const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
|
|
1047
|
+
const textColor = Array.isArray(color) ? color[0] : color;
|
|
1048
|
+
const draw = () => {
|
|
1049
|
+
const canvas = canvasRef.current;
|
|
1050
|
+
if (!canvas) return;
|
|
1051
|
+
const ctx = canvas.getContext("2d");
|
|
1052
|
+
if (!ctx) return;
|
|
1053
|
+
const w = canvas.width;
|
|
1054
|
+
const h = canvas.height;
|
|
1055
|
+
ctx.clearRect(0, 0, w, h);
|
|
1056
|
+
let fillStyle = "";
|
|
1057
|
+
if (Array.isArray(color)) {
|
|
1058
|
+
const gradient = ctx.createLinearGradient(0, 0, w, 0);
|
|
1059
|
+
gradient.addColorStop(0, color[0]);
|
|
1060
|
+
gradient.addColorStop(1, color[1]);
|
|
1061
|
+
fillStyle = gradient;
|
|
1062
|
+
} else {
|
|
1063
|
+
fillStyle = color;
|
|
1064
|
+
}
|
|
1065
|
+
ctx.fillStyle = fillStyle;
|
|
1066
|
+
ctx.strokeStyle = fillStyle;
|
|
1067
|
+
if (style === "bar" && frequencyData) {
|
|
1068
|
+
const barWidth = w / bars;
|
|
1069
|
+
const step = Math.floor(frequencyData.length / bars);
|
|
1070
|
+
for (let i = 0; i < bars; i++) {
|
|
1071
|
+
const value = frequencyData[i * step] || 0;
|
|
1072
|
+
const percent = value / 255;
|
|
1073
|
+
const barHeight = h * percent;
|
|
1074
|
+
ctx.fillRect(i * barWidth, h - barHeight, barWidth - 2, barHeight);
|
|
1075
|
+
}
|
|
1076
|
+
} else if (style === "line" && timeDomainData) {
|
|
1077
|
+
ctx.lineWidth = 2;
|
|
1078
|
+
ctx.beginPath();
|
|
1079
|
+
const sliceWidth = w / timeDomainData.length;
|
|
1080
|
+
let x = 0;
|
|
1081
|
+
for (let i = 0; i < timeDomainData.length; i++) {
|
|
1082
|
+
const v = timeDomainData[i] / 128;
|
|
1083
|
+
const y = v * h / 2;
|
|
1084
|
+
if (i === 0) ctx.moveTo(x, y);
|
|
1085
|
+
else ctx.lineTo(x, y);
|
|
1086
|
+
x += sliceWidth;
|
|
1087
|
+
}
|
|
1088
|
+
ctx.stroke();
|
|
1089
|
+
} else if (style === "wave") {
|
|
1090
|
+
const avg = frequencyData ? frequencyData.reduce((a, b) => a + b, 0) / frequencyData.length : 0;
|
|
1091
|
+
const amplitude = avg / 255 * (h / 2);
|
|
1092
|
+
ctx.beginPath();
|
|
1093
|
+
for (let x = 0; x < w; x++) {
|
|
1094
|
+
const y = h / 2 + Math.sin(x * 0.1 + Date.now() * 0.01) * amplitude;
|
|
1095
|
+
if (x === 0) ctx.moveTo(x, y);
|
|
1096
|
+
else ctx.lineTo(x, y);
|
|
1097
|
+
}
|
|
1098
|
+
ctx.stroke();
|
|
1099
|
+
} else if (style === "circle" && frequencyData) {
|
|
1100
|
+
const avg = frequencyData.reduce((a, b) => a + b, 0) / frequencyData.length;
|
|
1101
|
+
const radius = avg / 255 * (Math.min(w, h) / 2);
|
|
1102
|
+
ctx.beginPath();
|
|
1103
|
+
ctx.arc(w / 2, h / 2, Math.max(5, radius), 0, 2 * Math.PI);
|
|
1104
|
+
ctx.fill();
|
|
1105
|
+
}
|
|
1106
|
+
if (isPlaying && !isPaused) {
|
|
1107
|
+
requestRef.current = requestAnimationFrame(draw);
|
|
1108
|
+
}
|
|
1109
|
+
};
|
|
1110
|
+
(0, import_react4.useEffect)(() => {
|
|
1111
|
+
if (isPlaying && !isPaused) {
|
|
1112
|
+
requestRef.current = requestAnimationFrame(draw);
|
|
1113
|
+
} else {
|
|
1114
|
+
draw();
|
|
1115
|
+
if (requestRef.current) cancelAnimationFrame(requestRef.current);
|
|
1116
|
+
}
|
|
1117
|
+
return () => {
|
|
1118
|
+
if (requestRef.current) cancelAnimationFrame(requestRef.current);
|
|
1119
|
+
};
|
|
1120
|
+
}, [
|
|
1121
|
+
isPlaying,
|
|
1122
|
+
isPaused,
|
|
1123
|
+
frequencyData,
|
|
1124
|
+
timeDomainData,
|
|
1125
|
+
style,
|
|
1126
|
+
color
|
|
1127
|
+
]);
|
|
1128
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1129
|
+
"div",
|
|
1130
|
+
{
|
|
1131
|
+
className,
|
|
1132
|
+
style: {
|
|
1133
|
+
display: "inline-flex",
|
|
1134
|
+
flexDirection: "column",
|
|
1135
|
+
gap: "4px",
|
|
1136
|
+
width,
|
|
1137
|
+
...styleObj
|
|
1138
|
+
},
|
|
1139
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1140
|
+
"canvas",
|
|
1141
|
+
{
|
|
1142
|
+
ref: canvasRef,
|
|
1143
|
+
width,
|
|
1144
|
+
height,
|
|
1145
|
+
style: { display: "block" }
|
|
1146
|
+
}
|
|
1147
|
+
)
|
|
1148
|
+
}
|
|
1149
|
+
);
|
|
1150
|
+
};
|
|
1151
|
+
var AudioWaveVisualizer_default = AudioWaveVisualizer;
|
|
1152
|
+
|
|
1153
|
+
// src/components/AudioProgressBar.tsx
|
|
1154
|
+
var import_react5 = require("react");
|
|
1155
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
1156
|
+
var AudioProgressBar = ({
|
|
1157
|
+
progress,
|
|
1158
|
+
width = "100%",
|
|
1159
|
+
height = 6,
|
|
1160
|
+
color = "#8b5cf6",
|
|
1161
|
+
backgroundColor = "rgba(128, 128, 128, 0.2)",
|
|
1162
|
+
showText = true,
|
|
1163
|
+
onSeek,
|
|
1164
|
+
className,
|
|
1165
|
+
style
|
|
1166
|
+
}) => {
|
|
1167
|
+
const progressBarRef = (0, import_react5.useRef)(null);
|
|
1168
|
+
const containerRef = (0, import_react5.useRef)(null);
|
|
1169
|
+
const progressTextRef = (0, import_react5.useRef)(null);
|
|
1170
|
+
const thumbRef = (0, import_react5.useRef)(null);
|
|
1171
|
+
const displayedProgress = (0, import_react5.useRef)(0);
|
|
1172
|
+
const requestRef = (0, import_react5.useRef)(null);
|
|
1173
|
+
const isDragging = (0, import_react5.useRef)(false);
|
|
1174
|
+
const isHovering = (0, import_react5.useRef)(false);
|
|
1175
|
+
const isTouch = (0, import_react5.useRef)(false);
|
|
1176
|
+
(0, import_react5.useEffect)(() => {
|
|
1177
|
+
const match = window.matchMedia("(pointer: coarse)");
|
|
1178
|
+
isTouch.current = match.matches;
|
|
1179
|
+
if (isTouch.current && thumbRef.current) {
|
|
1180
|
+
thumbRef.current.style.opacity = "1";
|
|
1181
|
+
thumbRef.current.style.transform = "translate(-50%, -50%) scale(1)";
|
|
1182
|
+
}
|
|
1183
|
+
}, []);
|
|
1184
|
+
const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
|
|
1185
|
+
const textColor = Array.isArray(color) ? color[0] : color;
|
|
1186
|
+
const animate = () => {
|
|
1187
|
+
if (isDragging.current) {
|
|
1188
|
+
requestRef.current = requestAnimationFrame(animate);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
const target = Math.min(100, Math.max(0, progress));
|
|
1192
|
+
const current = displayedProgress.current;
|
|
1193
|
+
const ease = 0.1;
|
|
1194
|
+
let next = current + (target - current) * ease;
|
|
1195
|
+
if (Math.abs(target - next) < 0.1) {
|
|
1196
|
+
next = target;
|
|
1197
|
+
}
|
|
1198
|
+
displayedProgress.current = next;
|
|
1199
|
+
if (progressBarRef.current) {
|
|
1200
|
+
progressBarRef.current.style.width = `${next}%`;
|
|
1201
|
+
}
|
|
1202
|
+
if (thumbRef.current) {
|
|
1203
|
+
thumbRef.current.style.left = `${next}%`;
|
|
1204
|
+
}
|
|
1205
|
+
if (showText && progressTextRef.current) {
|
|
1206
|
+
const rounded = Math.round(next);
|
|
1207
|
+
if (progressTextRef.current.textContent !== `${rounded}%`) {
|
|
1208
|
+
progressTextRef.current.textContent = `${rounded}%`;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
if (Math.abs(target - next) >= 0.1) {
|
|
1212
|
+
requestRef.current = requestAnimationFrame(animate);
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
(0, import_react5.useEffect)(() => {
|
|
1216
|
+
requestRef.current = requestAnimationFrame(animate);
|
|
1217
|
+
return () => {
|
|
1218
|
+
if (requestRef.current) {
|
|
1219
|
+
cancelAnimationFrame(requestRef.current);
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
}, [progress, showText]);
|
|
1223
|
+
const calculateProgress = (clientX) => {
|
|
1224
|
+
if (!containerRef.current) return 0;
|
|
1225
|
+
const rect = containerRef.current.getBoundingClientRect();
|
|
1226
|
+
const x = clientX - rect.left;
|
|
1227
|
+
const width2 = rect.width;
|
|
1228
|
+
const percentage = Math.min(100, Math.max(0, x / width2 * 100));
|
|
1229
|
+
return percentage;
|
|
1230
|
+
};
|
|
1231
|
+
const updateVisuals = (percentage) => {
|
|
1232
|
+
displayedProgress.current = percentage;
|
|
1233
|
+
if (progressBarRef.current) {
|
|
1234
|
+
progressBarRef.current.style.width = `${percentage}%`;
|
|
1235
|
+
}
|
|
1236
|
+
if (thumbRef.current) {
|
|
1237
|
+
thumbRef.current.style.left = `${percentage}%`;
|
|
1238
|
+
}
|
|
1239
|
+
if (showText && progressTextRef.current) {
|
|
1240
|
+
const rounded = Math.round(percentage);
|
|
1241
|
+
if (progressTextRef.current.textContent !== `${rounded}%`) {
|
|
1242
|
+
progressTextRef.current.textContent = `${rounded}%`;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
const updateThumbVisibility = (visible) => {
|
|
1247
|
+
if (!thumbRef.current || isTouch.current) return;
|
|
1248
|
+
if (visible) {
|
|
1249
|
+
thumbRef.current.style.opacity = "1";
|
|
1250
|
+
thumbRef.current.style.transform = "translate(-50%, -50%) scale(1)";
|
|
1251
|
+
} else {
|
|
1252
|
+
thumbRef.current.style.opacity = "0";
|
|
1253
|
+
thumbRef.current.style.transform = "translate(-50%, -50%) scale(0.8)";
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
const handleMouseEnter = () => {
|
|
1257
|
+
if (!onSeek) return;
|
|
1258
|
+
isHovering.current = true;
|
|
1259
|
+
updateThumbVisibility(true);
|
|
1260
|
+
};
|
|
1261
|
+
const handleMouseLeave = () => {
|
|
1262
|
+
if (!onSeek) return;
|
|
1263
|
+
isHovering.current = false;
|
|
1264
|
+
if (!isDragging.current) {
|
|
1265
|
+
updateThumbVisibility(false);
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
const handleMouseDown = (e) => {
|
|
1269
|
+
if (!onSeek) return;
|
|
1270
|
+
e.preventDefault();
|
|
1271
|
+
e.stopPropagation();
|
|
1272
|
+
isDragging.current = true;
|
|
1273
|
+
updateThumbVisibility(true);
|
|
1274
|
+
const percentage = calculateProgress(e.clientX);
|
|
1275
|
+
updateVisuals(percentage);
|
|
1276
|
+
const handleMouseMove = (e2) => {
|
|
1277
|
+
if (isDragging.current) {
|
|
1278
|
+
e2.preventDefault();
|
|
1279
|
+
const percentage2 = calculateProgress(e2.clientX);
|
|
1280
|
+
updateVisuals(percentage2);
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
const handleMouseUp = (e2) => {
|
|
1284
|
+
if (isDragging.current) {
|
|
1285
|
+
e2.preventDefault();
|
|
1286
|
+
isDragging.current = false;
|
|
1287
|
+
const percentage2 = calculateProgress(e2.clientX);
|
|
1288
|
+
onSeek(percentage2);
|
|
1289
|
+
if (!isHovering.current) {
|
|
1290
|
+
updateThumbVisibility(false);
|
|
1291
|
+
}
|
|
1292
|
+
window.removeEventListener("mousemove", handleMouseMove);
|
|
1293
|
+
window.removeEventListener("mouseup", handleMouseUp);
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
window.addEventListener("mousemove", handleMouseMove);
|
|
1297
|
+
window.addEventListener("mouseup", handleMouseUp);
|
|
1298
|
+
};
|
|
1299
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
1300
|
+
"div",
|
|
1301
|
+
{
|
|
1302
|
+
className,
|
|
1303
|
+
style: {
|
|
1304
|
+
display: "flex",
|
|
1305
|
+
alignItems: "center",
|
|
1306
|
+
gap: "8px",
|
|
1307
|
+
width,
|
|
1308
|
+
cursor: onSeek ? "pointer" : "default",
|
|
1309
|
+
userSelect: "none",
|
|
1310
|
+
WebkitUserSelect: "none",
|
|
1311
|
+
touchAction: "none",
|
|
1312
|
+
padding: "4px 0",
|
|
1313
|
+
// 增加点击区域
|
|
1314
|
+
...style
|
|
1315
|
+
},
|
|
1316
|
+
onMouseDown: handleMouseDown,
|
|
1317
|
+
onMouseEnter: handleMouseEnter,
|
|
1318
|
+
onMouseLeave: handleMouseLeave,
|
|
1319
|
+
children: [
|
|
1320
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
1321
|
+
"div",
|
|
1322
|
+
{
|
|
1323
|
+
ref: containerRef,
|
|
1324
|
+
style: {
|
|
1325
|
+
flex: 1,
|
|
1326
|
+
height: `${height}px`,
|
|
1327
|
+
backgroundColor,
|
|
1328
|
+
borderRadius: `${height / 2}px`,
|
|
1329
|
+
position: "relative"
|
|
1330
|
+
// 允许绝对定位子元素
|
|
1331
|
+
// overflow: "hidden", // 移除 overflow hidden 以显示滑块
|
|
1332
|
+
},
|
|
1333
|
+
children: [
|
|
1334
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1335
|
+
"div",
|
|
1336
|
+
{
|
|
1337
|
+
ref: progressBarRef,
|
|
1338
|
+
style: {
|
|
1339
|
+
width: `${displayedProgress.current}%`,
|
|
1340
|
+
// 初始值
|
|
1341
|
+
height: "100%",
|
|
1342
|
+
background: progressBackground,
|
|
1343
|
+
borderRadius: `${height / 2}px`
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
),
|
|
1347
|
+
onSeek && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1348
|
+
"div",
|
|
1349
|
+
{
|
|
1350
|
+
ref: thumbRef,
|
|
1351
|
+
style: {
|
|
1352
|
+
position: "absolute",
|
|
1353
|
+
left: `${displayedProgress.current}%`,
|
|
1354
|
+
top: "50%",
|
|
1355
|
+
transform: "translate(-50%, -50%) scale(0.8)",
|
|
1356
|
+
// 初始略微缩小
|
|
1357
|
+
width: `${Math.max(12, height * 2)}px`,
|
|
1358
|
+
height: `${Math.max(12, height * 2)}px`,
|
|
1359
|
+
borderRadius: "50%",
|
|
1360
|
+
backgroundColor: "#fff",
|
|
1361
|
+
border: `2px solid ${textColor}`,
|
|
1362
|
+
boxShadow: "0 2px 4px rgba(0,0,0,0.2)",
|
|
1363
|
+
opacity: 0,
|
|
1364
|
+
// 默认隐藏
|
|
1365
|
+
transition: "opacity 0.2s ease, transform 0.2s cubic-bezier(0.4, 0, 0.2, 1)",
|
|
1366
|
+
pointerEvents: "none",
|
|
1367
|
+
// 让点击穿透到 container
|
|
1368
|
+
zIndex: 10
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
)
|
|
1372
|
+
]
|
|
1373
|
+
}
|
|
1374
|
+
),
|
|
1375
|
+
showText && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
1376
|
+
"span",
|
|
1377
|
+
{
|
|
1378
|
+
ref: progressTextRef,
|
|
1379
|
+
style: {
|
|
1380
|
+
fontSize: "12px",
|
|
1381
|
+
color: textColor,
|
|
1382
|
+
minWidth: "32px",
|
|
1383
|
+
textAlign: "right",
|
|
1384
|
+
fontFamily: "monospace",
|
|
1385
|
+
lineHeight: 1
|
|
1386
|
+
},
|
|
1387
|
+
children: [
|
|
1388
|
+
Math.round(displayedProgress.current),
|
|
1389
|
+
"%"
|
|
1390
|
+
]
|
|
1391
|
+
}
|
|
1392
|
+
)
|
|
1393
|
+
]
|
|
1394
|
+
}
|
|
1395
|
+
);
|
|
1396
|
+
};
|
|
1397
|
+
var AudioProgressBar_default = AudioProgressBar;
|
|
1398
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1399
|
+
0 && (module.exports = {
|
|
1400
|
+
AudioProgressBar,
|
|
1401
|
+
AudioWaveVisualizer,
|
|
1402
|
+
splitTextByDelimiters,
|
|
1403
|
+
useMessageTTS,
|
|
1404
|
+
useVolcanoASR,
|
|
1405
|
+
useVolcanoTTS
|
|
1406
|
+
});
|