@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.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.
Files changed (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,73 @@
1
+ import { readSceneEnter } from '@openartifactsai/motion-core';
2
+ const MOST_SCENES = 24;
3
+ const LONGEST_FILM_S = 36000;
4
+ const SCENE_ID = /^[a-z][a-z0-9-]{0,31}$/;
5
+ /** One sentence and no stack, for the page to show as it is. */
6
+ function fault(why) {
7
+ const error = new Error('scenes.json: ' + why);
8
+ error.stack = '';
9
+ return error;
10
+ }
11
+ const fields = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : null;
12
+ const whole = (value, fallback) => (Number.isInteger(value) && Number(value) >= 16 && Number(value) <= 4096 ? Number(value) : fallback);
13
+ /** `modules/scenes.json` with its list, or a throw. */
14
+ export function readScenes(text) {
15
+ let parsed = null;
16
+ try {
17
+ parsed = JSON.parse(text);
18
+ }
19
+ catch {
20
+ // Said below.
21
+ }
22
+ const record = fields(parsed);
23
+ if (!record || record['v'] !== 1 || (record['stage'] !== 'rough' && record['stage'] !== 'final'))
24
+ throw fault('must be JSON like {"v": 1, "stage": "rough", "scenes": […]}');
25
+ const list = record['scenes'];
26
+ if (!Array.isArray(list) || list.length < 1 || list.length > MOST_SCENES)
27
+ throw fault('"scenes" must list 1 to ' + MOST_SCENES + ' scenes');
28
+ const seen = new Set();
29
+ const odd = [];
30
+ let total = 0;
31
+ const scenes = list.map((entry, i) => {
32
+ const label = 'scene ' + (i + 1);
33
+ const scene = fields(entry);
34
+ const id = scene?.['id'];
35
+ if (!scene || typeof id !== 'string' || !SCENE_ID.test(id))
36
+ throw fault(label + ' needs an "id" of lower-case letters, digits and "-", starting with a letter, 32 characters at most');
37
+ if (seen.has(id))
38
+ throw fault('two scenes are called ' + id);
39
+ seen.add(id);
40
+ const dur = scene['dur'];
41
+ if (typeof dur !== 'number' || !isFinite(dur) || !(dur > 0))
42
+ throw fault(label + ' needs a "dur" in seconds, above 0');
43
+ total += dur;
44
+ const name = scene['name'];
45
+ const { enter, odd: unread } = readSceneEnter(scene['enter']);
46
+ if (unread)
47
+ odd.push(label);
48
+ return { id, name: (typeof name === 'string' ? name.trim().slice(0, 48).trim() : '') || id, dur, ...(enter ? { enter } : {}) };
49
+ });
50
+ if (total > LONGEST_FILM_S)
51
+ throw fault('the scenes add up to ' + Math.round(total * 100) / 100 + ' s; an animation is ' + LONGEST_FILM_S + ' s at most');
52
+ const fps = record['fps'];
53
+ const background = record['background'];
54
+ const lang = record['lang'];
55
+ return {
56
+ stage: record['stage'],
57
+ width: whole(record['width'], 1920),
58
+ height: whole(record['height'], 1080),
59
+ fps: fps === 24 || fps === 30 ? fps : 60,
60
+ background: typeof background === 'string' && background !== '' && background.length <= 64 ? background : '#ffffff',
61
+ scenes,
62
+ ...(typeof lang === 'string' ? { lang } : {}),
63
+ odd,
64
+ };
65
+ }
66
+ /** Shared modules that run after shared.js, each in its own scope, in number order. */
67
+ const SHARED_PART = /^shared-\d+-.+\.js$/;
68
+ /** shared-<n>-<thing>.js in number order (shared-2 before shared-10), and the other shared-*.js names, which never run. */
69
+ export function sharedParts(names) {
70
+ const run = names.filter((name) => SHARED_PART.test(name)).sort((a, b) => a.localeCompare(b, 'en', { numeric: true }));
71
+ const skipped = names.filter((name) => /^shared-.*\.js$/.test(name) && !SHARED_PART.test(name));
72
+ return { run, skipped };
73
+ }
@@ -0,0 +1,13 @@
1
+ import type { Kit } from '../kit/index.js';
2
+ /** A scene being brought up: what deferred code threw meanwhile, and how to cut its init short. */
3
+ export interface Mounting {
4
+ late: {
5
+ error: unknown;
6
+ } | null;
7
+ no: ((error: unknown) => void) | null;
8
+ soon: ReturnType<typeof setTimeout> | 0;
9
+ /** It failed: what its init still does is not the film's. */
10
+ gone: boolean;
11
+ }
12
+ /** The film's kit on the scene's clock and in the scene's box. */
13
+ export declare function scopedKit(kit: Kit, box: HTMLElement, start: number, dur: number, me: Mounting, run: (name: string, kit: Kit, own: boolean) => unknown): Kit;
@@ -0,0 +1,46 @@
1
+ import { clipInScene } from "../clips.js";
2
+ /** The film's kit on the scene's clock and in the scene's box. */
3
+ export function scopedKit(kit, box, start, dur, me, run) {
4
+ const scoped = Object.create(kit);
5
+ const into = (node) => {
6
+ box.appendChild(node);
7
+ return node;
8
+ };
9
+ Object.defineProperties(scoped, {
10
+ stage: { value: box },
11
+ start: { value: start },
12
+ dur: { value: dur },
13
+ });
14
+ scoped.css = (text) => into(kit.el('style', { text: String(text) }));
15
+ scoped.cursor = (glyph) => {
16
+ const cursor = kit.cursor(glyph);
17
+ into(cursor.el);
18
+ return cursor;
19
+ };
20
+ scoped.svgLayer = () => into(kit.svgLayer());
21
+ scoped.chip = (text, options) => into(kit.chip(text, options));
22
+ // Its sound and words stay within the scene.
23
+ scoped.video = (source, options = {}) => {
24
+ const video = kit.video(source, { ...options, start: start + (Number(options.start) || 0) });
25
+ clipInScene(video, { lo: start, hi: start + dur, gone: () => me.gone });
26
+ return video;
27
+ };
28
+ scoped.typist = (text, at, cps, seed) => {
29
+ const times = kit.typing.times;
30
+ const before = times.length;
31
+ const made = kit.typist(text, at, cps, seed);
32
+ // A failed scene's init may run on: what it types then is not the film's.
33
+ if (me.gone)
34
+ times.length = before;
35
+ for (let i = before; i < times.length; i++)
36
+ times[i] = (times[i] ?? 0) + start;
37
+ return made;
38
+ };
39
+ // Rethrown, so the scene's code that called exec can catch it. It runs as in any film, with no scope of its own: its functions land on window.
40
+ scoped.exec = (name) => {
41
+ const thrown = run(name, scoped, false);
42
+ if (thrown)
43
+ throw thrown;
44
+ };
45
+ return scoped;
46
+ }
@@ -0,0 +1,32 @@
1
+ import type { ScoreData, VoiceParameters } from '@openartifactsai/motion-core';
2
+ /** What a score call is given: film code's own object, read field by field. */
3
+ export type ScoreOptions = Record<string, unknown>;
4
+ /** One note of a scene's music: track, beat from the scene's first beat, length in beats, MIDI note (a list: a chord) or null, loudness 0 to 1, instrument. */
5
+ export type BandNote = (track: unknown, beat?: unknown, beats?: unknown, pitch?: unknown, loud?: unknown, instrument?: unknown, options?: ScoreOptions) => void;
6
+ /** The beat, counted as BandNote counts them, that falls `seconds` into the scene. */
7
+ export type BandAt = (seconds?: unknown) => number;
8
+ export interface ScoreBuilder {
9
+ /** The music of one scene: its beats count from the beat nearest `t`, so no cut breaks the groove. */
10
+ scene(t: unknown, notesOf: unknown): ScoreBuilder;
11
+ tempo(bpm?: number, options?: ScoreOptions): ScoreBuilder;
12
+ beat(beat: number): number;
13
+ toBeat(time: number): number;
14
+ seed(seed: number): ScoreBuilder;
15
+ rand(): number;
16
+ program(track: string, patch?: string, options?: ScoreOptions): ScoreBuilder;
17
+ note(track: string, pitch: unknown, options?: ScoreOptions): ScoreBuilder;
18
+ chord(track: string, pitches: unknown, options?: ScoreOptions): ScoreBuilder;
19
+ pattern(track: string, pattern: string, options?: ScoreOptions): ScoreBuilder;
20
+ run(track: string, pitches: unknown, options?: ScoreOptions): ScoreBuilder;
21
+ drum(name: string, options?: ScoreOptions): ScoreBuilder;
22
+ drums(patterns: Record<string, string>, options?: ScoreOptions): ScoreBuilder;
23
+ fx(name: string, time: number, options?: ScoreOptions): ScoreBuilder;
24
+ ticks(times: ArrayLike<number>, options?: ScoreOptions): ScoreBuilder;
25
+ master(options?: ScoreOptions): ScoreBuilder;
26
+ }
27
+ export type ScoreParameters = VoiceParameters;
28
+ /**
29
+ * Runs `write(S)` and gives back plain score data on the film's own clock. A pitch that cannot be read costs its
30
+ * own note, not the score; past MOST_EVENTS refusals it throws all the same, so an endless list ends.
31
+ */
32
+ export declare function recordScore(write: (builder: ScoreBuilder) => void): ScoreData;
@@ -0,0 +1,262 @@
1
+ import { BAND_MOVES, BAND_TRACKS, BAND_TRACK_KINDS, MAX_PUMP_DB, isBandTrack } from '@openartifactsai/motion-core';
2
+ const MOST_EVENTS = 20000;
3
+ const LONGEST_S = 72000;
4
+ /** The longest band note, in seconds. */
5
+ const LONGEST_NOTE_S = 600;
6
+ const NOTE_STEPS = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
7
+ const VOICE_KEYS = ['pan', 'gain', 'rev', 'detune', 'bright'];
8
+ const FX_KEYS = [...VOICE_KEYS, 'into', 'rate'];
9
+ const num = (value, fallback) => (typeof value === 'number' && isFinite(value) ? value : fallback);
10
+ const field = (options, key) => (options ? options[key] : undefined);
11
+ function textOf(value) {
12
+ try {
13
+ return String(value);
14
+ }
15
+ catch {
16
+ return typeof value;
17
+ }
18
+ }
19
+ function isListLike(value) {
20
+ return value !== null && typeof value === 'object' && typeof Reflect.get(value, 'length') === 'number' && Number(Reflect.get(value, 'length')) >= 0;
21
+ }
22
+ /**
23
+ * Runs `write(S)` and gives back plain score data on the film's own clock. A pitch that cannot be read costs its
24
+ * own note, not the score; past MOST_EVENTS refusals it throws all the same, so an endless list ends.
25
+ */
26
+ export function recordScore(write) {
27
+ const events = [];
28
+ let master = { gain: -6, fadeOut: 1, loudness: -16 };
29
+ const grid = { bpm: 120, t0: 0 };
30
+ const tracks = new Map();
31
+ const refused = { count: 0, names: [] };
32
+ let seed = 7;
33
+ const random = () => {
34
+ seed = (seed + 0x6d2b79f5) | 0;
35
+ let x = Math.imul(seed ^ (seed >>> 15), 1 | seed);
36
+ x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;
37
+ return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
38
+ };
39
+ const secondsPerBeat = () => 60 / grid.bpm;
40
+ const beatTime = (beat) => grid.t0 + beat * secondsPerBeat();
41
+ const refuse = (pitch, where) => {
42
+ if (refused.count >= MOST_EVENTS)
43
+ throw new Error('score: over 20,000 pitches could not be read');
44
+ const name = (pitch === null || pitch === undefined || typeof pitch === 'number' ? String(pitch) : JSON.stringify(textOf(pitch).slice(0, 24))) + where;
45
+ refused.count++;
46
+ if (refused.names.length < 6 && !refused.names.includes(name))
47
+ refused.names.push(name);
48
+ return null;
49
+ };
50
+ const midiOf = (pitch) => {
51
+ let midi = NaN;
52
+ if (typeof pitch === 'number')
53
+ midi = pitch;
54
+ else {
55
+ const found = /^([A-Ga-g])([#b♯♭]?)(-?\d)$/.exec(textOf(pitch).trim());
56
+ if (found) {
57
+ const [, letter = 'C', accidental = '', octave = '0'] = found;
58
+ midi = 12 * (Number(octave) + 1) + (NOTE_STEPS[letter.toUpperCase()] ?? 0) + (/[#♯]/.test(accidental) ? 1 : accidental ? -1 : 0);
59
+ }
60
+ }
61
+ return Number.isNaN(midi) ? refuse(pitch, '') : Math.max(0, Math.min(127, Math.round(midi)));
62
+ };
63
+ // Anything that is not a list is refused whole: a chord's name such as "G7" would otherwise play as one very high note.
64
+ const listOf = (pitches, call) => (isListLike(pitches) ? pitches : (refuse(pitches, ' where ' + call + ' takes a list'), []));
65
+ /** {beat, beats} on the grid or {t, dur} in seconds. */
66
+ const when = (options, defaultDur) => {
67
+ const t = field(options, 't') !== undefined ? num(field(options, 't'), 0) : beatTime(num(field(options, 'beat'), 0));
68
+ const given = field(options, 'dur');
69
+ const beats = field(options, 'beats');
70
+ const dur = given !== undefined ? num(given, defaultDur) : beats !== undefined ? num(beats, 1) * secondsPerBeat() : defaultDur;
71
+ return [t, Math.max(0.01, dur)];
72
+ };
73
+ const push = (kind, patch, midi, at, dur, vel, params, track) => {
74
+ if (midi === null)
75
+ return;
76
+ // Humanized jitter round beat 0.
77
+ const t = at < 0 && at > -0.1 ? 0 : at;
78
+ if (!(t >= 0) || t > LONGEST_S)
79
+ return;
80
+ const loudness = Math.max(1, Math.min(127, Math.round(num(vel, 64))));
81
+ events.push({ t: t, dur, kind, patch: String(patch), midi, vel: loudness, ...(params ? { params } : {}), ...(track ? { track } : {}) });
82
+ };
83
+ // One note of S.scene, `from` beats into the grid. A move (stop, filter) and a drum or effect may go without a note.
84
+ const bandNote = (from, track, beat, beats, pitch, loud, instrument, options) => {
85
+ if (!isBandTrack(track))
86
+ throw new Error(JSON.stringify(textOf(track).slice(0, 24)) + ' is not a track (use ' + BAND_TRACKS.slice(0, -1).join(', ') + ' or air)');
87
+ const kind = BAND_TRACK_KINDS[track];
88
+ const name = instrument === null || instrument === undefined ? '' : textOf(instrument);
89
+ const move = BAND_MOVES.includes(name);
90
+ const loudness = num(loud, 0.5);
91
+ if (!(loudness > 0) && name !== 'filter')
92
+ return;
93
+ const t = beatTime(from + num(beat, 0));
94
+ const dur = Math.max(0.01, Math.min(LONGEST_NOTE_S, num(beats, 1) * secondsPerBeat()));
95
+ const bend = merged(null, options, ['bend']);
96
+ for (const each of Array.isArray(pitch) ? pitch : [pitch]) {
97
+ if (events.length >= MOST_EVENTS)
98
+ return;
99
+ const midi = (each === null || each === undefined) && (kind !== 'note' || move) ? 0 : midiOf(each);
100
+ push(kind, name, midi, t, dur, loudness * 127, bend, track);
101
+ }
102
+ };
103
+ const notes = (patch, pitch, t, dur, vel, params) => {
104
+ for (const each of Array.isArray(pitch) ? pitch : [pitch])
105
+ push('note', patch, midiOf(each), t, dur, vel, params);
106
+ };
107
+ const trackOf = (name) => tracks.get(name) ?? { patch: String(name), params: null };
108
+ const merged = (base, options, keys) => {
109
+ let out = base ? { ...base } : null;
110
+ for (const key of keys) {
111
+ const value = field(options, key);
112
+ if (typeof value === 'number')
113
+ out = { ...(out ?? {}), [key]: value };
114
+ }
115
+ return out;
116
+ };
117
+ const builder = {
118
+ // A scene whose notes throw keeps those it wrote, and costs the other scenes nothing.
119
+ scene(t, notesOf) {
120
+ const at = num(t, 0);
121
+ const start = (at - grid.t0) / secondsPerBeat();
122
+ const from = Math.round(start);
123
+ if (typeof notesOf !== 'function')
124
+ return builder;
125
+ const note = (track, beat, beats, pitch, loud, instrument, options) => bandNote(from, track, beat, beats, pitch, loud, instrument, options);
126
+ const beatOf = (seconds) => start - from + num(seconds, 0) / secondsPerBeat();
127
+ try {
128
+ Reflect.apply(notesOf, undefined, [note, beatOf]);
129
+ }
130
+ catch (error) {
131
+ console.error('score: S.scene(' + textOf(t).slice(0, 24) + ') stopped, and its later notes are left out: ' + (error instanceof Error ? error.message : textOf(error)));
132
+ }
133
+ return builder;
134
+ },
135
+ // The grid: beats per minute and where beat 0 sits on the film clock.
136
+ tempo(bpm, options) {
137
+ grid.bpm = Math.max(20, Math.min(300, num(bpm, 120)));
138
+ grid.t0 = num(field(options, 't0'), 0);
139
+ return builder;
140
+ },
141
+ beat: (beat) => beatTime(beat),
142
+ toBeat: (time) => (time - grid.t0) / secondsPerBeat(),
143
+ seed(next) {
144
+ seed = num(next, 7) | 0;
145
+ return builder;
146
+ },
147
+ rand: random,
148
+ program(track, patch, options) {
149
+ tracks.set(track, { patch: String(patch || track), params: merged(null, options, VOICE_KEYS) });
150
+ return builder;
151
+ },
152
+ note(track, pitch, options) {
153
+ const chosen = trackOf(track);
154
+ const [t, dur] = when(options, 0.3);
155
+ notes(chosen.patch, pitch, t, dur, field(options, 'vel'), merged(chosen.params, options, VOICE_KEYS));
156
+ return builder;
157
+ },
158
+ chord(track, pitches, options) {
159
+ const chosen = trackOf(track);
160
+ const [t, dur] = when(options, 1);
161
+ const spread = num(field(options, 'spread'), 0);
162
+ const list = listOf(pitches, 'chord');
163
+ for (let i = 0; i < list.length; i++)
164
+ notes(chosen.patch, list[i], t + i * spread, Math.max(0.02, dur - i * spread), field(options, 'vel'), merged(chosen.params, options, VOICE_KEYS));
165
+ return builder;
166
+ },
167
+ // 'x.x.x-xx' on a step grid from beat `from`: x a hit (the pitch list cycles), X an accent, anything else a rest.
168
+ pattern(track, pattern, options = {}) {
169
+ const chosen = trackOf(track);
170
+ const step = num(options['step'], 0.5);
171
+ const from = num(options['from'], 0);
172
+ const repeats = Math.max(1, num(options['repeat'], 1));
173
+ const given = options['pitch'];
174
+ const pitches = Array.isArray(given) ? given : [given !== undefined ? given : 'C4'];
175
+ const vel = num(options['vel'], 64);
176
+ const human = num(options['humanize'], 0.004);
177
+ let hit = 0;
178
+ for (let r = 0; r < repeats; r++)
179
+ for (let i = 0; i < pattern.length; i++) {
180
+ const mark = pattern.charAt(i);
181
+ if (mark !== 'x' && mark !== 'X')
182
+ continue;
183
+ const beat = from + (r * pattern.length + i) * step;
184
+ notes(chosen.patch, pitches[hit % pitches.length], beatTime(beat) + (random() * 2 - 1) * human, num(options['dur'], step * secondsPerBeat() * 0.9), vel + (mark === 'X' ? 12 : 0) + Math.round((random() * 2 - 1) * num(options['velJitter'], 4)), merged(chosen.params, options, VOICE_KEYS));
185
+ hit++;
186
+ }
187
+ return builder;
188
+ },
189
+ // Evenly stepped, in seconds.
190
+ run(track, pitches, options = {}) {
191
+ const chosen = trackOf(track);
192
+ const t = num(options['t'], 0);
193
+ const step = num(options['step'], 0.1);
194
+ const list = listOf(pitches, 'run');
195
+ for (let i = 0; i < list.length; i++)
196
+ notes(chosen.patch, list[i], t + i * step, num(options['dur'], 0.3), num(options['vel'], 64) + num(options['cresc'], 0) * i, merged(chosen.params, options, VOICE_KEYS));
197
+ return builder;
198
+ },
199
+ drum(name, options) {
200
+ const [t, dur] = when(options, 0.1);
201
+ push('drum', name, 0, t, dur, field(options, 'vel'), merged(null, options, VOICE_KEYS));
202
+ return builder;
203
+ },
204
+ drums(patterns, options = {}) {
205
+ const step = num(options['step'], 0.5);
206
+ const from = num(options['from'], 0);
207
+ const repeats = Math.max(1, num(options['repeat'], 1));
208
+ for (const [name, written] of Object.entries(patterns)) {
209
+ const pattern = String(written);
210
+ for (let r = 0; r < repeats; r++)
211
+ for (let i = 0; i < pattern.length; i++) {
212
+ const mark = pattern.charAt(i);
213
+ if (mark !== 'x' && mark !== 'X' && mark !== 'o')
214
+ continue;
215
+ const accent = mark === 'X' ? 16 : mark === 'o' ? -22 : 0;
216
+ push('drum', name, 0, beatTime(from + (r * pattern.length + i) * step), 0.1, num(options['vel'], 70) + accent + Math.round((random() * 2 - 1) * 4), merged(null, options, VOICE_KEYS));
217
+ }
218
+ }
219
+ return builder;
220
+ },
221
+ // Foley at an exact film time, off the grid.
222
+ fx(name, time, options = {}) {
223
+ const pitch = options['pitch'];
224
+ push('fx', name, (pitch !== undefined && midiOf(pitch)) || 0, num(time, 0), Math.max(0.01, num(options['dur'], 0.25)), num(options['vel'], 64), merged(null, options, FX_KEYS));
225
+ return builder;
226
+ },
227
+ // One tick per time: typing.
228
+ ticks(times, options = {}) {
229
+ const jitter = num(options['jitter'], 6);
230
+ const name = typeof options['name'] === 'string' ? options['name'] : 'tick';
231
+ for (let i = 0; i < times.length; i++)
232
+ builder.fx(name, num(times[i], 0), { vel: num(options['vel'], 36) + Math.round((random() * 2 - 1) * jitter), pan: options['pan'] });
233
+ return builder;
234
+ },
235
+ // gain trims before the master; loudness is the integrated LUFS the host normalizes to (false leaves it raw).
236
+ master(options = {}) {
237
+ const loudness = options['loudness'];
238
+ master = {
239
+ gain: Math.max(-40, Math.min(6, num(options['gain'], -6))),
240
+ fadeOut: Math.max(0, Math.min(10, num(options['fadeOut'], 1))),
241
+ loudness: loudness === false || loudness === null ? null : Math.max(-40, Math.min(-6, num(loudness, -16))),
242
+ };
243
+ const pump = num(options['pump'], 0);
244
+ if (pump > 0)
245
+ master.pump = Math.min(MAX_PUMP_DB, pump);
246
+ return builder;
247
+ },
248
+ };
249
+ write(builder);
250
+ if (refused.count > 0)
251
+ console.error('score: ' +
252
+ refused.count +
253
+ (refused.count === 1 ? ' pitch could not be read and was' : ' pitches could not be read and were') +
254
+ ' left out: ' +
255
+ refused.names.slice(0, 5).join(', ') +
256
+ (refused.names.length > 5 ? ' and others' : '') +
257
+ ' (write "C4", "F#3", "Bb2" or a MIDI number)');
258
+ events.sort((a, b) => a.t - b.t);
259
+ if (events.length > MOST_EVENTS)
260
+ events.length = MOST_EVENTS;
261
+ return { events, master, tempo: { bpm: grid.bpm, t0: grid.t0 } };
262
+ }
@@ -0,0 +1,3 @@
1
+ import type { Sketcher } from '../scenes/install.js';
2
+ /** The pen for one rough scene: a call that sketches the scene as it stands, made after every render, since a scene may show a state by its borders. */
3
+ export declare const sketchScene: Sketcher;