@lubomirklimes/character-runtime 0.0.0-stage → 0.1.0-preview.1
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/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/acs/acs15.d.ts +9 -0
- package/dist/acs/acs15.js +382 -0
- package/dist/acs/acs2-reader.d.ts +2 -0
- package/dist/acs/acs2-reader.js +277 -0
- package/dist/acs/binary.d.ts +1 -0
- package/dist/acs/binary.js +107 -0
- package/dist/acs/compound-file.d.ts +1 -0
- package/dist/acs/compound-file.js +326 -0
- package/dist/acs/document.d.ts +33 -0
- package/dist/acs/document.js +99 -0
- package/dist/acs/errors.d.ts +28 -0
- package/dist/acs/errors.js +41 -0
- package/dist/acs/image.d.ts +30 -0
- package/dist/acs/image.js +113 -0
- package/dist/acs/index.d.ts +11 -0
- package/dist/acs/index.js +10 -0
- package/dist/acs/lz.d.ts +7 -0
- package/dist/acs/lz.js +102 -0
- package/dist/acs/metadata.d.ts +14 -0
- package/dist/acs/metadata.js +126 -0
- package/dist/acs/model.d.ts +130 -0
- package/dist/acs/model.js +12 -0
- package/dist/acs/mouth.d.ts +37 -0
- package/dist/acs/mouth.js +43 -0
- package/dist/acs/wave.d.ts +53 -0
- package/dist/acs/wave.js +422 -0
- package/dist/browser/animation-frame-clock.d.ts +18 -0
- package/dist/browser/animation-frame-clock.js +42 -0
- package/dist/browser/canvas-renderer.d.ts +50 -0
- package/dist/browser/canvas-renderer.js +145 -0
- package/dist/browser/host.d.ts +84 -0
- package/dist/browser/host.js +206 -0
- package/dist/browser/index.d.ts +6 -0
- package/dist/browser/index.js +8 -0
- package/dist/browser/web-audio.d.ts +48 -0
- package/dist/browser/web-audio.js +205 -0
- package/dist/browser/web-speech.d.ts +131 -0
- package/dist/browser/web-speech.js +302 -0
- package/dist/core/activities.d.ts +10 -0
- package/dist/core/activities.js +479 -0
- package/dist/core/agent.d.ts +263 -0
- package/dist/core/agent.js +764 -0
- package/dist/core/audio.d.ts +17 -0
- package/dist/core/audio.js +174 -0
- package/dist/core/balloon.d.ts +43 -0
- package/dist/core/balloon.js +94 -0
- package/dist/core/clock.d.ts +32 -0
- package/dist/core/clock.js +51 -0
- package/dist/core/compositor.d.ts +22 -0
- package/dist/core/compositor.js +67 -0
- package/dist/core/events.d.ts +131 -0
- package/dist/core/events.js +1 -0
- package/dist/core/host.d.ts +29 -0
- package/dist/core/host.js +77 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +13 -0
- package/dist/core/lipsync.d.ts +41 -0
- package/dist/core/lipsync.js +209 -0
- package/dist/core/random.d.ts +52 -0
- package/dist/core/random.js +105 -0
- package/dist/core/request.d.ts +68 -0
- package/dist/core/request.js +131 -0
- package/dist/core/resources.d.ts +70 -0
- package/dist/core/resources.js +243 -0
- package/dist/core/scheduler.d.ts +25 -0
- package/dist/core/scheduler.js +766 -0
- package/dist/core/source.d.ts +21 -0
- package/dist/core/source.js +54 -0
- package/dist/core/speech.d.ts +91 -0
- package/dist/core/speech.js +86 -0
- package/dist/core/text.d.ts +38 -0
- package/dist/core/text.js +97 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/package.json +68 -4
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type CharacterDocument } from '../acs/document.js';
|
|
2
|
+
import type { Character } from '../acs/model.js';
|
|
3
|
+
/** Inputs a character can be loaded from. A string is a URL (fetched with `fetch`). */
|
|
4
|
+
export type CharacterInput = string | URL | Blob | ArrayBuffer | ArrayBufferView;
|
|
5
|
+
/**
|
|
6
|
+
* An opened, immutable character file (ACS 2.x or ACS 1.5). Several agents may play the same source: its bytes and parsed model exist
|
|
7
|
+
* once. Images and sounds are decoded on demand by the resources of each agent.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CharacterSource {
|
|
10
|
+
readonly document: CharacterDocument;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
private constructor();
|
|
13
|
+
/** The read-only character model. */
|
|
14
|
+
get character(): Character;
|
|
15
|
+
/** Opens a character from bytes (the array is kept, not copied). Throws {@link AcsFormatError} for an unreadable file. */
|
|
16
|
+
static fromBytes(bytes: ArrayBuffer | ArrayBufferView, name?: string): CharacterSource;
|
|
17
|
+
/** Loads a character from a URL (with `fetch`), a `Blob`/`File`, an `ArrayBuffer` or a typed array. */
|
|
18
|
+
static load(input: CharacterInput, options?: {
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
}): Promise<CharacterSource>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { openCharacterDocument } from '../acs/document.js';
|
|
2
|
+
import { InvalidArgumentError } from '../acs/errors.js';
|
|
3
|
+
import { MAX_FILE_BYTES } from '../acs/acs2-reader.js';
|
|
4
|
+
import { AcsFormatError } from '../acs/errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* An opened, immutable character file (ACS 2.x or ACS 1.5). Several agents may play the same source: its bytes and parsed model exist
|
|
7
|
+
* once. Images and sounds are decoded on demand by the resources of each agent.
|
|
8
|
+
*/
|
|
9
|
+
export class CharacterSource {
|
|
10
|
+
document;
|
|
11
|
+
name;
|
|
12
|
+
constructor(document, name) {
|
|
13
|
+
this.document = document;
|
|
14
|
+
this.name = name;
|
|
15
|
+
}
|
|
16
|
+
/** The read-only character model. */
|
|
17
|
+
get character() {
|
|
18
|
+
return this.document.character;
|
|
19
|
+
}
|
|
20
|
+
/** Opens a character from bytes (the array is kept, not copied). Throws {@link AcsFormatError} for an unreadable file. */
|
|
21
|
+
static fromBytes(bytes, name = 'character') {
|
|
22
|
+
const view = bytes instanceof Uint8Array ? bytes
|
|
23
|
+
: ArrayBuffer.isView(bytes) ? new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
24
|
+
: bytes instanceof ArrayBuffer ? new Uint8Array(bytes)
|
|
25
|
+
: null;
|
|
26
|
+
if (!view)
|
|
27
|
+
throw new InvalidArgumentError('Expected an ArrayBuffer or a typed array.');
|
|
28
|
+
return new CharacterSource(openCharacterDocument(view), name);
|
|
29
|
+
}
|
|
30
|
+
/** Loads a character from a URL (with `fetch`), a `Blob`/`File`, an `ArrayBuffer` or a typed array. */
|
|
31
|
+
static async load(input, options = {}) {
|
|
32
|
+
if (typeof input === 'string' || input instanceof URL) {
|
|
33
|
+
const response = await fetch(input, { signal: options.signal ?? null });
|
|
34
|
+
if (!response.ok)
|
|
35
|
+
throw new InvalidArgumentError(`Loading ${String(input)} failed: HTTP ${response.status}.`);
|
|
36
|
+
const length = Number(response.headers.get('content-length') ?? 0);
|
|
37
|
+
if (length > MAX_FILE_BYTES)
|
|
38
|
+
throw new AcsFormatError('limit', 'File exceeds 256 MiB policy.');
|
|
39
|
+
return CharacterSource.fromBytes(await response.arrayBuffer(), nameOf(String(input)));
|
|
40
|
+
}
|
|
41
|
+
if (typeof Blob !== 'undefined' && input instanceof Blob) {
|
|
42
|
+
if (input.size > MAX_FILE_BYTES)
|
|
43
|
+
throw new AcsFormatError('limit', 'File exceeds 256 MiB policy.');
|
|
44
|
+
const name = 'name' in input && typeof input.name === 'string' ? input.name : 'character';
|
|
45
|
+
return CharacterSource.fromBytes(await input.arrayBuffer(), name);
|
|
46
|
+
}
|
|
47
|
+
return CharacterSource.fromBytes(input);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function nameOf(url) {
|
|
51
|
+
const path = url.split(/[?#]/)[0] ?? url;
|
|
52
|
+
const last = path.split('/').pop() ?? path;
|
|
53
|
+
return decodeURIComponent(last) || 'character';
|
|
54
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { TtsMetadata } from '../acs/model.js';
|
|
2
|
+
import { WaveAudio } from '../acs/wave.js';
|
|
3
|
+
import { type SpeechPhoneme, type VisemeCue } from './lipsync.js';
|
|
4
|
+
import { type SpeechWordMark } from './text.js';
|
|
5
|
+
export type SpeechVoiceGender = 'unknown' | 'female' | 'male' | 'neutral';
|
|
6
|
+
/** A voice a speech provider can use. `id` is the provider's own identifier; it is never written into a character. */
|
|
7
|
+
export interface SpeechVoice {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/** BCP 47 language tag, for example en-US (empty when unknown). */
|
|
11
|
+
readonly language: string;
|
|
12
|
+
readonly gender: SpeechVoiceGender;
|
|
13
|
+
readonly provider: string;
|
|
14
|
+
}
|
|
15
|
+
/** One synthesis request. */
|
|
16
|
+
export interface SpeechSynthesisRequest {
|
|
17
|
+
readonly text: string;
|
|
18
|
+
readonly voice: SpeechVoice | null;
|
|
19
|
+
/** Speaking rate relative to the voice's normal rate (100 = normal). */
|
|
20
|
+
readonly ratePercent: number;
|
|
21
|
+
}
|
|
22
|
+
/** Synthesized or recorded speech: optional audio, its duration and whatever timing is known. Offsets are milliseconds from its start. */
|
|
23
|
+
export interface SpeechAudio {
|
|
24
|
+
readonly audio: WaveAudio | null;
|
|
25
|
+
readonly durationMs: number;
|
|
26
|
+
readonly words: readonly SpeechWordMark[];
|
|
27
|
+
/** Mouth cues already mapped to ACS shapes. */
|
|
28
|
+
readonly visemes: readonly VisemeCue[];
|
|
29
|
+
readonly phonemes: readonly SpeechPhoneme[];
|
|
30
|
+
readonly voice: SpeechVoice | null;
|
|
31
|
+
/** How the speech was produced (diagnostic). */
|
|
32
|
+
readonly note?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The portable speech-provider contract (Runtime.md §16.1). `synthesize` may return its result synchronously or as a promise; a thrown
|
|
36
|
+
* error or a rejected promise is a provider failure: the speech is then paced without audio and a `speech` runtime error is reported.
|
|
37
|
+
* The runtime aborts `signal` when the request is cancelled.
|
|
38
|
+
*/
|
|
39
|
+
export interface SpeechProvider {
|
|
40
|
+
readonly name: string;
|
|
41
|
+
getVoices(): readonly SpeechVoice[];
|
|
42
|
+
synthesize(request: SpeechSynthesisRequest, signal: AbortSignal): SpeechAudio | PromiseLike<SpeechAudio>;
|
|
43
|
+
}
|
|
44
|
+
/** Recorded speech: text for the balloon, optional audio, duration and cues relative to the start of the audio. */
|
|
45
|
+
export interface SpeechContent {
|
|
46
|
+
readonly text: string;
|
|
47
|
+
readonly audio?: WaveAudio | null;
|
|
48
|
+
readonly durationMs?: number;
|
|
49
|
+
readonly words?: readonly SpeechWordMark[];
|
|
50
|
+
readonly mouth?: readonly VisemeCue[];
|
|
51
|
+
}
|
|
52
|
+
/** Which voice a session uses and why. A historical SAPI 4 voice is never claimed to be reproduced. */
|
|
53
|
+
export interface SpeechVoiceDecision {
|
|
54
|
+
readonly voice: SpeechVoice | null;
|
|
55
|
+
readonly reason: string;
|
|
56
|
+
}
|
|
57
|
+
/** The language tag of a Windows LANGID from a fixed table (identical on every platform); empty when unknown. */
|
|
58
|
+
export declare function languageOfLangId(langId: number): string;
|
|
59
|
+
/**
|
|
60
|
+
* Chooses a voice (Runtime.md §16.2): an explicit installed voice; otherwise the first voice with the character's language and gender,
|
|
61
|
+
* then its language, then its base language, then the provider's first voice.
|
|
62
|
+
*/
|
|
63
|
+
export declare function selectVoice(voices: readonly SpeechVoice[], legacy: TtsMetadata | null, explicitVoiceId: string | null): SpeechVoiceDecision;
|
|
64
|
+
/** Options of {@link FakeSpeechProvider}. */
|
|
65
|
+
export interface FakeSpeechOptions {
|
|
66
|
+
readonly voices?: readonly SpeechVoice[];
|
|
67
|
+
/** Milliseconds per text element (default 60). */
|
|
68
|
+
readonly characterMs?: number;
|
|
69
|
+
/** Report mouth cues as a viseme-capable engine would (default true). */
|
|
70
|
+
readonly reportVisemes?: boolean;
|
|
71
|
+
/** Produce silent 8 kHz PCM audio of the speech duration. */
|
|
72
|
+
readonly produceAudio?: boolean;
|
|
73
|
+
/** Synthesis completes when this promise settles (simulates a slow provider). */
|
|
74
|
+
readonly latency?: PromiseLike<unknown>;
|
|
75
|
+
/** Synthesis fails with this message. */
|
|
76
|
+
readonly failure?: string;
|
|
77
|
+
}
|
|
78
|
+
/** A deterministic speech provider for tests and headless hosts: fixed timing per text element, word marks and spelling mouth cues. */
|
|
79
|
+
export declare class FakeSpeechProvider implements SpeechProvider {
|
|
80
|
+
private readonly options;
|
|
81
|
+
static readonly english: SpeechVoice;
|
|
82
|
+
static readonly czech: SpeechVoice;
|
|
83
|
+
readonly name = "Fake";
|
|
84
|
+
readonly requests: SpeechSynthesisRequest[];
|
|
85
|
+
constructor(options?: FakeSpeechOptions);
|
|
86
|
+
getVoices(): readonly SpeechVoice[];
|
|
87
|
+
synthesize(request: SpeechSynthesisRequest): SpeechAudio | PromiseLike<SpeechAudio>;
|
|
88
|
+
private result;
|
|
89
|
+
}
|
|
90
|
+
/** Silent 8 kHz, 8-bit, mono PCM of the given duration (8 samples of 0x80 per millisecond). */
|
|
91
|
+
export declare function silentPcm(durationMs: number): WaveAudio;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { pcm16Wave, WaveAudio } from '../acs/wave.js';
|
|
2
|
+
import { approximateCues } from './lipsync.js';
|
|
3
|
+
import { textLength, textWords } from './text.js';
|
|
4
|
+
const CULTURES = {
|
|
5
|
+
0x0409: 'en-US', 0x0809: 'en-GB', 0x0405: 'cs-CZ', 0x0407: 'de-DE', 0x040c: 'fr-FR', 0x0410: 'it-IT', 0x0c0a: 'es-ES', 0x040a: 'es-ES',
|
|
6
|
+
0x0411: 'ja-JP', 0x0412: 'ko-KR', 0x0404: 'zh-TW', 0x0804: 'zh-CN', 0x0416: 'pt-BR', 0x0816: 'pt-PT', 0x0413: 'nl-NL', 0x0419: 'ru-RU',
|
|
7
|
+
0x0415: 'pl-PL', 0x041b: 'sk-SK', 0x0406: 'da-DK', 0x041d: 'sv-SE', 0x0414: 'nb-NO', 0x040b: 'fi-FI', 0x040e: 'hu-HU', 0x0408: 'el-GR',
|
|
8
|
+
0x041f: 'tr-TR', 0x040d: 'he-IL', 0x0401: 'ar-SA',
|
|
9
|
+
};
|
|
10
|
+
/** The language tag of a Windows LANGID from a fixed table (identical on every platform); empty when unknown. */
|
|
11
|
+
export function languageOfLangId(langId) {
|
|
12
|
+
return CULTURES[langId] ?? '';
|
|
13
|
+
}
|
|
14
|
+
const same = (a, b) => a.toLowerCase() === b.toLowerCase();
|
|
15
|
+
/**
|
|
16
|
+
* Chooses a voice (Runtime.md §16.2): an explicit installed voice; otherwise the first voice with the character's language and gender,
|
|
17
|
+
* then its language, then its base language, then the provider's first voice.
|
|
18
|
+
*/
|
|
19
|
+
export function selectVoice(voices, legacy, explicitVoiceId) {
|
|
20
|
+
const legacyText = legacy === null ? 'The character has no TTS metadata' : `The character's TTS metadata names engine ${legacy.engine} mode ${legacy.mode}`;
|
|
21
|
+
if (voices.length === 0)
|
|
22
|
+
return { voice: null, reason: legacyText + '; no voice is available, so speech has no audio.' };
|
|
23
|
+
if (explicitVoiceId !== null) {
|
|
24
|
+
const chosen = voices.find((v) => same(v.id, explicitVoiceId));
|
|
25
|
+
if (chosen)
|
|
26
|
+
return { voice: chosen, reason: `Host-selected voice '${chosen.name}'. ${legacyText}; it is not reproduced.` };
|
|
27
|
+
return { voice: voices[0], reason: `Host-selected voice '${explicitVoiceId}' is not available; using '${voices[0].name}'.` };
|
|
28
|
+
}
|
|
29
|
+
const language = legacy?.voice ? languageOfLangId(legacy.voice.language) : null;
|
|
30
|
+
const gender = legacy?.voice?.gender === 1 ? 'female' : legacy?.voice?.gender === 2 ? 'male' : 'unknown';
|
|
31
|
+
const sameLanguage = (v) => language !== null && same(v.language, language);
|
|
32
|
+
const sameBase = (v) => language !== null && same(v.language.split('-')[0], language.split('-')[0]);
|
|
33
|
+
const pick = voices.find((v) => sameLanguage(v) && gender !== 'unknown' && v.gender === gender) ?? voices.find(sameLanguage) ?? voices.find(sameBase);
|
|
34
|
+
if (pick)
|
|
35
|
+
return { voice: pick, reason: `${legacyText}, which CharacterRuntime does not provide; using '${pick.name}' chosen by language.` };
|
|
36
|
+
return { voice: voices[0], reason: `${legacyText}; no voice matches its language, using '${voices[0].name}'.` };
|
|
37
|
+
}
|
|
38
|
+
/** A deterministic speech provider for tests and headless hosts: fixed timing per text element, word marks and spelling mouth cues. */
|
|
39
|
+
export class FakeSpeechProvider {
|
|
40
|
+
options;
|
|
41
|
+
static english = { id: 'fake-en-US-female', name: 'Fake English', language: 'en-US', gender: 'female', provider: 'Fake' };
|
|
42
|
+
static czech = { id: 'fake-cs-CZ-male', name: 'Fake Czech', language: 'cs-CZ', gender: 'male', provider: 'Fake' };
|
|
43
|
+
name = 'Fake';
|
|
44
|
+
requests = [];
|
|
45
|
+
constructor(options = {}) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
}
|
|
48
|
+
getVoices() {
|
|
49
|
+
return this.options.voices ?? [FakeSpeechProvider.english, FakeSpeechProvider.czech];
|
|
50
|
+
}
|
|
51
|
+
synthesize(request) {
|
|
52
|
+
this.requests.push(request);
|
|
53
|
+
const { latency, failure } = this.options;
|
|
54
|
+
if (latency)
|
|
55
|
+
return Promise.resolve(latency).then(() => this.result(request));
|
|
56
|
+
if (failure)
|
|
57
|
+
throw new Error(failure);
|
|
58
|
+
return this.result(request);
|
|
59
|
+
}
|
|
60
|
+
result(request) {
|
|
61
|
+
if (this.options.failure)
|
|
62
|
+
throw new Error(this.options.failure);
|
|
63
|
+
const characterMs = this.options.characterMs ?? 60;
|
|
64
|
+
const scale = 100 / Math.min(1000, Math.max(10, request.ratePercent));
|
|
65
|
+
const words = textWords(request.text).map((w) => ({ offsetMs: w.start * characterMs * scale, textStart: w.start, textLength: w.length }));
|
|
66
|
+
const duration = Math.max(characterMs * scale, textLength(request.text) * characterMs * scale);
|
|
67
|
+
const visemes = this.options.reportVisemes ?? true ? approximateCues(request.text, words, duration) : [];
|
|
68
|
+
const audio = this.options.produceAudio ? silentPcm(duration) : null;
|
|
69
|
+
return { audio, durationMs: audio?.durationMs ?? duration, words, visemes, phonemes: [], voice: request.voice };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Silent 8 kHz, 8-bit, mono PCM of the given duration (8 samples of 0x80 per millisecond). */
|
|
73
|
+
export function silentPcm(durationMs) {
|
|
74
|
+
const samples = Math.ceil(Math.min(durationMs, 60_000) * 8);
|
|
75
|
+
const bytes = new Uint8Array(44 + samples);
|
|
76
|
+
const header = pcm16Wave(new Int16Array(0), 1, 8000);
|
|
77
|
+
bytes.set(header.subarray(0, 44));
|
|
78
|
+
const view = new DataView(bytes.buffer);
|
|
79
|
+
view.setUint32(4, 36 + samples, true);
|
|
80
|
+
view.setUint32(28, 8000, true);
|
|
81
|
+
view.setUint16(32, 1, true);
|
|
82
|
+
view.setUint16(34, 8, true);
|
|
83
|
+
view.setUint32(40, samples, true);
|
|
84
|
+
bytes.fill(0x80, 44);
|
|
85
|
+
return WaveAudio.read(bytes);
|
|
86
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** A word of a text: UTF-16 start offset and length. */
|
|
2
|
+
export interface TextWord {
|
|
3
|
+
readonly start: number;
|
|
4
|
+
readonly length: number;
|
|
5
|
+
}
|
|
6
|
+
/** White space as Unicode defines it (Zs, Zl, Zp and the control spaces), per UTF-16 code unit. */
|
|
7
|
+
export declare function isWhiteSpace(code: number): boolean;
|
|
8
|
+
/** The text elements of a string (grapheme clusters; code points where the platform has no segmentation). */
|
|
9
|
+
export declare function textElements(text: string): string[];
|
|
10
|
+
/** The number of text elements. */
|
|
11
|
+
export declare function textLength(text: string): number;
|
|
12
|
+
/** The words of a text. */
|
|
13
|
+
export declare function textWords(text: string): TextWord[];
|
|
14
|
+
/** Reading-time pacing settings (Runtime.md §14.1). */
|
|
15
|
+
export interface PacingOptions {
|
|
16
|
+
readonly characterMs: number;
|
|
17
|
+
readonly wordMs: number;
|
|
18
|
+
readonly minimumMs: number;
|
|
19
|
+
}
|
|
20
|
+
/** A word mark: offset from the start of the speech and the word's UTF-16 range. */
|
|
21
|
+
export interface SpeechWordMark {
|
|
22
|
+
readonly offsetMs: number;
|
|
23
|
+
readonly textStart: number;
|
|
24
|
+
readonly textLength: number;
|
|
25
|
+
}
|
|
26
|
+
/** Reading-time pacing: each word takes wordMs + characterMs per text element; the duration is at least minimumMs. */
|
|
27
|
+
export declare function pacedSpeech(text: string, options: PacingOptions): {
|
|
28
|
+
durationMs: number;
|
|
29
|
+
words: SpeechWordMark[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Wraps at whitespace to at most `charsPerLine` text elements per line; explicit line breaks are kept and longer words are broken.
|
|
33
|
+
* Returns each line with its UTF-16 start offset in the text.
|
|
34
|
+
*/
|
|
35
|
+
export declare function wrapText(text: string, charsPerLine: number): {
|
|
36
|
+
start: number;
|
|
37
|
+
text: string;
|
|
38
|
+
}[];
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Text segmentation shared by pacing, balloons and approximate lip-sync. Words are maximal runs of non-whitespace UTF-16 code units;
|
|
2
|
+
// lengths for pacing and wrapping are counted in text elements (extended grapheme clusters), so diacritics and combining sequences
|
|
3
|
+
// count once (Runtime.md §14.3, §20).
|
|
4
|
+
/** White space as Unicode defines it (Zs, Zl, Zp and the control spaces), per UTF-16 code unit. */
|
|
5
|
+
export function isWhiteSpace(code) {
|
|
6
|
+
return (code >= 0x09 && code <= 0x0d) || code === 0x20 || code === 0x85 || code === 0xa0 || code === 0x1680 ||
|
|
7
|
+
(code >= 0x2000 && code <= 0x200a) || code === 0x2028 || code === 0x2029 || code === 0x202f || code === 0x205f || code === 0x3000;
|
|
8
|
+
}
|
|
9
|
+
let segmenter;
|
|
10
|
+
function graphemes() {
|
|
11
|
+
if (segmenter === undefined)
|
|
12
|
+
segmenter = typeof Intl !== 'undefined' && 'Segmenter' in Intl ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
|
|
13
|
+
return segmenter;
|
|
14
|
+
}
|
|
15
|
+
/** The text elements of a string (grapheme clusters; code points where the platform has no segmentation). */
|
|
16
|
+
export function textElements(text) {
|
|
17
|
+
const s = graphemes();
|
|
18
|
+
if (s)
|
|
19
|
+
return Array.from(s.segment(text), (x) => x.segment);
|
|
20
|
+
return Array.from(text);
|
|
21
|
+
}
|
|
22
|
+
/** The number of text elements. */
|
|
23
|
+
export function textLength(text) {
|
|
24
|
+
return textElements(text).length;
|
|
25
|
+
}
|
|
26
|
+
/** The words of a text. */
|
|
27
|
+
export function textWords(text) {
|
|
28
|
+
const words = [];
|
|
29
|
+
let start = -1;
|
|
30
|
+
for (let i = 0; i <= text.length; i++) {
|
|
31
|
+
const space = i === text.length || isWhiteSpace(text.charCodeAt(i));
|
|
32
|
+
if (!space && start < 0)
|
|
33
|
+
start = i;
|
|
34
|
+
else if (space && start >= 0) {
|
|
35
|
+
words.push({ start, length: i - start });
|
|
36
|
+
start = -1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return words;
|
|
40
|
+
}
|
|
41
|
+
/** Reading-time pacing: each word takes wordMs + characterMs per text element; the duration is at least minimumMs. */
|
|
42
|
+
export function pacedSpeech(text, options) {
|
|
43
|
+
const words = [];
|
|
44
|
+
let t = 0;
|
|
45
|
+
for (const w of textWords(text)) {
|
|
46
|
+
words.push({ offsetMs: t, textStart: w.start, textLength: w.length });
|
|
47
|
+
t += options.wordMs + options.characterMs * textLength(text.substring(w.start, w.start + w.length));
|
|
48
|
+
}
|
|
49
|
+
return { durationMs: Math.max(options.minimumMs, t), words };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Wraps at whitespace to at most `charsPerLine` text elements per line; explicit line breaks are kept and longer words are broken.
|
|
53
|
+
* Returns each line with its UTF-16 start offset in the text.
|
|
54
|
+
*/
|
|
55
|
+
export function wrapText(text, charsPerLine) {
|
|
56
|
+
charsPerLine = Math.max(1, charsPerLine);
|
|
57
|
+
const lines = [];
|
|
58
|
+
let paragraphStart = 0;
|
|
59
|
+
for (const paragraph of text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n')) {
|
|
60
|
+
let lineStart = -1, line = '', lineLength = 0;
|
|
61
|
+
for (const word of textWords(paragraph)) {
|
|
62
|
+
let w = paragraph.substring(word.start, word.start + word.length);
|
|
63
|
+
let wl = textLength(w);
|
|
64
|
+
if (lineLength > 0 && lineLength + 1 + wl > charsPerLine) {
|
|
65
|
+
lines.push({ start: paragraphStart + lineStart, text: line });
|
|
66
|
+
line = '';
|
|
67
|
+
lineLength = 0;
|
|
68
|
+
}
|
|
69
|
+
while (wl > charsPerLine) {
|
|
70
|
+
if (lineLength > 0) {
|
|
71
|
+
lines.push({ start: paragraphStart + lineStart, text: line });
|
|
72
|
+
line = '';
|
|
73
|
+
lineLength = 0;
|
|
74
|
+
}
|
|
75
|
+
const head = textElements(w).slice(0, charsPerLine).join('');
|
|
76
|
+
lines.push({ start: paragraphStart + word.start + (word.length - w.length), text: head });
|
|
77
|
+
w = w.substring(head.length);
|
|
78
|
+
wl -= charsPerLine;
|
|
79
|
+
}
|
|
80
|
+
if (lineLength === 0) {
|
|
81
|
+
lineStart = word.start + (word.length - w.length);
|
|
82
|
+
line = w;
|
|
83
|
+
lineLength = wl;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
line += ' ' + w;
|
|
87
|
+
lineLength += 1 + wl;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (lineLength > 0)
|
|
91
|
+
lines.push({ start: paragraphStart + lineStart, text: line });
|
|
92
|
+
else if (paragraph.length === 0 && lines.length > 0)
|
|
93
|
+
lines.push({ start: paragraphStart, text: '' });
|
|
94
|
+
paragraphStart += paragraph.length + 1;
|
|
95
|
+
}
|
|
96
|
+
return lines;
|
|
97
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lubomirklimes/character-runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-preview.1",
|
|
4
|
+
"description": "Native TypeScript runtime for animated characters in the Microsoft Agent ACS format.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Lubomir Klimes",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/lubomirklimes/CharacterRuntime.JS.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/lubomirklimes/CharacterRuntime.JS#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/lubomirklimes/CharacterRuntime.JS/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"acs",
|
|
18
|
+
"microsoft-agent",
|
|
19
|
+
"animated-character",
|
|
20
|
+
"character-runtime",
|
|
21
|
+
"typescript",
|
|
22
|
+
"canvas",
|
|
23
|
+
"web-audio"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./browser": {
|
|
34
|
+
"types": "./dist/browser/index.d.ts",
|
|
35
|
+
"default": "./dist/browser/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist/**/*.js",
|
|
41
|
+
"dist/**/*.d.ts",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"clean": "node -e \"for (const d of ['dist','build']) require('fs').rmSync(d,{recursive:true,force:true})\"",
|
|
53
|
+
"build": "npm run clean && tsc -p tsconfig.build.json",
|
|
54
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p samples/CharacterPlayer.Web/tsconfig.json",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"conformance": "tsc -p tsconfig.tools.json && node build/tools/conformance/cli.js",
|
|
57
|
+
"test:browser": "npm run build && npm run player:build && tsc -p tsconfig.tools.json && playwright test",
|
|
58
|
+
"pack:check": "npm run build && node tools/check-package.mjs",
|
|
59
|
+
"player": "vite --config samples/CharacterPlayer.Web/vite.config.ts",
|
|
60
|
+
"player:build": "vite build --config samples/CharacterPlayer.Web/vite.config.ts",
|
|
61
|
+
"player:preview": "node samples/CharacterPlayer.Web/tests/static-server.mjs"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@playwright/test": "^1.65.1",
|
|
65
|
+
"@types/node": "^22.20.7",
|
|
66
|
+
"typescript": "^5.9.3",
|
|
67
|
+
"vite": "^6.4.6",
|
|
68
|
+
"vitest": "^4.1.12"
|
|
69
|
+
}
|
|
70
|
+
}
|