@hackbuild/ai-views 0.1.2 → 0.1.6
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 +3 -3
- package/dist/chat.d.ts +35 -2
- package/dist/chat.js +47 -3
- package/dist/elements.d.ts +3 -1
- package/dist/elements.js +2 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -1
- package/dist/loaders.js +40 -7
- package/dist/mount-mask.d.ts +56 -6
- package/dist/mount-mask.js +334 -36
- package/dist/mount-talk.d.ts +92 -0
- package/dist/mount-talk.js +502 -0
- package/dist/mount-transcribe.d.ts +22 -0
- package/dist/mount-transcribe.js +90 -4
- package/dist/pick.d.ts +23 -0
- package/dist/pick.js +51 -0
- package/dist/single/elements.iife.js +1784 -627
- package/dist/single/elements.js +1774 -628
- package/dist/single/index.iife.js +1478 -56
- package/dist/single/index.js +1473 -57
- package/dist/single/vue.iife.js +1720 -622
- package/dist/single/vue.js +1714 -623
- package/dist/speaker.d.ts +55 -0
- package/dist/speaker.js +109 -0
- package/dist/talk.d.ts +89 -0
- package/dist/talk.js +121 -0
- package/dist/transcribe.d.ts +36 -1
- package/dist/transcribe.js +19 -3
- package/dist/vue.d.ts +137 -5
- package/dist/vue.js +80 -9
- package/llms.md +376 -14
- package/package.json +7 -7
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state behind VoiceClone: lines spoken one after another through a
|
|
3
|
+
* loaded voice engine, handed to whatever plays them (a character stage
|
|
4
|
+
* moves a mouth on the word times), and the voice clone of an engine that
|
|
5
|
+
* clones (pocket) from an audio file or seconds of mic, kept as a wav.
|
|
6
|
+
* Framework free; the mic and audio decoding need a browser.
|
|
7
|
+
*/
|
|
8
|
+
import { type Engine, type PocketEngine, type VoiceConfig } from "@hackbuild/speak";
|
|
9
|
+
/** True when the engine clones a voice from reference audio. */
|
|
10
|
+
export declare const canClone: (e: unknown) => e is PocketEngine;
|
|
11
|
+
/** One line ready to play: the text, and for a rendering engine the audio with its word and phoneme times. */
|
|
12
|
+
export interface SpokenLine {
|
|
13
|
+
text: string;
|
|
14
|
+
audio?: AudioBuffer;
|
|
15
|
+
words?: readonly {
|
|
16
|
+
t0: number;
|
|
17
|
+
t1: number;
|
|
18
|
+
start?: number;
|
|
19
|
+
end?: number;
|
|
20
|
+
text?: string;
|
|
21
|
+
}[];
|
|
22
|
+
phonemes?: readonly {
|
|
23
|
+
t0: number;
|
|
24
|
+
t1: number;
|
|
25
|
+
phoneme?: string;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
/** Options of `speaker`. */
|
|
29
|
+
export interface SpeakerOptions {
|
|
30
|
+
/** Plays one line; resolves when it is over. A character stage's `say` fits. With none, the line is dropped after `onLine`. */
|
|
31
|
+
play?: (line: SpokenLine) => Promise<void>;
|
|
32
|
+
/** Stops what `play` is playing. */
|
|
33
|
+
halt?: () => void;
|
|
34
|
+
/** Each line as it starts, for a caption. */
|
|
35
|
+
onLine?: (text: string) => void;
|
|
36
|
+
/** The voice config each line uses; a clone sets its own. */
|
|
37
|
+
config?: () => VoiceConfig;
|
|
38
|
+
}
|
|
39
|
+
/** Spoken lines in order, and the clone. */
|
|
40
|
+
export interface Speaker {
|
|
41
|
+
/** Queues a line; resolves when it has been said or dropped. */
|
|
42
|
+
say(text: string): Promise<void>;
|
|
43
|
+
stop(): void;
|
|
44
|
+
/** Clones from an audio file; resolves to seconds of reference used. */
|
|
45
|
+
cloneFile(file: Blob): Promise<number>;
|
|
46
|
+
/** Records `seconds` of mic and clones from it; `onTick` gets the seconds left. */
|
|
47
|
+
cloneMic(seconds: number, onTick?: (left: number) => void): Promise<number>;
|
|
48
|
+
/** The prepared reference as a wav, or null with no clone. */
|
|
49
|
+
cloneWav(): Blob | null;
|
|
50
|
+
clearClone(): void;
|
|
51
|
+
readonly cloned: boolean;
|
|
52
|
+
subscribe(fn: () => void): () => void;
|
|
53
|
+
}
|
|
54
|
+
/** A speaker over the engine `engine()` returns at each line (null: text only, timed by `play`). */
|
|
55
|
+
export declare function speaker(engine: () => Engine | null, o?: SpeakerOptions): Speaker;
|
package/dist/speaker.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state behind VoiceClone: lines spoken one after another through a
|
|
3
|
+
* loaded voice engine, handed to whatever plays them (a character stage
|
|
4
|
+
* moves a mouth on the word times), and the voice clone of an engine that
|
|
5
|
+
* clones (pocket) from an audio file or seconds of mic, kept as a wav.
|
|
6
|
+
* Framework free; the mic and audio decoding need a browser.
|
|
7
|
+
*/
|
|
8
|
+
import { prepareClone, renderSpeech, renderedBuffer, renderedWav, toMono } from "@hackbuild/speak";
|
|
9
|
+
/** True when the engine clones a voice from reference audio. */
|
|
10
|
+
export const canClone = (e) => !!e && typeof e.cloneVoice === "function";
|
|
11
|
+
/** Pocket takes its reference at this rate (vendor). */
|
|
12
|
+
const RATE = 24000;
|
|
13
|
+
/** A speaker over the engine `engine()` returns at each line (null: text only, timed by `play`). */
|
|
14
|
+
export function speaker(engine, o = {}) {
|
|
15
|
+
let ac = null;
|
|
16
|
+
let chain = Promise.resolve();
|
|
17
|
+
let ctl = new AbortController();
|
|
18
|
+
let ref = null;
|
|
19
|
+
const subs = new Set();
|
|
20
|
+
const emit = () => subs.forEach((f) => f());
|
|
21
|
+
const audio = () => (ac ??= new AudioContext());
|
|
22
|
+
const config = () => (ref ? { voice: "custom" } : o.config?.() ?? {});
|
|
23
|
+
const speak = async (text, signal) => {
|
|
24
|
+
if (signal.aborted)
|
|
25
|
+
return;
|
|
26
|
+
o.onLine?.(text);
|
|
27
|
+
const e = engine();
|
|
28
|
+
const play = o.play ?? (async () => undefined);
|
|
29
|
+
if (!e)
|
|
30
|
+
return void (await play({ text }));
|
|
31
|
+
if (e.kind === "native") {
|
|
32
|
+
void e.speak(text, config(), { onBoundary: () => undefined }, signal);
|
|
33
|
+
return void (await play({ text }));
|
|
34
|
+
}
|
|
35
|
+
const r = await renderSpeech(e, text, { voice: config(), signal });
|
|
36
|
+
if (signal.aborted)
|
|
37
|
+
return;
|
|
38
|
+
await play({ text, audio: renderedBuffer(audio(), r), words: r.words, phonemes: r.phonemes });
|
|
39
|
+
};
|
|
40
|
+
const decode = async (blob) => {
|
|
41
|
+
const buf = await audio().decodeAudioData(await blob.arrayBuffer());
|
|
42
|
+
const chans = Array.from({ length: buf.numberOfChannels }, (_, i) => buf.getChannelData(i));
|
|
43
|
+
return { samples: toMono(chans), rate: buf.sampleRate };
|
|
44
|
+
};
|
|
45
|
+
const clone = async (samples, rate) => {
|
|
46
|
+
const e = engine();
|
|
47
|
+
if (!canClone(e))
|
|
48
|
+
throw new Error("this voice cannot clone. load pocket in the voice list first.");
|
|
49
|
+
const next = prepareClone(samples, rate);
|
|
50
|
+
await e.cloneVoice(next, RATE);
|
|
51
|
+
ref = next;
|
|
52
|
+
emit();
|
|
53
|
+
return ref.length / RATE;
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
get cloned() {
|
|
57
|
+
return !!ref;
|
|
58
|
+
},
|
|
59
|
+
subscribe(fn) {
|
|
60
|
+
subs.add(fn);
|
|
61
|
+
return () => subs.delete(fn);
|
|
62
|
+
},
|
|
63
|
+
say(text) {
|
|
64
|
+
const signal = ctl.signal;
|
|
65
|
+
chain = chain.then(() => speak(text, signal)).catch(() => undefined);
|
|
66
|
+
return chain;
|
|
67
|
+
},
|
|
68
|
+
stop() {
|
|
69
|
+
ctl.abort();
|
|
70
|
+
ctl = new AbortController();
|
|
71
|
+
chain = Promise.resolve();
|
|
72
|
+
o.halt?.();
|
|
73
|
+
},
|
|
74
|
+
async cloneFile(file) {
|
|
75
|
+
const { samples, rate } = await decode(file);
|
|
76
|
+
return clone(samples, rate);
|
|
77
|
+
},
|
|
78
|
+
async cloneMic(seconds, onTick) {
|
|
79
|
+
const media = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
80
|
+
try {
|
|
81
|
+
const rec = new MediaRecorder(media);
|
|
82
|
+
const parts = [];
|
|
83
|
+
rec.ondataavailable = (ev) => parts.push(ev.data);
|
|
84
|
+
const done = new Promise((ok) => (rec.onstop = () => ok()));
|
|
85
|
+
rec.start();
|
|
86
|
+
for (let left = seconds; left > 0; left--) {
|
|
87
|
+
onTick?.(left);
|
|
88
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
89
|
+
}
|
|
90
|
+
rec.stop();
|
|
91
|
+
await done;
|
|
92
|
+
const { samples, rate } = await decode(new Blob(parts, { type: rec.mimeType }));
|
|
93
|
+
return await clone(samples, rate);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
media.getTracks().forEach((t) => t.stop());
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
cloneWav() {
|
|
100
|
+
if (!ref)
|
|
101
|
+
return null;
|
|
102
|
+
return renderedWav({ samples: ref, sampleRate: RATE, duration: ref.length / RATE, engine: "clone", words: [], phonemes: [] });
|
|
103
|
+
},
|
|
104
|
+
clearClone() {
|
|
105
|
+
ref = null;
|
|
106
|
+
emit();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/dist/talk.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state behind PersonaRack and VoiceRow: a persona in three parts fitted
|
|
3
|
+
* to a token budget with presets, and spoken replies over a native voice.
|
|
4
|
+
* Framework free.
|
|
5
|
+
*/
|
|
6
|
+
import type { NativeEngine } from "@hackbuild/speak";
|
|
7
|
+
import type { MemoryUsed } from "./chat.js";
|
|
8
|
+
/** A persona in three parts, highest priority first, with its name and first line. */
|
|
9
|
+
export interface Persona {
|
|
10
|
+
name: string;
|
|
11
|
+
/** Who you are and how to answer: always sent. */
|
|
12
|
+
profile: string;
|
|
13
|
+
/** Extra notes: dropped before the profile when the request runs out of room. */
|
|
14
|
+
notes: string;
|
|
15
|
+
/** Extra rules: dropped first. */
|
|
16
|
+
rules: string;
|
|
17
|
+
first?: string;
|
|
18
|
+
}
|
|
19
|
+
/** A named persona a person can load in one press. */
|
|
20
|
+
export interface PersonaPresetX {
|
|
21
|
+
label: string;
|
|
22
|
+
persona: Persona;
|
|
23
|
+
}
|
|
24
|
+
/** The persona state: the current persona, presets, and the system prompt fitted to a budget. */
|
|
25
|
+
export interface PersonaRack {
|
|
26
|
+
snapshot(): {
|
|
27
|
+
persona: Persona;
|
|
28
|
+
presets: readonly PersonaPresetX[];
|
|
29
|
+
on: number;
|
|
30
|
+
builtIn: number;
|
|
31
|
+
};
|
|
32
|
+
subscribe(fn: (s: ReturnType<PersonaRack["snapshot"]>) => void): () => void;
|
|
33
|
+
set(p: Partial<Persona>): void;
|
|
34
|
+
load(i: number): void;
|
|
35
|
+
save(label: string): void;
|
|
36
|
+
removePreset(i: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* The system prompt: profile, memory, notes, rules, in that priority, fitted
|
|
39
|
+
* to `budgetTokens` (about 4 characters a token, inferred); the lowest go
|
|
40
|
+
* first. Returns the text and the parts dropped.
|
|
41
|
+
*/
|
|
42
|
+
system(mem?: readonly MemoryUsed[], budgetTokens?: number): {
|
|
43
|
+
text: string;
|
|
44
|
+
dropped: string[];
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Creates a persona rack with built in presets and any saved ones; `onSave` keeps the saved ones. */
|
|
48
|
+
export declare function personaRack(o?: {
|
|
49
|
+
presets?: readonly PersonaPresetX[];
|
|
50
|
+
saved?: readonly PersonaPresetX[];
|
|
51
|
+
persona?: Persona;
|
|
52
|
+
onSave?: (saved: PersonaPresetX[]) => void;
|
|
53
|
+
}): PersonaRack;
|
|
54
|
+
/** Spoken replies: auto speak, the voice, rate and pitch, speak and stop. */
|
|
55
|
+
export interface VoiceRow {
|
|
56
|
+
snapshot(): {
|
|
57
|
+
auto: boolean;
|
|
58
|
+
voice: string;
|
|
59
|
+
rate: number;
|
|
60
|
+
pitch: number;
|
|
61
|
+
voices: string[];
|
|
62
|
+
speaking: boolean;
|
|
63
|
+
error: string;
|
|
64
|
+
};
|
|
65
|
+
subscribe(fn: (s: ReturnType<VoiceRow["snapshot"]>) => void): () => void;
|
|
66
|
+
set(p: Partial<{
|
|
67
|
+
auto: boolean;
|
|
68
|
+
voice: string;
|
|
69
|
+
rate: number;
|
|
70
|
+
pitch: number;
|
|
71
|
+
}>): void;
|
|
72
|
+
speak(text: string): Promise<void>;
|
|
73
|
+
stop(): void;
|
|
74
|
+
/** Speaks `text` when auto speak is on: hand it to a chat thread's onReply. */
|
|
75
|
+
reply(text: string): void;
|
|
76
|
+
}
|
|
77
|
+
/** Creates the voice state over a native engine (`webSpeechEngine()` of @hackbuild/speak). */
|
|
78
|
+
export declare function voiceRow(engine: NativeEngine, o?: {
|
|
79
|
+
auto?: boolean;
|
|
80
|
+
voice?: string;
|
|
81
|
+
rate?: number;
|
|
82
|
+
pitch?: number;
|
|
83
|
+
onChange?: (s: {
|
|
84
|
+
auto: boolean;
|
|
85
|
+
voice: string;
|
|
86
|
+
rate: number;
|
|
87
|
+
pitch: number;
|
|
88
|
+
}) => void;
|
|
89
|
+
}): VoiceRow;
|
package/dist/talk.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state behind PersonaRack and VoiceRow: a persona in three parts fitted
|
|
3
|
+
* to a token budget with presets, and spoken replies over a native voice.
|
|
4
|
+
* Framework free.
|
|
5
|
+
*/
|
|
6
|
+
import { budget } from "@hackbuild/memory";
|
|
7
|
+
const blank = () => ({ name: "assistant", profile: "", notes: "", rules: "" });
|
|
8
|
+
/** Creates a persona rack with built in presets and any saved ones; `onSave` keeps the saved ones. */
|
|
9
|
+
export function personaRack(o = {}) {
|
|
10
|
+
const builtIn = [...(o.presets ?? [])];
|
|
11
|
+
let saved = [...(o.saved ?? [])];
|
|
12
|
+
let persona = { ...blank(), ...(o.persona ?? builtIn[0]?.persona ?? {}) };
|
|
13
|
+
let on = o.persona ? -1 : builtIn.length ? 0 : -1;
|
|
14
|
+
const subs = new Set();
|
|
15
|
+
const all = () => [...builtIn, ...saved];
|
|
16
|
+
const snap = () => ({ persona, presets: all(), on, builtIn: builtIn.length });
|
|
17
|
+
const emit = () => subs.forEach((f) => f(snap()));
|
|
18
|
+
return {
|
|
19
|
+
snapshot: snap,
|
|
20
|
+
subscribe(fn) {
|
|
21
|
+
subs.add(fn);
|
|
22
|
+
fn(snap());
|
|
23
|
+
return () => subs.delete(fn);
|
|
24
|
+
},
|
|
25
|
+
set(p) {
|
|
26
|
+
persona = { ...persona, ...p };
|
|
27
|
+
on = -1;
|
|
28
|
+
emit();
|
|
29
|
+
},
|
|
30
|
+
load(i) {
|
|
31
|
+
const p = all()[i];
|
|
32
|
+
if (!p)
|
|
33
|
+
return;
|
|
34
|
+
persona = { ...blank(), ...p.persona };
|
|
35
|
+
on = i;
|
|
36
|
+
emit();
|
|
37
|
+
},
|
|
38
|
+
save(label) {
|
|
39
|
+
const name = label.trim() || persona.name || "preset";
|
|
40
|
+
saved = [...saved.filter((s) => s.label !== name), { label: name, persona: { ...persona } }];
|
|
41
|
+
on = builtIn.length + saved.length - 1;
|
|
42
|
+
o.onSave?.(saved);
|
|
43
|
+
emit();
|
|
44
|
+
},
|
|
45
|
+
removePreset(i) {
|
|
46
|
+
const j = i - builtIn.length;
|
|
47
|
+
if (j < 0 || j >= saved.length)
|
|
48
|
+
return;
|
|
49
|
+
saved = saved.filter((_, k) => k !== j);
|
|
50
|
+
if (on === i)
|
|
51
|
+
on = -1;
|
|
52
|
+
o.onSave?.(saved);
|
|
53
|
+
emit();
|
|
54
|
+
},
|
|
55
|
+
system(mem = [], budgetTokens = 3000) {
|
|
56
|
+
const sections = [
|
|
57
|
+
{ name: "profile", priority: 40, text: persona.profile.trim() },
|
|
58
|
+
{ name: "memory", priority: 30, text: mem.length ? `things you remember:\n${mem.map((m) => `- ${m.label}`).join("\n")}` : "" },
|
|
59
|
+
{ name: "notes", priority: 20, text: persona.notes.trim() ? `notes:\n${persona.notes.trim()}` : "" },
|
|
60
|
+
{ name: "rules", priority: 10, text: persona.rules.trim() ? `rules:\n${persona.rules.trim()}` : "" },
|
|
61
|
+
].filter((s) => s.text);
|
|
62
|
+
const a = budget({ budgetTokens, sections });
|
|
63
|
+
// the profile stays even when it alone is over budget
|
|
64
|
+
const text = a.kept.includes("profile") || !persona.profile.trim() ? a.text : [persona.profile.trim(), a.text].filter(Boolean).join("\n\n");
|
|
65
|
+
return { text, dropped: a.dropped };
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Creates the voice state over a native engine (`webSpeechEngine()` of @hackbuild/speak). */
|
|
70
|
+
export function voiceRow(engine, o = {}) {
|
|
71
|
+
let s = { auto: o.auto ?? false, voice: o.voice ?? "", rate: o.rate ?? 1, pitch: o.pitch ?? 1, speaking: false, error: "" };
|
|
72
|
+
let ac = null;
|
|
73
|
+
const subs = new Set();
|
|
74
|
+
const snap = () => ({ ...s, voices: engine.voices?.() ?? [] });
|
|
75
|
+
const emit = () => subs.forEach((f) => f(snap()));
|
|
76
|
+
const g = globalThis;
|
|
77
|
+
g.speechSynthesis?.addEventListener?.("voiceschanged", emit);
|
|
78
|
+
const row = {
|
|
79
|
+
snapshot: snap,
|
|
80
|
+
subscribe(fn) {
|
|
81
|
+
subs.add(fn);
|
|
82
|
+
fn(snap());
|
|
83
|
+
return () => subs.delete(fn);
|
|
84
|
+
},
|
|
85
|
+
set(p) {
|
|
86
|
+
s = { ...s, ...p };
|
|
87
|
+
o.onChange?.({ auto: s.auto, voice: s.voice, rate: s.rate, pitch: s.pitch });
|
|
88
|
+
emit();
|
|
89
|
+
},
|
|
90
|
+
async speak(text) {
|
|
91
|
+
row.stop();
|
|
92
|
+
const mine = (ac = new AbortController());
|
|
93
|
+
s = { ...s, speaking: true, error: "" };
|
|
94
|
+
emit();
|
|
95
|
+
try {
|
|
96
|
+
await engine.load({ signal: mine.signal });
|
|
97
|
+
await engine.speak(text, { ...(s.voice ? { voice: s.voice } : {}), rate: s.rate, pitch: s.pitch }, { onBoundary: () => undefined }, mine.signal);
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
if (!mine.signal.aborted)
|
|
101
|
+
s = { ...s, error: e instanceof Error ? e.message : String(e) };
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
if (ac === mine) {
|
|
105
|
+
ac = null;
|
|
106
|
+
s = { ...s, speaking: false };
|
|
107
|
+
emit();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
stop() {
|
|
112
|
+
ac?.abort();
|
|
113
|
+
engine.stop();
|
|
114
|
+
},
|
|
115
|
+
reply(text) {
|
|
116
|
+
if (s.auto && text.trim())
|
|
117
|
+
void row.speak(text);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
return row;
|
|
121
|
+
}
|
package/dist/transcribe.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* edit (with undo) and export as txt, srt, vtt or json. The engine is a
|
|
5
5
|
* function the page passes, so a stub runs the view with no weights.
|
|
6
6
|
*/
|
|
7
|
-
import { type Dropped, type Seg } from "@hackbuild/transcribe";
|
|
7
|
+
import { type Dropped, type FrontEndOptions, type Manifest, type Seg, type Transcript } from "@hackbuild/transcribe";
|
|
8
8
|
import { type StripFigure, type StripStatus } from "@virgilvox/hackbuild-ui/vanilla";
|
|
9
9
|
/** What a transcriber returns: segments, dropped lines, duration, language and model. */
|
|
10
10
|
export interface RunResult {
|
|
@@ -14,12 +14,32 @@ export interface RunResult {
|
|
|
14
14
|
duration?: number;
|
|
15
15
|
language?: string;
|
|
16
16
|
model?: string;
|
|
17
|
+
/** Levels and speed of the run (computed), for the readout and the zip manifest. */
|
|
18
|
+
stats?: Transcript["stats"];
|
|
19
|
+
/** Loudest level per bin in dBFS and whether the gate kept it, for a level strip. */
|
|
20
|
+
rail?: {
|
|
21
|
+
db: number[];
|
|
22
|
+
kept: number[];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Run settings a transcriber reads: the capture front end (target level, lift, denoise, gate
|
|
27
|
+
* margin, rumble filter, skip silence, re-level), the channel, the language and the phantom filter.
|
|
28
|
+
*/
|
|
29
|
+
export interface TranscribeRunOptions extends FrontEndOptions {
|
|
30
|
+
/** auto picks the loudest side when one dominates, else mixes. */
|
|
31
|
+
channel?: "auto" | "mix" | "L" | "R";
|
|
32
|
+
language?: string;
|
|
33
|
+
/** The phantom text filter. Default on. */
|
|
34
|
+
halluc?: boolean;
|
|
17
35
|
}
|
|
18
36
|
/** What a transcriber gets: an abort signal and a progress callback. */
|
|
19
37
|
export interface RunContext {
|
|
20
38
|
signal: AbortSignal;
|
|
21
39
|
/** 0 to 1. */
|
|
22
40
|
onProgress: (fraction: number) => void;
|
|
41
|
+
/** The queue's run settings at the start of this file. */
|
|
42
|
+
options?: TranscribeRunOptions;
|
|
23
43
|
}
|
|
24
44
|
/** Turns one file into a transcript. A page wires @hackbuild/transcribe here; a stub needs none. */
|
|
25
45
|
export type Transcriber = (file: Blob & {
|
|
@@ -42,6 +62,11 @@ export interface Channel {
|
|
|
42
62
|
language?: string;
|
|
43
63
|
model?: string;
|
|
44
64
|
error?: string;
|
|
65
|
+
stats?: Transcript["stats"];
|
|
66
|
+
rail?: {
|
|
67
|
+
db: number[];
|
|
68
|
+
kept: number[];
|
|
69
|
+
};
|
|
45
70
|
meta: StripFigure[];
|
|
46
71
|
}
|
|
47
72
|
/** A transcript export format: txt, srt, vtt or json. */
|
|
@@ -83,10 +108,20 @@ export interface TranscribeQueue {
|
|
|
83
108
|
text: string;
|
|
84
109
|
type: string;
|
|
85
110
|
} | null;
|
|
111
|
+
/** Every finished channel in four formats, with manifest.json first, as one zip; null with none. */
|
|
112
|
+
zip(o?: {
|
|
113
|
+
tool?: string;
|
|
114
|
+
date?: Date;
|
|
115
|
+
}): {
|
|
116
|
+
name: string;
|
|
117
|
+
data: Uint8Array;
|
|
118
|
+
manifest: Manifest;
|
|
119
|
+
} | null;
|
|
86
120
|
setTranscriber(t: Transcriber): void;
|
|
87
121
|
}
|
|
88
122
|
/** Creates a transcribe queue that runs each file through `run`, one at a time. */
|
|
89
123
|
export declare function transcribeQueue(o: {
|
|
90
124
|
run: Transcriber;
|
|
91
125
|
autostart?: boolean;
|
|
126
|
+
options?: () => TranscribeRunOptions;
|
|
92
127
|
}): TranscribeQueue;
|
package/dist/transcribe.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* edit (with undo) and export as txt, srt, vtt or json. The engine is a
|
|
5
5
|
* function the page passes, so a stub runs the view with no weights.
|
|
6
6
|
*/
|
|
7
|
-
import { asJson, asSrt, asTxt, asVtt, baseName } from "@hackbuild/transcribe";
|
|
7
|
+
import { asJson, asSrt, asTxt, asVtt, baseName, bundle } from "@hackbuild/transcribe";
|
|
8
8
|
import { stripMeta } from "@virgilvox/hackbuild-ui/vanilla";
|
|
9
9
|
const TYPES = { txt: "text/plain", srt: "application/x-subrip", vtt: "text/vtt", json: "application/json" };
|
|
10
10
|
/** Creates a transcribe queue that runs each file through `run`, one at a time. */
|
|
@@ -29,7 +29,10 @@ export function transcribeQueue(o) {
|
|
|
29
29
|
channels = channels.map((c) => (c.id === id ? { ...c, ...p } : c));
|
|
30
30
|
emit();
|
|
31
31
|
};
|
|
32
|
-
const metaOf = (c) =>
|
|
32
|
+
const metaOf = (c) => {
|
|
33
|
+
const s = c.stats;
|
|
34
|
+
return stripMeta({ size: c.size, ...(c.duration ? { duration: c.duration } : {}), ...(s ? { snrDb: s.snrDb, gainDb: s.gainDb, density: s.density, rtf: s.rtf } : {}), ...(c.status === "done" ? { lines: c.segments.length } : {}) });
|
|
35
|
+
};
|
|
33
36
|
const q = {
|
|
34
37
|
snapshot: snap,
|
|
35
38
|
subscribe(fn) {
|
|
@@ -64,7 +67,7 @@ export function transcribeQueue(o) {
|
|
|
64
67
|
ac = new AbortController();
|
|
65
68
|
patch(next.id, { status: "working", pct: 0 });
|
|
66
69
|
try {
|
|
67
|
-
const r = await run(file, { signal: ac.signal, onProgress: (f) => patch(next.id, { pct: Math.round(Math.min(1, Math.max(0, f)) * 100) }) });
|
|
70
|
+
const r = await run(file, { signal: ac.signal, onProgress: (f) => patch(next.id, { pct: Math.round(Math.min(1, Math.max(0, f)) * 100) }), ...(o.options ? { options: o.options() } : {}) });
|
|
68
71
|
const c = channels.find((x) => x.id === next.id);
|
|
69
72
|
if (!c)
|
|
70
73
|
continue;
|
|
@@ -75,6 +78,10 @@ export function transcribeQueue(o) {
|
|
|
75
78
|
done.language = r.language;
|
|
76
79
|
if (r.model)
|
|
77
80
|
done.model = r.model;
|
|
81
|
+
if (r.stats)
|
|
82
|
+
done.stats = r.stats;
|
|
83
|
+
if (r.rail)
|
|
84
|
+
done.rail = r.rail;
|
|
78
85
|
done.meta = metaOf(done);
|
|
79
86
|
patch(next.id, done);
|
|
80
87
|
if (selected === null || channels.find((x) => x.id === selected)?.status !== "done")
|
|
@@ -167,6 +174,15 @@ export function transcribeQueue(o) {
|
|
|
167
174
|
: asJson({ source: c.name, segments: c.segments, dropped: c.dropped, model: c.model ?? "unknown", language: c.language ?? "unknown", ...(c.duration !== undefined ? { duration: c.duration } : {}) });
|
|
168
175
|
return { name: `${baseName(c.name)}.${kind}`, text, type: TYPES[kind] };
|
|
169
176
|
},
|
|
177
|
+
zip(zo = {}) {
|
|
178
|
+
const done = channels.filter((c) => c.segments.length);
|
|
179
|
+
if (!done.length)
|
|
180
|
+
return null;
|
|
181
|
+
const docs = done.map((c) => ({ source: c.name, segments: c.segments, dropped: c.dropped, model: c.model ?? "unknown", language: c.language ?? "unknown", ...(c.stats ? { stats: c.stats } : {}), ...(c.duration !== undefined ? { duration: c.duration } : {}) }));
|
|
182
|
+
const capture = o.options?.() ?? null;
|
|
183
|
+
const out = bundle(docs, { ...(zo.tool ? { tool: zo.tool } : {}), capture: capture, ...(zo.date ? { date: zo.date } : {}) });
|
|
184
|
+
return { name: `transcripts-${(zo.date ?? new Date()).toISOString().slice(0, 10)}.zip`, data: out.zip, manifest: out.manifest };
|
|
185
|
+
},
|
|
170
186
|
setTranscriber(t) {
|
|
171
187
|
run = t;
|
|
172
188
|
},
|