jbc-dssi 1.1.148 → 1.1.149
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 +22 -13
- package/lib/ogg-opus.js +101 -0
- package/lib/tts.js +121 -52
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2171,23 +2171,32 @@ class BridgeConnection {
|
|
|
2171
2171
|
const op = payload?.op;
|
|
2172
2172
|
const id = typeof payload?.id === 'string' ? payload.id : '';
|
|
2173
2173
|
if (id === '') return;
|
|
2174
|
-
if (
|
|
2175
|
-
this.tts?.stop(id);
|
|
2176
|
-
return;
|
|
2177
|
-
}
|
|
2178
|
-
if (op !== 'start' || this.closed) return;
|
|
2174
|
+
if (this.closed) return;
|
|
2179
2175
|
this.tts ??= createTtsEngine({
|
|
2180
2176
|
log: (message) => this.opts.log(message),
|
|
2181
2177
|
resolveKey: () => this.#deepseekKey(),
|
|
2182
2178
|
});
|
|
2183
|
-
|
|
2184
|
-
id
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2179
|
+
if (op === 'stop') {
|
|
2180
|
+
this.tts.stop(id);
|
|
2181
|
+
return;
|
|
2182
|
+
}
|
|
2183
|
+
if (op === 'open') {
|
|
2184
|
+
await this.tts.open({
|
|
2185
|
+
id,
|
|
2186
|
+
voice: payload?.voice,
|
|
2187
|
+
onPage: (frame) => this.send(FRAME.TTS_AUDIO, frame),
|
|
2188
|
+
onDone: (frame) => this.send(FRAME.TTS_AUDIO, { id, done: true, pages: frame?.pages ?? 0 }),
|
|
2189
|
+
onError: (frame) => this.send(FRAME.TTS_AUDIO, { id, error: frame?.error }),
|
|
2190
|
+
});
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
if (op === 'text') {
|
|
2194
|
+
this.tts.text(id, payload?.text);
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
if (op === 'end') {
|
|
2198
|
+
this.tts.end(id);
|
|
2199
|
+
}
|
|
2191
2200
|
}
|
|
2192
2201
|
|
|
2193
2202
|
async #deepseekKey() {
|
package/lib/ogg-opus.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const SEGMENT_MAX = 255;
|
|
2
|
+
const CRC_TABLE = (() => {
|
|
3
|
+
const table = new Int32Array(256);
|
|
4
|
+
for (let i = 0; i < 256; i += 1) {
|
|
5
|
+
let r = i << 24;
|
|
6
|
+
for (let j = 0; j < 8; j += 1) {
|
|
7
|
+
r = (r & 0x80000000) !== 0 ? ((r << 1) ^ 0x04c11db7) : (r << 1);
|
|
8
|
+
}
|
|
9
|
+
table[i] = r;
|
|
10
|
+
}
|
|
11
|
+
return table;
|
|
12
|
+
})();
|
|
13
|
+
|
|
14
|
+
function crc32(buffer, start, end) {
|
|
15
|
+
let crc = 0;
|
|
16
|
+
for (let i = start; i < end; i += 1) {
|
|
17
|
+
crc = ((crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff]) >>> 0;
|
|
18
|
+
}
|
|
19
|
+
return crc >>> 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function page({ headerType, granule, serial, sequence, packets }) {
|
|
23
|
+
const segments = [];
|
|
24
|
+
for (const packet of packets) {
|
|
25
|
+
let rest = packet.length;
|
|
26
|
+
while (rest >= SEGMENT_MAX) {
|
|
27
|
+
segments.push(SEGMENT_MAX);
|
|
28
|
+
rest -= SEGMENT_MAX;
|
|
29
|
+
}
|
|
30
|
+
segments.push(rest);
|
|
31
|
+
}
|
|
32
|
+
const body = Buffer.concat(packets);
|
|
33
|
+
const header = Buffer.alloc(27 + segments.length);
|
|
34
|
+
header.write('OggS', 0, 'ascii');
|
|
35
|
+
header.writeUInt8(0, 4);
|
|
36
|
+
header.writeUInt8(headerType, 5);
|
|
37
|
+
header.writeBigInt64LE(BigInt(granule), 6);
|
|
38
|
+
header.writeUInt32LE(serial >>> 0, 14);
|
|
39
|
+
header.writeUInt32LE(sequence >>> 0, 18);
|
|
40
|
+
header.writeUInt32LE(0, 22);
|
|
41
|
+
header.writeUInt8(segments.length, 26);
|
|
42
|
+
for (let i = 0; i < segments.length; i += 1) header.writeUInt8(segments[i], 27 + i);
|
|
43
|
+
const out = Buffer.concat([header, body]);
|
|
44
|
+
out.writeUInt32LE(crc32(out, 0, out.length), 22);
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const OPUS_HEAD = (() => {
|
|
49
|
+
const b = Buffer.alloc(19);
|
|
50
|
+
b.write('OpusHead', 0, 'ascii');
|
|
51
|
+
b.writeUInt8(1, 8);
|
|
52
|
+
b.writeUInt8(1, 9);
|
|
53
|
+
b.writeUInt16LE(3840, 10);
|
|
54
|
+
b.writeUInt32LE(48000, 12);
|
|
55
|
+
b.writeInt16LE(0, 16);
|
|
56
|
+
b.writeUInt8(0, 18);
|
|
57
|
+
return b;
|
|
58
|
+
})();
|
|
59
|
+
|
|
60
|
+
const OPUS_TAGS = (() => {
|
|
61
|
+
const vendor = Buffer.from('jbc-dssi', 'ascii');
|
|
62
|
+
const b = Buffer.alloc(8 + 4 + vendor.length + 4);
|
|
63
|
+
b.write('OpusTags', 0, 'ascii');
|
|
64
|
+
b.writeUInt32LE(vendor.length, 8);
|
|
65
|
+
vendor.copy(b, 12);
|
|
66
|
+
b.writeUInt32LE(0, 12 + vendor.length);
|
|
67
|
+
return b;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
export function createOggOpusStream({ samplesPerPacket = 960 } = {}) {
|
|
71
|
+
const serial = Math.floor(Math.random() * 0x7fffffff);
|
|
72
|
+
let sequence = 0;
|
|
73
|
+
let granule = 0;
|
|
74
|
+
const pages = [];
|
|
75
|
+
|
|
76
|
+
function header() {
|
|
77
|
+
pages.push(page({ headerType: 0x02, granule: 0, serial, sequence: sequence++, packets: [OPUS_HEAD] }));
|
|
78
|
+
pages.push(page({ headerType: 0x00, granule: 0, serial, sequence: sequence++, packets: [OPUS_TAGS] }));
|
|
79
|
+
return drain();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function packet(data) {
|
|
83
|
+
granule += samplesPerPacket;
|
|
84
|
+
pages.push(page({ headerType: 0x00, granule, serial, sequence: sequence++, packets: [data] }));
|
|
85
|
+
return drain();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function end() {
|
|
89
|
+
const out = [];
|
|
90
|
+
out.push(page({ headerType: 0x04, granule, serial, sequence: sequence++, packets: [Buffer.alloc(0)] }));
|
|
91
|
+
pages.push(...out);
|
|
92
|
+
return drain();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function drain() {
|
|
96
|
+
const out = pages.splice(0, pages.length);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return { header, packet, end };
|
|
101
|
+
}
|
package/lib/tts.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
+
import { createOggOpusStream } from './ogg-opus.js';
|
|
2
3
|
|
|
3
4
|
export const TTS_VOICES = [
|
|
4
5
|
{ id: 'stella', name: '海星', gender: '女声' },
|
|
@@ -6,7 +7,7 @@ export const TTS_VOICES = [
|
|
|
6
7
|
];
|
|
7
8
|
|
|
8
9
|
const TTS_WS = 'wss://api.deepseek.com/v1/tools/tts';
|
|
9
|
-
const MAX_CHARS =
|
|
10
|
+
const MAX_CHARS = 8000;
|
|
10
11
|
const MAX_INFLIGHT = 2;
|
|
11
12
|
const CONNECT_TIMEOUT_MS = 15_000;
|
|
12
13
|
|
|
@@ -14,114 +15,182 @@ function voiceOf(id) {
|
|
|
14
15
|
return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const ERROR_TEXT = {
|
|
19
|
+
content_filter: '这段内容没通过语音审核',
|
|
20
|
+
invalid_input: '这段内容无法合成语音',
|
|
21
|
+
auth: 'DeepSeek Key 无效或没有语音权限',
|
|
22
|
+
quota: 'DeepSeek 余额不足',
|
|
23
|
+
rate_limit: '语音合成太频繁,请稍后再试',
|
|
24
|
+
service_error: '语音服务繁忙,请重试',
|
|
25
|
+
no_key: '电脑上没有 DeepSeek Key',
|
|
26
|
+
busy: '语音合成排队中,请稍后再试',
|
|
27
|
+
network: '语音合成失败,检查电脑网络',
|
|
28
|
+
timeout: '语音合成超时',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function normalizeError(code, message) {
|
|
32
|
+
return { code: code ?? 'service_error', message: ERROR_TEXT[code] ?? message ?? '语音合成失败' };
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket }) {
|
|
30
|
-
const
|
|
36
|
+
const sessions = new Map();
|
|
31
37
|
|
|
32
|
-
async function
|
|
38
|
+
async function open({ id, voice, onPage, onDone, onError }) {
|
|
33
39
|
if (typeof id !== 'string' || id === '') return;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
onError?.({ code: 'invalid_input', message: '没有可朗读的文本' });
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (inflight.size >= MAX_INFLIGHT) {
|
|
40
|
-
onError?.({ code: 'busy', message: '语音合成排队中,请稍后再试' });
|
|
40
|
+
if (sessions.size >= MAX_INFLIGHT) {
|
|
41
|
+
onError?.({ id, error: normalizeError('busy') });
|
|
41
42
|
return;
|
|
42
43
|
}
|
|
43
44
|
const key = await resolveKey();
|
|
44
45
|
if (!key) {
|
|
45
|
-
onError?.({
|
|
46
|
+
onError?.({ id, error: normalizeError('no_key') });
|
|
46
47
|
return;
|
|
47
48
|
}
|
|
48
|
-
|
|
49
49
|
const url = `${TTS_WS}?voice_id=${voiceOf(voice)}&format=opus`;
|
|
50
|
-
let settled = false;
|
|
51
50
|
let ws;
|
|
52
51
|
try {
|
|
53
52
|
ws = new WebSocketImpl(url, { headers: { Authorization: `Bearer ${key}` } });
|
|
54
53
|
} catch (err) {
|
|
55
|
-
onError?.({
|
|
54
|
+
onError?.({ id, error: normalizeError('network', `语音连接失败:${err?.message ?? err}`) });
|
|
56
55
|
return;
|
|
57
56
|
}
|
|
58
|
-
inflight.set(id, ws);
|
|
59
|
-
let seq = 0;
|
|
60
|
-
const timer = setTimeout(() => finish({ code: 'timeout', message: '语音合成超时' }), CONNECT_TIMEOUT_MS);
|
|
61
57
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
const mux = createOggOpusStream();
|
|
59
|
+
const session = {
|
|
60
|
+
ws,
|
|
61
|
+
mux,
|
|
62
|
+
settled: false,
|
|
63
|
+
seq: 0,
|
|
64
|
+
chars: 0,
|
|
65
|
+
started: false,
|
|
66
|
+
pending: [],
|
|
67
|
+
onPage,
|
|
68
|
+
onDone,
|
|
69
|
+
onError,
|
|
70
|
+
};
|
|
71
|
+
sessions.set(id, session);
|
|
72
|
+
|
|
73
|
+
const timer = setTimeout(() => finish(session, id, { code: 'timeout' }), CONNECT_TIMEOUT_MS);
|
|
74
|
+
|
|
75
|
+
function flushPages(bytes) {
|
|
76
|
+
for (const chunk of bytes) {
|
|
77
|
+
if (session.settled) return;
|
|
78
|
+
session.onPage?.({ id, seq: session.seq++, data: chunk.toString('base64'), codec: 'ogg-opus' });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function finish(target, sessionId, error) {
|
|
83
|
+
if (target.settled) return;
|
|
84
|
+
target.settled = true;
|
|
65
85
|
clearTimeout(timer);
|
|
66
|
-
|
|
86
|
+
sessions.delete(sessionId);
|
|
67
87
|
try {
|
|
68
|
-
ws.close();
|
|
88
|
+
target.ws.close();
|
|
69
89
|
} catch {
|
|
70
90
|
/* 已关闭 */
|
|
71
91
|
}
|
|
72
|
-
if (error) onError?.(
|
|
73
|
-
else onDone?.({ id,
|
|
92
|
+
if (error) target.onError?.({ id: sessionId, error: normalizeError(error.code, error.message) });
|
|
93
|
+
else target.onDone?.({ id: sessionId, pages: target.seq });
|
|
74
94
|
}
|
|
75
95
|
|
|
76
96
|
ws.on('open', () => {
|
|
97
|
+
session.started = true;
|
|
98
|
+
flushPages(mux.header());
|
|
99
|
+
for (const text of session.pending) sendText(text);
|
|
100
|
+
session.pending = [];
|
|
101
|
+
if (session.pendingFinish) sendFinish();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
function sendText(text) {
|
|
105
|
+
if (session.settled) return;
|
|
106
|
+
if (!session.started) {
|
|
107
|
+
session.pending.push(text);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (session.chars >= MAX_CHARS) return;
|
|
111
|
+
session.chars += text.length;
|
|
77
112
|
try {
|
|
78
|
-
ws.send(JSON.stringify({ event: 'text', text
|
|
79
|
-
ws.send(JSON.stringify({ event: 'finish_text' }));
|
|
113
|
+
ws.send(JSON.stringify({ event: 'text', text }));
|
|
80
114
|
} catch (err) {
|
|
81
|
-
finish({ code: 'network', message: `语音发送失败:${err?.message ?? err}` });
|
|
115
|
+
finish(session, id, { code: 'network', message: `语音发送失败:${err?.message ?? err}` });
|
|
82
116
|
}
|
|
83
|
-
}
|
|
117
|
+
}
|
|
84
118
|
|
|
85
119
|
ws.on('message', (data, isBinary) => {
|
|
86
120
|
if (isBinary) {
|
|
87
|
-
|
|
121
|
+
flushPages(mux.packet(Buffer.from(data)));
|
|
88
122
|
return;
|
|
89
123
|
}
|
|
90
|
-
let
|
|
124
|
+
let parsed;
|
|
91
125
|
try {
|
|
92
|
-
|
|
126
|
+
parsed = JSON.parse(String(data));
|
|
93
127
|
} catch {
|
|
94
128
|
return;
|
|
95
129
|
}
|
|
96
|
-
if (
|
|
97
|
-
if (
|
|
98
|
-
|
|
130
|
+
if (parsed?.event !== 'finish') return;
|
|
131
|
+
if (parsed.code === 'success') {
|
|
132
|
+
flushPages(mux.end());
|
|
133
|
+
finish(session, id, null);
|
|
134
|
+
} else {
|
|
135
|
+
finish(session, id, { code: parsed.code, message: parsed.message });
|
|
136
|
+
}
|
|
99
137
|
});
|
|
100
138
|
|
|
101
139
|
ws.on('error', (err) => {
|
|
102
140
|
log?.(`[tts] 连接异常: ${err?.message ?? err}`);
|
|
103
|
-
finish({ code: 'network'
|
|
141
|
+
finish(session, id, { code: 'network' });
|
|
104
142
|
});
|
|
105
143
|
|
|
106
144
|
ws.on('close', () => {
|
|
107
|
-
if (!settled) finish({ code: 'network', message: '语音连接中断' });
|
|
145
|
+
if (!session.settled) finish(session, id, { code: 'network', message: '语音连接中断' });
|
|
108
146
|
});
|
|
147
|
+
|
|
148
|
+
function sendFinish() {
|
|
149
|
+
if (session.settled) return;
|
|
150
|
+
if (!session.started) {
|
|
151
|
+
session.pendingFinish = true;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
ws.send(JSON.stringify({ event: 'finish_text' }));
|
|
156
|
+
} catch (err) {
|
|
157
|
+
log?.(`[tts] finish 发送失败: ${err?.message ?? err}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
session.sendText = sendText;
|
|
162
|
+
session.finish = sendFinish;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function text(id, body) {
|
|
166
|
+
const session = sessions.get(id);
|
|
167
|
+
if (!session) return;
|
|
168
|
+
const value = String(body ?? '');
|
|
169
|
+
if (value.trim() === '') return;
|
|
170
|
+
session.sendText(value);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function end(id) {
|
|
174
|
+
const session = sessions.get(id);
|
|
175
|
+
if (!session) return;
|
|
176
|
+
session.finish();
|
|
109
177
|
}
|
|
110
178
|
|
|
111
179
|
function stop(id) {
|
|
112
|
-
const
|
|
113
|
-
if (!
|
|
114
|
-
|
|
180
|
+
const session = sessions.get(id);
|
|
181
|
+
if (!session) return;
|
|
182
|
+
session.settled = true;
|
|
183
|
+
sessions.delete(id);
|
|
115
184
|
try {
|
|
116
|
-
ws.close();
|
|
185
|
+
session.ws.close();
|
|
117
186
|
} catch {
|
|
118
187
|
/* 已关闭 */
|
|
119
188
|
}
|
|
120
189
|
}
|
|
121
190
|
|
|
122
191
|
function stopAll() {
|
|
123
|
-
for (const id of [...
|
|
192
|
+
for (const id of [...sessions.keys()]) stop(id);
|
|
124
193
|
}
|
|
125
194
|
|
|
126
|
-
return {
|
|
195
|
+
return { open, text, end, stop, stopAll, sessions };
|
|
127
196
|
}
|