jbc-dssi 1.1.163 → 1.1.165
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/index.js +32 -3
- package/lib/edge-tts.js +140 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -27,6 +27,7 @@ import { createScheduleEngine } from './lib/schedules.js';
|
|
|
27
27
|
import { createProcEngine } from './lib/proc-commands.js';
|
|
28
28
|
import { createTtsEngine } from './lib/tts.js';
|
|
29
29
|
import { createEdgeTts, EDGE_VOICES } from './lib/edge-tts.js';
|
|
30
|
+
import { createEdgeStreamSession } from './lib/edge-tts.js';
|
|
30
31
|
import { createAppReleaseWatch } from './lib/app-release-watch.js';
|
|
31
32
|
import { PLUGIN_FEATURES } from './lib/plugin-features.js';
|
|
32
33
|
import { MAX_FILE_BYTES, mimeOfFile, mimeOfImage, resolveWorkspaceFilePath } from './lib/workspace-bytes.js';
|
|
@@ -2229,14 +2230,20 @@ class BridgeConnection {
|
|
|
2229
2230
|
if (op === 'stop') {
|
|
2230
2231
|
this.tts.stop(id);
|
|
2231
2232
|
this.edge?.stop(id);
|
|
2233
|
+
const live = this.edgeSessions?.get(id);
|
|
2234
|
+
if (live) {
|
|
2235
|
+
live.close();
|
|
2236
|
+
this.edgeSessions.delete(id);
|
|
2237
|
+
}
|
|
2232
2238
|
return;
|
|
2233
2239
|
}
|
|
2234
2240
|
if (op === 'voices') {
|
|
2235
2241
|
this.send(FRAME.VOICE_LIST, { voices: EDGE_VOICES });
|
|
2236
2242
|
return;
|
|
2237
2243
|
}
|
|
2244
|
+
this.edge ??= createEdgeTts({ log: (message) => this.opts.log(message) });
|
|
2245
|
+
this.edgeSessions ??= new Map();
|
|
2238
2246
|
if (op === 'player') {
|
|
2239
|
-
this.edge ??= createEdgeTts({ log: (message) => this.opts.log(message) });
|
|
2240
2247
|
const result = await this.edge.synthesize({
|
|
2241
2248
|
id,
|
|
2242
2249
|
text: payload?.text,
|
|
@@ -2248,6 +2255,20 @@ class BridgeConnection {
|
|
|
2248
2255
|
return;
|
|
2249
2256
|
}
|
|
2250
2257
|
if (op === 'open') {
|
|
2258
|
+
const voice = typeof payload?.voice === 'string' ? payload.voice : '';
|
|
2259
|
+
if (EDGE_VOICES.some((v) => v.id === voice)) {
|
|
2260
|
+
const session = createEdgeStreamSession({
|
|
2261
|
+
log: (message) => this.opts.log(message),
|
|
2262
|
+
voice,
|
|
2263
|
+
onSegment: (bytes) =>
|
|
2264
|
+
this.send(FRAME.TTS_AUDIO, { id, segment: bytes.toString('base64'), codec: 'mp3' }),
|
|
2265
|
+
onError: (message) =>
|
|
2266
|
+
this.send(FRAME.TTS_AUDIO, { id, error: { code: 'edge', message } }),
|
|
2267
|
+
onDone: () => this.send(FRAME.TTS_AUDIO, { id, done: true }),
|
|
2268
|
+
});
|
|
2269
|
+
this.edgeSessions.set(id, session);
|
|
2270
|
+
return;
|
|
2271
|
+
}
|
|
2251
2272
|
await this.tts.open({
|
|
2252
2273
|
id,
|
|
2253
2274
|
voice: payload?.voice,
|
|
@@ -2258,11 +2279,19 @@ class BridgeConnection {
|
|
|
2258
2279
|
return;
|
|
2259
2280
|
}
|
|
2260
2281
|
if (op === 'text') {
|
|
2261
|
-
this.
|
|
2282
|
+
const live = this.edgeSessions?.get(id);
|
|
2283
|
+
if (live) live.send(payload?.text);
|
|
2284
|
+
else this.tts.text(id, payload?.text);
|
|
2262
2285
|
return;
|
|
2263
2286
|
}
|
|
2264
2287
|
if (op === 'end') {
|
|
2265
|
-
this.
|
|
2288
|
+
const live = this.edgeSessions?.get(id);
|
|
2289
|
+
if (live) {
|
|
2290
|
+
this.edgeSessions.delete(id);
|
|
2291
|
+
live.finish();
|
|
2292
|
+
} else {
|
|
2293
|
+
this.tts.end(id);
|
|
2294
|
+
}
|
|
2266
2295
|
}
|
|
2267
2296
|
}
|
|
2268
2297
|
|
package/lib/edge-tts.js
CHANGED
|
@@ -68,6 +68,146 @@ function ssmlMessage(text, voice, requestId) {
|
|
|
68
68
|
return `X-RequestId:${requestId}\r\nContent-Type:application/ssml+xml\r\nX-Timestamp:${stamp()}\r\nPath:ssml\r\n\r\n${ssml(text, voice)}`;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 流式会话:一条 WebSocket 连续合成多段文本。
|
|
74
|
+
* 每段合成完(turn.end)回调一次完整 mp3;段与段共用连接,省掉 1 秒左右的握手。
|
|
75
|
+
*/
|
|
76
|
+
export function createEdgeStreamSession({ log, voice, onSegment, onError, onDone }) {
|
|
77
|
+
const segments = [];
|
|
78
|
+
let ws = null;
|
|
79
|
+
let connecting = null;
|
|
80
|
+
let busy = false;
|
|
81
|
+
let closed = false;
|
|
82
|
+
let finished = false;
|
|
83
|
+
let buffer = [];
|
|
84
|
+
let turnEnd = null;
|
|
85
|
+
|
|
86
|
+
async function ensureWs() {
|
|
87
|
+
if (ws) return ws;
|
|
88
|
+
if (connecting) return connecting;
|
|
89
|
+
connecting = (async () => {
|
|
90
|
+
const requestId = randomUUID();
|
|
91
|
+
const ticks = Math.floor(Date.now() / 1000) + 11644473600;
|
|
92
|
+
const rounded = ticks - (ticks % 300);
|
|
93
|
+
let secMsGec = '';
|
|
94
|
+
try {
|
|
95
|
+
secMsGec = await hashSecMsGecV2(`${rounded * 10000000}${TOKEN}`);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
log?.(`[edge-tts] GEC 计算失败: ${err?.message ?? err}`);
|
|
98
|
+
}
|
|
99
|
+
const url = `${BASE}?TrustedClientToken=${TOKEN}&Sec-MS-GEC=${secMsGec}&Sec-MS-GEC-Version=2-143.0.3650.96&ConnectionId=${requestId}`;
|
|
100
|
+
const socket = new WebSocket(url, {
|
|
101
|
+
headers: {
|
|
102
|
+
Origin: 'chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold',
|
|
103
|
+
Pragma: 'no-cache',
|
|
104
|
+
'Cache-Control': 'no-cache',
|
|
105
|
+
'User-Agent':
|
|
106
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0',
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
await new Promise((resolve, reject) => {
|
|
110
|
+
socket.once('open', resolve);
|
|
111
|
+
socket.once('error', reject);
|
|
112
|
+
});
|
|
113
|
+
socket.send(configMessage());
|
|
114
|
+
socket.on('message', (data, isBinary) => {
|
|
115
|
+
if (!isBinary) {
|
|
116
|
+
const textFrame = data.toString('utf8');
|
|
117
|
+
if (textFrame.includes('Path:turn.end') && turnEnd) {
|
|
118
|
+
const done = turnEnd;
|
|
119
|
+
turnEnd = null;
|
|
120
|
+
done();
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const raw = Buffer.from(data);
|
|
125
|
+
const headerLength = raw.readUInt16BE(0);
|
|
126
|
+
const header = raw.subarray(2, 2 + headerLength).toString('utf8');
|
|
127
|
+
if (!header.includes('Path:audio')) return;
|
|
128
|
+
const audio = raw.subarray(2 + headerLength);
|
|
129
|
+
if (audio.length > 0) buffer.push(audio);
|
|
130
|
+
});
|
|
131
|
+
socket.on('close', () => {
|
|
132
|
+
ws = null;
|
|
133
|
+
connecting = null;
|
|
134
|
+
if (turnEnd) {
|
|
135
|
+
const done = turnEnd;
|
|
136
|
+
turnEnd = null;
|
|
137
|
+
done();
|
|
138
|
+
}
|
|
139
|
+
if (!closed) onError?.('微软语音连接中断');
|
|
140
|
+
});
|
|
141
|
+
socket.on('error', (err) => {
|
|
142
|
+
log?.(`[edge-tts] 流式连接异常: ${err?.message ?? err}`);
|
|
143
|
+
});
|
|
144
|
+
ws = socket;
|
|
145
|
+
return socket;
|
|
146
|
+
})();
|
|
147
|
+
try {
|
|
148
|
+
return await connecting;
|
|
149
|
+
} finally {
|
|
150
|
+
connecting = null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function pump() {
|
|
155
|
+
if (busy || closed) return;
|
|
156
|
+
const text = segments.shift();
|
|
157
|
+
if (text == null) {
|
|
158
|
+
if (finished) onDone?.();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
busy = true;
|
|
162
|
+
try {
|
|
163
|
+
const socket = await ensureWs();
|
|
164
|
+
buffer = [];
|
|
165
|
+
socket.send(ssmlMessage(text, voiceOf(voice), randomUUID()));
|
|
166
|
+
await new Promise((resolve) => {
|
|
167
|
+
turnEnd = resolve;
|
|
168
|
+
setTimeout(() => {
|
|
169
|
+
if (turnEnd === resolve) {
|
|
170
|
+
turnEnd = null;
|
|
171
|
+
resolve();
|
|
172
|
+
}
|
|
173
|
+
}, 60_000);
|
|
174
|
+
});
|
|
175
|
+
const bytes = Buffer.concat(buffer);
|
|
176
|
+
buffer = [];
|
|
177
|
+
if (bytes.length > 0) onSegment?.(bytes);
|
|
178
|
+
else onError?.('语音服务没有返回音频');
|
|
179
|
+
} catch (err) {
|
|
180
|
+
onError?.(`语音合成失败:${err?.message ?? err}`);
|
|
181
|
+
} finally {
|
|
182
|
+
busy = false;
|
|
183
|
+
}
|
|
184
|
+
pump();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
send(text) {
|
|
189
|
+
const body = String(text ?? '');
|
|
190
|
+
if (body.trim() === '' || closed) return;
|
|
191
|
+
segments.push(body);
|
|
192
|
+
pump();
|
|
193
|
+
},
|
|
194
|
+
finish() {
|
|
195
|
+
finished = true;
|
|
196
|
+
pump();
|
|
197
|
+
},
|
|
198
|
+
close() {
|
|
199
|
+
closed = true;
|
|
200
|
+
segments.length = 0;
|
|
201
|
+
try {
|
|
202
|
+
ws?.close();
|
|
203
|
+
} catch {
|
|
204
|
+
/* 已关闭 */
|
|
205
|
+
}
|
|
206
|
+
ws = null;
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
71
211
|
export function createEdgeTts({ log }) {
|
|
72
212
|
const sessions = new Map();
|
|
73
213
|
|