@senseihq/emaki-cli 0.0.0-stage → 0.1.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 (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +34 -2
  3. package/package.json +42 -4
  4. package/plugin/.claude-plugin/plugin.json +19 -0
  5. package/plugin/PLAYBOOK.md +137 -0
  6. package/plugin/README.md +51 -0
  7. package/plugin/agents/fact-checker.md +28 -0
  8. package/plugin/agents/localizer.md +34 -0
  9. package/plugin/agents/scene-builder.md +43 -0
  10. package/plugin/agents/story-planner.md +42 -0
  11. package/plugin/agents/story-writer.md +34 -0
  12. package/plugin/agents/visual-reviewer.md +33 -0
  13. package/plugin/modules/campaign/README.md +23 -0
  14. package/plugin/modules/campaign/calendar.csv +2 -0
  15. package/plugin/modules/campaign/day-script.template.md +41 -0
  16. package/plugin/modules/posting-desk/README.md +37 -0
  17. package/plugin/skills/emaki/SKILL.md +107 -0
  18. package/plugin/skills/emaki/references/01-interview.md +68 -0
  19. package/plugin/skills/emaki/references/02-architecture.md +69 -0
  20. package/plugin/skills/emaki/references/03-content-model.md +65 -0
  21. package/plugin/skills/emaki/references/04-scene-authoring.md +96 -0
  22. package/plugin/skills/emaki/references/05-voice-and-video.md +64 -0
  23. package/plugin/skills/emaki/references/06-i18n.md +37 -0
  24. package/plugin/skills/emaki/references/07-quality-gates.md +44 -0
  25. package/plugin/skills/emaki/references/08-feedback-loop.md +32 -0
  26. package/plugin/skills/emaki/references/09-publishing.md +11 -0
  27. package/plugin/skills/story-build/SKILL.md +37 -0
  28. package/plugin/skills/story-localize/SKILL.md +24 -0
  29. package/plugin/skills/story-media/SKILL.md +36 -0
  30. package/plugin/skills/story-publish/SKILL.md +22 -0
  31. package/plugin/skills/story-review/SKILL.md +29 -0
  32. package/plugin/templates/brief.md +32 -0
  33. package/plugin/templates/change-request.md +7 -0
  34. package/plugin/templates/changelog.md +4 -0
  35. package/plugin/templates/decisions.md +5 -0
  36. package/plugin/templates/glossary.md +4 -0
  37. package/plugin/templates/item-card.md +34 -0
  38. package/plugin/templates/outline.md +19 -0
  39. package/plugin/templates/review-sheet.md +12 -0
  40. package/plugin/templates/style-guide.md +21 -0
  41. package/plugin/templates/tracker.md +11 -0
  42. package/src/api.js +9 -0
  43. package/src/capture.js +169 -0
  44. package/src/config.js +32 -0
  45. package/src/films.js +145 -0
  46. package/src/index.js +195 -0
  47. package/src/init.js +109 -0
  48. package/src/plugin.js +106 -0
  49. package/src/tts.js +290 -0
  50. package/src/util.js +59 -0
  51. package/src/voice.js +216 -0
  52. package/templates/content/films/index.json +1 -0
  53. package/templates/content/films/measure-the-right-work/film.json +59 -0
  54. package/templates/content/films/measure-the-right-work/look.css +16 -0
  55. package/templates/content/films/measure-the-right-work/scene-close.json +153 -0
  56. package/templates/content/films/measure-the-right-work/scene-groups.json +99 -0
  57. package/templates/content/films/measure-the-right-work/scene-hook.json +96 -0
  58. package/templates/content/films/measure-the-right-work/scene-pair.json +190 -0
  59. package/templates/content/films/measure-the-right-work/scene-problem.json +162 -0
  60. package/templates/content/films/measure-the-right-work/score.js +19 -0
  61. package/templates/content/films/measure-the-right-work/strings/en.json +50 -0
  62. package/templates/content/films/measure-the-right-work/strings/es.json +50 -0
  63. package/templates/content/films/measure-the-right-work/voice/en.json +7 -0
  64. package/templates/content/films/measure-the-right-work/voice/es.json +7 -0
  65. package/templates/content/logos/brand.svg +1 -0
  66. package/templates/content/site.json +10 -0
  67. package/templates/content/stories/en/measure-the-right-work.json +13 -0
  68. package/templates/content/stories/es/measure-the-right-work.json +13 -0
  69. package/templates/content/stories/index.json +12 -0
  70. package/templates/content/ui/en.json +1 -0
  71. package/templates/content/ui/es.json +1 -0
  72. package/templates/emaki.config.json +67 -0
  73. package/templates/film/look.css +16 -0
  74. package/templates/film/scene-close.json +153 -0
  75. package/templates/film/scene-template.json +71 -0
  76. package/templates/film/score.js +12 -0
package/src/tts.js ADDED
@@ -0,0 +1,290 @@
1
+ // Text-to-speech providers for the voice step. Pick one per locale in emaki.config.json (or `emaki set-voice`):
2
+ //
3
+ // "voice": {
4
+ // "default": { "provider": "piper", "model": "voices/en_US-lessac-medium.onnx" },
5
+ // "es": { "provider": "azure", "voice": "es-ES-ElviraNeural" }
6
+ // }
7
+ //
8
+ // Every provider writes a 48 kHz mono WAV with 0.1 s of air at the head (what fit and build expect). Keys come from
9
+ // environment variables only, never from the config file.
10
+ //
11
+ // | provider | runs | needs |
12
+ // |------------|------------------|-----------------------------------------------------------------|
13
+ // | piper | offline, any OS | `piper` (pip install piper-tts) + a voice .onnx/.onnx.json |
14
+ // | kokoro | offline, any OS | python3 with kokoro + soundfile (pip install kokoro soundfile) |
15
+ // | macos | offline, macOS | the built-in `say` |
16
+ // | espeak | offline, any OS | espeak-ng (robotic: timing drafts) |
17
+ // | elevenlabs | cloud | ELEVENLABS_API_KEY |
18
+ // | openai | cloud | OPENAI_API_KEY (Azure OpenAI: AZURE_OPENAI_ENDPOINT + AZURE_OPENAI_API_KEY) |
19
+ // | azure | cloud (Microsoft)| AZURE_SPEECH_KEY + AZURE_SPEECH_REGION |
20
+ // | google | cloud (Google) | GOOGLE_TTS_API_KEY, or `gcloud auth login` |
21
+ // | polly | cloud (AWS) | the `aws` CLI with credentials |
22
+ // | manual | a person | recorded take-NN.wav files |
23
+ import { spawnSync } from 'node:child_process'
24
+ import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
25
+ import { tmpdir } from 'node:os'
26
+ import { join } from 'node:path'
27
+ import { which } from './util.js'
28
+
29
+ export class TTSError extends Error {}
30
+ const fail = (m) => { throw new TTSError(m) }
31
+ const lang = (locale) => locale.split('-')[0]
32
+
33
+ function sh(cmd, args, input) {
34
+ const r = spawnSync(cmd, args, { input, encoding: input == null ? 'utf-8' : undefined, maxBuffer: 256 * 1024 * 1024 })
35
+ if (r.error) fail(`${cmd}: ${r.error.code === 'ENOENT' ? 'not installed' : r.error.message}`)
36
+ if (r.status) fail(`${cmd}: ${String(r.stderr ?? '').slice(-400)}`)
37
+ return r.stdout
38
+ }
39
+
40
+ /** Any audio file or bytes → 48 kHz mono WAV with 0.1 s head air at dst. */
41
+ export function toTake(src, dst) {
42
+ const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
43
+ try {
44
+ let file = src
45
+ if (typeof src !== 'string') { file = join(dir, 'raw'); writeFileSync(file, src) }
46
+ sh('ffmpeg', ['-v', 'error', '-y', '-i', file, '-af', 'adelay=100|100', '-ar', '48000', '-ac', '1', dst])
47
+ } finally {
48
+ rmSync(dir, { recursive: true, force: true })
49
+ }
50
+ }
51
+ function inTemp(fn) {
52
+ const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
53
+ try { return fn(dir) } finally { rmSync(dir, { recursive: true, force: true }) }
54
+ }
55
+
56
+ function env(...names) {
57
+ for (const n of names) if (process.env[n]) return process.env[n]
58
+ return fail(`set ${names.join(' or ')} in the environment (never in files)`)
59
+ }
60
+
61
+ async function post(url, body, headers, as = 'bytes') {
62
+ const r = await fetch(url, { method: 'POST', body: typeof body === 'string' || body instanceof Uint8Array ? body : JSON.stringify(body), headers })
63
+ if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status} ${(await r.text()).slice(0, 300)}`)
64
+ return as === 'json' ? r.json() : Buffer.from(await r.arrayBuffer())
65
+ }
66
+ async function get(url, headers) {
67
+ const r = await fetch(url, { headers })
68
+ if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status}`)
69
+ return r.json()
70
+ }
71
+
72
+ // ── Providers: check(cfg, locale) throws TTSError when not ready; synth(text, dst, cfg, locale, ctx) ─────────────
73
+
74
+ const piper = {
75
+ label: 'Piper (offline)',
76
+ check(cfg) {
77
+ if (!which('piper')) fail('install it: pip install piper-tts')
78
+ if (!cfg.model || !existsSync(cfg.model)) fail(`voice model not found: "${cfg.model ?? ''}". Download a voice (.onnx and .onnx.json) from the Piper voices list and set "model"`)
79
+ },
80
+ async synth(text, dst, cfg) {
81
+ inTemp((d) => {
82
+ const raw = join(d, 'raw.wav')
83
+ const args = ['--model', cfg.model, '--output_file', raw]
84
+ if (cfg.speaker != null) args.push('--speaker', String(cfg.speaker))
85
+ if (cfg.length_scale) args.push('--length_scale', String(cfg.length_scale)) // >1 is slower
86
+ sh('piper', args, text)
87
+ if (!existsSync(raw)) fail('piper wrote nothing')
88
+ toTake(raw, dst)
89
+ })
90
+ },
91
+ voices: async () => ['Piper voices are files: pick one for this language from the Piper voice samples page, download its .onnx and .onnx.json, set "model".']
92
+ }
93
+
94
+ const KOKORO_LANG = { en: 'a', 'en-GB': 'b', es: 'e', fr: 'f', hi: 'h', it: 'i', ja: 'j', pt: 'p', zh: 'z' }
95
+ const KOKORO_PY = `import sys, json, numpy as np, soundfile as sf
96
+ from kokoro import KPipeline
97
+ a = json.loads(sys.argv[1]); text = sys.stdin.read()
98
+ pipe = KPipeline(lang_code=a['code'])
99
+ audio = [x for _, _, x in pipe(text, voice=a['voice'], speed=a['speed'])]
100
+ sf.write(a['out'], np.concatenate(audio), 24000)`
101
+ const kokoro = {
102
+ label: 'Kokoro (offline)',
103
+ check(cfg, locale) {
104
+ const py = cfg.python ?? 'python3'
105
+ const r = spawnSync(py, ['-c', 'import kokoro, soundfile'], { encoding: 'utf-8' })
106
+ if (r.error || r.status) fail(`install it: ${py} -m pip install kokoro soundfile (and espeak-ng for some languages)`)
107
+ if (!KOKORO_LANG[locale] && !KOKORO_LANG[lang(locale)]) fail(`kokoro has no voices for ${locale}`)
108
+ },
109
+ async synth(text, dst, cfg, locale) {
110
+ inTemp((d) => {
111
+ const out = join(d, 'raw.wav')
112
+ const code = cfg.lang_code ?? KOKORO_LANG[locale] ?? KOKORO_LANG[lang(locale)]
113
+ sh(cfg.python ?? 'python3', ['-c', KOKORO_PY, JSON.stringify({ code, voice: cfg.voice ?? 'af_heart', speed: cfg.speed ?? 1, out })], text)
114
+ toTake(out, dst)
115
+ })
116
+ },
117
+ voices: async () => ['af_heart, af_bella, am_michael, bf_emma, bm_george (en); ef_dora, em_alex (es); ff_siwis (fr); if_sara (it); pf_dora (pt); jf_alpha (ja); zf_xiaobei (zh); see the Kokoro VOICES list']
118
+ }
119
+
120
+ const macos = {
121
+ label: 'macOS say (offline, drafts)',
122
+ check() { if (!which('say')) fail('`say` exists only on macOS') },
123
+ async synth(text, dst, cfg) {
124
+ inTemp((d) => {
125
+ const raw = join(d, 'raw.aiff')
126
+ sh('say', ['-o', raw, ...(cfg.voice ? ['-v', cfg.voice] : []), ...(cfg.rate ? ['-r', String(cfg.rate)] : []), text])
127
+ toTake(raw, dst)
128
+ })
129
+ },
130
+ async voices(cfg, locale) {
131
+ const all = String(sh('say', ['-v', '?'])).split('\n').filter(Boolean)
132
+ const l = lang(locale), mine = all.filter((x) => x.includes(` ${l}_`) || x.includes(` ${l}-`))
133
+ return mine.length ? mine : all.slice(0, 40)
134
+ }
135
+ }
136
+
137
+ const espeak = {
138
+ label: 'eSpeak NG (offline, robotic)',
139
+ check() { if (!which('espeak-ng')) fail('install espeak-ng (brew/apt/choco)') },
140
+ async synth(text, dst, cfg, locale) {
141
+ inTemp((d) => {
142
+ const raw = join(d, 'raw.wav')
143
+ sh('espeak-ng', ['-v', cfg.voice ?? locale, '-s', String(cfg.speed ?? 160), '-w', raw, text])
144
+ toTake(raw, dst)
145
+ })
146
+ },
147
+ voices: async (cfg, locale) => String(sh('espeak-ng', [`--voices=${lang(locale)}`])).split('\n').filter(Boolean)
148
+ }
149
+
150
+ const elevenlabs = {
151
+ label: 'ElevenLabs',
152
+ check(cfg) {
153
+ env('ELEVENLABS_API_KEY')
154
+ if (!cfg.voice_id && !cfg.voice) fail('set "voice_id"')
155
+ },
156
+ async synth(text, dst, cfg, locale, ctx) {
157
+ const body = {
158
+ text, model_id: cfg.model ?? 'eleven_multilingual_v2', language_code: lang(locale),
159
+ voice_settings: { stability: cfg.stability ?? 0.55, similarity_boost: cfg.similarity ?? 0.8 }
160
+ }
161
+ if (ctx.prev) body.previous_text = ctx.prev
162
+ if (ctx.next) body.next_text = ctx.next
163
+ const audio = await post(`https://api.elevenlabs.io/v1/text-to-speech/${cfg.voice_id ?? cfg.voice}?output_format=mp3_44100_128`, body,
164
+ { 'xi-api-key': env('ELEVENLABS_API_KEY'), 'content-type': 'application/json' })
165
+ toTake(audio, dst)
166
+ },
167
+ async voices() {
168
+ const data = await get('https://api.elevenlabs.io/v1/voices', { 'xi-api-key': env('ELEVENLABS_API_KEY') })
169
+ return (data.voices ?? []).map((v) => `${v.voice_id} ${v.name} ${JSON.stringify(v.labels ?? {})}`)
170
+ }
171
+ }
172
+
173
+ const openai = {
174
+ label: 'OpenAI (or Azure OpenAI)',
175
+ check() {
176
+ if (process.env.AZURE_OPENAI_ENDPOINT) env('AZURE_OPENAI_API_KEY')
177
+ else env('OPENAI_API_KEY')
178
+ },
179
+ async synth(text, dst, cfg) {
180
+ const body = { model: cfg.model ?? 'gpt-4o-mini-tts', voice: cfg.voice ?? 'alloy', input: text, response_format: 'wav' }
181
+ if (cfg.instructions) body.instructions = cfg.instructions // e.g. "Calm, warm, unhurried explainer."
182
+ let url, headers
183
+ if (process.env.AZURE_OPENAI_ENDPOINT) {
184
+ url = `${process.env.AZURE_OPENAI_ENDPOINT.replace(/\/$/, '')}/openai/deployments/${cfg.deployment ?? body.model}/audio/speech?api-version=${cfg.api_version ?? '2025-03-01-preview'}`
185
+ headers = { 'api-key': env('AZURE_OPENAI_API_KEY'), 'content-type': 'application/json' }
186
+ } else {
187
+ url = 'https://api.openai.com/v1/audio/speech'
188
+ headers = { authorization: `Bearer ${env('OPENAI_API_KEY')}`, 'content-type': 'application/json' }
189
+ }
190
+ toTake(await post(url, body, headers), dst)
191
+ },
192
+ voices: async () => ['alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse (all speak every supported language; set "instructions" for tone)']
193
+ }
194
+
195
+ const xml = (s) => s.replace(/[<>&'"]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;', "'": '&apos;', '"': '&quot;' })[c])
196
+ const azure = {
197
+ label: 'Azure AI Speech',
198
+ check(cfg) {
199
+ env('AZURE_SPEECH_KEY'); env('AZURE_SPEECH_REGION')
200
+ if (!cfg.voice) fail('set "voice", e.g. "en-US-AvaMultilingualNeural" or "es-ES-ElviraNeural"')
201
+ },
202
+ async synth(text, dst, cfg, locale) {
203
+ const ssml = `<speak version='1.0' xml:lang='${locale}'><voice name='${cfg.voice}'><prosody rate='${cfg.rate ?? '0%'}'>${xml(text)}</prosody></voice></speak>`
204
+ const audio = await post(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/v1`, ssml, {
205
+ 'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY'), 'content-type': 'application/ssml+xml',
206
+ 'X-Microsoft-OutputFormat': 'riff-48khz-16bit-mono-pcm', 'user-agent': 'emaki'
207
+ })
208
+ toTake(audio, dst)
209
+ },
210
+ async voices(cfg, locale) {
211
+ const data = await get(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/voices/list`, { 'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY') })
212
+ return data.filter((v) => v.Locale.startsWith(locale) || v.Locale.split('-')[0] === lang(locale)).map((v) => `${v.ShortName} ${v.Gender}`)
213
+ }
214
+ }
215
+
216
+ function googleAuth() {
217
+ if (process.env.GOOGLE_TTS_API_KEY) return { 'X-Goog-Api-Key': process.env.GOOGLE_TTS_API_KEY }
218
+ if (which('gcloud')) {
219
+ const tok = spawnSync('gcloud', ['auth', 'print-access-token'], { encoding: 'utf-8' }).stdout?.trim()
220
+ if (tok) return { authorization: `Bearer ${tok}` }
221
+ }
222
+ return fail('set GOOGLE_TTS_API_KEY, or sign in with `gcloud auth login`')
223
+ }
224
+ const google = {
225
+ label: 'Google Cloud Text-to-Speech',
226
+ check(cfg) {
227
+ googleAuth()
228
+ if (!cfg.voice) fail('set "voice", e.g. "en-US-Neural2-F" or "es-ES-Chirp3-HD-Aoede"')
229
+ },
230
+ async synth(text, dst, cfg) {
231
+ const body = {
232
+ input: { text }, voice: { languageCode: cfg.voice.split('-').slice(0, 2).join('-'), name: cfg.voice },
233
+ audioConfig: { audioEncoding: 'LINEAR16', sampleRateHertz: 48000, speakingRate: cfg.speed ?? 1 }
234
+ }
235
+ const data = await post('https://texttospeech.googleapis.com/v1/text:synthesize', body, { ...googleAuth(), 'content-type': 'application/json' }, 'json')
236
+ toTake(Buffer.from(data.audioContent, 'base64'), dst)
237
+ },
238
+ async voices(cfg, locale) {
239
+ const data = await get(`https://texttospeech.googleapis.com/v1/voices?languageCode=${locale}`, googleAuth())
240
+ return (data.voices ?? []).map((v) => `${v.name} ${v.ssmlGender}`)
241
+ }
242
+ }
243
+
244
+ const polly = {
245
+ label: 'Amazon Polly',
246
+ check(cfg) {
247
+ if (!which('aws')) fail('install the AWS CLI and sign in (aws configure / SSO); credentials stay in your AWS profile')
248
+ if (!cfg.voice) fail('set "voice", e.g. "Joanna" (en-US) or "Lucia" (es-ES)')
249
+ },
250
+ async synth(text, dst, cfg, locale) {
251
+ inTemp((d) => {
252
+ const raw = join(d, 'raw.mp3')
253
+ sh('aws', ['polly', 'synthesize-speech', '--output-format', 'mp3', '--voice-id', cfg.voice, '--engine', cfg.engine ?? 'neural',
254
+ '--language-code', cfg.language ?? locale, '--text', text, ...(cfg.region ? ['--region', cfg.region] : []), raw])
255
+ toTake(raw, dst)
256
+ })
257
+ },
258
+ async voices(cfg, locale) {
259
+ const out = JSON.parse(String(sh('aws', ['polly', 'describe-voices', '--language-code', cfg.language ?? locale, '--engine', cfg.engine ?? 'neural', ...(cfg.region ? ['--region', cfg.region] : [])])))
260
+ return out.Voices.map((v) => `${v.Id} ${v.Gender}`)
261
+ }
262
+ }
263
+
264
+ const manual = {
265
+ label: 'Recorded by a person',
266
+ check() {},
267
+ async synth() { fail('manual: record the takes and place them as take-NN.wav; nothing to synthesise') },
268
+ voices: async () => ['Record one file per scene, in scene order, named take-01.wav, take-02.wav, …']
269
+ }
270
+
271
+ export const PROVIDERS = { piper, kokoro, macos, espeak, elevenlabs, openai, azure, google, polly, manual }
272
+ export const OFFLINE = ['piper', 'kokoro', 'macos', 'espeak']
273
+
274
+ export function provider(name) {
275
+ if (!PROVIDERS[name]) fail(`unknown provider "${name}"; one of: ${Object.keys(PROVIDERS).join(', ')}`)
276
+ return PROVIDERS[name]
277
+ }
278
+
279
+ /** The voice block for a locale: voice.default, then voice.<language>, then voice.<locale>; most specific wins. A block
280
+ * that switches provider starts fresh, so another provider's options (voice, model) don't carry over. */
281
+ export function voiceConfig(cfg, locale) {
282
+ const v = cfg.voice ?? {}
283
+ let out = {}
284
+ for (const k of ['default', lang(locale), locale]) {
285
+ const block = v[k] ?? {}
286
+ out = block.provider && block.provider !== out.provider ? { ...block } : { ...out, ...block }
287
+ }
288
+ return out
289
+ }
290
+
package/src/util.js ADDED
@@ -0,0 +1,59 @@
1
+ // Small helpers shared by the commands. Errors that the user should read go through die(), which throws a
2
+ // MotionError: the CLI prints its message and exits 1; tests catch it.
3
+ import { execFileSync, spawnSync } from 'node:child_process'
4
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
5
+ import { dirname } from 'node:path'
6
+
7
+ export class MotionError extends Error {}
8
+ /** @returns {never} */
9
+ export function die(msg) {
10
+ throw new MotionError(msg)
11
+ }
12
+
13
+ export const load = (p) => JSON.parse(readFileSync(p, 'utf-8'))
14
+ export function save(p, data) {
15
+ mkdirSync(dirname(p), { recursive: true })
16
+ writeFileSync(p, JSON.stringify(data, null, 2) + '\n')
17
+ }
18
+ export const exists = (p) => existsSync(p)
19
+
20
+ /** Runs a program (no shell) and returns stdout; dies with the tail of stderr on failure. */
21
+ export function run(cmd, args, opts = {}) {
22
+ const r = spawnSync(cmd, args, { encoding: 'utf-8', maxBuffer: 64 * 1024 * 1024, ...opts })
23
+ if (r.error) die(`${cmd}: ${r.error.code === 'ENOENT' ? 'not installed (not on PATH)' : r.error.message}`)
24
+ if (r.status) die(`${cmd} failed: ${(r.stderr || '').slice(-800)}`)
25
+ return r.stdout
26
+ }
27
+
28
+ /** True when `cmd` is on PATH. */
29
+ export function which(cmd) {
30
+ try {
31
+ execFileSync(process.platform === 'win32' ? 'where' : 'which', [cmd], { stdio: 'ignore' })
32
+ return true
33
+ } catch {
34
+ return false
35
+ }
36
+ }
37
+
38
+ /** Length of an audio or video file in seconds (ffprobe). */
39
+ export const mediaLength = (p) =>
40
+ parseFloat(run('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', p]).trim())
41
+
42
+ /** key=value pairs from --opt, values parsed as JSON when they are JSON (numbers, booleans). */
43
+ export function parseOpts(items) {
44
+ const out = {}
45
+ for (const it of [items ?? []].flat()) {
46
+ const i = String(it).indexOf('=')
47
+ if (i < 0) continue
48
+ const k = it.slice(0, i), v = it.slice(i + 1)
49
+ try {
50
+ out[k] = JSON.parse(v)
51
+ } catch {
52
+ out[k] = v
53
+ }
54
+ }
55
+ return out
56
+ }
57
+
58
+ export const pad2 = (n) => String(n).padStart(2, '0')
59
+ export const today = () => new Date().toISOString().slice(0, 10)
package/src/voice.js ADDED
@@ -0,0 +1,216 @@
1
+ // The narration: choose a voice per locale, synthesise takes, fit scene times to them, and build the final videos
2
+ // (voice + score + captions, -14 LUFS).
3
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
4
+ import { join } from 'node:path'
5
+ import { SUFFIX } from './config.js'
6
+ import { OFFLINE, provider, TTSError, voiceConfig } from './tts.js'
7
+ import { die, load, mediaLength, pad2, run, save } from './util.js'
8
+
9
+ export const NO_PROVIDER = (loc) => `no voice provider is set for ${loc}. Choose one and record it with set-voice, for example:
10
+ offline piper (good, free) kokoro (very natural, free) macos (Mac drafts) espeak (robotic drafts)
11
+ cloud elevenlabs openai azure (Microsoft shops) google (Google shops) polly (AWS shops)
12
+ person manual (record take-NN.wav files yourself)
13
+ emaki set-voice --locale default --provider piper --opt model=voices/en_US-lessac-medium.onnx`
14
+
15
+ export function chosen(p, locale, override, opts = {}) {
16
+ let v = voiceConfig(p.cfg, locale)
17
+ if (override) v = { ...v, provider: override }
18
+ v = { ...v, ...opts }
19
+ if (!v.provider) die(NO_PROVIDER(locale))
20
+ return { vcfg: v, prov: provider(v.provider) }
21
+ }
22
+
23
+ /** Per locale: the configured provider and whether it is ready here; and which offline engines this machine has. */
24
+ export function ttsCheck(p) {
25
+ const rows = p.site.locales.map((loc) => {
26
+ const v = voiceConfig(p.cfg, loc)
27
+ if (!v.provider) return { locale: loc, provider: null, ready: false, note: 'no provider set' }
28
+ try {
29
+ provider(v.provider).check(v, loc)
30
+ return { locale: loc, provider: v.provider, label: provider(v.provider).label, ready: true }
31
+ } catch (e) {
32
+ return { locale: loc, provider: v.provider, label: provider(v.provider).label, ready: false, note: e.message }
33
+ }
34
+ })
35
+ const here = []
36
+ for (const name of OFFLINE) {
37
+ try {
38
+ provider(name).check(name === 'piper' ? { model: '__probe__' } : {}, p.site.defaultLocale)
39
+ here.push(name)
40
+ } catch (e) {
41
+ if (name === 'piper' && /voice model/.test(e.message)) here.push('piper (needs a voice model)')
42
+ }
43
+ }
44
+ return { rows, offline: here }
45
+ }
46
+
47
+ /** Records the provider (and options) for a locale in emaki.config.json. Keys never go there. */
48
+ export function setVoice(p, locale, name, opts = {}) {
49
+ provider(name)
50
+ const cfg = existsSync(p.cfgPath) ? load(p.cfgPath) : {}
51
+ const block = ((cfg.voice ??= {})[locale] ??= {})
52
+ block.provider = name
53
+ Object.assign(block, opts)
54
+ const dropped = Object.keys(block).filter((k) => /key|secret|token/i.test(k))
55
+ for (const k of dropped) delete block[k]
56
+ save(p.cfgPath, cfg)
57
+ p.cfg = cfg
58
+ return { block, dropped }
59
+ }
60
+
61
+ export async function listVoices(p, locale, override) {
62
+ const { vcfg, prov } = chosen(p, locale, override)
63
+ return prov.voices(vcfg, locale)
64
+ }
65
+
66
+ /** Synthesises take-NN.wav per voice line (or prints the recording list for `manual`). */
67
+ export async function voice(p, slug, { locale, only, provider: override, opts = {}, log = console.info } = {}) {
68
+ const d = p.filmDir(slug)
69
+ const lines = load(join(d, 'voice', `${locale}.json`))
70
+ const pick = only ? new Set(String(only).split(',').map(Number)) : null
71
+ const { vcfg, prov } = chosen(p, locale, override, opts)
72
+ try {
73
+ prov.check(vcfg, locale)
74
+ } catch (e) {
75
+ die(`${prov.label}: ${e.message}`)
76
+ }
77
+ const takes = p.prod(slug, locale, 'takes')
78
+ if (vcfg.provider === 'manual') {
79
+ log(`manual: record ${lines.length} takes into ${p.rel(takes)}/ as take-01.wav … take-${pad2(lines.length)}.wav, in this order:`)
80
+ lines.forEach((l, i) => log(` take-${pad2(i + 1)} (${l.scene}): ${l.text}`))
81
+ return []
82
+ }
83
+ mkdirSync(takes, { recursive: true })
84
+ const spoken = lines.map((l) => l.say || l.text), made = []
85
+ for (const [i, l] of lines.entries()) {
86
+ const n = i + 1
87
+ if (pick && !pick.has(n)) continue
88
+ const ctx = { prev: spoken[i - 1] ?? null, next: spoken[i + 1] ?? null }
89
+ const dst = join(takes, `take-${pad2(n)}.wav`)
90
+ try {
91
+ await prov.synth(spoken[i], dst, vcfg, locale, ctx)
92
+ } catch (e) {
93
+ die(`take-${pad2(n)}: ${e instanceof TTSError ? e.message : e}`)
94
+ }
95
+ const len = mediaLength(dst)
96
+ made.push({ take: n, scene: l.scene, seconds: len })
97
+ log(`take-${pad2(n)} (${l.scene}): ${len.toFixed(1)} s · ${prov.label}`)
98
+ }
99
+ const keep = Object.fromEntries(Object.entries(vcfg).filter(([k]) => ['provider', 'voice', 'voice_id', 'model'].includes(k)))
100
+ writeFileSync(join(takes, 'provider.json'), JSON.stringify(keep, null, 1) + '\n')
101
+ return made
102
+ }
103
+
104
+ /** scene id → [take file, voice line] for the takes that exist. */
105
+ export function takesFor(p, slug, locale) {
106
+ const d = p.filmDir(slug), lines = load(join(d, 'voice', `${locale}.json`)), folder = p.prod(slug, locale, 'takes')
107
+ const out = new Map()
108
+ lines.forEach((l, i) => {
109
+ for (const ext of ['wav', 'mp3', 'm4a', 'aiff']) {
110
+ const f = join(folder, `take-${pad2(i + 1)}.${ext}`)
111
+ if (existsSync(f)) { out.set(l.scene, [f, l]); break }
112
+ }
113
+ })
114
+ return out
115
+ }
116
+
117
+ /** timing.<locale>.json: each scene runs at least lead + take + tail; shorter takes keep the design time. */
118
+ export function fit(p, slug, { locale, check = false, length = mediaLength } = {}) {
119
+ const d = p.filmDir(slug), film = load(join(d, 'film.json')), takes = takesFor(p, slug, locale)
120
+ const tp = join(d, `timing.${locale}.json`)
121
+ const timing = existsSync(tp) ? load(tp) : { scenes: {} }
122
+ const tsc = (timing.scenes ??= {})
123
+ const rows = []
124
+ for (const s of film.scenes) {
125
+ const c = tsc[s.id] ?? {}
126
+ const holds = (c.holds ?? []).reduce((n, h) => n + h[1], 0)
127
+ const L = takes.has(s.id) ? length(takes.get(s.id)[0]) : 0
128
+ const need = L ? Math.ceil((p.lead + L + p.tail) * 10) / 10 : 0
129
+ const base = s.dur + holds, runs = Math.max(base, need)
130
+ if (runs > base + 1e-6) c.dur = runs
131
+ else delete c.dur
132
+ if (Object.keys(c).length) tsc[s.id] = c
133
+ else delete tsc[s.id]
134
+ rows.push({ scene: s.id, design: s.dur, holds, take: L, runs, longer: runs > base + 1e-6 })
135
+ }
136
+ if (!check) save(tp, timing)
137
+ return { rows, total: rows.reduce((n, r) => n + r.runs, 0), path: p.rel(tp), written: !check }
138
+ }
139
+
140
+ const stamp = (t, sep = '.') => {
141
+ const h = Math.floor(t / 3600), m = Math.floor((t % 3600) / 60), s = t % 60
142
+ return `${pad2(h)}:${pad2(m)}:${s.toFixed(3).padStart(6, '0')}`.replace('.', sep)
143
+ }
144
+
145
+ /** Captions from the voice lines, spread over each take by sentence length. */
146
+ export function captions(takes, starts, lead, length = mediaLength) {
147
+ const vtt = ['WEBVTT', ''], srt = []
148
+ let k = 0
149
+ for (const [sid, [f, line]] of takes) {
150
+ let t0 = starts.get(sid)[0] + lead
151
+ const span = length(f) - 0.1
152
+ const parts = line.text.split(/(?<=[.!?¿¡;:])\s+/).map((x) => x.trim()).filter(Boolean)
153
+ const total = parts.reduce((n, x) => n + x.length, 0)
154
+ for (const x of parts) {
155
+ const dd = (span * x.length) / total
156
+ k++
157
+ vtt.push(`${stamp(t0)} --> ${stamp(t0 + dd)}`, x, '')
158
+ srt.push(String(k), `${stamp(t0, ',')} --> ${stamp(t0 + dd, ',')}`, x, '')
159
+ t0 += dd
160
+ }
161
+ }
162
+ return { vtt: vtt.join('\n'), srt: srt.join('\n') }
163
+ }
164
+
165
+ /** Final videos per orientation: picture + voice + ducked score, captions burned in (and shipped as .srt/.vtt). */
166
+ export function build(p, slug, { locale, noCaptions = false, log = console.info } = {}) {
167
+ const d = p.filmDir(slug), film = load(join(d, 'film.json'))
168
+ const tp = join(d, `timing.${locale}.json`)
169
+ const tm = existsSync(tp) ? (load(tp).scenes ?? {}) : {}
170
+ const takes = takesFor(p, slug, locale)
171
+ if (!takes.size) die(`no takes in ${p.rel(p.prod(slug, locale, 'takes'))}; run \`emaki voice\` (or record them) first`)
172
+ const starts = new Map()
173
+ let at = 0
174
+ for (const s of film.scenes) {
175
+ const holds = (tm[s.id]?.holds ?? []).reduce((n, h) => n + h[1], 0)
176
+ const dur = Math.max(s.dur + holds, tm[s.id]?.dur ?? 0)
177
+ starts.set(s.id, [at, dur])
178
+ at += dur
179
+ }
180
+ const render = p.prod(slug, locale, 'render'), out = p.prod(slug, locale, 'out')
181
+ mkdirSync(out, { recursive: true })
182
+ const cap = captions(takes, starts, p.lead)
183
+ const srtPath = join(out, `${slug}-${locale}.srt`)
184
+ writeFileSync(join(out, `${slug}-${locale}.vtt`), cap.vtt)
185
+ writeFileSync(srtPath, cap.srt)
186
+ const inputs = [], filters = []
187
+ ;[...takes].forEach(([sid, [f]], i) => {
188
+ inputs.push('-i', f)
189
+ const ms = Math.round((starts.get(sid)[0] + p.lead) * 1000)
190
+ filters.push(`[${i}:a]aresample=48000,adelay=${ms}|${ms},apad[v${i}]`)
191
+ })
192
+ const voicePath = join(out, 'voice.wav')
193
+ run('ffmpeg', ['-y', '-v', 'error', ...inputs, '-filter_complex',
194
+ filters.join(';') + ';' + [...takes].map((_, i) => `[v${i}]`).join('') + `amix=inputs=${takes.size}:normalize=0,atrim=0:${at.toFixed(3)}[vo]`,
195
+ '-map', '[vo]', '-ac', '2', voicePath])
196
+ const score = join(render, 'score.wav'), hasScore = existsSync(score), built = []
197
+ for (const layout of ['landscape', 'portrait']) {
198
+ const pic = join(render, `${slug}-${locale}-${SUFFIX[layout]}.mp4`)
199
+ if (!existsSync(pic)) continue
200
+ const dst = join(out, `${slug}-${locale}-${SUFFIX[layout]}.mp4`)
201
+ const mix = hasScore
202
+ ? `[1:a]volume=1[vo];[2:a]volume=${p.cfg.scoreLevel ?? 0.55}[sc];[sc][vo]sidechaincompress=threshold=0.05:ratio=6:release=400[duck];[duck][1:a]amix=inputs=2:normalize=0,loudnorm=I=-14:TP=-1.5:LRA=11[a]`
203
+ : '[1:a]loudnorm=I=-14:TP=-1.5:LRA=11[a]'
204
+ // libass lays SRT out on a 384x288 canvas scaled to the video, so these are in 288ths of the height:
205
+ // landscape 16 ≈ 60 px text 127 px up; portrait 9 ≈ 60 px text 333 px up (clear of the platform's buttons).
206
+ const size = layout === 'landscape' ? 16 : 9, margin = layout === 'landscape' ? 34 : 50
207
+ const burn = noCaptions ? [] : ['-vf', `subtitles=${srtPath.replace(/([:\\'])/g, '\\$1')}:force_style='FontName=Inter,FontSize=${size},PrimaryColour=&H00FFFFFF,OutlineColour=&H66000000,BackColour=&H66000000,BorderStyle=3,Outline=5,Shadow=0,MarginV=${margin}'`]
208
+ run('ffmpeg', ['-y', '-v', 'error', '-i', pic, '-i', voicePath, ...(hasScore ? ['-i', score] : []), '-filter_complex', mix,
209
+ '-map', '0:v', '-map', '[a]', ...burn, '-c:v', 'libx264', '-crf', '18', '-preset', 'medium', '-pix_fmt', 'yuv420p',
210
+ '-c:a', 'aac', '-b:a', '192k', '-t', at.toFixed(3), '-movflags', '+faststart', dst])
211
+ log(`built ${p.rel(dst)}`)
212
+ built.push(dst)
213
+ }
214
+ log(`captions: ${p.rel(out)}/${slug}-${locale}.vtt/.srt`)
215
+ return { built, duration: at }
216
+ }
@@ -0,0 +1 @@
1
+ ["measure-the-right-work"]
@@ -0,0 +1,59 @@
1
+ {
2
+ "title": "Measure the Right Work",
3
+ "look": "Session board: counters, an activity chart, two paired metric cards and metric groups on a dot-grid desk. Washi paper, sumi ink, rationed vermillion for rework, jade for work that landed. The brand mark on the close.",
4
+ "background": "#F3F0E9",
5
+ "palette": [
6
+ "#F9F6F1",
7
+ "#48413D",
8
+ "#1F1916",
9
+ "#C25237"
10
+ ],
11
+ "fonts": {
12
+ "display": {
13
+ "family": "Fraunces",
14
+ "weights": [
15
+ 300,
16
+ 400
17
+ ]
18
+ },
19
+ "sans": {
20
+ "family": "Inter",
21
+ "weights": [
22
+ 400,
23
+ 600
24
+ ]
25
+ }
26
+ },
27
+ "scenes": [
28
+ {
29
+ "id": "hook",
30
+ "name": "More",
31
+ "dur": 6.5,
32
+ "desc": "Tokens, commits and lines count up. Is any of it right?"
33
+ },
34
+ {
35
+ "id": "problem",
36
+ "name": "Busy",
37
+ "dur": 8.5,
38
+ "desc": "A bad week looks busier than a good one: fixing things is activity too."
39
+ },
40
+ {
41
+ "id": "pair",
42
+ "name": "The pair",
43
+ "dur": 13,
44
+ "desc": "First-turn resolution and rework share, read together: high resolution with rising rework means work deferred, not done."
45
+ },
46
+ {
47
+ "id": "groups",
48
+ "name": "Grouped",
49
+ "dur": 9,
50
+ "desc": "The dashboard groups metrics by outcome, quality, velocity, usage and cost, and shows velocity next to its quality companion."
51
+ },
52
+ {
53
+ "id": "close",
54
+ "name": "Close",
55
+ "dur": 5.5,
56
+ "desc": "End card with the brand mark: measure the right work, not more work."
57
+ }
58
+ ]
59
+ }
@@ -0,0 +1,16 @@
1
+ #stage { --ground:#F9F6F1; --main:#48413D; --second:#1F1916; --accent:#C25237; color:var(--second); font-family:var(--sans, system-ui, sans-serif) }
2
+ #stage { --jade:#599576; --board:#F3F0E9; --dot:#DCD7CD; --tile:#FCFAF6; --hair:#DCD7CD; --mute:#7F7975; --wire:#B2ADA9; --edge:color-mix(in srgb, var(--second) 14%, transparent); --shade:color-mix(in srgb, var(--second) 7%, transparent); --soft:var(--main) }
3
+ .ground { position:absolute; inset:0; background:var(--board) }
4
+ .art { position:absolute; inset:0; width:100%; height:100%; overflow:visible }
5
+ .cam { position:absolute; inset:0; transform-origin:960px 540px }
6
+ .words { position:absolute; margin:0; display:flex; flex-direction:column; gap:20px } .words > * { margin:0 }
7
+ .line { font-family:var(--display, var(--sans, system-ui, sans-serif)); font-size:84px; line-height:1.08; font-weight:400; letter-spacing:-0.02em }
8
+ .sub { font-size:36px; line-height:1.35; color:var(--soft) }
9
+ .eyebrow { font-size:26px; font-weight:600; letter-spacing:0.16em; text-transform:uppercase; color:var(--accent) }
10
+ .card { position:absolute; background:var(--tile); border:2px solid var(--hair); border-radius:18px }
11
+ .tag { position:absolute; transform:translate(-50%,0); padding:8px 18px; border-radius:999px; font-size:28px; font-weight:600; white-space:nowrap; background:var(--tile); color:var(--second); border:2px solid var(--hair) }
12
+ .tag.hot { background:var(--accent); color:var(--ground); border-color:var(--accent) } .tag.ink { background:var(--main); color:var(--ground); border-color:var(--main) } .tag.ok { background:var(--jade); color:var(--ground); border-color:var(--jade) }
13
+ .checks { position:absolute; display:flex; flex-direction:column; gap:14px }
14
+ .chk { display:flex; align-items:flex-start; gap:18px; padding:16px 22px; background:var(--tile); border:2px solid var(--hair); border-radius:14px; font-size:32px; line-height:1.25; font-weight:500 }
15
+ .chk i { flex:none; width:30px; height:30px; margin-top:3px; border-radius:50%; background:var(--jade); position:relative } .chk i::after { content:''; position:absolute; left:9px; top:5px; width:8px; height:14px; border:solid var(--ground); border-width:0 4px 4px 0; transform:rotate(45deg) }
16
+ .mono { font-family:var(--mono, ui-monospace, monospace) }