@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 CHANGED
@@ -1,6 +1,7 @@
1
- import type { Animation as RawAnimation, Mobject as RawMobject, Rate as RawRate, Chapter as RawChapter, Narration, NarrationWait, Screen, LexiconEntry, SayPart } from "./generated/interfaces/aula-engine-engine.js";
1
+ import type { Animation as RawAnimation, Mobject as RawMobject, Rate as RawRate, Chapter as RawChapter, Narration, NarrationWait, Screen, LexiconEntry, SayPart, PlayedEmbed } from "./generated/interfaces/aula-engine-engine.js";
2
2
  import type { Color, Diagnostic, Location, Point, ChapterText as RawChapterText, ParagraphText as RawParagraphText } from "./generated/interfaces/aula-engine-types.js";
3
3
  import type { Lexicon } from "./lexicon.ts";
4
+ import type { MediaInfo } from "./media.ts";
4
5
  import type { Theme } from "./theme.ts";
5
6
  import { type CapStyleType, type Complex, type LineJointType } from "./utils.ts";
6
7
  export type { Instant, Screen, Shown } from "./generated/interfaces/aula-engine-engine.js";
@@ -1308,6 +1309,13 @@ type RowData = {
1308
1309
  readonly kind: "say";
1309
1310
  readonly call: Call;
1310
1311
  readonly parts: readonly SayPart[];
1312
+ } | {
1313
+ readonly kind: "embed";
1314
+ readonly call: Call;
1315
+ readonly note: string;
1316
+ /** The path of the file, as written. */
1317
+ readonly path: string;
1318
+ readonly image: string | undefined;
1311
1319
  } | {
1312
1320
  readonly kind: "show";
1313
1321
  readonly call: Call;
@@ -1457,6 +1465,22 @@ export declare function say(strings: TemplateStringsArray, ...words: readonly Pr
1457
1465
  * proportion to their Manim lengths, each at least 0.5 seconds. The longer one wins: the picture holds until the `say`
1458
1466
  * ends, or the narration waits until the move ends. */
1459
1467
  export declare function show(strings: TemplateStringsArray): Untimed;
1468
+ /** The options of ``embed`note`(path, options)``. */
1469
+ export interface EmbedOptions {
1470
+ /** The picture that stays on screen while a sound plays: a PNG or JPEG file of the project
1471
+ * folder. Without it, the picture before the clip stays. */
1472
+ readonly image?: string;
1473
+ }
1474
+ /** ``embed`note`(path)``: a prepared clip that plays between two `say` rows, while the
1475
+ * narration is silent. The note says what the clip shows. The `path` is a file of the project
1476
+ * folder: an MP4 (H.264, AAC) or WebM (VP8 or VP9, Opus or Vorbis) video, which fills the
1477
+ * picture in 16:9 on the theme background, or a WAV (PCM) or MP3 sound, which plays over the
1478
+ * picture that stays (or over `{ image }`, a PNG or JPEG file). The clip plays after the `say`
1479
+ * above it, and the next `say` starts 0.25 s after it ends: the narration waits for it, and no
1480
+ * move runs. Write it after a `say` that ends a sentence. `aula check` gives an error at the row
1481
+ * for a file that is missing or too big (50 MB), with a format that aula does not play or a
1482
+ * header with no length. */
1483
+ export declare function embed(strings: TemplateStringsArray): (path: string, options?: EmbedOptions) => Row;
1460
1484
  /** The heading cue that a card draws: the input of the hook of the chapter card. */
1461
1485
  export interface Heading {
1462
1486
  /** The heading of the chapter, or the project title on the title card. */
@@ -1573,6 +1597,9 @@ export interface ChapterSlot {
1573
1597
  frames(error: Error): readonly StackFrame[];
1574
1598
  /** The project lexicon in the engine's form. */
1575
1599
  lexicon: readonly LexiconEntry[];
1600
+ /** What the file `path` of an `embed` is (a path from the project folder), or why it cannot
1601
+ * play: the message of the error. */
1602
+ clip(path: string): MediaInfo | string;
1576
1603
  }
1577
1604
  /** Reads a chapter: calls `rows` and no move. It gives the text and the text problems; the
1578
1605
  * paragraph checks run only in `drawRows`. A chapter error is thrown (a AulaError, a WIT
@@ -1615,6 +1642,9 @@ export interface ChapterRun {
1615
1642
  * move that is longer than its `say` ends. The audio of the chapter has them
1616
1643
  * (`@beff/aula-narration`'s `withWaits`). */
1617
1644
  waits: NarrationWait[];
1645
+ /** Each clip of an `embed` that plays, in order: its file, and the audio sample where it
1646
+ * starts on the clock of the picture, in its wait (so inside the audio with the waits). */
1647
+ embeds: PlayedEmbed[];
1618
1648
  /** The narration on the clock of the picture: each cue and word after a wait comes later by
1619
1649
  * the wait. */
1620
1650
  narration: Narration;
package/dist/engine.js CHANGED
@@ -3070,6 +3070,34 @@ export function show(strings) {
3070
3070
  function timingText(strings) {
3071
3071
  return strings.raw.join("${…}");
3072
3072
  }
3073
+ /** ``embed`note`(path)``: a prepared clip that plays between two `say` rows, while the
3074
+ * narration is silent. The note says what the clip shows. The `path` is a file of the project
3075
+ * folder: an MP4 (H.264, AAC) or WebM (VP8 or VP9, Opus or Vorbis) video, which fills the
3076
+ * picture in 16:9 on the theme background, or a WAV (PCM) or MP3 sound, which plays over the
3077
+ * picture that stays (or over `{ image }`, a PNG or JPEG file). The clip plays after the `say`
3078
+ * above it, and the next `say` starts 0.25 s after it ends: the narration waits for it, and no
3079
+ * move runs. Write it after a `say` that ends a sentence. `aula check` gives an error at the row
3080
+ * for a file that is missing or too big (50 MB), with a format that aula does not play or a
3081
+ * header with no length. */
3082
+ export function embed(strings) {
3083
+ const note = tagText("embed", strings);
3084
+ const usage = 'embed`note`("samples/a.wav", { image: "samples/a.png" })';
3085
+ return (path, options) => {
3086
+ if (typeof path !== "string")
3087
+ fault("embed: give the path of the file after the note", 'embed`note`("clips/demo.mp4")');
3088
+ const given = options;
3089
+ if (given !== undefined && (typeof given !== "object" || given === null)) {
3090
+ fault("embed: the options are an object", usage);
3091
+ }
3092
+ const unknown = given === undefined ? undefined : Object.keys(given).find((k) => k !== "image");
3093
+ if (unknown !== undefined)
3094
+ fault(`embed: the options have ${JSON.stringify(unknown)}; the only option is image`, usage);
3095
+ const image = options?.image;
3096
+ if (image !== undefined && typeof image !== "string")
3097
+ fault("embed: image is the path of a PNG or JPEG file", usage);
3098
+ return row("embed", (call) => ({ kind: "embed", call, note, path, image }));
3099
+ };
3100
+ }
3073
3101
  /** The scene of a move, as `self` in a Manim `construct`: the picture calls. */
3074
3102
  export class Scene {
3075
3103
  #raw;
@@ -3249,6 +3277,28 @@ function placeIn(call, slot) {
3249
3277
  const frame = slot.frames(call).find((f) => f.file === slot.source);
3250
3278
  return { file: slot.file, line: frame?.line ?? 0, col: frame?.col ?? 0 };
3251
3279
  }
3280
+ const err = (val) => ({ tag: "err", val });
3281
+ /** What the engine gets for an `embed`: the kind and the length of the clip, or the problem of
3282
+ * its file. */
3283
+ function clipFile(slot, path, image) {
3284
+ const main = slot.clip(path);
3285
+ if (typeof main === "string")
3286
+ return err(main);
3287
+ if (main.kind === "image") {
3288
+ return err(`embed: ${JSON.stringify(path)} is a picture, and a picture goes with a sound: embed\`note\`("sound.wav", { image: ${JSON.stringify(path)} })`);
3289
+ }
3290
+ if (image !== undefined) {
3291
+ if (main.kind === "video")
3292
+ return err(`embed: ${JSON.stringify(path)} is a video, which has its own picture: leave out { image }`);
3293
+ const picture = slot.clip(image);
3294
+ if (typeof picture === "string")
3295
+ return err(picture);
3296
+ if (picture.kind !== "image") {
3297
+ return err(`embed: the image ${JSON.stringify(image)} is not a PNG or JPEG file`);
3298
+ }
3299
+ }
3300
+ return { tag: "ok", val: { kind: main.kind, seconds: main.seconds } };
3301
+ }
3252
3302
  /** The rows of a chapter: a new call of its `rows`, with the seed of `shuffle` at its start. */
3253
3303
  function rowsOf(chapter) {
3254
3304
  freshSeed();
@@ -3271,6 +3321,9 @@ function write(handle, chapter, rows, slot, scene, card) {
3271
3321
  else if (r.kind === "say") {
3272
3322
  handle.say(at, [...r.parts]);
3273
3323
  }
3324
+ else if (r.kind === "embed") {
3325
+ handle.embed(at, r.note, r.path, r.image, clipFile(slot, r.path, r.image));
3326
+ }
3274
3327
  else if (r.move === undefined) {
3275
3328
  handle.open(at, r.note, r.timing);
3276
3329
  }
@@ -3383,7 +3436,15 @@ export function checkRows(chapter, theme, card, slot, exists) {
3383
3436
  export function checkTitleCard(title, theme, card, count) {
3384
3437
  freshSeed();
3385
3438
  const handle = engine.Chapter.orderPass(0, count + 1);
3386
- const slot = { number: 0, count, source: "", file: "", frames: () => [], lexicon: [] };
3439
+ const slot = {
3440
+ number: 0,
3441
+ count,
3442
+ source: "",
3443
+ file: "",
3444
+ frames: () => [],
3445
+ lexicon: [],
3446
+ clip: () => "no files",
3447
+ };
3387
3448
  const scene = playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
3388
3449
  handle.finish();
3389
3450
  return { diagnostics: [...handle.report(), ...scene.warnings] };
@@ -3396,6 +3457,7 @@ function finished(handle) {
3396
3457
  frames: handle.frames(),
3397
3458
  chapterSeconds: handle.chapterSeconds(),
3398
3459
  waits: handle.waits(),
3460
+ embeds: handle.played(),
3399
3461
  narration: handle.narration(),
3400
3462
  };
3401
3463
  }
@@ -3419,7 +3481,15 @@ export function drawTitle(title, theme, card, drawing) {
3419
3481
  freshSeed();
3420
3482
  const handle = engine.Chapter.render(drawing.narration, theme.background, 0, drawing.count, drawing.name, drawing.lint);
3421
3483
  const count = drawing.count - 1;
3422
- const slot = { number: 0, count, source: "", file: "", frames: () => [], lexicon: [] };
3484
+ const slot = {
3485
+ number: 0,
3486
+ count,
3487
+ source: "",
3488
+ file: "",
3489
+ frames: () => [],
3490
+ lexicon: [],
3491
+ clip: () => "no files",
3492
+ };
3423
3493
  playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
3424
3494
  return finished(handle);
3425
3495
  }
@@ -3427,7 +3497,15 @@ export function drawTitle(title, theme, card, drawing) {
3427
3497
  * second, on a black background. There is no project, no narration and no heading. */
3428
3498
  export function renderScene(draw) {
3429
3499
  const handle = engine.Chapter.scene();
3430
- const slot = { number: 0, count: 1, source: "scene", file: "scene", frames: () => [], lexicon: [] };
3500
+ const slot = {
3501
+ number: 0,
3502
+ count: 1,
3503
+ source: "scene",
3504
+ file: "scene",
3505
+ frames: () => [],
3506
+ lexicon: [],
3507
+ clip: () => "no files",
3508
+ };
3431
3509
  draw(new Scene(handle, slot));
3432
3510
  handle.finish();
3433
3511
  const trace = JSON.parse(handle.trace());
Binary file