musicfit 0.1.2 → 0.2.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.
@@ -5,6 +5,12 @@
5
5
  // below, and which part of the track went where between them.
6
6
 
7
7
  const zlib = require("node:zlib");
8
+ const { promisify } = require("node:util");
9
+ const { CHUNK, blocks } = require("./breathe");
10
+
11
+ // Compressed in the thread pool, not on the main thread, which has to stay
12
+ // free to hear a stop.
13
+ const deflate = promisify(zlib.deflate);
8
14
 
9
15
  // ─── canvas ────────────────────────────────────────────────────────────────
10
16
 
@@ -125,6 +131,24 @@ Z ##### ....# ...#. ..#.. .#... #.... #####
125
131
  < ...#. ..#.. .#... #.... .#... ..#.. ...#.
126
132
  ' ..#.. ..#.. .#... ..... ..... ..... .....
127
133
  ? .###. #...# ....# ...#. ..#.. ..... ..#..
134
+ _ ..... ..... ..... ..... ..... ..... #####
135
+ ! ..#.. ..#.. ..#.. ..#.. ..#.. ..... ..#..
136
+ & .##.. #..#. #.#.. .#... #.#.# #..#. .##.#
137
+ # .#.#. .#.#. ##### .#.#. ##### .#.#. .#.#.
138
+ @ .###. #...# #.### #.#.# #.### #.... .###.
139
+ [ .###. .#... .#... .#... .#... .#... .###.
140
+ ] .###. ...#. ...#. ...#. ...#. ...#. .###.
141
+ * ..... ..#.. #.#.# .###. #.#.# ..#.. .....
142
+ ; ..... .##.. .##.. ..... .##.. ..#.. .#...
143
+ " .#.#. .#.#. .#.#. ..... ..... ..... .....
144
+ ~ ..... ..... .#... #.#.# ...#. ..... .....
145
+ $ ..#.. .#### #.#.. .###. ..#.# ####. ..#..
146
+ | ..#.. ..#.. ..#.. ..#.. ..#.. ..#.. ..#..
147
+ { ...## ..#.. ..#.. .#... ..#.. ..#.. ...##
148
+ } ##... ..#.. ..#.. ...#. ..#.. ..#.. ##...
149
+ ^ ..#.. .#.#. #...# ..... ..... ..... .....
150
+ \` .#... ..#.. ..... ..... ..... ..... .....
151
+ \\ ..... #.... .#... ..#.. ...#. ....# .....
128
152
  `;
129
153
 
130
154
  const FONT = { " ": Array(7).fill(".....") };
@@ -133,6 +157,36 @@ for (const line of GLYPHS.trim().split("\n")) {
133
157
  FONT[ch] = rows;
134
158
  }
135
159
 
160
+ // Latin letters the font draws as others, which no mark comes off of, and
161
+ // the punctuation names often have, drawn in its plain form: dashes,
162
+ // curly quotes, a times sign, a middle dot.
163
+ const FOLD = {
164
+ "Ø": "O", "Æ": "AE", "Œ": "OE", "Ł": "L", "Đ": "D", "Ð": "D", "Þ": "TH", "Ħ": "H", "Ŧ": "T", "Ŋ": "N", "Ŀ": "L", "ẞ": "SS",
165
+ "‐": "-", "‑": "-", "‒": "-", "–": "-", "—": "-", "―": "-", "−": "-", "‘": "'", "’": "'", "‚": ",", "′": "'", "“": '"', "”": '"', "„": '"', "″": '"',
166
+ "×": "X", "·": ".", "•": ".", "«": "<", "»": ">", "‹": "<", "›": ">",
167
+ };
168
+
169
+ // A name as the font draws it: in capitals, Latin letters without their
170
+ // marks (ş as S, ø as O), and the signs it has no shape for left out (an
171
+ // emoji, a note); null when it has a letter or a digit the font has no
172
+ // shape for, as a name in Cyrillic or Chinese, or nothing but signs before
173
+ // its extension once those are left out. Such a name is drawn as the
174
+ // track rather than as question marks.
175
+ function drawable(text) {
176
+ const plain = String(text).toUpperCase().normalize("NFKD").replace(/\p{M}/gu, "");
177
+ const folded = Array.from(plain, (ch) => FOLD[ch] || ch);
178
+ if (folded.some((ch) => !FONT[ch] && /[\p{L}\p{N}]/u.test(ch))) return null;
179
+ const all = folded.join("");
180
+ const kept = folded
181
+ .filter((ch) => FONT[ch])
182
+ .join("")
183
+ .replace(/ {2,}/g, " ")
184
+ .replace(/ (\.[A-Z0-9]+)$/, "$1")
185
+ .trim();
186
+ if (kept !== all && !/[A-Z0-9]/.test(kept.replace(/\.[A-Z0-9]+$/, ""))) return null;
187
+ return kept;
188
+ }
189
+
136
190
  // ─── PNG ───────────────────────────────────────────────────────────────────
