@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.
Files changed (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/dist/acs/acs15.d.ts +9 -0
  4. package/dist/acs/acs15.js +382 -0
  5. package/dist/acs/acs2-reader.d.ts +2 -0
  6. package/dist/acs/acs2-reader.js +277 -0
  7. package/dist/acs/binary.d.ts +1 -0
  8. package/dist/acs/binary.js +107 -0
  9. package/dist/acs/compound-file.d.ts +1 -0
  10. package/dist/acs/compound-file.js +326 -0
  11. package/dist/acs/document.d.ts +33 -0
  12. package/dist/acs/document.js +99 -0
  13. package/dist/acs/errors.d.ts +28 -0
  14. package/dist/acs/errors.js +41 -0
  15. package/dist/acs/image.d.ts +30 -0
  16. package/dist/acs/image.js +113 -0
  17. package/dist/acs/index.d.ts +11 -0
  18. package/dist/acs/index.js +10 -0
  19. package/dist/acs/lz.d.ts +7 -0
  20. package/dist/acs/lz.js +102 -0
  21. package/dist/acs/metadata.d.ts +14 -0
  22. package/dist/acs/metadata.js +126 -0
  23. package/dist/acs/model.d.ts +130 -0
  24. package/dist/acs/model.js +12 -0
  25. package/dist/acs/mouth.d.ts +37 -0
  26. package/dist/acs/mouth.js +43 -0
  27. package/dist/acs/wave.d.ts +53 -0
  28. package/dist/acs/wave.js +422 -0
  29. package/dist/browser/animation-frame-clock.d.ts +18 -0
  30. package/dist/browser/animation-frame-clock.js +42 -0
  31. package/dist/browser/canvas-renderer.d.ts +50 -0
  32. package/dist/browser/canvas-renderer.js +145 -0
  33. package/dist/browser/host.d.ts +84 -0
  34. package/dist/browser/host.js +206 -0
  35. package/dist/browser/index.d.ts +6 -0
  36. package/dist/browser/index.js +8 -0
  37. package/dist/browser/web-audio.d.ts +48 -0
  38. package/dist/browser/web-audio.js +205 -0
  39. package/dist/browser/web-speech.d.ts +131 -0
  40. package/dist/browser/web-speech.js +302 -0
  41. package/dist/core/activities.d.ts +10 -0
  42. package/dist/core/activities.js +479 -0
  43. package/dist/core/agent.d.ts +263 -0
  44. package/dist/core/agent.js +764 -0
  45. package/dist/core/audio.d.ts +17 -0
  46. package/dist/core/audio.js +174 -0
  47. package/dist/core/balloon.d.ts +43 -0
  48. package/dist/core/balloon.js +94 -0
  49. package/dist/core/clock.d.ts +32 -0
  50. package/dist/core/clock.js +51 -0
  51. package/dist/core/compositor.d.ts +22 -0
  52. package/dist/core/compositor.js +67 -0
  53. package/dist/core/events.d.ts +131 -0
  54. package/dist/core/events.js +1 -0
  55. package/dist/core/host.d.ts +29 -0
  56. package/dist/core/host.js +77 -0
  57. package/dist/core/index.d.ts +16 -0
  58. package/dist/core/index.js +13 -0
  59. package/dist/core/lipsync.d.ts +41 -0
  60. package/dist/core/lipsync.js +209 -0
  61. package/dist/core/random.d.ts +52 -0
  62. package/dist/core/random.js +105 -0
  63. package/dist/core/request.d.ts +68 -0
  64. package/dist/core/request.js +131 -0
  65. package/dist/core/resources.d.ts +70 -0
  66. package/dist/core/resources.js +243 -0
  67. package/dist/core/scheduler.d.ts +25 -0
  68. package/dist/core/scheduler.js +766 -0
  69. package/dist/core/source.d.ts +21 -0
  70. package/dist/core/source.js +54 -0
  71. package/dist/core/speech.d.ts +91 -0
  72. package/dist/core/speech.js +86 -0
  73. package/dist/core/text.d.ts +38 -0
  74. package/dist/core/text.js +97 -0
  75. package/dist/index.d.ts +2 -0
  76. package/dist/index.js +4 -0
  77. package/package.json +68 -4
@@ -0,0 +1,11 @@
1
+ export { CharacterRuntimeError, AcsFormatError, NotSupportedError, InvalidArgumentError, DisposedError } from './errors.js';
2
+ export { decodeLz, MAX_DECODED_BYTES } from './lz.js';
3
+ export type { CharacterFormat, LocalizedName, FrameLayer, FrameBranch, MouthRecord, Frame, Animation, RetainedBlock, TtsMetadata, TtsVoice, BalloonMetadata, Character, } from './model.js';
4
+ export { CharacterStyle } from './model.js';
5
+ export { parseTtsBlock, parseBalloonBlock, characterMetadata, type CharacterMetadata } from './metadata.js';
6
+ export { IndexedImage, indexedToRgba, type RgbaImage, MAX_DIMENSION, MAX_BITMAP_BYTES, MAX_IMAGES, MAX_LAYERS_PER_FRAME } from './image.js';
7
+ export { MouthShape, checkMouth, mouthRefusal, MAX_MOUTH_RECORDS, type MouthShapeCheck } from './mouth.js';
8
+ export { WaveAudio, WaveCodec, pcm16Wave, muLawToPcm, aLawToPcm, floatToPcm, MAX_SOUND_BYTES, MAX_SOUND_MS } from './wave.js';
9
+ export { type UnavailableAnimation, ACS15_STREAM_CACHE_BYTES } from './acs15.js';
10
+ export { openCharacterDocument, detectFormat, type CharacterDocument } from './document.js';
11
+ export { MAX_FILE_BYTES } from './acs2-reader.js';
@@ -0,0 +1,10 @@
1
+ export { CharacterRuntimeError, AcsFormatError, NotSupportedError, InvalidArgumentError, DisposedError } from './errors.js';
2
+ export { decodeLz, MAX_DECODED_BYTES } from './lz.js';
3
+ export { CharacterStyle } from './model.js';
4
+ export { parseTtsBlock, parseBalloonBlock, characterMetadata } from './metadata.js';
5
+ export { IndexedImage, indexedToRgba, MAX_DIMENSION, MAX_BITMAP_BYTES, MAX_IMAGES, MAX_LAYERS_PER_FRAME } from './image.js';
6
+ export { MouthShape, checkMouth, mouthRefusal, MAX_MOUTH_RECORDS } from './mouth.js';
7
+ export { WaveAudio, WaveCodec, pcm16Wave, muLawToPcm, aLawToPcm, floatToPcm, MAX_SOUND_BYTES, MAX_SOUND_MS } from './wave.js';
8
+ export { ACS15_STREAM_CACHE_BYTES } from './acs15.js';
9
+ export { openCharacterDocument, detectFormat } from './document.js';
10
+ export { MAX_FILE_BYTES } from './acs2-reader.js';
@@ -0,0 +1,7 @@
1
+ /** Largest output of one LZ stream (CharacterRuntime policy, ACS.md §8). */
2
+ export declare const MAX_DECODED_BYTES: number;
3
+ /**
4
+ * Decodes an ACS LZ stream (ACS.md §8) to exactly `size` bytes. With `requireComplete`, the stream must end with the end marker, 1-bit
5
+ * padding and only 0xFF bytes (region suffix mode). Every failure is an {@link AcsFormatError} with code `compression`.
6
+ */
7
+ export declare function decodeLz(source: Uint8Array, size: number, requireComplete?: boolean): Uint8Array;
package/dist/acs/lz.js ADDED
@@ -0,0 +1,102 @@
1
+ import { AcsFormatError } from './errors.js';
2
+ /** Largest output of one LZ stream (CharacterRuntime policy, ACS.md §8). */
3
+ export const MAX_DECODED_BYTES = 16 * 1024 * 1024;
4
+ function lzError(message) {
5
+ return new AcsFormatError('compression', message);
6
+ }
7
+ /** LSB-first bit reader over an LZ stream; reading past the end is an error. */
8
+ class BitReader {
9
+ source;
10
+ bit = 8; // bits are read from byte 1 on
11
+ total;
12
+ constructor(source) {
13
+ this.source = source;
14
+ this.total = source.length * 8;
15
+ }
16
+ get remaining() {
17
+ return this.total - this.bit;
18
+ }
19
+ /** Reads `count` (0–24) bits as an unsigned value, least significant bit first. */
20
+ read(count) {
21
+ if (this.bit + count > this.total)
22
+ throw lzError('Truncated compressed bitstream.');
23
+ if (count === 0)
24
+ return 0;
25
+ const s = this.source;
26
+ const at = this.bit >>> 3;
27
+ const word = (s[at] | ((s[at + 1] ?? 0) << 8) | ((s[at + 2] ?? 0) << 16) | ((s[at + 3] ?? 0) << 24)) >>> 0;
28
+ const value = (word >>> (this.bit & 7)) & (count === 32 ? 0xffffffff : (1 << count) - 1);
29
+ this.bit += count;
30
+ return value >>> 0;
31
+ }
32
+ /** Complete-stream check after the end marker: the rest of the byte is 1 bits and every following byte is 0xFF. */
33
+ requireOnes() {
34
+ while ((this.bit & 7) !== 0 && this.remaining > 0)
35
+ if (this.read(1) !== 1)
36
+ throw lzError('Unknown bits after compressed end marker.');
37
+ for (let at = this.bit >>> 3; at < this.source.length; at++) {
38
+ if (this.source[at] !== 0xff)
39
+ throw lzError('Unknown bytes after compressed end marker.');
40
+ }
41
+ this.bit = this.total;
42
+ }
43
+ }
44
+ /**
45
+ * Decodes an ACS LZ stream (ACS.md §8) to exactly `size` bytes. With `requireComplete`, the stream must end with the end marker, 1-bit
46
+ * padding and only 0xFF bytes (region suffix mode). Every failure is an {@link AcsFormatError} with code `compression`.
47
+ */
48
+ export function decodeLz(source, size, requireComplete = false) {
49
+ if (!(size >= 0) || size > MAX_DECODED_BYTES)
50
+ throw lzError('Decoded size exceeds policy.');
51
+ if (source.length < 8 || source[0] !== 0)
52
+ throw lzError('Missing compressed stream header/trailer.');
53
+ for (let i = source.length - 5; i < source.length; i++)
54
+ if (source[i] !== 0xff)
55
+ throw lzError('Missing compressed stream header/trailer.');
56
+ const bits = new BitReader(source);
57
+ const output = new Uint8Array(size);
58
+ let position = 0;
59
+ while (position < size) {
60
+ if (bits.read(1) === 0) {
61
+ output[position++] = bits.read(8);
62
+ continue;
63
+ }
64
+ let distance;
65
+ let bias = 1;
66
+ if (bits.read(1) === 0)
67
+ distance = bits.read(6) + 1;
68
+ else if (bits.read(1) === 0)
69
+ distance = bits.read(9) + 65;
70
+ else if (bits.read(1) === 0)
71
+ distance = bits.read(12) + 577;
72
+ else {
73
+ const value = bits.read(20);
74
+ if (value === 0xfffff)
75
+ throw lzError('End marker before expected output length.');
76
+ distance = value + 4673;
77
+ bias = 2;
78
+ }
79
+ let k = 0;
80
+ while (bits.read(1) !== 0)
81
+ if (++k > 11)
82
+ throw lzError('Run length prefix exceeds supported limit.');
83
+ const length = (1 << k) + bits.read(k) + bias;
84
+ if (distance > position || length > size - position)
85
+ throw lzError('Invalid back-reference distance or length.');
86
+ if (distance >= length) {
87
+ output.copyWithin(position, position - distance, position - distance + length);
88
+ position += length;
89
+ }
90
+ else {
91
+ // Overlapping copies repeat data: byte by byte.
92
+ for (let i = 0; i < length; i++, position++)
93
+ output[position] = output[position - distance];
94
+ }
95
+ }
96
+ if (requireComplete) {
97
+ if (bits.read(24) !== 0xffffff)
98
+ throw lzError('Missing final compressed end marker.');
99
+ bits.requireOnes();
100
+ }
101
+ return output;
102
+ }
@@ -0,0 +1,14 @@
1
+ import type { BalloonMetadata, Character, TtsMetadata } from './model.js';
2
+ /** Parses a TTS block (ACS.md §3.2); null when it is not well formed. */
3
+ export declare function parseTtsBlock(block: Uint8Array): TtsMetadata | null;
4
+ /** Parses a balloon block (ACS.md §3.3); null when it is not well formed. */
5
+ export declare function parseBalloonBlock(block: Uint8Array): BalloonMetadata | null;
6
+ /** The metadata blocks of a character: typed views and whether a present block is malformed. */
7
+ export interface CharacterMetadata {
8
+ readonly tts: TtsMetadata | null;
9
+ readonly ttsMalformed: boolean;
10
+ readonly balloon: BalloonMetadata | null;
11
+ readonly balloonMalformed: boolean;
12
+ }
13
+ /** The typed TTS and balloon views of a character's retained blocks. */
14
+ export declare function characterMetadata(character: Character): CharacterMetadata;
@@ -0,0 +1,126 @@
1
+ import { guidText, i16, i32, u16, u32, utf16, writeU16, writeU32 } from './binary.js';
2
+ const MAX_STRING = 65536;
3
+ /** A strict reader of one retained metadata block: false on any problem (the block then stays opaque). */
4
+ class BlockReader {
5
+ data;
6
+ pos = 0;
7
+ constructor(data) {
8
+ this.data = data;
9
+ }
10
+ get end() {
11
+ return this.pos === this.data.length;
12
+ }
13
+ take(count) {
14
+ if (count < 0 || count > this.data.length - this.pos)
15
+ return null;
16
+ const at = this.pos;
17
+ this.pos += count;
18
+ return at;
19
+ }
20
+ u8() {
21
+ const at = this.take(1);
22
+ return at === null ? null : this.data[at];
23
+ }
24
+ u16() {
25
+ const at = this.take(2);
26
+ return at === null ? null : u16(this.data, at);
27
+ }
28
+ i16() {
29
+ const at = this.take(2);
30
+ return at === null ? null : i16(this.data, at);
31
+ }
32
+ u32() {
33
+ const at = this.take(4);
34
+ return at === null ? null : u32(this.data, at);
35
+ }
36
+ i32() {
37
+ const at = this.take(4);
38
+ return at === null ? null : i32(this.data, at);
39
+ }
40
+ guid() {
41
+ const at = this.take(16);
42
+ return at === null ? null : guidText(this.data, at);
43
+ }
44
+ string() {
45
+ const count = this.u32();
46
+ if (count === null)
47
+ return null;
48
+ if (count === 0)
49
+ return '';
50
+ if (count > MAX_STRING)
51
+ return null;
52
+ const at = this.take(count * 2);
53
+ const terminator = this.u16();
54
+ if (at === null || terminator === null || terminator !== 0)
55
+ return null;
56
+ return utf16(this.data, at, count);
57
+ }
58
+ }
59
+ /** Parses a TTS block (ACS.md §3.2); null when it is not well formed. */
60
+ export function parseTtsBlock(block) {
61
+ const r = new BlockReader(block);
62
+ const engine = r.guid(), mode = r.guid(), speed = r.i32(), pitch = r.i16(), flag = r.u8();
63
+ if (engine === null || mode === null || speed === null || pitch === null || flag === null || flag > 1)
64
+ return null;
65
+ let voice = null;
66
+ if (flag === 1) {
67
+ const language = r.u16(), languageText = r.string(), gender = r.u16(), age = r.u16(), style = r.string();
68
+ if (language === null || languageText === null || gender === null || age === null || style === null)
69
+ return null;
70
+ voice = { language, languageText, gender, age, style };
71
+ }
72
+ return r.end ? { engine, mode, speed, pitch, voice } : null;
73
+ }
74
+ /** Parses a balloon block (ACS.md §3.3); null when it is not well formed. */
75
+ export function parseBalloonBlock(block) {
76
+ const r = new BlockReader(block);
77
+ const lines = r.u8(), charsPerLine = r.u8(), foreColor = r.u32(), backColor = r.u32(), borderColor = r.u32();
78
+ if (lines === null || charsPerLine === null || foreColor === null || backColor === null || borderColor === null)
79
+ return null;
80
+ const fontName = r.string();
81
+ if (fontName === null)
82
+ return null;
83
+ const fontHeight = r.i32(), fontWeight = r.u16(), unknownWord1 = r.u16(), unknownWord2 = r.u16();
84
+ if (fontHeight === null || fontWeight === null || unknownWord1 === null || unknownWord2 === null || !r.end)
85
+ return null;
86
+ return { lines, charsPerLine, foreColor, backColor, borderColor, fontName, fontHeight, fontWeight, unknownWord1, unknownWord2 };
87
+ }
88
+ /** The typed TTS and balloon views of a character's retained blocks. */
89
+ export function characterMetadata(character) {
90
+ const ttsBlock = character.retained.find((b) => b.kind === 'tts');
91
+ const balloonBlock = character.retained.find((b) => b.kind === 'balloon');
92
+ const tts = ttsBlock ? parseTtsBlock(ttsBlock.bytes) : null;
93
+ const balloon = balloonBlock ? parseBalloonBlock(balloonBlock.bytes) : null;
94
+ return { tts, ttsMalformed: !!ttsBlock && tts === null, balloon, balloonMalformed: !!balloonBlock && balloon === null };
95
+ }
96
+ /** @internal A growable little-endian writer for re-serialized blocks. */
97
+ export class BlockWriter {
98
+ bytes = [];
99
+ raw(values) {
100
+ for (let i = 0; i < values.length; i++)
101
+ this.bytes.push(values[i] & 0xff);
102
+ }
103
+ u8(v) {
104
+ this.bytes.push(v & 0xff);
105
+ }
106
+ u16(v) {
107
+ const b = new Uint8Array(2);
108
+ writeU16(b, 0, v);
109
+ this.raw(b);
110
+ }
111
+ u32(v) {
112
+ const b = new Uint8Array(4);
113
+ writeU32(b, 0, v);
114
+ this.raw(b);
115
+ }
116
+ string(units, count) {
117
+ this.u32(count);
118
+ if (count > 0) {
119
+ this.raw(units);
120
+ this.u16(0);
121
+ }
122
+ }
123
+ toArray() {
124
+ return Uint8Array.from(this.bytes);
125
+ }
126
+ }
@@ -0,0 +1,130 @@
1
+ /** The character model shared by ACS 2.x and ACS 1.5 files (ACS.md §3–§5, ACS15.md §6). Read-only by contract. */
2
+ /** The file format a character was read from. */
3
+ export type CharacterFormat = 'acs2' | 'acs15';
4
+ /** A localized name block entry. */
5
+ export interface LocalizedName {
6
+ /** Windows LANGID. */
7
+ readonly language: number;
8
+ readonly name: string;
9
+ readonly description: string;
10
+ readonly extra: string;
11
+ }
12
+ /** One image layer of a frame: an image index and its offset in frame coordinates. Layer 0 is in front. */
13
+ export interface FrameLayer {
14
+ readonly image: number;
15
+ readonly x: number;
16
+ readonly y: number;
17
+ }
18
+ /** A frame branch record: zero-based target frame and probability in percent. */
19
+ export interface FrameBranch {
20
+ readonly target: number;
21
+ readonly percent: number;
22
+ }
23
+ /** A 14-byte mouth overlay record (ACS.md §5). */
24
+ export interface MouthRecord {
25
+ /** Shape 0–6 (Closed, Wide1, Wide2, Wide3, Wide4, Medium, Narrow); other values are preserved and never drawn. */
26
+ readonly shape: number;
27
+ /** Replace byte: 1 = the overlay replaces layer 0. */
28
+ readonly replace: number;
29
+ /** 32-bit image index. */
30
+ readonly image: number;
31
+ readonly x: number;
32
+ readonly y: number;
33
+ /** Anchor words: preserved, not used by CharacterRuntime. */
34
+ readonly anchorX: number;
35
+ readonly anchorY: number;
36
+ }
37
+ export interface Frame {
38
+ readonly layers: readonly FrameLayer[];
39
+ /** −1 none; ≥ 0 a sound index; other negative values play nothing. */
40
+ readonly sound: number;
41
+ /** Duration in 1/100 s; 0 = untimed control frame. */
42
+ readonly durationTicks: number;
43
+ /** Exit value (Runtime.md §6). */
44
+ readonly exitFrame: number;
45
+ readonly branches: readonly FrameBranch[];
46
+ readonly mouths: readonly MouthRecord[];
47
+ }
48
+ export interface Animation {
49
+ /** The name in the animation index (lookups ignore case). */
50
+ readonly name: string;
51
+ /** The name stored in the animation record. */
52
+ readonly storedName: string;
53
+ /** 0 named return, 1 exit branches, 2 none; above 2 is refused by playback. */
54
+ readonly returnType: number;
55
+ readonly returnName: string;
56
+ readonly frames: readonly Frame[];
57
+ /** Bytes after the last frame (preserved; playback refuses characters that have any). */
58
+ readonly trailingBytes: Uint8Array;
59
+ }
60
+ /** Data that is preserved without being interpreted (ACS.md §11). */
61
+ export interface RetainedBlock {
62
+ readonly kind: string;
63
+ readonly offset: number;
64
+ readonly bytes: Uint8Array;
65
+ }
66
+ /** The parsed TTS block (ACS.md §3.2). */
67
+ export interface TtsMetadata {
68
+ readonly engine: string;
69
+ readonly mode: string;
70
+ readonly speed: number;
71
+ readonly pitch: number;
72
+ readonly voice: TtsVoice | null;
73
+ }
74
+ export interface TtsVoice {
75
+ readonly language: number;
76
+ readonly languageText: string;
77
+ /** 0 neutral, 1 female, 2 male. */
78
+ readonly gender: number;
79
+ readonly age: number;
80
+ readonly style: string;
81
+ }
82
+ /** The parsed balloon block (ACS.md §3.3). Colors are COLORREF words (0x00BBGGRR). */
83
+ export interface BalloonMetadata {
84
+ readonly lines: number;
85
+ readonly charsPerLine: number;
86
+ readonly foreColor: number;
87
+ readonly backColor: number;
88
+ readonly borderColor: number;
89
+ readonly fontName: string;
90
+ readonly fontHeight: number;
91
+ readonly fontWeight: number;
92
+ readonly unknownWord1: number;
93
+ readonly unknownWord2: number;
94
+ }
95
+ /** A loaded character (ACS 2.x, or ACS 1.5 presented through the same model). */
96
+ export interface Character {
97
+ readonly format: CharacterFormat;
98
+ readonly major: number;
99
+ readonly minor: number;
100
+ /** GUID in its standard lowercase text form. */
101
+ readonly guid: string;
102
+ readonly width: number;
103
+ readonly height: number;
104
+ readonly transparentIndex: number;
105
+ readonly style: number;
106
+ /** The u32 after Style (ACS 2.x; 0 for ACS 1.5). */
107
+ readonly headerWord: number;
108
+ readonly names: readonly LocalizedName[];
109
+ /** Palette words 0x00RRGGBB as stored (the high byte is reserved, not alpha). */
110
+ readonly palette: Uint32Array;
111
+ /** States in file order: name as stored → animation names. */
112
+ readonly states: ReadonlyMap<string, readonly string[]>;
113
+ readonly animations: readonly Animation[];
114
+ readonly imageCount: number;
115
+ readonly soundCount: number;
116
+ readonly retained: readonly RetainedBlock[];
117
+ /** Format observations that do not prevent loading. */
118
+ readonly warnings: readonly string[];
119
+ }
120
+ /** Style bits (ACS.md §3.1). */
121
+ export declare const CharacterStyle: {
122
+ readonly voiceNone: 16;
123
+ readonly voiceTts: 32;
124
+ readonly balloonNone: 256;
125
+ readonly balloonOn: 512;
126
+ readonly sizeToText: 65536;
127
+ readonly noAutoHide: 131072;
128
+ readonly noAutoPace: 262144;
129
+ readonly systemCharacter: 1048576;
130
+ };
@@ -0,0 +1,12 @@
1
+ /** The character model shared by ACS 2.x and ACS 1.5 files (ACS.md §3–§5, ACS15.md §6). Read-only by contract. */
2
+ /** Style bits (ACS.md §3.1). */
3
+ export const CharacterStyle = {
4
+ voiceNone: 0x10,
5
+ voiceTts: 0x20,
6
+ balloonNone: 0x100,
7
+ balloonOn: 0x200,
8
+ sizeToText: 0x10000,
9
+ noAutoHide: 0x20000,
10
+ noAutoPace: 0x40000,
11
+ systemCharacter: 0x100000,
12
+ };
@@ -0,0 +1,37 @@
1
+ import type { MouthRecord } from './model.js';
2
+ /** The seven ACS mouth shapes, in record type order. */
3
+ export declare const MouthShape: {
4
+ readonly closed: 0;
5
+ readonly wide1: 1;
6
+ readonly wide2: 2;
7
+ readonly wide3: 3;
8
+ readonly wide4: 4;
9
+ readonly medium: 5;
10
+ readonly narrow: 6;
11
+ };
12
+ /** A mouth shape 0–6. */
13
+ export type MouthShape = (typeof MouthShape)[keyof typeof MouthShape];
14
+ /** Most mouth records a frame may carry. */
15
+ export declare const MAX_MOUTH_RECORDS = 7;
16
+ /** The image range assumed where the character's image count is not passed: indices that fit in 16 bits. */
17
+ export declare const SIXTEEN_BIT_IMAGE_RANGE = 65536;
18
+ /** The facts of the mouth rule (ACS.md §5) for one shape on one frame. */
19
+ export interface MouthShapeCheck {
20
+ readonly shapeDefined: boolean;
21
+ readonly hasLayers: boolean;
22
+ readonly recordCount: number;
23
+ readonly matches: number;
24
+ /** The first record of the shape, or null. */
25
+ readonly record: MouthRecord | null;
26
+ readonly tooManyRecords: boolean;
27
+ /** The single matching record's image index is not below the image count. */
28
+ readonly imageOutOfRange: boolean;
29
+ /** The single matching record cannot be drawn: Replace above 1, or the image is out of range. */
30
+ readonly reservedFlags: boolean;
31
+ /** The shape can be drawn on this frame. */
32
+ readonly canDraw: boolean;
33
+ }
34
+ /** Evaluates the mouth rule for `shape` on a frame with `layerCount` image layers in a character with `imageCount` images. */
35
+ export declare function checkMouth(records: readonly MouthRecord[], shape: number, layerCount: number, imageCount?: number): MouthShapeCheck;
36
+ /** The refusal code of the mouth rule in its normative order (Conformance.md §7), or null when the shape is drawn. */
37
+ export declare function mouthRefusal(check: MouthShapeCheck): string | null;
@@ -0,0 +1,43 @@
1
+ /** The seven ACS mouth shapes, in record type order. */
2
+ export const MouthShape = { closed: 0, wide1: 1, wide2: 2, wide3: 3, wide4: 4, medium: 5, narrow: 6 };
3
+ /** Most mouth records a frame may carry. */
4
+ export const MAX_MOUTH_RECORDS = 7;
5
+ /** The image range assumed where the character's image count is not passed: indices that fit in 16 bits. */
6
+ export const SIXTEEN_BIT_IMAGE_RANGE = 0x10000;
7
+ /** Evaluates the mouth rule for `shape` on a frame with `layerCount` image layers in a character with `imageCount` images. */
8
+ export function checkMouth(records, shape, layerCount, imageCount = SIXTEEN_BIT_IMAGE_RANGE) {
9
+ let record = null;
10
+ let matches = 0;
11
+ for (const r of records)
12
+ if (r.shape === shape) {
13
+ record ??= r;
14
+ matches++;
15
+ }
16
+ const shapeDefined = Number.isInteger(shape) && shape >= 0 && shape <= 6;
17
+ const hasLayers = layerCount > 0;
18
+ const tooManyRecords = records.length > MAX_MOUTH_RECORDS;
19
+ const imageOutOfRange = matches === 1 && !(record.image < imageCount);
20
+ const reservedFlags = matches === 1 && !(record.replace <= 1 && record.image < imageCount);
21
+ return {
22
+ shapeDefined, hasLayers, recordCount: records.length, matches, record, tooManyRecords, imageOutOfRange, reservedFlags,
23
+ canDraw: shapeDefined && hasLayers && !tooManyRecords && matches === 1 && !reservedFlags,
24
+ };
25
+ }
26
+ /** The refusal code of the mouth rule in its normative order (Conformance.md §7), or null when the shape is drawn. */
27
+ export function mouthRefusal(check) {
28
+ if (!check.shapeDefined)
29
+ return 'mouth-shape';
30
+ if (!check.hasLayers)
31
+ return 'mouth-no-layers';
32
+ if (check.tooManyRecords)
33
+ return 'mouth-too-many-records';
34
+ if (check.matches === 0)
35
+ return 'mouth-shape-missing';
36
+ if (check.matches > 1)
37
+ return 'mouth-shape-duplicate';
38
+ if (check.imageOutOfRange)
39
+ return 'mouth-image-out-of-range';
40
+ if (check.reservedFlags)
41
+ return 'mouth-replace-flag';
42
+ return null;
43
+ }
@@ -0,0 +1,53 @@
1
+ /** Largest sound record (16 MiB). */
2
+ export declare const MAX_SOUND_BYTES: number;
3
+ /** Longest sound (60 s). */
4
+ export declare const MAX_SOUND_MS = 60000;
5
+ /** WAVE format tags of the codecs CharacterRuntime reads (ACS.md §9). */
6
+ export declare const WaveCodec: {
7
+ readonly pcm: 1;
8
+ readonly msAdpcm: 2;
9
+ readonly ieeeFloat: 3;
10
+ readonly aLaw: 6;
11
+ readonly muLaw: 7;
12
+ readonly imaAdpcm: 17;
13
+ readonly mpegLayer3: 85;
14
+ };
15
+ /**
16
+ * A decoded RIFF/WAVE sound. PCM is kept as stored; Microsoft ADPCM, IMA ADPCM, G.711 and IEEE float are decoded to 16-bit PCM;
17
+ * MPEG Layer-3 is validated by its frame headers and kept as its MPEG stream (decoding it is a platform service).
18
+ */
19
+ export declare class WaveAudio {
20
+ /** WAVE format tag of the source. */
21
+ readonly sourceCodec: number;
22
+ readonly channels: number;
23
+ readonly sampleRate: number;
24
+ /** Bits per sample of the playable PCM (8 or 16); 0 for an MPEG stream. */
25
+ readonly bitsPerSample: number;
26
+ readonly durationMs: number;
27
+ /** `pcm` (PCM samples available through {@link samples}) or `mpeg-layer3` (the MPEG stream in {@link mpegStream}). */
28
+ readonly playableFormat: 'pcm' | 'mpeg-layer3';
29
+ private readonly pcmData;
30
+ private readonly decoded;
31
+ private readonly mpeg;
32
+ /** Bytes the sound occupies in memory (cache accounting). */
33
+ readonly byteLength: number;
34
+ private constructor();
35
+ /** The playable samples as interleaved signed 16-bit PCM (8-bit PCM widened as (s − 128) × 256). Null for an MPEG stream. */
36
+ samples(): Int16Array | null;
37
+ /** The MPEG Layer-3 elementary stream (source data chunk), or null for PCM. */
38
+ get mpegStream(): Uint8Array | null;
39
+ /** Sample frames of the playable audio. */
40
+ get frames(): number;
41
+ /** A canonical 16-bit PCM RIFF/WAVE file of the playable samples (null for MPEG). */
42
+ toWav(): Uint8Array | null;
43
+ /** Reads a RIFF/WAVE record (ACS.md §9.1 check order). Throws {@link NotSupportedError} or {@link InvalidArgumentError}. */
44
+ static read(data: Uint8Array): WaveAudio;
45
+ }
46
+ /** A canonical 44-byte-header 16-bit PCM RIFF/WAVE file. */
47
+ export declare function pcm16Wave(samples: Int16Array, channels: number, rate: number): Uint8Array;
48
+ /** One µ-law code to 16-bit PCM. */
49
+ export declare function muLawToPcm(code: number): number;
50
+ /** One A-law code to 16-bit PCM. */
51
+ export declare function aLawToPcm(code: number): number;
52
+ /** One float sample to 16-bit PCM: x × 32768 rounded half to even, clipped; NaN → 0. */
53
+ export declare function floatToPcm(sample: number): number;