@beff/aula 0.4.2 → 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/README.md +1 -1
- 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/licenses/fonts-OFL-NotoSans.txt +94 -0
- 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";
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/latin-greek-cyrillic)
|
|
2
|
+
Copyright 2016 Google LLC. All Rights Reserved. (Noto Sans Symbols: the arrows)
|
|
3
|
+
|
|
4
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
5
|
+
This license is copied below, and is also available with a FAQ at:
|
|
6
|
+
https://openfontlicense.org
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
-----------------------------------------------------------
|
|
10
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
11
|
+
-----------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
PREAMBLE
|
|
14
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
15
|
+
development of collaborative font projects, to support the font creation
|
|
16
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
17
|
+
open framework in which fonts may be shared and improved in partnership
|
|
18
|
+
with others.
|
|
19
|
+
|
|
20
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
21
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
22
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
23
|
+
redistributed and/or sold with any software provided that any reserved
|
|
24
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
25
|
+
however, cannot be released under any other type of license. The
|
|
26
|
+
requirement for fonts to remain under this license does not apply
|
|
27
|
+
to any document created using the fonts or their derivatives.
|
|
28
|
+
|
|
29
|
+
DEFINITIONS
|
|
30
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
31
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
32
|
+
include source files, build scripts and documentation.
|
|
33
|
+
|
|
34
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
35
|
+
copyright statement(s).
|
|
36
|
+
|
|
37
|
+
"Original Version" refers to the collection of Font Software components as
|
|
38
|
+
distributed by the Copyright Holder(s).
|
|
39
|
+
|
|
40
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
41
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
42
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
43
|
+
new environment.
|
|
44
|
+
|
|
45
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
46
|
+
writer or other person who contributed to the Font Software.
|
|
47
|
+
|
|
48
|
+
PERMISSION & CONDITIONS
|
|
49
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
50
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
51
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
52
|
+
Software, subject to the following conditions:
|
|
53
|
+
|
|
54
|
+
1) Neither the Font Software nor any of its individual components,
|
|
55
|
+
in Original or Modified Versions, may be sold by itself.
|
|
56
|
+
|
|
57
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
58
|
+
redistributed and/or sold with any software, provided that each copy
|
|
59
|
+
contains the above copyright notice and this license. These can be
|
|
60
|
+
included either as stand-alone text files, human-readable headers or
|
|
61
|
+
in the appropriate machine-readable metadata fields within text or
|
|
62
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
63
|
+
|
|
64
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
65
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
66
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
67
|
+
presented to the users.
|
|
68
|
+
|
|
69
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
70
|
+
Software shall not be used to promote, endorse or advertise any
|
|
71
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
72
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
73
|
+
permission.
|
|
74
|
+
|
|
75
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
76
|
+
must be distributed entirely under this license, and must not be
|
|
77
|
+
distributed under any other license. The requirement for fonts to
|
|
78
|
+
remain under this license does not apply to any document created
|
|
79
|
+
using the Font Software.
|
|
80
|
+
|
|
81
|
+
TERMINATION
|
|
82
|
+
This license becomes null and void if any of the above conditions are
|
|
83
|
+
not met.
|
|
84
|
+
|
|
85
|
+
DISCLAIMER
|
|
86
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
87
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
88
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
89
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
90
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
91
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
92
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
93
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
94
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
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>;
|