@beff/aula 0.4.3 → 0.5.0
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/dist/engine.d.ts +31 -1
- package/dist/engine.js +81 -3
- package/dist/generated/aula.core5.wasm +0 -0
- package/dist/generated/aula.js +2495 -867
- package/dist/generated/interfaces/aula-engine-engine.d.ts +18 -0
- package/dist/generated/interfaces/aula-engine-types.d.ts +18 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/media.d.ts +33 -0
- package/dist/media.js +390 -0
- package/dist/run.d.ts +9 -3
- package/dist/run.js +23 -0
- package/dist/text.d.ts +2 -2
- package/package.json +1 -1
|
@@ -246,6 +246,7 @@ export type Diagnostic = import('./aula-engine-types.js').Diagnostic;
|
|
|
246
246
|
export type Location = import('./aula-engine-types.js').Location;
|
|
247
247
|
export type Note = import('./aula-engine-types.js').Note;
|
|
248
248
|
export type ChapterText = import('./aula-engine-types.js').ChapterText;
|
|
249
|
+
export type EmbedKind = import('./aula-engine-types.js').EmbedKind;
|
|
249
250
|
export interface Shown {
|
|
250
251
|
id: number,
|
|
251
252
|
place: Location,
|
|
@@ -545,6 +546,17 @@ export interface NarrationWait {
|
|
|
545
546
|
sample: number,
|
|
546
547
|
samples: number,
|
|
547
548
|
}
|
|
549
|
+
export interface ClipFile {
|
|
550
|
+
kind: EmbedKind,
|
|
551
|
+
seconds: number,
|
|
552
|
+
}
|
|
553
|
+
export interface PlayedEmbed {
|
|
554
|
+
path: string,
|
|
555
|
+
image?: string,
|
|
556
|
+
kind: EmbedKind,
|
|
557
|
+
start: number,
|
|
558
|
+
frames: number,
|
|
559
|
+
}
|
|
548
560
|
export type Pronunciation = PronunciationWords | PronunciationPhonemes;
|
|
549
561
|
export interface PronunciationWords {
|
|
550
562
|
tag: 'words',
|
|
@@ -601,6 +613,7 @@ export interface ParagraphFrames {
|
|
|
601
613
|
start: number,
|
|
602
614
|
moves: Uint32Array,
|
|
603
615
|
}
|
|
616
|
+
export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };
|
|
604
617
|
|
|
605
618
|
export class Animation implements Disposable {
|
|
606
619
|
/**
|
|
@@ -704,6 +717,10 @@ export class Chapter implements Disposable {
|
|
|
704
717
|
/**
|
|
705
718
|
* @throws {Error & { payload: Diagnostic }} The error of the result, as the `payload` of the thrown error.
|
|
706
719
|
*/
|
|
720
|
+
embed(place: Location, note: string, path: string, image: string | undefined, file: Result<ClipFile, string>): void;
|
|
721
|
+
/**
|
|
722
|
+
* @throws {Error & { payload: Diagnostic }} The error of the result, as the `payload` of the thrown error.
|
|
723
|
+
*/
|
|
707
724
|
open(place: Location, note: string, timing: string | undefined): void;
|
|
708
725
|
/**
|
|
709
726
|
* @throws {Error & { payload: Diagnostic }} The error of the result, as the `payload` of the thrown error.
|
|
@@ -783,6 +800,7 @@ export class Chapter implements Disposable {
|
|
|
783
800
|
screens(): Array<Screen>;
|
|
784
801
|
closed(): Uint32Array;
|
|
785
802
|
waits(): Array<NarrationWait>;
|
|
803
|
+
played(): Array<PlayedEmbed>;
|
|
786
804
|
narration(): Narration;
|
|
787
805
|
lottie(): string;
|
|
788
806
|
trace(): string;
|
|
@@ -47,11 +47,29 @@ export interface Move {
|
|
|
47
47
|
done: boolean,
|
|
48
48
|
say: number,
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* # Variants
|
|
52
|
+
*
|
|
53
|
+
* ## `"video"`
|
|
54
|
+
*
|
|
55
|
+
* ## `"sound"`
|
|
56
|
+
*/
|
|
57
|
+
export type EmbedKind = "video" | "sound";
|
|
58
|
+
export interface Embed {
|
|
59
|
+
place: Location,
|
|
60
|
+
note: string,
|
|
61
|
+
path: string,
|
|
62
|
+
image?: string,
|
|
63
|
+
kind: EmbedKind,
|
|
64
|
+
frames: number,
|
|
65
|
+
say: number,
|
|
66
|
+
}
|
|
50
67
|
export interface ParagraphText {
|
|
51
68
|
place?: Location,
|
|
52
69
|
notes: Array<Note>,
|
|
53
70
|
says: Array<Say>,
|
|
54
71
|
moves: Array<Move>,
|
|
72
|
+
embeds: Array<Embed>,
|
|
55
73
|
}
|
|
56
74
|
export interface ChapterText {
|
|
57
75
|
place?: Location,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * as m from "./manim.ts";
|
|
2
|
-
export { defineChapter, defineProject, isProjectDefinition, paragraph, pronounce, say, show, tagText, } from "./engine.ts";
|
|
2
|
+
export { defineChapter, defineProject, embed, isProjectDefinition, paragraph, pronounce, say, show, tagText, } from "./engine.ts";
|
|
3
3
|
export { checkChapter, checkLexicon, checkTitle, readChapter, renderChapter, renderScene } from "./run.ts";
|
|
4
|
-
export type { Card, ChapterDefinition, ChapterFields, ChapterRun, CheckedChapter, CheckedParagraph, Check, Finding, Heading, Notes, OpenMove, Leaf, Pronounce, Reading, SceneRun, Untimed, ProjectDefinition, ProjectFields, Instant, Row, SayRow, Screen, Shown, } from "./engine.ts";
|
|
4
|
+
export type { Card, ChapterDefinition, ChapterFields, ChapterRun, CheckedChapter, CheckedParagraph, Check, Finding, Heading, Notes, OpenMove, Leaf, Pronounce, Reading, SceneRun, Untimed, ProjectDefinition, ProjectFields, Instant, EmbedOptions, Row, SayRow, Screen, Shown, } from "./engine.ts";
|
|
5
5
|
export type { ChapterResult, Host, ReadOptions, RenderOptions, StackFrame } from "./run.ts";
|
|
6
6
|
export type { Lexicon } from "./lexicon.ts";
|
package/dist/index.js
CHANGED
|
@@ -7,5 +7,5 @@
|
|
|
7
7
|
// readChapter, checkChapter and renderChapter run a chapter, as the CLI and the web app do.
|
|
8
8
|
// renderScene runs a Manim construct, with no narration.
|
|
9
9
|
export * as m from "./manim.js";
|
|
10
|
-
export { defineChapter, defineProject, isProjectDefinition, paragraph, pronounce, say, show, tagText, } from "./engine.js";
|
|
10
|
+
export { defineChapter, defineProject, embed, isProjectDefinition, paragraph, pronounce, say, show, tagText, } from "./engine.js";
|
|
11
11
|
export { checkChapter, checkLexicon, checkTitle, readChapter, renderChapter, renderScene } from "./run.js";
|
package/dist/media.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The most bytes of one file of an `embed`. */
|
|
2
|
+
export declare const MAX_CLIP_BYTES: number;
|
|
3
|
+
/** What a file is: a video, a sound or a picture (the picture of a sound), its format and, for a
|
|
4
|
+
* video and a sound, its length in seconds. */
|
|
5
|
+
export type MediaInfo = {
|
|
6
|
+
readonly kind: "video";
|
|
7
|
+
readonly format: "mp4" | "webm";
|
|
8
|
+
readonly seconds: number;
|
|
9
|
+
} | {
|
|
10
|
+
readonly kind: "sound";
|
|
11
|
+
readonly format: "wav" | "mp3";
|
|
12
|
+
readonly seconds: number;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: "image";
|
|
15
|
+
readonly format: "png" | "jpeg";
|
|
16
|
+
};
|
|
17
|
+
/** A file of the project that the host can read: its size, and its bytes. */
|
|
18
|
+
export interface MediaSource {
|
|
19
|
+
readonly size: number;
|
|
20
|
+
read(): Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
/** The formats that an `embed` plays, for the messages. */
|
|
23
|
+
export declare const MEDIA_FORMATS = "MP4 (H.264, AAC) or WebM (VP8 or VP9, Opus or Vorbis) video; WAV (PCM) or MP3 sound; PNG or JPEG picture";
|
|
24
|
+
/** What the file `source` is, or why aula cannot play it (a problem that fits after the path of
|
|
25
|
+
* the file). The size is checked before the read. */
|
|
26
|
+
export declare function probeMedia(source: MediaSource): MediaInfo | string;
|
|
27
|
+
/** The path `given` of a file of the project, with no `.` and no `..` (from the project folder),
|
|
28
|
+
* or the problem with it: a URL, an absolute path, or a path outside the project folder. */
|
|
29
|
+
export declare function projectPath(given: string): {
|
|
30
|
+
path: string;
|
|
31
|
+
} | {
|
|
32
|
+
problem: string;
|
|
33
|
+
};
|
package/dist/media.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// The files that an `embed` plays: what a file is and how long it is, read from its header.
|
|
2
|
+
// `probeMedia` is pure: the host gives the bytes of the file (`Host.media`), the same for the
|
|
3
|
+
// CLI and the web app. The length is the one in the header, so every command gets the same
|
|
4
|
+
// number. Not here: decoding. The export and the browser play the file.
|
|
5
|
+
/** The most bytes of one file of an `embed`. */
|
|
6
|
+
export const MAX_CLIP_BYTES = 50 * 1024 * 1024;
|
|
7
|
+
/** The formats that an `embed` plays, for the messages. */
|
|
8
|
+
export const MEDIA_FORMATS = "MP4 (H.264, AAC) or WebM (VP8 or VP9, Opus or Vorbis) video; WAV (PCM) or MP3 sound; PNG or JPEG picture";
|
|
9
|
+
const ascii = (bytes, at, length) => String.fromCharCode(...bytes.subarray(at, at + length));
|
|
10
|
+
const view = (bytes) => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
11
|
+
const SIGNATURE_PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
12
|
+
/** The MB of `bytes`, for a message. */
|
|
13
|
+
const megabytes = (bytes) => (bytes / (1024 * 1024)).toFixed(1);
|
|
14
|
+
/** What the file `source` is, or why aula cannot play it (a problem that fits after the path of
|
|
15
|
+
* the file). The size is checked before the read. */
|
|
16
|
+
export function probeMedia(source) {
|
|
17
|
+
if (source.size > MAX_CLIP_BYTES) {
|
|
18
|
+
return `is ${megabytes(source.size)} MB, and the limit is ${megabytes(MAX_CLIP_BYTES)} MB`;
|
|
19
|
+
}
|
|
20
|
+
const bytes = source.read();
|
|
21
|
+
if (bytes.length >= 8 && SIGNATURE_PNG.every((b, i) => bytes[i] === b))
|
|
22
|
+
return { kind: "image", format: "png" };
|
|
23
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
|
|
24
|
+
return { kind: "image", format: "jpeg" };
|
|
25
|
+
}
|
|
26
|
+
if (bytes.length >= 12 && ascii(bytes, 0, 4) === "RIFF" && ascii(bytes, 8, 4) === "WAVE")
|
|
27
|
+
return probeWav(bytes);
|
|
28
|
+
if (bytes.length >= 12 && ascii(bytes, 4, 4) === "ftyp")
|
|
29
|
+
return probeMp4(bytes);
|
|
30
|
+
if (bytes.length >= 4 && bytes[0] === 0x1a && bytes[1] === 0x45 && bytes[2] === 0xdf && bytes[3] === 0xa3) {
|
|
31
|
+
return probeWebm(bytes);
|
|
32
|
+
}
|
|
33
|
+
const mp3 = probeMp3(bytes);
|
|
34
|
+
if (mp3 !== undefined)
|
|
35
|
+
return mp3;
|
|
36
|
+
return `has an unknown format: aula plays ${MEDIA_FORMATS}`;
|
|
37
|
+
}
|
|
38
|
+
/** A length in seconds that is a number above 0, or the problem of a header with no length. */
|
|
39
|
+
function lengthOf(seconds) {
|
|
40
|
+
return Number.isFinite(seconds) && seconds > 0 ? seconds : "has no length in its header";
|
|
41
|
+
}
|
|
42
|
+
// --- WAV ------------------------------------------------------------------------------------
|
|
43
|
+
function probeWav(bytes) {
|
|
44
|
+
const data = view(bytes);
|
|
45
|
+
let at = 12;
|
|
46
|
+
let byteRate = 0;
|
|
47
|
+
let format = -1;
|
|
48
|
+
while (at + 8 <= bytes.length) {
|
|
49
|
+
const id = ascii(bytes, at, 4);
|
|
50
|
+
const size = data.getUint32(at + 4, true);
|
|
51
|
+
const body = at + 8;
|
|
52
|
+
if (id === "fmt " && body + 16 <= bytes.length) {
|
|
53
|
+
format = data.getUint16(body, true);
|
|
54
|
+
// WAVE_FORMAT_EXTENSIBLE: the format is the first two bytes of its sub-format.
|
|
55
|
+
if (format === 0xfffe && size >= 26 && body + 26 <= bytes.length)
|
|
56
|
+
format = data.getUint16(body + 24, true);
|
|
57
|
+
byteRate = data.getUint32(body + 8, true);
|
|
58
|
+
}
|
|
59
|
+
else if (id === "data") {
|
|
60
|
+
if (format !== 1)
|
|
61
|
+
return `is a WAV file that is not PCM (format ${String(format)})`;
|
|
62
|
+
// A size that is 0 or past the end of the file (a stream) is the rest of the file.
|
|
63
|
+
const length = size === 0 || size > bytes.length - body ? bytes.length - body : size;
|
|
64
|
+
const seconds = lengthOf(byteRate > 0 ? length / byteRate : 0);
|
|
65
|
+
return typeof seconds === "string" ? seconds : { kind: "sound", format: "wav", seconds };
|
|
66
|
+
}
|
|
67
|
+
at = body + size + (size % 2);
|
|
68
|
+
}
|
|
69
|
+
return format === -1 ? "has a WAV header with no fmt chunk" : "has a WAV header with no data chunk";
|
|
70
|
+
}
|
|
71
|
+
// --- MP3 ------------------------------------------------------------------------------------
|
|
72
|
+
const MP3_BITRATES = {
|
|
73
|
+
// MPEG 1, layer III.
|
|
74
|
+
1: [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320],
|
|
75
|
+
// MPEG 2 and 2.5, layer III.
|
|
76
|
+
2: [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
|
|
77
|
+
};
|
|
78
|
+
const MP3_RATES = [44100, 48000, 32000];
|
|
79
|
+
/** The layer III frame header at `at`, or undefined. */
|
|
80
|
+
function mp3Frame(bytes, at) {
|
|
81
|
+
const [b0, b1, b2] = [bytes[at], bytes[at + 1], bytes[at + 2]];
|
|
82
|
+
if (b0 !== 0xff || b1 === undefined || b2 === undefined || (b1 & 0xe0) !== 0xe0)
|
|
83
|
+
return undefined;
|
|
84
|
+
const version = (b1 >> 3) & 3; // 0: 2.5, 2: 2, 3: 1
|
|
85
|
+
const layer = (b1 >> 1) & 3; // 1: III
|
|
86
|
+
if (version === 1 || layer !== 1)
|
|
87
|
+
return undefined;
|
|
88
|
+
const bitrateIndex = b2 >> 4;
|
|
89
|
+
const rateIndex = (b2 >> 2) & 3;
|
|
90
|
+
if (bitrateIndex === 0 || bitrateIndex === 15 || rateIndex === 3)
|
|
91
|
+
return undefined;
|
|
92
|
+
const mpeg1 = version === 3;
|
|
93
|
+
const bitrate = MP3_BITRATES[mpeg1 ? 1 : 2][bitrateIndex] ?? 0;
|
|
94
|
+
const rate = (MP3_RATES[rateIndex] ?? 0) / (version === 3 ? 1 : version === 2 ? 2 : 4);
|
|
95
|
+
const padding = (b2 >> 1) & 1;
|
|
96
|
+
const samples = mpeg1 ? 1152 : 576;
|
|
97
|
+
const length = Math.floor(((mpeg1 ? 144000 : 72000) * bitrate) / rate) + padding;
|
|
98
|
+
return length > 4 ? { length, samples, rate } : undefined;
|
|
99
|
+
}
|
|
100
|
+
/** An MP3 file (undefined when the bytes are not one): the sum of the samples of its frames. */
|
|
101
|
+
function probeMp3(bytes) {
|
|
102
|
+
let at = 0;
|
|
103
|
+
if (bytes.length >= 10 && ascii(bytes, 0, 3) === "ID3") {
|
|
104
|
+
const size = ((bytes[6] ?? 0) << 21) | ((bytes[7] ?? 0) << 14) | ((bytes[8] ?? 0) << 7) | (bytes[9] ?? 0);
|
|
105
|
+
at = 10 + size;
|
|
106
|
+
}
|
|
107
|
+
// The first frame must start at once (after the tag): a file with another format has no frames.
|
|
108
|
+
let first = mp3Frame(bytes, at);
|
|
109
|
+
for (let skip = 0; first === undefined && skip < 4096 && at < bytes.length; skip++)
|
|
110
|
+
first = mp3Frame(bytes, ++at);
|
|
111
|
+
if (first === undefined)
|
|
112
|
+
return undefined;
|
|
113
|
+
const firstAt = at;
|
|
114
|
+
const next = mp3Frame(bytes, at + first.length);
|
|
115
|
+
if (next === undefined && at + first.length < bytes.length - 128)
|
|
116
|
+
return undefined;
|
|
117
|
+
let samples = 0;
|
|
118
|
+
let frames = 0;
|
|
119
|
+
while (at < bytes.length) {
|
|
120
|
+
const frame = mp3Frame(bytes, at);
|
|
121
|
+
if (frame === undefined)
|
|
122
|
+
break;
|
|
123
|
+
if (at + frame.length > bytes.length + 1)
|
|
124
|
+
break;
|
|
125
|
+
frames += 1;
|
|
126
|
+
samples += frame.samples;
|
|
127
|
+
at += frame.length;
|
|
128
|
+
}
|
|
129
|
+
// A Xing or Info frame has no sound: it is the first frame, and it is not in the length.
|
|
130
|
+
const tag = /Xing|Info/.test(ascii(bytes, firstAt + 4, 40));
|
|
131
|
+
const seconds = lengthOf((samples - (tag && frames > 1 ? first.samples : 0)) / first.rate);
|
|
132
|
+
return typeof seconds === "string" ? seconds : { kind: "sound", format: "mp3", seconds };
|
|
133
|
+
}
|
|
134
|
+
/** The boxes of `bytes` from `from` to `to`. A box that does not fit stops the list. */
|
|
135
|
+
function boxes(bytes, from, to) {
|
|
136
|
+
const data = view(bytes);
|
|
137
|
+
const out = [];
|
|
138
|
+
let at = from;
|
|
139
|
+
while (at + 8 <= to) {
|
|
140
|
+
let size = data.getUint32(at, false);
|
|
141
|
+
const type = ascii(bytes, at + 4, 4);
|
|
142
|
+
let head = 8;
|
|
143
|
+
if (size === 1) {
|
|
144
|
+
if (at + 16 > to)
|
|
145
|
+
break;
|
|
146
|
+
size = Number(data.getBigUint64(at + 8, false));
|
|
147
|
+
head = 16;
|
|
148
|
+
}
|
|
149
|
+
else if (size === 0) {
|
|
150
|
+
size = to - at;
|
|
151
|
+
}
|
|
152
|
+
if (size < head || at + size > to)
|
|
153
|
+
break;
|
|
154
|
+
out.push({ type, start: at + head, end: at + size });
|
|
155
|
+
at += size;
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
const childOf = (bytes, box, type, skip = 0) => box === undefined ? undefined : boxes(bytes, box.start + skip, box.end).find((b) => b.type === type);
|
|
160
|
+
/** The codec of the AAC description of an `mp4a` entry (`esds`): its object type, or undefined. */
|
|
161
|
+
function aacObjectType(bytes, entry) {
|
|
162
|
+
// An audio sample entry has 28 bytes before its boxes.
|
|
163
|
+
const esds = childOf(bytes, entry, "esds", 28);
|
|
164
|
+
if (esds === undefined)
|
|
165
|
+
return undefined;
|
|
166
|
+
// version and flags (4), then descriptors: ES (tag 3), then DecoderConfig (tag 4).
|
|
167
|
+
let at = esds.start + 4;
|
|
168
|
+
const length = () => {
|
|
169
|
+
let size = 0;
|
|
170
|
+
for (let i = 0; i < 4 && at < esds.end; i++) {
|
|
171
|
+
const b = bytes[at++] ?? 0;
|
|
172
|
+
size = (size << 7) | (b & 0x7f);
|
|
173
|
+
if ((b & 0x80) === 0)
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
return size;
|
|
177
|
+
};
|
|
178
|
+
if (bytes[at++] !== 3)
|
|
179
|
+
return undefined;
|
|
180
|
+
length();
|
|
181
|
+
at += 2;
|
|
182
|
+
const flags = bytes[at++] ?? 0;
|
|
183
|
+
if (flags & 0x80)
|
|
184
|
+
at += 2;
|
|
185
|
+
if (flags & 0x40)
|
|
186
|
+
at += 1 + (bytes[at] ?? 0);
|
|
187
|
+
if (flags & 0x20)
|
|
188
|
+
at += 2;
|
|
189
|
+
if (bytes[at++] !== 4)
|
|
190
|
+
return undefined;
|
|
191
|
+
length();
|
|
192
|
+
return bytes[at];
|
|
193
|
+
}
|
|
194
|
+
function probeMp4(bytes) {
|
|
195
|
+
const data = view(bytes);
|
|
196
|
+
const moov = boxes(bytes, 0, bytes.length).find((b) => b.type === "moov");
|
|
197
|
+
if (moov === undefined)
|
|
198
|
+
return "has an MP4 header with no movie box (moov): it may be cut, or not ready for streaming";
|
|
199
|
+
const mvhd = childOf(bytes, moov, "mvhd");
|
|
200
|
+
let seconds = 0;
|
|
201
|
+
if (mvhd !== undefined) {
|
|
202
|
+
const version = bytes[mvhd.start] ?? 0;
|
|
203
|
+
const at = mvhd.start + 4 + (version === 1 ? 16 : 8);
|
|
204
|
+
if (at + (version === 1 ? 12 : 8) <= mvhd.end) {
|
|
205
|
+
const scale = data.getUint32(at, false);
|
|
206
|
+
const duration = version === 1 ? Number(data.getBigUint64(at + 4, false)) : data.getUint32(at + 4, false);
|
|
207
|
+
seconds = scale > 0 ? duration / scale : 0;
|
|
208
|
+
}
|
|
209
|
+
if (seconds === 0) {
|
|
210
|
+
// A fragmented file has its length in `mvex/mehd`.
|
|
211
|
+
const mehd = childOf(bytes, childOf(bytes, moov, "mvex"), "mehd");
|
|
212
|
+
if (mehd !== undefined && scaleOf(data, mvhd) > 0) {
|
|
213
|
+
const wide = (bytes[mehd.start] ?? 0) === 1;
|
|
214
|
+
const fragment = wide
|
|
215
|
+
? Number(data.getBigUint64(mehd.start + 4, false))
|
|
216
|
+
: data.getUint32(mehd.start + 4, false);
|
|
217
|
+
seconds = fragment / scaleOf(data, mvhd);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
let video;
|
|
222
|
+
let audio;
|
|
223
|
+
for (const trak of boxes(bytes, moov.start, moov.end).filter((b) => b.type === "trak")) {
|
|
224
|
+
const mdia = childOf(bytes, trak, "mdia");
|
|
225
|
+
const hdlr = childOf(bytes, mdia, "hdlr");
|
|
226
|
+
const handler = hdlr === undefined ? "" : ascii(bytes, hdlr.start + 8, 4);
|
|
227
|
+
const stsd = childOf(bytes, childOf(bytes, childOf(bytes, mdia, "minf"), "stbl"), "stsd");
|
|
228
|
+
// version and flags (4), the number of entries (4), then the first entry.
|
|
229
|
+
const entry = stsd === undefined ? undefined : boxes(bytes, stsd.start + 8, stsd.end)[0];
|
|
230
|
+
if (entry === undefined)
|
|
231
|
+
continue;
|
|
232
|
+
if (handler === "vide")
|
|
233
|
+
video ??= entry.type;
|
|
234
|
+
if (handler === "soun") {
|
|
235
|
+
audio ??= entry.type;
|
|
236
|
+
if (entry.type === "mp4a") {
|
|
237
|
+
const type = aacObjectType(bytes, entry);
|
|
238
|
+
if (type !== undefined && ![0x40, 0x66, 0x67, 0x68].includes(type)) {
|
|
239
|
+
audio = `mp4a with object type 0x${type.toString(16)}`;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (video === undefined)
|
|
245
|
+
return "has no video track: a sound is a WAV or MP3 file";
|
|
246
|
+
if (video !== "avc1" && video !== "avc3")
|
|
247
|
+
return `has the video codec ${video}, and aula plays H.264 (avc1) in MP4`;
|
|
248
|
+
if (audio !== undefined && audio !== "mp4a")
|
|
249
|
+
return `has the audio codec ${audio}, and aula plays AAC (mp4a) in MP4`;
|
|
250
|
+
const length = lengthOf(seconds);
|
|
251
|
+
return typeof length === "string" ? length : { kind: "video", format: "mp4", seconds: length };
|
|
252
|
+
}
|
|
253
|
+
const scaleOf = (data, mvhd) => data.getUint32(mvhd.start + 4 + (data.getUint8(mvhd.start) === 1 ? 16 : 8), false);
|
|
254
|
+
/** The element at `at`, or undefined at the end of the bytes or at a wrong id or size. */
|
|
255
|
+
function element(bytes, at) {
|
|
256
|
+
const first = bytes[at];
|
|
257
|
+
if (first === undefined || first === 0)
|
|
258
|
+
return undefined;
|
|
259
|
+
const idLength = Math.clz32(first) - 24 + 1;
|
|
260
|
+
if (idLength > 4 || at + idLength >= bytes.length)
|
|
261
|
+
return undefined;
|
|
262
|
+
let id = 0;
|
|
263
|
+
for (let i = 0; i < idLength; i++)
|
|
264
|
+
id = id * 256 + (bytes[at + i] ?? 0);
|
|
265
|
+
const sizeAt = at + idLength;
|
|
266
|
+
const sizeFirst = bytes[sizeAt];
|
|
267
|
+
if (sizeFirst === undefined || sizeFirst === 0)
|
|
268
|
+
return undefined;
|
|
269
|
+
const sizeLength = Math.clz32(sizeFirst) - 24 + 1;
|
|
270
|
+
if (sizeLength > 8 || sizeAt + sizeLength > bytes.length)
|
|
271
|
+
return undefined;
|
|
272
|
+
let size = sizeFirst & (0xff >> sizeLength);
|
|
273
|
+
let unknown = size === 0xff >> sizeLength;
|
|
274
|
+
for (let i = 1; i < sizeLength; i++) {
|
|
275
|
+
const b = bytes[sizeAt + i] ?? 0;
|
|
276
|
+
size = size * 256 + b;
|
|
277
|
+
if (b !== 0xff)
|
|
278
|
+
unknown = false;
|
|
279
|
+
}
|
|
280
|
+
const start = sizeAt + sizeLength;
|
|
281
|
+
return { id, start, end: unknown ? Infinity : start + size };
|
|
282
|
+
}
|
|
283
|
+
/** The children of the element body `[from, to)`, up to the first cluster. */
|
|
284
|
+
function children(bytes, from, to) {
|
|
285
|
+
const out = [];
|
|
286
|
+
let at = from;
|
|
287
|
+
while (at < to && at < bytes.length) {
|
|
288
|
+
const e = element(bytes, at);
|
|
289
|
+
if (e === undefined)
|
|
290
|
+
break;
|
|
291
|
+
out.push(e);
|
|
292
|
+
if (e.end === Infinity)
|
|
293
|
+
break;
|
|
294
|
+
at = e.end;
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
const WEBM = {
|
|
299
|
+
segment: 0x18538067,
|
|
300
|
+
info: 0x1549a966,
|
|
301
|
+
tracks: 0x1654ae6b,
|
|
302
|
+
entry: 0xae,
|
|
303
|
+
type: 0x83,
|
|
304
|
+
codec: 0x86,
|
|
305
|
+
scale: 0x2ad7b1,
|
|
306
|
+
duration: 0x4489,
|
|
307
|
+
docType: 0x4282,
|
|
308
|
+
header: 0x1a45dfa3,
|
|
309
|
+
};
|
|
310
|
+
function probeWebm(bytes) {
|
|
311
|
+
const data = view(bytes);
|
|
312
|
+
const top = children(bytes, 0, bytes.length);
|
|
313
|
+
const head = top.find((e) => e.id === WEBM.header);
|
|
314
|
+
const docType = head && children(bytes, head.start, head.end).find((e) => e.id === WEBM.docType);
|
|
315
|
+
if (docType === undefined || ascii(bytes, docType.start, docType.end - docType.start) !== "webm") {
|
|
316
|
+
return `has the EBML header of a file that is not WebM: aula plays ${MEDIA_FORMATS}`;
|
|
317
|
+
}
|
|
318
|
+
const segment = top.find((e) => e.id === WEBM.segment);
|
|
319
|
+
if (segment === undefined)
|
|
320
|
+
return "has no segment: a WebM file is cut";
|
|
321
|
+
const parts = children(bytes, segment.start, Math.min(segment.end, bytes.length));
|
|
322
|
+
const info = parts.find((e) => e.id === WEBM.info);
|
|
323
|
+
const tracks = parts.find((e) => e.id === WEBM.tracks);
|
|
324
|
+
if (info === undefined || tracks === undefined)
|
|
325
|
+
return "has no length in its header";
|
|
326
|
+
const fields = children(bytes, info.start, info.end);
|
|
327
|
+
const scale = fields.find((e) => e.id === WEBM.scale);
|
|
328
|
+
let timecode = 1_000_000;
|
|
329
|
+
if (scale !== undefined) {
|
|
330
|
+
timecode = 0;
|
|
331
|
+
for (let i = scale.start; i < scale.end; i++)
|
|
332
|
+
timecode = timecode * 256 + (bytes[i] ?? 0);
|
|
333
|
+
}
|
|
334
|
+
const long = fields.find((e) => e.id === WEBM.duration);
|
|
335
|
+
let seconds = 0;
|
|
336
|
+
if (long !== undefined) {
|
|
337
|
+
const size = long.end - long.start;
|
|
338
|
+
if (size === 4)
|
|
339
|
+
seconds = (data.getFloat32(long.start, false) * timecode) / 1e9;
|
|
340
|
+
if (size === 8)
|
|
341
|
+
seconds = (data.getFloat64(long.start, false) * timecode) / 1e9;
|
|
342
|
+
}
|
|
343
|
+
let video = false;
|
|
344
|
+
for (const entry of children(bytes, tracks.start, tracks.end).filter((e) => e.id === WEBM.entry)) {
|
|
345
|
+
const fieldsOf = children(bytes, entry.start, entry.end);
|
|
346
|
+
const type = fieldsOf.find((e) => e.id === WEBM.type);
|
|
347
|
+
const codec = fieldsOf.find((e) => e.id === WEBM.codec);
|
|
348
|
+
const name = codec === undefined ? "" : ascii(bytes, codec.start, codec.end - codec.start);
|
|
349
|
+
if (type !== undefined && bytes[type.start] === 1) {
|
|
350
|
+
video = true;
|
|
351
|
+
if (name !== "V_VP8" && name !== "V_VP9")
|
|
352
|
+
return `has the video codec ${name}, and aula plays VP8 or VP9 in WebM`;
|
|
353
|
+
}
|
|
354
|
+
else if (type !== undefined && bytes[type.start] === 2) {
|
|
355
|
+
if (name !== "A_OPUS" && name !== "A_VORBIS") {
|
|
356
|
+
return `has the audio codec ${name}, and aula plays Opus or Vorbis in WebM`;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (!video)
|
|
361
|
+
return "has no video track: a sound is a WAV or MP3 file";
|
|
362
|
+
const length = lengthOf(seconds);
|
|
363
|
+
return typeof length === "string" ? length : { kind: "video", format: "webm", seconds: length };
|
|
364
|
+
}
|
|
365
|
+
// --- paths ----------------------------------------------------------------------------------
|
|
366
|
+
/** The path `given` of a file of the project, with no `.` and no `..` (from the project folder),
|
|
367
|
+
* or the problem with it: a URL, an absolute path, or a path outside the project folder. */
|
|
368
|
+
export function projectPath(given) {
|
|
369
|
+
if (given.trim() === "")
|
|
370
|
+
return { problem: "has an empty path" };
|
|
371
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(given)) {
|
|
372
|
+
return { problem: `is a URL (${JSON.stringify(given)}): aula plays files of the project folder` };
|
|
373
|
+
}
|
|
374
|
+
if (given.startsWith("/") || given.includes("\\")) {
|
|
375
|
+
return { problem: `is not a path from the project folder with "/" (${JSON.stringify(given)})` };
|
|
376
|
+
}
|
|
377
|
+
const parts = [];
|
|
378
|
+
for (const part of given.split("/")) {
|
|
379
|
+
if (part === "" || part === ".")
|
|
380
|
+
continue;
|
|
381
|
+
if (part === "..") {
|
|
382
|
+
if (parts.pop() === undefined)
|
|
383
|
+
return { problem: `is outside the project folder (${JSON.stringify(given)})` };
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
parts.push(part);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return parts.length === 0 ? { problem: "has an empty path" } : { path: parts.join("/") };
|
|
390
|
+
}
|
package/dist/run.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { ChapterRun, ProjectDefinition, StackFrame } from "./engine.ts";
|
|
2
2
|
import type { Diagnostic, Location, ChapterText, Narration } from "./text.ts";
|
|
3
|
+
import { type MediaSource } from "./media.ts";
|
|
3
4
|
export { importUserModule } from "./user-code.ts";
|
|
4
5
|
export { renderScene } from "./engine.ts";
|
|
5
6
|
export type { Leaf, SceneRun } from "./engine.ts";
|
|
6
7
|
export type { StackFrame } from "./engine.ts";
|
|
8
|
+
export { MAX_CLIP_BYTES, MEDIA_FORMATS, probeMedia } from "./media.ts";
|
|
9
|
+
export type { MediaInfo, MediaSource } from "./media.ts";
|
|
7
10
|
/** How a host reads stack frames and names their files. */
|
|
8
11
|
export interface Host {
|
|
9
12
|
/** The frames of an error: of a call that the core keeps, or of a thrown error. The first
|
|
@@ -16,6 +19,9 @@ export interface Host {
|
|
|
16
19
|
* file. The paragraph checks get it (`CheckedChapter.exists`). The CLI reads the file
|
|
17
20
|
* system; the web app has no files, and gives true. */
|
|
18
21
|
exists(path: string): boolean;
|
|
22
|
+
/** The file at `path` (from the working directory, as `exists`), or undefined when there is
|
|
23
|
+
* none: what an `embed` reads in its header (`probeMedia`). A host with no files gives none. */
|
|
24
|
+
media?(path: string): MediaSource | undefined;
|
|
19
25
|
}
|
|
20
26
|
/** Frames per second of the Lottie files: the engine's rate (`engine.fps()`, which
|
|
21
27
|
* `constants.test.ts` checks), as a value here: an import does not load the engine. */
|
|
@@ -59,12 +65,12 @@ export interface ReadOptions {
|
|
|
59
65
|
}
|
|
60
66
|
/** Reads chapter `index` (from 1) of `project`: its text and its text problems, with no move
|
|
61
67
|
* run. The paragraph checks run only when the chapter is checked (`checkChapter`). */
|
|
62
|
-
export declare function readChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon">, index: number, options?: ReadOptions): ChapterResult<ChapterText>;
|
|
68
|
+
export declare function readChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon" | "call">, index: number, options?: ReadOptions): ChapterResult<ChapterText>;
|
|
63
69
|
/** Checks chapter `index` (0 is the title card) of `project`, as `aula check` does: it runs
|
|
64
70
|
* the moves in list order with no clock and no narration, and gives the text problems, the
|
|
65
71
|
* engine's checks of the picture after each animation, the kit findings and the paragraph
|
|
66
72
|
* checks. With sync by construction, no check depends on the length of a `say`. */
|
|
67
|
-
export declare function checkChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon" | "theme" | "card">, index: number, options?: ReadOptions): ChapterResult<{
|
|
73
|
+
export declare function checkChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon" | "theme" | "card" | "call">, index: number, options?: ReadOptions): ChapterResult<{
|
|
68
74
|
diagnostics: Diagnostic[];
|
|
69
75
|
}>;
|
|
70
76
|
/** The problems of the project lexicon (`defineProject({ lexicon })`), at the `defineProject`
|
|
@@ -89,4 +95,4 @@ export interface RenderOptions extends ReadOptions {
|
|
|
89
95
|
/** Draws chapter `index` (0 is the title card) of `project` on its narration: its Lottie, its
|
|
90
96
|
* length in frames, the waits of the narration and the warnings at the moves that the
|
|
91
97
|
* narration waits for. */
|
|
92
|
-
export declare function renderChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon" | "theme" | "card">, index: number, options: RenderOptions): ChapterResult<ChapterRun>;
|
|
98
|
+
export declare function renderChapter(project: Pick<ProjectDefinition, "title" | "chapters" | "lexicon" | "theme" | "card" | "call">, index: number, options: RenderOptions): ChapterResult<ChapterRun>;
|
package/dist/run.js
CHANGED
|
@@ -6,8 +6,10 @@ import { engine, isComponentError } from "./load.js";
|
|
|
6
6
|
import { hasPending, takePending } from "./host.js";
|
|
7
7
|
import { runUserCode } from "./user-code.js";
|
|
8
8
|
import { badReadings, lexiconEntries } from "./lexicon.js";
|
|
9
|
+
import { probeMedia, projectPath } from "./media.js";
|
|
9
10
|
export { importUserModule } from "./user-code.js";
|
|
10
11
|
export { renderScene } from "./engine.js";
|
|
12
|
+
export { MAX_CLIP_BYTES, MEDIA_FORMATS, probeMedia } from "./media.js";
|
|
11
13
|
/** Frames per second of the Lottie files: the engine's rate (`engine.fps()`, which
|
|
12
14
|
* `constants.test.ts` checks), as a value here: an import does not load the engine. */
|
|
13
15
|
export const FPS = 15;
|
|
@@ -184,6 +186,26 @@ function chapterOf(project, index) {
|
|
|
184
186
|
return (project.chapters[index - 1] ??
|
|
185
187
|
error(`the project has chapters 1 to ${String(project.chapters.length)}; there is no chapter ${String(index)}`));
|
|
186
188
|
}
|
|
189
|
+
/** The folder of the project, as the host names it (`.` for the working directory): where the
|
|
190
|
+
* path of an `embed` starts. */
|
|
191
|
+
function projectFolder(project, host) {
|
|
192
|
+
const file = definedAt(project, host).file;
|
|
193
|
+
const name = host.name(file) ?? file;
|
|
194
|
+
const slash = name.lastIndexOf("/");
|
|
195
|
+
return slash === -1 ? "." : name.slice(0, slash);
|
|
196
|
+
}
|
|
197
|
+
/** The file `given` of an `embed` (a path from the project folder): what it is, or why it
|
|
198
|
+
* cannot play. */
|
|
199
|
+
function clipOf(host, folder, given) {
|
|
200
|
+
const found = projectPath(given);
|
|
201
|
+
if ("problem" in found)
|
|
202
|
+
return `embed: ${JSON.stringify(given)} ${found.problem}`;
|
|
203
|
+
const source = host.media?.(folder === "." ? found.path : `${folder}/${found.path}`);
|
|
204
|
+
if (source === undefined)
|
|
205
|
+
return `embed: no file ${JSON.stringify(given)} in the project folder`;
|
|
206
|
+
const probed = probeMedia(source);
|
|
207
|
+
return typeof probed === "string" ? `embed: ${JSON.stringify(given)} ${probed}` : probed;
|
|
208
|
+
}
|
|
187
209
|
/** The slot of chapter `index` (from 1) of `project`. */
|
|
188
210
|
function slotOf(project, chapter, index, host) {
|
|
189
211
|
const source = definedAt(chapter, host).file;
|
|
@@ -194,6 +216,7 @@ function slotOf(project, chapter, index, host) {
|
|
|
194
216
|
file: host.name(source) ?? source,
|
|
195
217
|
frames: (e) => host.frames(e),
|
|
196
218
|
lexicon: lexiconEntries(project.lexicon),
|
|
219
|
+
clip: (given) => clipOf(host, projectFolder(project, host), given),
|
|
197
220
|
};
|
|
198
221
|
}
|
|
199
222
|
/** Reads chapter `index` (from 1) of `project`: its text and its text problems, with no move
|
package/dist/text.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { type Lexicon } from "./lexicon.ts";
|
|
|
2
2
|
import type { Diagnostic, Location, ChapterText } from "./generated/interfaces/aula-engine-types.js";
|
|
3
3
|
import type { Narration, ParagraphFrames, SayPart, ScriptCue, SpokenCue, UnclearWord } from "./generated/interfaces/aula-engine-engine.js";
|
|
4
4
|
export type { Diagnostic, Location, ChapterText };
|
|
5
|
-
export type { Frame, Move, ParagraphText, Related, Say, Severity } from "./generated/interfaces/aula-engine-types.js";
|
|
6
|
-
export type { CueKind, Narration, NarrationCue, NarrationWait, NarrationWord, ParagraphFrames, SayPart, ScriptCue, SpokenCue, UnclearKind, UnclearWord, } from "./generated/interfaces/aula-engine-engine.js";
|
|
5
|
+
export type { Embed, EmbedKind, Frame, Move, ParagraphText, Related, Say, Severity, } from "./generated/interfaces/aula-engine-types.js";
|
|
6
|
+
export type { CueKind, Narration, NarrationCue, NarrationWait, NarrationWord, ParagraphFrames, PlayedEmbed, SayPart, ScriptCue, SpokenCue, UnclearKind, UnclearWord, } from "./generated/interfaces/aula-engine-engine.js";
|
|
7
7
|
export type { Theme } from "./theme.ts";
|
|
8
8
|
export type { Lexicon } from "./lexicon.ts";
|
|
9
9
|
/** `# <title>` and each chapter's Markdown, with the chapter line of each heading and paragraph:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beff/aula",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The aula engine for narrated explainer videos: the Manim CE API, defineChapter, its rows, readChapter and renderChapter over a Rust WebAssembly component. Chapters and kits import it; the aula command is @beff/aula-cli.",
|
|
6
6
|
"license": "MIT AND GPL-3.0-or-later",
|