decklight 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -108,13 +108,36 @@ const clean = (s) => s
108
108
  .replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&')
109
109
  .replace(/\s+/g, ' ')
110
110
  .trim();
111
- const slides = sections.map((sec, i) => {
111
+ const raw = sections.map((sec) => {
112
112
  const aside = sec.match(NOTES_ASIDE);
113
- if (aside) return clean(aside[1]);
113
+ if (aside) return aside[1];
114
114
  const md = sec.match(/^Note:\s*$([\s\S]*?)(?=^Rehearse:\s*$|<\/script>)/m);
115
- if (md) return clean(md[1]);
116
- return '';
115
+ return md ? md[1] : '';
117
116
  });
117
+ const slides = raw.map(clean);
118
+ /**
119
+ * A slide's ⟨CLICK⟩ segments, or null when there is only one.
120
+ *
121
+ * These are the author's own build cues, and the sync rule they carry already
122
+ * ships: src/core/narration.js speaks segment k over build step k in live
123
+ * playback, and captions, rehearse mode and the notes editor all read the same
124
+ * segmentation. Recording them separately is what lets a rendered mp4 mirror
125
+ * that (SPEC PRESENTING). An empty segment is dropped rather than synthesised
126
+ * as silence — a ⟨CLICK⟩ at the very start or end of a note is punctuation,
127
+ * not a beat.
128
+ */
129
+ export const notesSegments = (notes) => {
130
+ const parts = String(notes ?? '').split('⟨CLICK⟩').map(clean).filter(Boolean);
131
+ return parts.length > 1 ? parts : null;
132
+ };
133
+ // Segmenting needs ffmpeg specifically: the per-slide file is CONCATENATED from
134
+ // the segments, and afconvert (Core Audio, macOS-only) has no concat demuxer.
135
+ // Without it the slide is synthesised whole, exactly as before.
136
+ const canSegment = encoder === 'ffmpeg';
137
+ const segmented = raw.map((r) => (canSegment ? notesSegments(r) : null));
138
+ if (!canSegment && raw.some((r) => notesSegments(r))) {
139
+ console.log(' note: ⟨CLICK⟩ segments need ffmpeg to concatenate — narrating each slide whole');
140
+ }
118
141
  console.log(`${basename(deckPath)}: ${slides.length} slides, ${slides.filter(Boolean).length} with notes`);
119
142
 
120
143
  // ── optional narration pass through a local model ────────────────────────────
@@ -164,17 +187,52 @@ for (let i = 0; i < slides.length; i++) {
164
187
  const hash = slideHash(text);
165
188
  manifest.push({ file: `slide-${n}.m4a`, hash });
166
189
  if (prev?.slides?.[i]?.hash === hash && existsSync(m4a)) {
190
+ // Carry the segments across, or an unchanged slide loses its build sync on
191
+ // the next rerun: the audio is still on disk and the manifest is the only
192
+ // thing that knows it is there.
193
+ const kept = prev.slides[i].segments;
194
+ if (kept?.every((sg) => existsSync(join(outDir, sg.file)))) manifest[i].segments = kept;
167
195
  skipped++;
168
- console.log(` slide ${n}: unchanged — kept`);
196
+ console.log(` slide ${n}: unchanged — kept${manifest[i].segments ? ` (${manifest[i].segments.length} segments)` : ''}`);
169
197
  continue;
170
198
  }
171
- const { wav: buf, usage } = await tts.synth(text, { voice, style });
172
- writeFileSync(wav, buf);
173
- totalCost += usage.cost;
199
+ // A segmented slide is synthesised segment by segment, and the per-slide file
200
+ // is CONCATENATED from those — one synthesis, both shapes. TTS is billed per
201
+ // character; asking for the same words twice to get the same audio cut two
202
+ // ways would be a bill nobody agreed to. --reuse-text takes the whole-slide
203
+ // path, since its prior text is one script rather than segments.
204
+ const segs = prior ? null : segmented[i];
205
+ let usage = { cost: 0 };
206
+ if (segs) {
207
+ const parts = [];
208
+ for (let k = 0; k < segs.length; k++) {
209
+ const kk = String(k + 1).padStart(2, '0');
210
+ const sWav = join(outDir, `slide-${n}-${kk}.wav`);
211
+ const sM4a = join(outDir, `slide-${n}-${kk}.m4a`);
212
+ const out = await tts.synth(segs[k], { voice, style });
213
+ writeFileSync(sWav, out.wav);
214
+ usage = { cost: usage.cost + (out.usage?.cost ?? 0) };
215
+ toAac(sWav, sM4a);
216
+ if (!keepWav) rmSync(sWav);
217
+ parts.push({ file: `slide-${n}-${kk}.m4a`, path: sM4a });
218
+ }
219
+ const list = join(outDir, `slide-${n}.concat`);
220
+ writeFileSync(list, `${parts.map((p) => `file '${p.path.replaceAll("'", "'\\''")}'`).join('\n')}\n`);
221
+ execFileSync('ffmpeg', ['-y', '-f', 'concat', '-safe', '0', '-i', list, '-c', 'copy', m4a],
222
+ { stdio: 'ignore' });
223
+ rmSync(list, { force: true });
224
+ manifest[i].segments = parts.map((p) => ({ file: p.file }));
225
+ } else {
226
+ const out = await tts.synth(text, { voice, style });
227
+ writeFileSync(wav, out.wav);
228
+ usage = out.usage ?? { cost: 0 };
229
+ toAac(wav, m4a);
230
+ if (!keepWav) rmSync(wav);
231
+ }
232
+ totalCost += usage.cost ?? 0;
174
233
  const costNote = usage.cost ? ` · ~$${usage.cost.toFixed(4)}` : '';
175
- toAac(wav, m4a);
176
- if (!keepWav) rmSync(wav);
177
- console.log(` slide ${n}: ${text.length} chars → ${basename(m4a)}${costNote}`);
234
+ console.log(` slide ${n}: ${text.length} chars → ${basename(m4a)}`
235
+ + `${segs ? ` (${segs.length} ⟨CLICK⟩ segments)` : ''}${costNote}`);
178
236
  // crash-safe: persist progress after every slide so an interrupted run
179
237
  // resumes incrementally instead of re-synthesizing everything
180
238
  writeFileSync(join(outDir, 'manifest.json'),