@conversation/spec 0.0.0-stage → 0.1.2
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/LICENSE +22 -0
- package/README.md +132 -2
- package/errors.d.ts +9 -0
- package/errors.js +36 -0
- package/esm/errors.js +31 -0
- package/esm/include.js +15 -0
- package/esm/index.js +7 -0
- package/esm/parse.js +469 -0
- package/esm/plugin.js +50 -0
- package/esm/plugins/effects.js +17 -0
- package/esm/plugins/index.js +10 -0
- package/esm/plugins/slides.js +48 -0
- package/esm/plugins/speakers.js +52 -0
- package/esm/plugins/video.js +39 -0
- package/esm/schema.js +100 -0
- package/esm/segments.js +60 -0
- package/esm/suggest.js +51 -0
- package/esm/types.js +1 -0
- package/esm/values.js +228 -0
- package/include.d.ts +23 -0
- package/include.js +18 -0
- package/index.d.ts +8 -0
- package/index.js +48 -0
- package/package.json +42 -4
- package/parse.d.ts +15 -0
- package/parse.js +472 -0
- package/plugin.d.ts +52 -0
- package/plugin.js +54 -0
- package/plugins/effects.d.ts +7 -0
- package/plugins/effects.js +20 -0
- package/plugins/index.d.ts +7 -0
- package/plugins/index.js +18 -0
- package/plugins/slides.d.ts +6 -0
- package/plugins/slides.js +51 -0
- package/plugins/speakers.d.ts +9 -0
- package/plugins/speakers.js +56 -0
- package/plugins/video.d.ts +6 -0
- package/plugins/video.js +42 -0
- package/schema.d.ts +56 -0
- package/schema.js +113 -0
- package/segments.d.ts +9 -0
- package/segments.js +63 -0
- package/suggest.d.ts +7 -0
- package/suggest.js +56 -0
- package/types.d.ts +93 -0
- package/types.js +2 -0
- package/values.d.ts +36 -0
- package/values.js +236 -0
package/esm/parse.js
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import { ConvError } from './errors';
|
|
2
|
+
import { buildRegistry } from './plugin';
|
|
3
|
+
import { defaultPlugins } from './plugins';
|
|
4
|
+
import { shown } from './schema';
|
|
5
|
+
import { splitSegments } from './segments';
|
|
6
|
+
import { didYouMean } from './suggest';
|
|
7
|
+
import { check, readConfig, readOverrides, readPlay } from './values';
|
|
8
|
+
const INCLUDE_RE = /^@include\b\s*(.*)$/;
|
|
9
|
+
const PAUSE_RE = /^\[pause\s+([\d.]+)\]$/i;
|
|
10
|
+
const LEGACY_MUSIC_RE = /^\[music:\s*([^\s\]]+)\s*([^\]]*)\]$/i;
|
|
11
|
+
const DIRECTIVE_RE = /^\[([a-z][a-z0-9_-]*)(?:\s+([^\]]*))?\]$/;
|
|
12
|
+
const CUE_RE = /^\[([A-Z][A-Z0-9_-]*)(?:\s+([^\]]*))?\]$/;
|
|
13
|
+
const SOUND_NAME_RE = /^[A-Z][A-Z0-9_-]*$/;
|
|
14
|
+
const SOUND_DECL_RE = /^([A-Z][A-Z0-9_-]*)\s*:\s*\{(.+)\}\s*$/;
|
|
15
|
+
const CHAR_DECL_RE = /^([A-Z][A-Z0-9_]*)\s*:\s*\{(.+)\}\s*$/;
|
|
16
|
+
const TRACK_DECL_RE = /^([a-z][a-z0-9_-]*)\s*:\s*\{(.*)\}\s*$/;
|
|
17
|
+
const LINE_RE = /^([A-Z][A-Z0-9_]*)(?:\[([^\]]*)\])?\s*:\s*(.+)$/;
|
|
18
|
+
const V1_CHAR_RE = /^([A-Z][A-Z0-9_]*)\s*:\s*([a-z][a-z0-9_-]*)\s*$/;
|
|
19
|
+
const HEADERS = ['@characters', '@sounds', '@tracks', '@include'];
|
|
20
|
+
/** Parse a `.conv` script. */
|
|
21
|
+
export function parse(text, options = {}) {
|
|
22
|
+
const reg = buildRegistry(options.plugins ?? defaultPlugins);
|
|
23
|
+
const stack = options.source ? [options.source] : [];
|
|
24
|
+
return parseWith(text, options, reg, false, stack);
|
|
25
|
+
}
|
|
26
|
+
function loadIncludes(lines, options, reg, stack) {
|
|
27
|
+
const inc = { sounds: {}, tracks: {}, origin: new Map(), problems: [], clashes: [] };
|
|
28
|
+
const resolver = options.include;
|
|
29
|
+
const banks = () => resolver?.list?.() ?? [];
|
|
30
|
+
for (let i = 0; i < lines.length; i++) {
|
|
31
|
+
const s = lines[i].trim();
|
|
32
|
+
if (s === '---')
|
|
33
|
+
break;
|
|
34
|
+
const m = INCLUDE_RE.exec(s);
|
|
35
|
+
if (!m)
|
|
36
|
+
continue;
|
|
37
|
+
const problem = (message) => inc.problems.push({ line: i + 1, text: s, message });
|
|
38
|
+
const ref = m[1].trim();
|
|
39
|
+
if (!ref) {
|
|
40
|
+
const list = banks();
|
|
41
|
+
problem('`@include` needs a bank, e.g. `@include podcast`' +
|
|
42
|
+
(list.length ? `\nBanks: ${list.join(', ')}` : ''));
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!resolver) {
|
|
46
|
+
problem(`can't include \`${ref}\` — this parser was given no include resolver`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const res = resolver.resolve(ref, stack[stack.length - 1]);
|
|
50
|
+
if ('tried' in res) {
|
|
51
|
+
const list = banks();
|
|
52
|
+
problem(`can't find \`${ref}\` — looked for ${res.tried.join(', ')}` +
|
|
53
|
+
didYouMean(ref, list) +
|
|
54
|
+
(list.length ? `\nBanks: ${list.join(', ')}` : ''));
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (stack.includes(res.id)) {
|
|
58
|
+
problem(`\`@include ${ref}\` goes round in a circle: ${[...stack, res.id].join(' -> ')}`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
let bank;
|
|
62
|
+
try {
|
|
63
|
+
bank = parseWith(res.text, { ...options, source: res.id, strict: true }, reg, true, [
|
|
64
|
+
...stack,
|
|
65
|
+
res.id,
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (!(e instanceof ConvError))
|
|
70
|
+
throw e;
|
|
71
|
+
const detail = e.problems
|
|
72
|
+
.map((p) => `line ${p.line}: ` + p.message.replace(/\n/g, '\n '))
|
|
73
|
+
.join('\n');
|
|
74
|
+
problem(`\`${ref}\` (${res.id}) has problems — fix them there:\n${detail}`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const merge = (kind, got, into) => {
|
|
78
|
+
for (const [name, cfg] of Object.entries(got)) {
|
|
79
|
+
const prior = inc.origin.get(`${kind}:${name}`);
|
|
80
|
+
if (prior) {
|
|
81
|
+
inc.clashes.push({
|
|
82
|
+
kind,
|
|
83
|
+
name,
|
|
84
|
+
problem: {
|
|
85
|
+
line: i + 1,
|
|
86
|
+
text: s,
|
|
87
|
+
message: `${kind} \`${name}\` comes from both \`${prior.ref}\` and \`${ref}\` — which one ` +
|
|
88
|
+
`is meant is ambiguous.\nDeclare \`${name}\` in this script to choose, or include ` +
|
|
89
|
+
'only one of them.',
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
into[name] = { ...cfg };
|
|
95
|
+
inc.origin.set(`${kind}:${name}`, { ref, line: i + 1 });
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
merge('sound', bank.sounds, inc.sounds);
|
|
99
|
+
merge('track', bank.tracks, inc.tracks);
|
|
100
|
+
}
|
|
101
|
+
return inc;
|
|
102
|
+
}
|
|
103
|
+
function hintFor(s, mode) {
|
|
104
|
+
let m = /^pause\s+([\d.]+)$/i.exec(s);
|
|
105
|
+
if (m)
|
|
106
|
+
return `pauses go in brackets: \`[pause ${m[1]}]\` (this is the old v1 form)`;
|
|
107
|
+
m = /^([A-Za-z][\w-]*)\s*:\s*\{/.exec(s);
|
|
108
|
+
if (m && m[1] !== m[1].toUpperCase()) {
|
|
109
|
+
return mode === 'tracks'
|
|
110
|
+
? `track names are lowercase: \`${m[1].toLowerCase()}: { … }\``
|
|
111
|
+
: `names are UPPERCASE: \`${m[1].toUpperCase()}: { … }\``;
|
|
112
|
+
}
|
|
113
|
+
m = /^([A-Za-z][\w-]*)(\[[^\]]*\])?\s*:\s*(.+)$/.exec(s);
|
|
114
|
+
if (m) {
|
|
115
|
+
const name = m[1];
|
|
116
|
+
if (name.includes('-')) {
|
|
117
|
+
return `speaker names use letters, digits and _ — not -: \`${name.replace(/-/g, '_').toUpperCase()}: …\``;
|
|
118
|
+
}
|
|
119
|
+
if (name !== name.toUpperCase()) {
|
|
120
|
+
return `speaker names are UPPERCASE: \`${name.toUpperCase()}${m[2] ?? ''}: ${m[3].slice(0, 30)}…\``;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
m = /^\[([A-Za-z][\w-]*)/.exec(s);
|
|
124
|
+
if (m) {
|
|
125
|
+
return (`\`[${m[1]} …]\` — sound names are UPPERCASE (\`[${m[1].toUpperCase()}]\`), ` +
|
|
126
|
+
`track directives lowercase (\`[${m[1].toLowerCase()} SOUND]\`)`);
|
|
127
|
+
}
|
|
128
|
+
if (mode === 'tracks')
|
|
129
|
+
return 'inside @tracks every line is `name: { key: value, … }`';
|
|
130
|
+
return ('expected one of: `NAME: text`, `NAME[emotion]: text`, `[pause N]`, `[SOUND]`, ' +
|
|
131
|
+
'`[track SOUND]`, `# comment`, or a header (@characters, @sounds, @tracks, ---)');
|
|
132
|
+
}
|
|
133
|
+
function parseWith(text, options, reg, fragment, stack) {
|
|
134
|
+
const lines = text.split(/\r?\n/);
|
|
135
|
+
const characters = {};
|
|
136
|
+
const sounds = {};
|
|
137
|
+
const tracks = {};
|
|
138
|
+
const items = [];
|
|
139
|
+
const problems = [];
|
|
140
|
+
const refs = [];
|
|
141
|
+
const index = { character: {}, sound: {}, track: {}, speaker: {}, file: {} };
|
|
142
|
+
const declare = (kind, name, d, keepFirst = false) => {
|
|
143
|
+
if (!(keepFirst && index[kind][name]))
|
|
144
|
+
index[kind][name] = d;
|
|
145
|
+
};
|
|
146
|
+
const inc = loadIncludes(lines, options, reg, stack);
|
|
147
|
+
Object.assign(sounds, inc.sounds);
|
|
148
|
+
Object.assign(tracks, inc.tracks);
|
|
149
|
+
problems.push(...inc.problems);
|
|
150
|
+
for (const [key, o] of inc.origin) {
|
|
151
|
+
const [kind, name] = key.split(/:(.*)/s);
|
|
152
|
+
declare(kind, name, { line: o.line, include: o.ref });
|
|
153
|
+
}
|
|
154
|
+
// first pass: track names, so anything above @tracks can already name one
|
|
155
|
+
{
|
|
156
|
+
let m = null;
|
|
157
|
+
for (const raw of lines) {
|
|
158
|
+
const s = raw.trim();
|
|
159
|
+
if (s === '@tracks')
|
|
160
|
+
m = 'tracks';
|
|
161
|
+
else if (s.startsWith('@') || s === '---')
|
|
162
|
+
m = null;
|
|
163
|
+
else if (m === 'tracks') {
|
|
164
|
+
const tm = /^([a-z][a-z0-9_-]*)\s*:\s*\{/.exec(s);
|
|
165
|
+
if (tm && !reg.reserved.has(tm[1]) && !(tm[1] in tracks))
|
|
166
|
+
tracks[tm[1]] = null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const allTracks = () => new Set([...reg.tracks, ...Object.keys(tracks)]);
|
|
171
|
+
const knownTrack = (n) => reg.tracks.includes(n) || n in tracks;
|
|
172
|
+
const opts = (what, context) => ({
|
|
173
|
+
reg,
|
|
174
|
+
tracks: allTracks(),
|
|
175
|
+
what,
|
|
176
|
+
context,
|
|
177
|
+
});
|
|
178
|
+
let mode = null;
|
|
179
|
+
let bodyStarted = false;
|
|
180
|
+
for (let i = 0; i < lines.length; i++) {
|
|
181
|
+
const lineno = i + 1;
|
|
182
|
+
const s = lines[i].trim();
|
|
183
|
+
const errs = [];
|
|
184
|
+
const flush = () => {
|
|
185
|
+
for (const message of errs)
|
|
186
|
+
problems.push({ line: lineno, text: s, message });
|
|
187
|
+
};
|
|
188
|
+
const problem = (message) => problems.push({ line: lineno, text: s, message });
|
|
189
|
+
if (!s || s.startsWith('#'))
|
|
190
|
+
continue;
|
|
191
|
+
if (INCLUDE_RE.test(s)) {
|
|
192
|
+
if (mode === 'body' || bodyStarted)
|
|
193
|
+
problem('`@include` belongs in the header, above `---`');
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (fragment && (s === '@characters' || s === '@voices' || s === '---')) {
|
|
197
|
+
problem('a sound bank holds only @sounds and @tracks (and @include) — characters and the ' +
|
|
198
|
+
'script belong in the .conv');
|
|
199
|
+
mode = 'rejected';
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (s === '@characters' || s === '@voices') {
|
|
203
|
+
mode = 'chars';
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (s === '@sounds') {
|
|
207
|
+
mode = 'sounds';
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (s === '@tracks') {
|
|
211
|
+
mode = 'tracks';
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (s === '---') {
|
|
215
|
+
mode = 'body';
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (s.startsWith('@')) {
|
|
219
|
+
problem(`unknown header \`${s}\` — headers are @characters, @sounds, @tracks and @include` +
|
|
220
|
+
didYouMean(s, HEADERS));
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (fragment && mode !== 'sounds' && mode !== 'tracks') {
|
|
224
|
+
if (mode !== 'rejected') {
|
|
225
|
+
problem('a sound bank holds only @sounds and @tracks — put this line under one of those headers');
|
|
226
|
+
}
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
let m = PAUSE_RE.exec(s);
|
|
230
|
+
if (m) {
|
|
231
|
+
items.push({ type: 'pause', line: lineno, seconds: Number(m[1]) });
|
|
232
|
+
bodyStarted = true;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (/^\[pause\b/i.test(s)) {
|
|
236
|
+
problem('`[pause N]` needs seconds, e.g. `[pause 1.5]`');
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (mode === 'tracks') {
|
|
240
|
+
m = TRACK_DECL_RE.exec(s);
|
|
241
|
+
if (m && reg.reserved.has(m[1])) {
|
|
242
|
+
problem(`\`${m[1]}\` is a reserved word and can't name a track — pick another name`);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (m) {
|
|
246
|
+
const pairs = readConfig(m[2], errs);
|
|
247
|
+
tracks[m[1]] = check(pairs, reg.schemas.track, opts(`track \`${m[1]}\``), errs);
|
|
248
|
+
declare('track', m[1], { line: lineno });
|
|
249
|
+
flush();
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
m = LEGACY_MUSIC_RE.exec(s);
|
|
254
|
+
if (m) {
|
|
255
|
+
const overrides = check(readPlay(m[2]), reg.schemas.play, opts('`[music: …]`'), errs);
|
|
256
|
+
items.push({
|
|
257
|
+
type: 'track',
|
|
258
|
+
line: lineno,
|
|
259
|
+
track: 'music',
|
|
260
|
+
action: 'play',
|
|
261
|
+
sound: null,
|
|
262
|
+
file: m[1],
|
|
263
|
+
overrides,
|
|
264
|
+
});
|
|
265
|
+
declare('file', m[1], { line: lineno }, true);
|
|
266
|
+
flush();
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
m = DIRECTIVE_RE.exec(s);
|
|
270
|
+
const directive = m && reg.directives.get(m[1]);
|
|
271
|
+
if (m && directive) {
|
|
272
|
+
const data = directive((m[2] ?? '').trim(), {
|
|
273
|
+
error: (msg) => errs.push(msg),
|
|
274
|
+
suggest: (w, o) => didYouMean(w, o),
|
|
275
|
+
});
|
|
276
|
+
if (!errs.length)
|
|
277
|
+
items.push({ type: 'mark', line: lineno, kind: m[1], data: data || {} });
|
|
278
|
+
bodyStarted = true;
|
|
279
|
+
flush();
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (m && !reg.reserved.has(m[1])) {
|
|
283
|
+
const name = m[1];
|
|
284
|
+
if (!knownTrack(name)) {
|
|
285
|
+
const upper = name.toUpperCase();
|
|
286
|
+
problem(`unknown track \`${name}\`` +
|
|
287
|
+
(upper in sounds
|
|
288
|
+
? ` — sound names are UPPERCASE: \`[${upper}${m[2] ? ' ' + m[2] : ''}]\``
|
|
289
|
+
: didYouMean(name, allTracks())) +
|
|
290
|
+
`\nBuilt-in tracks: ${reg.tracks.join(', ')}. Declare others under @tracks, ` +
|
|
291
|
+
`e.g. \`${name}: { level: -24 }\``);
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
items.push(trackDirective(name, m[2] ?? '', lineno, s, errs));
|
|
295
|
+
flush();
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (mode === 'sounds') {
|
|
299
|
+
m = SOUND_DECL_RE.exec(s);
|
|
300
|
+
if (m) {
|
|
301
|
+
const cfg = check(readConfig(m[2], errs), reg.schemas.sound, opts(`sound \`${m[1]}\``), errs);
|
|
302
|
+
if (!cfg.prompt && !cfg.file) {
|
|
303
|
+
errs.push(`sound \`${m[1]}\` needs a source: \`file: path/to.mp3\` or \`prompt: "what it sounds like"\``);
|
|
304
|
+
}
|
|
305
|
+
else if (cfg.prompt && cfg.file) {
|
|
306
|
+
errs.push(`sound \`${m[1]}\` has both \`file:\` and \`prompt:\` — keep one`);
|
|
307
|
+
}
|
|
308
|
+
sounds[m[1]] = cfg;
|
|
309
|
+
declare('sound', m[1], { line: lineno });
|
|
310
|
+
flush();
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (mode !== 'body') {
|
|
315
|
+
m = CHAR_DECL_RE.exec(s);
|
|
316
|
+
if (m) {
|
|
317
|
+
const cfg = check(readConfig(m[2], errs), reg.schemas.character, opts(`character \`${m[1]}\``), errs);
|
|
318
|
+
const alias = cfg.voice ?? cfg.speaker;
|
|
319
|
+
if (alias && !cfg.persona)
|
|
320
|
+
cfg.persona = String(alias).toLowerCase();
|
|
321
|
+
characters[m[1]] = cfg;
|
|
322
|
+
declare('character', m[1], { line: lineno });
|
|
323
|
+
mode = 'chars';
|
|
324
|
+
flush();
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (mode === 'chars' && !bodyStarted) {
|
|
329
|
+
m = V1_CHAR_RE.exec(s);
|
|
330
|
+
if (m) {
|
|
331
|
+
problem(`character definitions need braces: \`${m[1]}: { persona: ${m[2]} }\` ` +
|
|
332
|
+
`(\`${m[1]}: ${m[2]}\` is the old v1 form)`);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
m = CUE_RE.exec(s);
|
|
337
|
+
if (m) {
|
|
338
|
+
const overrides = check(readPlay(m[2] ?? ''), reg.schemas.play, opts(`[${m[1]}]`), errs);
|
|
339
|
+
refs.push({ line: lineno, text: s, sound: m[1] });
|
|
340
|
+
items.push({ type: 'cue', line: lineno, sound: m[1], track: 'foreground', overrides });
|
|
341
|
+
flush();
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
m = LINE_RE.exec(s);
|
|
345
|
+
if (m) {
|
|
346
|
+
mode = 'body';
|
|
347
|
+
bodyStarted = true;
|
|
348
|
+
const lineText = m[3].trim();
|
|
349
|
+
const overrides = check(readOverrides(m[2] ?? '', errs), reg.schemas.line, opts(`\`${m[1]}[…]\``, 'line'), errs);
|
|
350
|
+
const segments = splitSegments(lineText).map((seg) => {
|
|
351
|
+
if (seg.type === 'break')
|
|
352
|
+
return { type: 'break', seconds: seg.seconds };
|
|
353
|
+
if (seg.type === 'sfx') {
|
|
354
|
+
refs.push({ line: lineno, text: s, sound: seg.sound });
|
|
355
|
+
return {
|
|
356
|
+
type: 'sfx',
|
|
357
|
+
sound: seg.sound,
|
|
358
|
+
overrides: check(readPlay(seg.raw), reg.schemas.play, opts(`<sfx ${seg.sound}>`), errs),
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
const ov = seg.raw
|
|
362
|
+
? check(readOverrides(seg.raw, errs), reg.schemas.span, opts('an inline `{…: …}` span', 'span'), errs)
|
|
363
|
+
: {};
|
|
364
|
+
return { type: 'text', text: seg.text, overrides: ov };
|
|
365
|
+
});
|
|
366
|
+
items.push({ type: 'line', line: lineno, speaker: m[1], text: lineText, overrides, segments });
|
|
367
|
+
declare('speaker', m[1], { line: lineno }, true);
|
|
368
|
+
flush();
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
problem(hintFor(s, mode));
|
|
372
|
+
}
|
|
373
|
+
/** `[music BED fade-in:3]`, `[music off]`, `[music vol:0.5 ramp:2]` */
|
|
374
|
+
function trackDirective(name, rest, lineno, s, errs) {
|
|
375
|
+
let tokens = rest
|
|
376
|
+
.trim()
|
|
377
|
+
.split(/[,\s]+/)
|
|
378
|
+
.filter(Boolean);
|
|
379
|
+
let sound = null;
|
|
380
|
+
let action = 'set';
|
|
381
|
+
if (tokens.length && ['off', 'none', 'stop'].includes(tokens[0].toLowerCase())) {
|
|
382
|
+
action = 'off';
|
|
383
|
+
tokens = tokens.slice(1);
|
|
384
|
+
if (tokens.length && SOUND_NAME_RE.test(tokens[0]))
|
|
385
|
+
[sound, ...tokens] = tokens;
|
|
386
|
+
}
|
|
387
|
+
else if (tokens.length && SOUND_NAME_RE.test(tokens[0])) {
|
|
388
|
+
action = 'play';
|
|
389
|
+
[sound, ...tokens] = tokens;
|
|
390
|
+
}
|
|
391
|
+
const pairs = readPlay(tokens.join(','));
|
|
392
|
+
let schema;
|
|
393
|
+
if (action === 'play')
|
|
394
|
+
schema = reg.schemas.play;
|
|
395
|
+
else if (action === 'off')
|
|
396
|
+
schema = reg.schemas.off;
|
|
397
|
+
else if (!bodyStarted)
|
|
398
|
+
schema = { ...reg.schemas.track, ...reg.schemas.set };
|
|
399
|
+
else
|
|
400
|
+
schema = reg.schemas.set;
|
|
401
|
+
if (action === 'set') {
|
|
402
|
+
if (!pairs.length) {
|
|
403
|
+
errs.push(`\`[${name}]\` does nothing — play a sound (\`[${name} SOUND]\`), stop (\`[${name} off]\`), ` +
|
|
404
|
+
`or set something (\`[${name} vol:0.5]\`)`);
|
|
405
|
+
}
|
|
406
|
+
else if (bodyStarted) {
|
|
407
|
+
const extra = pairs.filter((p) => !(p.key in schema));
|
|
408
|
+
if (extra.length && extra.every((p) => p.key in reg.schemas.track)) {
|
|
409
|
+
const e = extra[0];
|
|
410
|
+
errs.push(`\`${extra.map((p) => shown(p.key)).join(', ')}\` can't change mid-scene — only \`vol\` ` +
|
|
411
|
+
`(with \`ramp\`) moves over time.\nPut it on the track instead: under @tracks, ` +
|
|
412
|
+
`\`${name}: { ${shown(e.key)}: ${e.value} }\``);
|
|
413
|
+
return { type: 'track', line: lineno, track: name, action, sound, overrides: {} };
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const what = action === 'play' ? `a [${name} …] play` : action === 'off' ? `[${name} off]` : `[${name} …]`;
|
|
418
|
+
const overrides = check(pairs, schema, opts(what), errs);
|
|
419
|
+
const item = { type: 'track', line: lineno, track: name, action, sound, overrides };
|
|
420
|
+
for (const rule of reg.trackRules.get(name) ?? [])
|
|
421
|
+
rule(item, { error: (msg) => errs.push(msg) });
|
|
422
|
+
if (sound)
|
|
423
|
+
refs.push({ line: lineno, text: s, sound });
|
|
424
|
+
return item;
|
|
425
|
+
}
|
|
426
|
+
// two banks defining one name is an error unless the script settles it
|
|
427
|
+
for (const c of inc.clashes) {
|
|
428
|
+
if (index[c.kind][c.name]?.include !== undefined)
|
|
429
|
+
problems.push(c.problem);
|
|
430
|
+
}
|
|
431
|
+
for (const r of refs) {
|
|
432
|
+
if (!(r.sound in sounds)) {
|
|
433
|
+
problems.push({
|
|
434
|
+
line: r.line,
|
|
435
|
+
text: r.text,
|
|
436
|
+
message: `sound \`${r.sound}\` isn't declared under @sounds` +
|
|
437
|
+
didYouMean(r.sound, Object.keys(sounds)) +
|
|
438
|
+
`\nAdd it, e.g. \`${r.sound}: { file: sounds/${r.sound.toLowerCase()}.wav }\` ` +
|
|
439
|
+
`or \`${r.sound}: { prompt: "…", duration: 3 }\``,
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
// a bare [CUE] plays on the track its sound names, else on foreground
|
|
444
|
+
for (const it of items) {
|
|
445
|
+
if (it.type !== 'cue')
|
|
446
|
+
continue;
|
|
447
|
+
const cfg = sounds[it.sound] ?? {};
|
|
448
|
+
const lay = (cfg.track ?? cfg.layer);
|
|
449
|
+
if (lay && knownTrack(lay))
|
|
450
|
+
it.track = lay;
|
|
451
|
+
}
|
|
452
|
+
const scene = {
|
|
453
|
+
characters,
|
|
454
|
+
sounds,
|
|
455
|
+
tracks: Object.fromEntries(Object.entries(tracks).map(([k, v]) => [k, v ?? {}])),
|
|
456
|
+
items,
|
|
457
|
+
problems,
|
|
458
|
+
index,
|
|
459
|
+
};
|
|
460
|
+
for (const finalize of reg.finalizers) {
|
|
461
|
+
finalize(scene, {
|
|
462
|
+
problem: (item, message) => problems.push({ line: item.line, text: lines[item.line - 1]?.trim() ?? null, message }),
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
if (problems.length && options.strict !== false)
|
|
466
|
+
throw new ConvError(problems, options.source);
|
|
467
|
+
scene.problems = new ConvError(problems).problems;
|
|
468
|
+
return scene;
|
|
469
|
+
}
|
package/esm/plugin.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { CORE_RESERVED, CORE_SCHEMAS, CORE_TRACKS, KEY_CONTEXTS, } from './schema';
|
|
2
|
+
export function buildRegistry(plugins) {
|
|
3
|
+
const schemas = {};
|
|
4
|
+
for (const ctx of KEY_CONTEXTS)
|
|
5
|
+
schemas[ctx] = { ...CORE_SCHEMAS[ctx] };
|
|
6
|
+
const reg = {
|
|
7
|
+
schemas,
|
|
8
|
+
enums: {},
|
|
9
|
+
tracks: [...CORE_TRACKS],
|
|
10
|
+
reserved: new Set(CORE_RESERVED),
|
|
11
|
+
directives: new Map(),
|
|
12
|
+
trackRules: new Map(),
|
|
13
|
+
finalizers: [],
|
|
14
|
+
};
|
|
15
|
+
const owner = new Map();
|
|
16
|
+
for (const p of plugins) {
|
|
17
|
+
for (const [ctx, keys] of Object.entries(p.keys ?? {})) {
|
|
18
|
+
Object.assign(reg.schemas[ctx], keys);
|
|
19
|
+
}
|
|
20
|
+
for (const [name, values] of Object.entries(p.enums ?? {})) {
|
|
21
|
+
const have = reg.enums[name] ?? [];
|
|
22
|
+
reg.enums[name] = [...have, ...values.filter((v) => !have.includes(v))];
|
|
23
|
+
}
|
|
24
|
+
for (const t of p.tracks ?? [])
|
|
25
|
+
if (!reg.tracks.includes(t))
|
|
26
|
+
reg.tracks.push(t);
|
|
27
|
+
for (const r of p.reserved ?? [])
|
|
28
|
+
reg.reserved.add(r);
|
|
29
|
+
for (const [name, fn] of Object.entries(p.directives ?? {})) {
|
|
30
|
+
if (reg.directives.has(name)) {
|
|
31
|
+
throw new Error(`conv plugins "${owner.get(name)}" and "${p.name}" both define the [${name}] directive`);
|
|
32
|
+
}
|
|
33
|
+
if (reg.tracks.includes(name)) {
|
|
34
|
+
throw new Error(`conv plugin "${p.name}": [${name}] is already a track`);
|
|
35
|
+
}
|
|
36
|
+
reg.directives.set(name, fn);
|
|
37
|
+
reg.reserved.add(name);
|
|
38
|
+
owner.set(name, p.name);
|
|
39
|
+
}
|
|
40
|
+
for (const [name, rule] of Object.entries(p.trackRules ?? {})) {
|
|
41
|
+
reg.trackRules.set(name, [...(reg.trackRules.get(name) ?? []), rule]);
|
|
42
|
+
}
|
|
43
|
+
if (p.finalize)
|
|
44
|
+
reg.finalizers.push(p.finalize.bind(p));
|
|
45
|
+
}
|
|
46
|
+
return reg;
|
|
47
|
+
}
|
|
48
|
+
export function enumValues(reg, spec) {
|
|
49
|
+
return Array.isArray(spec.values) ? spec.values : (reg.enums[spec.values.named] ?? []);
|
|
50
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { bool, named, num } from '../schema';
|
|
2
|
+
/** Per-character, per-line, per-span and per-track effect keys. */
|
|
3
|
+
export function effects(options = {}) {
|
|
4
|
+
const keys = {
|
|
5
|
+
reverb: named('reverb'),
|
|
6
|
+
reverb_mix: num(0, 1),
|
|
7
|
+
delay: num(0.01, 5),
|
|
8
|
+
delay_feedback: num(0, 0.95),
|
|
9
|
+
delay_mix: num(0, 1),
|
|
10
|
+
compress: bool(),
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
name: 'effects',
|
|
14
|
+
enums: { reverb: options.reverbPresets ?? ['room', 'hall', 'cathedral', 'plate'] },
|
|
15
|
+
keys: { character: keys, line: keys, span: keys, track: keys },
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { effects } from './effects';
|
|
2
|
+
import { slides } from './slides';
|
|
3
|
+
import { speakers } from './speakers';
|
|
4
|
+
import { video } from './video';
|
|
5
|
+
export { effects } from './effects';
|
|
6
|
+
export { slides } from './slides';
|
|
7
|
+
export { SPEAKER_WIDGETS, speakers } from './speakers';
|
|
8
|
+
export { video } from './video';
|
|
9
|
+
/** The plugins `parse` uses when none are given. */
|
|
10
|
+
export const defaultPlugins = [effects(), slides(), speakers(), video()];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `[slide ID]` / `[slide ID step:N]` — a deck shows slide ID (and its Nth build)
|
|
3
|
+
* from this point. Silent and timeless: a renderer records when it falls.
|
|
4
|
+
*/
|
|
5
|
+
export function slides() {
|
|
6
|
+
return {
|
|
7
|
+
name: 'slides',
|
|
8
|
+
directives: {
|
|
9
|
+
slide(args, ctx) {
|
|
10
|
+
let id = null;
|
|
11
|
+
let step = 0;
|
|
12
|
+
let bad = false;
|
|
13
|
+
for (const t of args.split(/[\s,]+/).filter(Boolean)) {
|
|
14
|
+
const i = t.indexOf(':');
|
|
15
|
+
if (i >= 0) {
|
|
16
|
+
const k = t.slice(0, i);
|
|
17
|
+
const v = t.slice(i + 1);
|
|
18
|
+
if (k !== 'step') {
|
|
19
|
+
ctx.error(`\`${k}:\` isn't a slide option — use \`[slide ID]\` or \`[slide ID step:N]\`` +
|
|
20
|
+
ctx.suggest(k, ['step']));
|
|
21
|
+
bad = true;
|
|
22
|
+
}
|
|
23
|
+
else if (!/^\d+$/.test(v)) {
|
|
24
|
+
ctx.error(`\`step:${v}\` — step is a whole number of builds, e.g. \`step:1\``);
|
|
25
|
+
bad = true;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
step = parseInt(v, 10);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (id === null && /^[a-z0-9][a-z0-9_-]*$/.test(t)) {
|
|
32
|
+
id = t;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
ctx.error(id === null
|
|
36
|
+
? `\`${t}\` — a slide id is lowercase, like the deck's ids (\`title\`, \`market-opportunity\`)`
|
|
37
|
+
: `\`${t}\` — one slide per marker; put the next on its own line`);
|
|
38
|
+
bad = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (id === null && !bad) {
|
|
42
|
+
ctx.error('`[slide]` needs the slide\'s id, e.g. `[slide problem]` or `[slide problem step:1]`');
|
|
43
|
+
}
|
|
44
|
+
return { id, step };
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { str } from '../schema';
|
|
2
|
+
/** What a player can show for whoever is speaking. */
|
|
3
|
+
export const SPEAKER_WIDGETS = ['faces', 'lower-third', 'waveform', 'captions'];
|
|
4
|
+
const ALWAYS = 'lower-third:always';
|
|
5
|
+
/** `faces` leaves the mouth alone (still is the default; `faces:still` says the same); `faces:talking` moves it with the voice. */
|
|
6
|
+
const TALKING = 'faces:talking';
|
|
7
|
+
/**
|
|
8
|
+
* `[speakers faces lower-third waveform captions]`, `[speakers all]`, `[speakers off]`
|
|
9
|
+
* — which on-screen widgets a player shows from here. Characters gain `name:` and
|
|
10
|
+
* `title:` for the lower-third.
|
|
11
|
+
*/
|
|
12
|
+
export function speakers() {
|
|
13
|
+
const choices = [...SPEAKER_WIDGETS, ALWAYS, TALKING, 'all', 'off'];
|
|
14
|
+
return {
|
|
15
|
+
name: 'speakers',
|
|
16
|
+
keys: { character: { name: str(), title: str() } },
|
|
17
|
+
directives: {
|
|
18
|
+
speakers(args, ctx) {
|
|
19
|
+
const want = args
|
|
20
|
+
.toLowerCase()
|
|
21
|
+
.split(/[\s,+]+/)
|
|
22
|
+
.filter(Boolean)
|
|
23
|
+
.map((w) => (w === 'lower-third:first' ? 'lower-third' : w === 'faces:still' ? 'faces' : w));
|
|
24
|
+
if (!want.length) {
|
|
25
|
+
ctx.error('`[speakers]` needs what to show, e.g. `[speakers faces lower-third]`, `[speakers all]` ' +
|
|
26
|
+
`or \`[speakers off]\` — widgets: ${SPEAKER_WIDGETS.join(', ')}`);
|
|
27
|
+
}
|
|
28
|
+
for (const w of want.filter((x) => !choices.includes(x))) {
|
|
29
|
+
ctx.error(`\`${w}\` isn't a speaker widget` +
|
|
30
|
+
ctx.suggest(w, choices) +
|
|
31
|
+
` — widgets: ${SPEAKER_WIDGETS.join(', ')} (\`${ALWAYS}\` to show it on every line, ` +
|
|
32
|
+
`\`${TALKING}\` for a face that moves its mouth with the voice), or \`all\` / \`off\``);
|
|
33
|
+
}
|
|
34
|
+
if ((want.includes('off') || want.includes('all')) && want.length > 1) {
|
|
35
|
+
ctx.error('`off` and `all` stand alone: `[speakers off]`, `[speakers all]`');
|
|
36
|
+
}
|
|
37
|
+
let show;
|
|
38
|
+
if (want[0] === 'off' && want.length === 1)
|
|
39
|
+
show = [];
|
|
40
|
+
else if (want[0] === 'all' && want.length === 1)
|
|
41
|
+
show = [...SPEAKER_WIDGETS];
|
|
42
|
+
else
|
|
43
|
+
show = [...SPEAKER_WIDGETS, ALWAYS, TALKING].filter((w) => want.includes(w));
|
|
44
|
+
if (show.includes(ALWAYS))
|
|
45
|
+
show = show.filter((w) => w !== 'lower-third');
|
|
46
|
+
if (show.includes(TALKING))
|
|
47
|
+
show = show.filter((w) => w !== 'faces');
|
|
48
|
+
return { show };
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|