@bojackduy/opencode-voice 0.11.1 → 0.11.3
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 +43 -37
- package/index.js +13 -12
- package/lib/boot.js +4 -4
- package/lib/conversation.js +1 -1
- package/lib/llm-client.js +3 -1
- package/lib/streaming-stt.js +20 -0
- package/lib/stt-log.js +39 -0
- package/lib/stt.js +241 -30
- package/lib/tts-player.js +356 -0
- package/lib/tts-status.js +266 -0
- package/lib/tts.js +661 -170
- package/lib/v2-compat.js +96 -23
- package/lib/v2-preview.js +141 -21
- package/package.json +1 -1
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// Measured media controller for TTS playback.
|
|
2
|
+
//
|
|
3
|
+
// Owns one buffered utterance and one `play` process at a time. Totals are
|
|
4
|
+
// measured from real audio frames (PCM byte counts, WAV data chunk), never
|
|
5
|
+
// estimated from sentence counts. The cursor is wall-clock derived at the
|
|
6
|
+
// current rate, so it is explicitly approximate; only the total is exact.
|
|
7
|
+
//
|
|
8
|
+
// Backend model: stock `play` (sox). True pause/resume uses SIGSTOP/SIGCONT
|
|
9
|
+
// on the owned process -- audio freezes mid-speech, no restart. Speed uses
|
|
10
|
+
// the `tempo` effect (preserves pitch), seeks respawn with `trim` at the
|
|
11
|
+
// new offset. Stale close events are fenced by generation token so a late
|
|
12
|
+
// exit from a previous process can never clear current playback.
|
|
13
|
+
//
|
|
14
|
+
// Wiring note: this controller manages process lifecycle only. Byte feeding
|
|
15
|
+
// (stdin piping of the buffered asset) is injected via `feed`, provided by
|
|
16
|
+
// the tts.js wiring batch. Tests inject a fake clock, fake spawn, and fake
|
|
17
|
+
// signals: no real audio, no timers.
|
|
18
|
+
|
|
19
|
+
export const PLAYER_RATE_MIN = 0.75;
|
|
20
|
+
export const PLAYER_RATE_MAX = 2;
|
|
21
|
+
|
|
22
|
+
function toPositiveNumber(value) {
|
|
23
|
+
const n = Number(value);
|
|
24
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Exact duration of buffered PCM from real frame counts. Accepts
|
|
28
|
+
// `{ bytes }` or a raw Buffer. Returns seconds, or null when unmeasurable.
|
|
29
|
+
export function measurePcmDurationSec(asset, sampleRate, channels = 1, bits = 16) {
|
|
30
|
+
const bytes = asset?.bytes ?? asset;
|
|
31
|
+
if (!Buffer.isBuffer(bytes)) return null;
|
|
32
|
+
const sr = toPositiveNumber(sampleRate);
|
|
33
|
+
const ch = toPositiveNumber(channels);
|
|
34
|
+
const bd = toPositiveNumber(bits);
|
|
35
|
+
if (sr === null || ch === null || bd === null) return null;
|
|
36
|
+
const bytesPerSec = sr * ch * (bd / 8);
|
|
37
|
+
if (!(bytesPerSec > 0)) return null;
|
|
38
|
+
return bytes.length / bytesPerSec;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Exact duration of a complete WAV buffer from its data chunk and the fmt
|
|
42
|
+
// byte rate. Returns seconds, or null when the buffer is not a readable WAV.
|
|
43
|
+
export function measureWavDurationSec(wav) {
|
|
44
|
+
if (!Buffer.isBuffer(wav) || wav.length < 44) return null;
|
|
45
|
+
if (wav.toString("ascii", 0, 4) !== "RIFF") return null;
|
|
46
|
+
if (wav.toString("ascii", 8, 12) !== "WAVE") return null;
|
|
47
|
+
let fmt = null;
|
|
48
|
+
let dataSize = null;
|
|
49
|
+
let offset = 12;
|
|
50
|
+
while (offset + 8 <= wav.length) {
|
|
51
|
+
const id = wav.toString("ascii", offset, offset + 4);
|
|
52
|
+
const size = wav.readUInt32LE(offset + 4);
|
|
53
|
+
const body = offset + 8;
|
|
54
|
+
if (id === "fmt " && size >= 16 && body + 16 <= wav.length) {
|
|
55
|
+
const channels = wav.readUInt16LE(body + 2);
|
|
56
|
+
const sampleRate = wav.readUInt32LE(body + 4);
|
|
57
|
+
const byteRate = wav.readUInt32LE(body + 8);
|
|
58
|
+
const bits = wav.readUInt16LE(body + 14);
|
|
59
|
+
if (channels > 0 && sampleRate > 0 && bits > 0) {
|
|
60
|
+
fmt = { channels, sampleRate, byteRate, bits };
|
|
61
|
+
}
|
|
62
|
+
} else if (id === "data") {
|
|
63
|
+
dataSize = size;
|
|
64
|
+
}
|
|
65
|
+
if (id === "data" && fmt) break;
|
|
66
|
+
offset = body + size;
|
|
67
|
+
}
|
|
68
|
+
if (!fmt || dataSize === null || dataSize <= 0) return null;
|
|
69
|
+
const bytesPerSec =
|
|
70
|
+
fmt.byteRate > 0 ? fmt.byteRate : (fmt.sampleRate * fmt.channels * fmt.bits) / 8;
|
|
71
|
+
if (!(bytesPerSec > 0)) return null;
|
|
72
|
+
return Math.min(dataSize, wav.length) / bytesPerSec;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// sox play argv. Input first, then effects: sox parses leading tokens as
|
|
76
|
+
// input files, so effects placed before the input make it report "Not
|
|
77
|
+
// enough input filenames" and exit instantly with no audio. Neutral
|
|
78
|
+
// playback carries no effects at all. The player always feeds self-
|
|
79
|
+
// describing WAV bytes on stdin (PCM assets are wrapped with a 44-byte
|
|
80
|
+
// header at load), so sox needs no `-t raw` format declaration and the
|
|
81
|
+
// argv here is the complete command.
|
|
82
|
+
export function buildPlayArgs({ offsetSec = 0, rate = 1 } = {}) {
|
|
83
|
+
const argv = ["-q", "-"];
|
|
84
|
+
const offset = Number(offsetSec) || 0;
|
|
85
|
+
if (offset > 0) argv.push("trim", String(offset));
|
|
86
|
+
const speed = Number(rate) || 1;
|
|
87
|
+
if (speed !== 1) argv.push("tempo", String(speed));
|
|
88
|
+
return argv;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Wrap raw PCM frames in a minimal WAV header so every asset handed to
|
|
92
|
+
// `play` is self-describing. Returns a complete WAV Buffer, or null when
|
|
93
|
+
// the parameters cannot describe the frames.
|
|
94
|
+
export function pcmToWav(pcm, { sampleRate, channels = 1, bits = 16 } = {}) {
|
|
95
|
+
if (!Buffer.isBuffer(pcm)) return null;
|
|
96
|
+
const sr = toPositiveNumber(sampleRate);
|
|
97
|
+
const ch = toPositiveNumber(channels);
|
|
98
|
+
const bd = toPositiveNumber(bits);
|
|
99
|
+
if (sr === null || ch === null || bd === null) return null;
|
|
100
|
+
if (![8, 16, 24, 32].includes(bd)) return null;
|
|
101
|
+
const bytesPerSec = (sr * ch * bd) / 8;
|
|
102
|
+
const header = Buffer.alloc(44);
|
|
103
|
+
header.write("RIFF", 0);
|
|
104
|
+
header.writeUInt32LE(36 + pcm.length, 4);
|
|
105
|
+
header.write("WAVE", 8);
|
|
106
|
+
header.write("fmt ", 12);
|
|
107
|
+
header.writeUInt32LE(16, 16);
|
|
108
|
+
header.writeUInt16LE(1, 20); // PCM
|
|
109
|
+
header.writeUInt16LE(ch, 22);
|
|
110
|
+
header.writeUInt32LE(sr, 24);
|
|
111
|
+
header.writeUInt32LE(bytesPerSec, 28);
|
|
112
|
+
header.writeUInt16LE((ch * bd) / 8, 32);
|
|
113
|
+
header.writeUInt16LE(bd, 34);
|
|
114
|
+
header.write("data", 36);
|
|
115
|
+
header.writeUInt32LE(pcm.length, 40);
|
|
116
|
+
return Buffer.concat([header, pcm]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function clamp(value, min, max) {
|
|
120
|
+
return Math.min(max, Math.max(min, value));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function roundMs(value) {
|
|
124
|
+
return Math.round(Number(value) * 1000) / 1000;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function createTtsPlayer({ spawn, signal, now = () => Date.now(), feed = () => {} } = {}) {
|
|
128
|
+
if (typeof spawn !== "function") throw new Error("createTtsPlayer requires a spawn adapter");
|
|
129
|
+
if (typeof signal !== "function") throw new Error("createTtsPlayer requires a signal adapter");
|
|
130
|
+
|
|
131
|
+
let asset = null; // { kind, totalSec, wav }
|
|
132
|
+
let status = "idle"; // idle | playing | paused
|
|
133
|
+
let rate = 1;
|
|
134
|
+
let cursorBaseSec = 0;
|
|
135
|
+
let anchorMs = 0;
|
|
136
|
+
let proc = null; // owned live process handle
|
|
137
|
+
let generation = 0; // fences stale close events
|
|
138
|
+
let playResolve = null;
|
|
139
|
+
let disposed = false;
|
|
140
|
+
let needsRespawn = false;
|
|
141
|
+
|
|
142
|
+
function currentCursorSec() {
|
|
143
|
+
if (!asset) return 0;
|
|
144
|
+
if (status !== "playing") return roundMs(clamp(cursorBaseSec, 0, asset.totalSec));
|
|
145
|
+
const elapsedSec = (now() - anchorMs) / 1000;
|
|
146
|
+
return roundMs(clamp(cursorBaseSec + elapsedSec * rate, 0, asset.totalSec));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function settleCurrent(result) {
|
|
150
|
+
const resolve = playResolve;
|
|
151
|
+
playResolve = null;
|
|
152
|
+
if (typeof resolve !== "function") return;
|
|
153
|
+
try {
|
|
154
|
+
resolve(result);
|
|
155
|
+
} catch {}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function killOwned(sig) {
|
|
159
|
+
const owned = proc;
|
|
160
|
+
proc = null;
|
|
161
|
+
if (owned) {
|
|
162
|
+
try {
|
|
163
|
+
signal(owned.pid, sig);
|
|
164
|
+
} catch {}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function spawnAt(offsetSec) {
|
|
169
|
+
generation += 1;
|
|
170
|
+
const token = generation;
|
|
171
|
+
const argv = buildPlayArgs({ offsetSec, rate });
|
|
172
|
+
const handle = spawn(argv);
|
|
173
|
+
proc = handle;
|
|
174
|
+
anchorMs = now();
|
|
175
|
+
cursorBaseSec = offsetSec;
|
|
176
|
+
needsRespawn = false;
|
|
177
|
+
try {
|
|
178
|
+
feed(handle, { wav: asset.wav, offsetSec, rate });
|
|
179
|
+
} catch {}
|
|
180
|
+
const onClose = handle.onClose;
|
|
181
|
+
if (typeof onClose === "function") {
|
|
182
|
+
try {
|
|
183
|
+
onClose((code) => {
|
|
184
|
+
if (token !== generation) return; // stale process: never clears current playback
|
|
185
|
+
proc = null;
|
|
186
|
+
status = "idle";
|
|
187
|
+
cursorBaseSec = asset ? asset.totalSec : 0;
|
|
188
|
+
settleCurrent({ ended: true, code });
|
|
189
|
+
});
|
|
190
|
+
} catch {}
|
|
191
|
+
}
|
|
192
|
+
return handle;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function startPlayback() {
|
|
196
|
+
if (!asset || disposed) return false;
|
|
197
|
+
let offset = clamp(currentCursorSec(), 0, asset.totalSec);
|
|
198
|
+
if (offset >= asset.totalSec) offset = 0;
|
|
199
|
+
killOwned("SIGKILL");
|
|
200
|
+
status = "playing";
|
|
201
|
+
spawnAt(offset);
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
load(input = {}) {
|
|
207
|
+
if (disposed) return false;
|
|
208
|
+
if (input.wav) {
|
|
209
|
+
const totalSec = measureWavDurationSec(input.wav);
|
|
210
|
+
if (totalSec === null || !(totalSec > 0)) return false;
|
|
211
|
+
asset = { kind: "wav", totalSec, wav: input.wav };
|
|
212
|
+
} else if (input.pcm) {
|
|
213
|
+
const sampleRate = input.sampleRate;
|
|
214
|
+
const channels = input.channels ?? 1;
|
|
215
|
+
const bits = input.bits ?? 16;
|
|
216
|
+
const totalSec = measurePcmDurationSec({ bytes: input.pcm }, sampleRate, channels, bits);
|
|
217
|
+
if (totalSec === null || !(totalSec > 0)) return false;
|
|
218
|
+
const wav = pcmToWav(input.pcm, { sampleRate, channels, bits });
|
|
219
|
+
if (!wav) return false;
|
|
220
|
+
asset = { kind: "pcm", totalSec, wav };
|
|
221
|
+
} else {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
status = "idle";
|
|
225
|
+
rate = clamp(Number(input.rate) || 1, PLAYER_RATE_MIN, PLAYER_RATE_MAX);
|
|
226
|
+
cursorBaseSec = 0;
|
|
227
|
+
anchorMs = now();
|
|
228
|
+
needsRespawn = false;
|
|
229
|
+
return true;
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
play() {
|
|
233
|
+
if (disposed || !asset) return false;
|
|
234
|
+
settleCurrent({ stopped: true, superseded: true });
|
|
235
|
+
const started = new Promise((resolve) => {
|
|
236
|
+
playResolve = resolve;
|
|
237
|
+
});
|
|
238
|
+
status = "playing";
|
|
239
|
+
startPlayback();
|
|
240
|
+
return started;
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
pause() {
|
|
244
|
+
if (disposed || status !== "playing" || !proc) return false;
|
|
245
|
+
cursorBaseSec = currentCursorSec();
|
|
246
|
+
status = "paused";
|
|
247
|
+
try {
|
|
248
|
+
signal(proc.pid, "SIGSTOP");
|
|
249
|
+
} catch {}
|
|
250
|
+
return true;
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
resume() {
|
|
254
|
+
if (disposed || status !== "paused") return false;
|
|
255
|
+
if (!asset) return false;
|
|
256
|
+
if (needsRespawn || !proc) {
|
|
257
|
+
status = "playing";
|
|
258
|
+
spawnAt(clamp(cursorBaseSec, 0, asset.totalSec));
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
status = "playing";
|
|
262
|
+
anchorMs = now();
|
|
263
|
+
try {
|
|
264
|
+
signal(proc.pid, "SIGCONT");
|
|
265
|
+
} catch {}
|
|
266
|
+
return true;
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
stop() {
|
|
270
|
+
if (disposed) return false;
|
|
271
|
+
generation += 1;
|
|
272
|
+
killOwned("SIGKILL");
|
|
273
|
+
if (status !== "idle") {
|
|
274
|
+
cursorBaseSec = currentCursorSec();
|
|
275
|
+
status = "idle";
|
|
276
|
+
}
|
|
277
|
+
needsRespawn = false;
|
|
278
|
+
settleCurrent({ stopped: true });
|
|
279
|
+
return true;
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
seek(sec) {
|
|
283
|
+
if (disposed || !asset) return 0;
|
|
284
|
+
const clamped = roundMs(clamp(Number(sec) || 0, 0, asset.totalSec));
|
|
285
|
+
cursorBaseSec = clamped;
|
|
286
|
+
anchorMs = now();
|
|
287
|
+
if (status === "playing") {
|
|
288
|
+
killOwned("SIGKILL");
|
|
289
|
+
spawnAt(clamped);
|
|
290
|
+
} else if (status === "paused") {
|
|
291
|
+
needsRespawn = true;
|
|
292
|
+
}
|
|
293
|
+
return clamped;
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
rewind(sec) {
|
|
297
|
+
if (disposed || !asset) return 0;
|
|
298
|
+
return this.seek(currentCursorSec() - (Number(sec) || 0));
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
forward(sec) {
|
|
302
|
+
if (disposed || !asset) return 0;
|
|
303
|
+
return this.seek(currentCursorSec() + (Number(sec) || 0));
|
|
304
|
+
},
|
|
305
|
+
|
|
306
|
+
replay() {
|
|
307
|
+
if (disposed || !asset) return false;
|
|
308
|
+
this.seek(0);
|
|
309
|
+
if (status === "paused") return this.resume();
|
|
310
|
+
if (status === "idle") return this.play();
|
|
311
|
+
return true;
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
setRate(next) {
|
|
315
|
+
const clamped = clamp(Number(next) || 1, PLAYER_RATE_MIN, PLAYER_RATE_MAX);
|
|
316
|
+
const changed = clamped !== rate;
|
|
317
|
+
if (changed && status === "playing") {
|
|
318
|
+
// Capture the cursor under the OLD rate first: recomputing after
|
|
319
|
+
// assigning the new rate would rescale already-elapsed audio time.
|
|
320
|
+
cursorBaseSec = currentCursorSec();
|
|
321
|
+
anchorMs = now();
|
|
322
|
+
rate = clamped;
|
|
323
|
+
killOwned("SIGKILL");
|
|
324
|
+
spawnAt(cursorBaseSec);
|
|
325
|
+
} else {
|
|
326
|
+
rate = clamped;
|
|
327
|
+
if (changed && status === "paused") {
|
|
328
|
+
cursorBaseSec = currentCursorSec();
|
|
329
|
+
needsRespawn = true;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return clamped;
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
dispose() {
|
|
336
|
+
if (disposed) return;
|
|
337
|
+
disposed = true;
|
|
338
|
+
generation += 1;
|
|
339
|
+
killOwned("SIGKILL");
|
|
340
|
+
status = "idle";
|
|
341
|
+
proc = null;
|
|
342
|
+
settleCurrent({ stopped: true, disposed: true });
|
|
343
|
+
},
|
|
344
|
+
|
|
345
|
+
getState() {
|
|
346
|
+
return {
|
|
347
|
+
status,
|
|
348
|
+
cursorSec: currentCursorSec(),
|
|
349
|
+
totalSec: asset ? roundMs(asset.totalSec) : 0,
|
|
350
|
+
rate,
|
|
351
|
+
exactTotal: asset !== null,
|
|
352
|
+
cursorApproximate: true,
|
|
353
|
+
};
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// Persistent TTS "now playing" line for v2 (mirrors lib/v2-preview.js).
|
|
2
|
+
//
|
|
3
|
+
// Why a slot and not a toast: v2 Toast.show is fire-and-forget text with no
|
|
4
|
+
// update or buttons, so a polling progress toast would stack like the old STT
|
|
5
|
+
// storm. This is one bounded line above the composer showing state, exact
|
|
6
|
+
// position/total, a bar, and speed. It renders ONLY while audio is live:
|
|
7
|
+
// readState returning null renders nothing, so idle TTS leaves no trace.
|
|
8
|
+
//
|
|
9
|
+
// Totals are measured from real audio frames by lib/tts-player.js (exact);
|
|
10
|
+
// the cursor is wall-clock derived (approximate) and marked as such only in
|
|
11
|
+
// tests - the line shows position plainly, like any media player.
|
|
12
|
+
|
|
13
|
+
import { sttLog } from "./stt-log.js";
|
|
14
|
+
|
|
15
|
+
const BAR_WIDTH = 20;
|
|
16
|
+
|
|
17
|
+
const SEG_KEYS = ["toggle", "main", "rw", "ff", "rate"];
|
|
18
|
+
const SEG_ACTION = { toggle: "toggle", main: null, rw: "rewind", ff: "forward", rate: "rate" };
|
|
19
|
+
const BAR_FULL = "█";
|
|
20
|
+
const BAR_EMPTY = "░";
|
|
21
|
+
|
|
22
|
+
export function fmtClock(totalSec) {
|
|
23
|
+
const s = Math.max(0, Math.floor(Number(totalSec) || 0));
|
|
24
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function progressBar(cursorSec, totalSec, width = BAR_WIDTH) {
|
|
28
|
+
const total = Number(totalSec) || 0;
|
|
29
|
+
const cursor = Math.max(0, Math.min(Number(cursorSec) || 0, total));
|
|
30
|
+
const ratio = total > 0 ? cursor / total : 0;
|
|
31
|
+
const full = Math.round(ratio * width);
|
|
32
|
+
return BAR_FULL.repeat(full) + BAR_EMPTY.repeat(Math.max(0, width - full));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const RATE_STEPS = [0.75, 1, 1.25, 1.5, 1.75, 2];
|
|
36
|
+
|
|
37
|
+
export function nextRate(current) {
|
|
38
|
+
const cur = Number(current);
|
|
39
|
+
for (const step of RATE_STEPS) {
|
|
40
|
+
if (step > cur + 1e-9) return step;
|
|
41
|
+
}
|
|
42
|
+
return RATE_STEPS[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function formatTtsStatus(state) {
|
|
46
|
+
if (!state) return "";
|
|
47
|
+
return statusSegments(state)
|
|
48
|
+
.map((seg) => seg.text)
|
|
49
|
+
.join(" ");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// One segment per clickable or static chunk. Static chunks carry no action;
|
|
53
|
+
// buttons do. The renderer maps each segment to its own text node so mouse
|
|
54
|
+
// hits land on exactly one action.
|
|
55
|
+
export function statusSegments(state) {
|
|
56
|
+
if (!state) return [];
|
|
57
|
+
if (state.status === "loading") {
|
|
58
|
+
// No buttons while nothing is playable yet; the animated dots prove the
|
|
59
|
+
// plugin is working through a slow model load or long synthesis.
|
|
60
|
+
const dots = ".".repeat(1 + (Math.floor(Date.now() / 500) % 3));
|
|
61
|
+
return [{ key: "main", text: `… Loading voice${dots}`, action: null }];
|
|
62
|
+
}
|
|
63
|
+
const playing = state.status !== "paused";
|
|
64
|
+
const rate = Number(state.rate) || 1;
|
|
65
|
+
return [
|
|
66
|
+
{ key: "toggle", text: playing ? "[⏸]" : "[▶]", action: "toggle" },
|
|
67
|
+
{
|
|
68
|
+
key: "main",
|
|
69
|
+
text: `${fmtClock(state.cursorSec)} / ${fmtClock(state.totalSec)} ${progressBar(state.cursorSec, state.totalSec)}`,
|
|
70
|
+
action: null,
|
|
71
|
+
},
|
|
72
|
+
{ key: "rw", text: "[-5s]", action: "rewind" },
|
|
73
|
+
{ key: "ff", text: "[+5s]", action: "forward" },
|
|
74
|
+
{ key: "rate", text: `[${rate}x]`, action: "rate" },
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function createTtsStatus(
|
|
79
|
+
ctx,
|
|
80
|
+
readState,
|
|
81
|
+
actions = {},
|
|
82
|
+
load = async () => {
|
|
83
|
+
const [solid, tui] = await Promise.all([import("solid-js"), import("@opentui/solid")]);
|
|
84
|
+
return { ...solid, ...tui };
|
|
85
|
+
},
|
|
86
|
+
) {
|
|
87
|
+
let generation = 0;
|
|
88
|
+
let active = false;
|
|
89
|
+
let lastTexts = new Map();
|
|
90
|
+
let renderErrorShown = false;
|
|
91
|
+
let renderCalls = 0;
|
|
92
|
+
let timer = null;
|
|
93
|
+
// One entry per mounted text node. Several slot claims may mount at once
|
|
94
|
+
// (composer.top + home.footer), so entries are keyed per mount, not per
|
|
95
|
+
// segment: redraw fans each segment's text out to every live node with
|
|
96
|
+
// that key. Nodes are only ever created inside render(), where the host
|
|
97
|
+
// provides a renderer context.
|
|
98
|
+
const nodes = new Set();
|
|
99
|
+
let setContent = null;
|
|
100
|
+
|
|
101
|
+
function segments() {
|
|
102
|
+
return statusSegments(readState?.() || { status: "idle", cursorSec: 0, totalSec: 0, rate: 1 });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function redraw() {
|
|
106
|
+
const segMap = new Map(segments().map((seg) => [seg.key, seg.text]));
|
|
107
|
+
if (!redraw.shapeLogged && nodes.size > 0) {
|
|
108
|
+
redraw.shapeLogged = true;
|
|
109
|
+
try {
|
|
110
|
+
sttLog("tts.status", `render segs=${[...segMap.keys()].join(",")} nodes=${nodes.size}`);
|
|
111
|
+
} catch {}
|
|
112
|
+
}
|
|
113
|
+
// Prune dead mounts first so a later text change recreates nothing here
|
|
114
|
+
// (creation happens only inside render, under a renderer context).
|
|
115
|
+
for (const entry of nodes) {
|
|
116
|
+
if (entry.node.isDestroyed) nodes.delete(entry);
|
|
117
|
+
}
|
|
118
|
+
for (const key of SEG_KEYS) {
|
|
119
|
+
const text = segMap.has(key) ? segMap.get(key) : "";
|
|
120
|
+
if (lastTexts.get(key) === text) continue;
|
|
121
|
+
lastTexts.set(key, text);
|
|
122
|
+
for (const entry of nodes) {
|
|
123
|
+
if (entry.key !== key || entry.node.isDestroyed) continue;
|
|
124
|
+
setContent?.(entry.node, text);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function loudRenderError(err) {
|
|
130
|
+
if (renderErrorShown) return;
|
|
131
|
+
renderErrorShown = true;
|
|
132
|
+
try {
|
|
133
|
+
ctx.ui?.toast?.show?.({
|
|
134
|
+
message: `Voice media display failed: ${err?.message || err}`,
|
|
135
|
+
variant: "error",
|
|
136
|
+
duration: 5000,
|
|
137
|
+
});
|
|
138
|
+
} catch {}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function stopTimer() {
|
|
142
|
+
if (timer) {
|
|
143
|
+
clearInterval(timer);
|
|
144
|
+
timer = null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let unclaims = [];
|
|
149
|
+
function clear() {
|
|
150
|
+
generation += 1;
|
|
151
|
+
active = false;
|
|
152
|
+
lastTexts = new Map();
|
|
153
|
+
renderCalls = 0;
|
|
154
|
+
redraw.shapeLogged = false;
|
|
155
|
+
stopTimer();
|
|
156
|
+
nodes.clear();
|
|
157
|
+
setContent = null;
|
|
158
|
+
for (const unclaim of unclaims.splice(0)) unclaim();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function press(action) {
|
|
162
|
+
if (!active) return;
|
|
163
|
+
try {
|
|
164
|
+
actions?.[action]?.();
|
|
165
|
+
} catch {}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
async begin() {
|
|
170
|
+
clear();
|
|
171
|
+
active = true;
|
|
172
|
+
renderErrorShown = false;
|
|
173
|
+
const mine = generation;
|
|
174
|
+
let createElement;
|
|
175
|
+
let setProp;
|
|
176
|
+
let insertNode;
|
|
177
|
+
let onCleanup;
|
|
178
|
+
try {
|
|
179
|
+
({ createElement, setProp, insertNode, onCleanup } = await load());
|
|
180
|
+
} catch (loadErr) {
|
|
181
|
+
loudRenderError(loadErr);
|
|
182
|
+
throw loadErr;
|
|
183
|
+
}
|
|
184
|
+
if (!active || mine !== generation) return false;
|
|
185
|
+
setContent = (node, value) => setProp(node, "content", value);
|
|
186
|
+
const themeText = ctx.theme?.text;
|
|
187
|
+
const fg =
|
|
188
|
+
typeof themeText === "string"
|
|
189
|
+
? themeText
|
|
190
|
+
: themeText?.base || themeText?.muted || "#eeeeee";
|
|
191
|
+
const render = () => {
|
|
192
|
+
if (!active || mine !== generation) return null;
|
|
193
|
+
renderCalls += 1;
|
|
194
|
+
try {
|
|
195
|
+
const box = createElement("box");
|
|
196
|
+
setProp(box, "height", 1);
|
|
197
|
+
setProp(box, "flexShrink", 0);
|
|
198
|
+
setProp(box, "flexDirection", "row");
|
|
199
|
+
setProp(box, "width", "100%");
|
|
200
|
+
// Create every segment node up front, even ones with no text yet
|
|
201
|
+
// (loading shows only `main`): nodes created later would run
|
|
202
|
+
// outside the host's renderer context and fail. Missing text
|
|
203
|
+
// renders as an empty, zero-width span.
|
|
204
|
+
const segMap = new Map(segments().map((seg) => [seg.key, seg.text]));
|
|
205
|
+
const mountEntries = [];
|
|
206
|
+
for (const key of SEG_KEYS) {
|
|
207
|
+
const node = createElement("text");
|
|
208
|
+
setProp(node, "maxHeight", 1);
|
|
209
|
+
setProp(node, "minHeight", 1);
|
|
210
|
+
setProp(node, "fg", fg);
|
|
211
|
+
const text = segMap.has(key) ? segMap.get(key) : "";
|
|
212
|
+
setProp(node, "content", text);
|
|
213
|
+
lastTexts.set(key, text);
|
|
214
|
+
const action = SEG_ACTION[key];
|
|
215
|
+
if (action) {
|
|
216
|
+
try {
|
|
217
|
+
setProp(node, "onMouseUp", () => press(action));
|
|
218
|
+
} catch {}
|
|
219
|
+
}
|
|
220
|
+
const entry = { key, node };
|
|
221
|
+
nodes.add(entry);
|
|
222
|
+
mountEntries.push(entry);
|
|
223
|
+
insertNode(box, node);
|
|
224
|
+
}
|
|
225
|
+
onCleanup?.(() => {
|
|
226
|
+
for (const entry of mountEntries) nodes.delete(entry);
|
|
227
|
+
});
|
|
228
|
+
return box;
|
|
229
|
+
} catch (renderErr) {
|
|
230
|
+
loudRenderError(renderErr);
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
// Same proven vertical slots as the voice preview: prompt.footer is a
|
|
235
|
+
// horizontal row and is absent on the home screen.
|
|
236
|
+
unclaims = [
|
|
237
|
+
ctx.ui.slot({ before: "session.composer.top", render }),
|
|
238
|
+
ctx.ui.slot({ before: "home.footer", render }),
|
|
239
|
+
];
|
|
240
|
+
// Cursor moves on wall-clock time; re-read state twice a second so the
|
|
241
|
+
// line advances without toast spam. Redraw is a no-op when unchanged.
|
|
242
|
+
stopTimer();
|
|
243
|
+
timer = setInterval(() => {
|
|
244
|
+
if (!active) return;
|
|
245
|
+
try {
|
|
246
|
+
redraw();
|
|
247
|
+
} catch {}
|
|
248
|
+
}, 500);
|
|
249
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
250
|
+
return true;
|
|
251
|
+
},
|
|
252
|
+
update() {
|
|
253
|
+
if (!active) return;
|
|
254
|
+
redraw();
|
|
255
|
+
},
|
|
256
|
+
renderCalls: () => renderCalls,
|
|
257
|
+
nodes: () => {
|
|
258
|
+
const keys = new Set();
|
|
259
|
+
for (const entry of nodes) {
|
|
260
|
+
if (!entry.node.isDestroyed) keys.add(entry.key);
|
|
261
|
+
}
|
|
262
|
+
return keys.size;
|
|
263
|
+
},
|
|
264
|
+
clear,
|
|
265
|
+
};
|
|
266
|
+
}
|