137
191
 
138
192
  const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
@@ -157,7 +211,7 @@ function chunk(type, data) {
157
211
  }
158
212
 
159
213
  // 8-bit RGB, each row filtered against the pixel to its left.
160
- function png(c, text = {}) {
214
+ async function png(c, text = {}) {
161
215
  const { width, height, px } = c;
162
216
  const stride = width * 3;
163
217
  const raw = Buffer.alloc(height * (stride + 1));
@@ -175,7 +229,7 @@ function png(c, text = {}) {
175
229
  header[8] = 8;
176
230
  header[9] = 2;
177
231
  const notes = Object.entries(text).map(([k, v]) => chunk("tEXt", Buffer.from(`${k}\0${v}`, "latin1")));
178
- return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", header), ...notes, chunk("IDAT", zlib.deflateSync(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0))]);
232
+ return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", header), ...notes, chunk("IDAT", await deflate(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0))]);
179
233
  }
180
234
 
181
235
  // ─── seams.png ─────────────────────────────────────────────────────────────
@@ -190,19 +244,24 @@ const INK = {
190
244
  seam: "#ff6a3d",
191
245
  hit: "#ff6a3d",
192
246
  voice: "#4cc9f0",
247
+ own: "#c77dff",
193
248
  };
194
249
  const SEGMENT = ["#7b6cff", "#2ec4b6", "#ffbe0b", "#f15bb5", "#9ef01a", "#4cc9f0"];
195
250
 
196
- // Peaks of a stereo signal in `columns` columns over [from, to) seconds.
197
- function peaks(L, R, rate, from, to, columns) {
251
+ // Peaks of a stereo signal in `columns` columns over [from, to) seconds,
252
+ // in groups of columns that hold about a block of samples each.
253
+ async function peaks(L, R, rate, from, to, columns) {
198
254
  const out = [];
199
- for (let x = 0; x < columns; x++) {
200
- const a = Math.max(0, Math.floor((from + ((to - from) * x) / columns) * rate));
201
- const b = Math.min(L.length, Math.floor((from + ((to - from) * (x + 1)) / columns) * rate));
202
- let hi = 0;
203
- for (let i = a; i < b; i++) hi = Math.max(hi, Math.abs(L[i]), Math.abs(R[i]));
204
- out.push(hi);
205
- }
255
+ const group = Math.max(1, Math.floor(CHUNK / Math.max(1, ((to - from) * rate) / columns)));
256
+ await blocks(0, columns, (first, last) => {
257
+ for (let x = first; x < last; x++) {
258
+ const a = Math.max(0, Math.floor((from + ((to - from) * x) / columns) * rate));
259
+ const b = Math.min(L.length, Math.floor((from + ((to - from) * (x + 1)) / columns) * rate));
260
+ let hi = 0;
261
+ for (let i = a; i < b; i++) hi = Math.max(hi, Math.abs(L[i]), Math.abs(R[i]));
262
+ out.push(hi);
263
+ }
264
+ }, group);
206
265
  return out;
207
266
  }
208
267
 
@@ -217,8 +276,23 @@ function ruler(c, x0, width, y, seconds, scale) {
217
276
  }
218
277
  }
219
278
 
279
+ // The words under the picture of a fit: a mark for each kind of line or
280
+ // span drawn on it, then what the colors mean.
281
+ const NOTE = "COLORS MATCH EACH PART OF THE TRACK TO WHERE IT PLAYS";
282
+
283
+ function legendOf(cues) {
284
+ const marks = [[INK.seam, "SEAM"], ["#ffffff", "HIT"]];
285
+ if (cues.voice) marks.push([INK.voice, "VOICE (MUSIC DUCKED)"]);
286
+ if (ownSpans(cues).length) marks.push([INK.own, "VIDEO'S OWN SOUND (MUSIC DIPPED)"]);
287
+ return marks;
288
+ }
289
+
290
+ // Where the music dips under the video's own sound, with --video-sound
291
+ // voice.
292
+ const ownSpans = (cues) => (cues.video && cues.video.duck !== null && cues.video.spans) || [];
293
+
220
294
  // cues: what report.cues() returns; source, fitted: { L, R } at `rate`.
221
- function seamsPng({ cues, source, fitted, rate, analysis }) {
295
+ async function seamsPng({ cues, source, fitted, rate, analysis }) {
222
296
  const W = 1600;
223
297
  const pad = 32;
224
298
  const inner = W - pad * 2;
@@ -226,23 +300,29 @@ function seamsPng({ cues, source, fitted, rate, analysis }) {
226
300
  const topY = 96;
227
301
  const mapH = 110;
228
302
  const fitY = topY + stripH + 34 + mapH;
229
- const H = fitY + stripH + 34 + 64;
303
+ // The note goes on a line of its own when the marks leave it no room.
304
+ const marks = cues.mode === "analysis" ? [] : legendOf(cues);
305
+ const width = (text) => String(text).length * 12 - 2;
306
+ const wrap = marks.reduce((x, [, label]) => x + 26 + width(label) + 30, pad) + width(NOTE) > W - pad;
307
+ const H = fitY + stripH + 34 + 64 + (wrap ? 22 : 0);
230
308
  const c = canvas(W, H, INK.ground);
231
309
 
232
310
  const srcSeconds = source.L.length / rate;
233
311
  const outSeconds = cues.length;
234
312
  const sx = inner / srcSeconds;
235
313
  const ox = inner / outSeconds;
236
- const title = cues.mode === "analysis" ? `MUSICFIT ${cues.track}` : `MUSICFIT ${cues.track} TO ${outSeconds.toFixed(2)} S`;
314
+ // The track by its name when the font can draw it, else as the track.
315
+ const track = drawable(cues.track) || "THE TRACK";
316
+ const title = cues.mode === "analysis" ? `MUSICFIT ${track}` : `MUSICFIT ${track} TO ${outSeconds.toFixed(2)} S`;
237
317
  c.text(pad, 24, title.slice(0, 120), INK.text, 3);
238
318
  const tempo = cues.tempo ? `${cues.tempo.bpm.toFixed(1)} BPM ${cues.tempo.meter}/4 ${cues.mode === "phrases" ? "CUTS IN PAIRS OF BARS" : cues.mode === "energy" ? "NO STEADY BEAT: LONG CROSSFADES" : cues.mode === "analysis" ? "" : "CUTS ON BAR LINES"}` : "NO STEADY BEAT: LONG CROSSFADES";
239
319
  c.text(pad, 60, tempo, INK.dim, 2);
240
320
 
241
321
  // The track, with the parts that were used in their segment's color.
242
- const drawStrip = (y, seconds, scale, L, R, color, label) => {
322
+ const drawStrip = async (y, seconds, scale, L, R, color, label) => {
243
323
  c.rect(pad, y, inner, stripH, INK.panel);
244
324
  const mid = y + stripH / 2;
245
- const p = peaks(L, R, rate, 0, seconds, inner);
325
+ const p = await peaks(L, R, rate, 0, seconds, inner);
246
326
  const top = Math.max(1e-6, ...p);
247
327
  for (let x = 0; x < inner; x++) {
248
328
  const h = Math.max(1, Math.round((p[x] / top) * (stripH / 2 - 8)));
@@ -256,7 +336,7 @@ function seamsPng({ cues, source, fitted, rate, analysis }) {
256
336
  const i = segments.findIndex((s) => t >= s.source[0] && t < s.source[1]);
257
337
  return i >= 0 ? segColor(i) : "#5a5862";
258
338
  };
259
- drawStrip(topY, srcSeconds, sx, source.L, source.R, cues.mode === "analysis" ? () => INK.wave : srcColor, "TRACK");
339
+ await drawStrip(topY, srcSeconds, sx, source.L, source.R, cues.mode === "analysis" ? () => INK.wave : srcColor, "TRACK");
260
340
  // Bar lines of the track, faint, and every fourth stronger.
261
341
  (analysis.bars || []).forEach((bar, i) => {
262
342
  if (bar.start < 0 || bar.start > srcSeconds) return;
@@ -290,10 +370,13 @@ function seamsPng({ cues, source, fitted, rate, analysis }) {
290
370
  const i = segments.findIndex((s, k) => t >= s.from && (t < s.to || k === segments.length - 1));
291
371
  return i >= 0 ? segColor(i) : INK.wave;
292
372
  };
293
- drawStrip(fitY, outSeconds, ox, fitted.L, fitted.R, fitColor, "FIT");
373
+ await drawStrip(fitY, outSeconds, ox, fitted.L, fitted.R, fitColor, "FIT");
294
374
  cues.times.bars.forEach((t, i) => c.vline(pad + t * ox, fitY + 30, fitY + stripH, INK.grid, i % 4 === 0 ? 0.9 : 0.45));
295
- // Voice spans along the bottom of the strip.
375
+ // Voice spans along the bottom of the strip, and above them where the
376
+ // music dips under the video's own sound.
296
377
  for (const span of (cues.voice && cues.voice.spans) || []) c.rect(pad + span.from * ox, fitY + stripH - 6, (span.to - span.from) * ox, 6, INK.voice, 0.85);
378
+ const ownY = fitY + stripH - (cues.voice ? 12 : 6);
379
+ for (const span of ownSpans(cues)) c.rect(pad + span.from * ox, ownY, Math.max(1, (span.to - span.from) * ox), 6, INK.own, 0.85);
297
380
  // The closing fade, darker towards its end.
298
381
  if (cues.end && cues.end.fade) {
299
382
  const { from, to } = cues.end.fade;
@@ -323,17 +406,14 @@ function seamsPng({ cues, source, fitted, rate, analysis }) {
323
406
 
324
407
  // The legend.
325
408
  let lx = pad;
326
- const ly = H - 34;
327
- c.rect(lx, ly + 3, 18, 8, INK.seam);
328
- lx = c.text(lx + 26, ly, "SEAM", INK.text, 2) + 30;
329
- c.rect(lx, ly + 3, 18, 8, "#ffffff");
330
- lx = c.text(lx + 26, ly, "HIT", INK.text, 2) + 30;
331
- if (cues.voice) {
332
- c.rect(lx, ly + 3, 18, 8, INK.voice);
333
- lx = c.text(lx + 26, ly, "VOICE (MUSIC DUCKED)", INK.text, 2) + 30;
409
+ const ly = H - 34 - (wrap ? 22 : 0);
410
+ for (const [color, label] of marks) {
411
+ c.rect(lx, ly + 3, 18, 8, color);
412
+ lx = c.text(lx + 26, ly, label, INK.text, 2) + 30;
334
413
  }
335
- c.text(lx, ly, "COLORS MATCH EACH PART OF THE TRACK TO WHERE IT PLAYS", INK.dim, 2);
414
+ if (wrap) c.text(pad, ly + 22, NOTE, INK.dim, 2);
415
+ else c.text(lx, ly, NOTE, INK.dim, 2);
336
416
  return png(c, { Software: "musicfit" });
337
417
  }
338
418
 
339
- module.exports = { seamsPng };
419
+ module.exports = { seamsPng, drawable };