beatsheet 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +63 -2
- package/bin/beatsheet.js +14 -0
- package/bin/register-fallback.mjs +23 -0
- package/bin/resolve-fallback.mjs +83 -0
- package/package.json +45 -4
- package/player/env.d.ts +5 -0
- package/player/gallery-audio.ts +147 -0
- package/player/gallery-css.ts +144 -0
- package/player/gallery-detail.ts +385 -0
- package/player/gallery-music.ts +257 -0
- package/player/gallery-pick-state.ts +167 -0
- package/player/gallery-pick.ts +749 -0
- package/player/gallery-sounds.ts +137 -0
- package/player/gallery-state.ts +457 -0
- package/player/gallery.ts +612 -0
- package/player/index.html +49 -0
- package/player/main.ts +83 -0
- package/skill/SKILL.md +658 -0
- package/skill/examples/library/assets/tour/avatar-mk.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard-insights.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard.svg +1 -0
- package/skill/examples/library/assets/tour/settings.svg +1 -0
- package/skill/examples/library/library-tour.plan.md +32 -0
- package/skill/examples/library/library-tour.video.json +268 -0
- package/skill/reference/scene-techniques.md +306 -0
- package/skill/scripts/beats.ts +34 -0
- package/skill/scripts/catalogue.ts +120 -0
- package/skill/scripts/frames.ts +105 -0
- package/src/args.ts +344 -0
- package/src/cli.ts +95 -0
- package/src/commands.ts +812 -0
- package/src/default-project.config.ts +9 -0
- package/src/index.ts +47 -0
- package/src/packages.ts +17 -0
- package/src/paths.ts +32 -0
- package/src/project.ts +200 -0
- package/src/render/browser.ts +185 -0
- package/src/render/capture.ts +320 -0
- package/src/render/ffmpeg.ts +229 -0
- package/src/render/gif-info.ts +61 -0
- package/src/render/render.ts +220 -0
- package/src/samples.ts +557 -0
- package/src/server/api.ts +494 -0
- package/src/server/jobs.ts +210 -0
- package/src/server/security.ts +47 -0
- package/src/server/server.ts +191 -0
- package/src/server/sse.ts +76 -0
- package/src/server/static.ts +127 -0
- package/src/server/vite.ts +100 -0
- package/src/skill.ts +111 -0
- package/src/snap.ts +212 -0
- package/src/speech.ts +689 -0
- package/src/svg-text.ts +136 -0
- package/src/voicefx.ts +311 -0
- package/studio/3rdpartylicenses.txt +287 -0
- package/studio/favicon.ico +0 -0
- package/studio/icons/apple-touch-icon.png +0 -0
- package/studio/icons/icon-192.png +0 -0
- package/studio/icons/icon-512.png +0 -0
- package/studio/icons/icon.svg +1 -0
- package/studio/icons/maskable-512.png +0 -0
- package/studio/index.html +18 -0
- package/studio/main-KMYDBWZH.js +15 -0
- package/studio/manifest.webmanifest +17 -0
- package/studio/offline.html +92 -0
- package/studio/styles-NJ2CULOW.css +1 -0
- package/studio/sw.js +71 -0
package/src/svg-text.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `check` warning for a project's SVG images that carry live text.
|
|
3
|
+
*
|
|
4
|
+
* Why: Chromium lays the text of an SVG image out in a font of (font-size x the picture's scale on
|
|
5
|
+
* screen) and reuses a cached font whose size is within a few thousandths of a pixel of it. Which
|
|
6
|
+
* font a text gets then depends on what was drawn before, so a frame's pixels can depend on the
|
|
7
|
+
* frames sought before it (measured: glyph edges off by up to 217 of 255). With
|
|
8
|
+
* text-rendering="geometricPrecision" the font has the size written in the file and the glyphs are
|
|
9
|
+
* scaled; text converted to outlines has no font at all. The file is its author's, so `check` says
|
|
10
|
+
* so and changes nothing.
|
|
11
|
+
*/
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import type { ValidationIssue } from '@beatsheet/core';
|
|
15
|
+
|
|
16
|
+
/** Comment, CDATA, processing instruction, declaration, end tag (1: name), start tag (2: name, 3: attributes). */
|
|
17
|
+
const TOKEN = /<!--[\s\S]*?-->|<!\[CDATA\[([\s\S]*?)\]\]>|<\?[\s\S]*?\?>|<![^>]*>|<\/([\w:.-]+)\s*>|<([\w:.-]+)((?:"[^"]*"|'[^']*'|[^>"'])*)>/g;
|
|
18
|
+
const ATTRIBUTE = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
19
|
+
const STYLE_TEXT_RENDERING = /(?:^|;)\s*text-rendering\s*:\s*([^;!]+)/i;
|
|
20
|
+
/** Elements whose character data is not drawn as text. */
|
|
21
|
+
const NOT_DRAWN = new Set(['title', 'desc', 'metadata', 'style', 'script']);
|
|
22
|
+
|
|
23
|
+
const local = (name: string): string => name.slice(name.indexOf(':') + 1).toLowerCase();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The text-rendering an element sets itself: its inline style, else its attribute; undefined when
|
|
27
|
+
* it sets none and inherits. A rule in a <style> block is not read: put the attribute on the element.
|
|
28
|
+
*/
|
|
29
|
+
function ownTextRendering(attributes: string): string | undefined {
|
|
30
|
+
let attribute: string | undefined;
|
|
31
|
+
let style: string | undefined;
|
|
32
|
+
for (const m of attributes.matchAll(ATTRIBUTE)) {
|
|
33
|
+
const name = m[1]!.toLowerCase();
|
|
34
|
+
const value = m[2] ?? m[3] ?? '';
|
|
35
|
+
if (name === 'text-rendering') attribute = value;
|
|
36
|
+
else if (name === 'style') style = STYLE_TEXT_RENDERING.exec(value)?.[1];
|
|
37
|
+
}
|
|
38
|
+
const set = style ?? attribute;
|
|
39
|
+
return set === undefined ? undefined : set.trim().toLowerCase();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The live text of an SVG that is drawn without text-rendering="geometricPrecision": the character
|
|
44
|
+
* data inside <text> (also in its <tspan>, <textPath> and <a>) for which neither the element nor an
|
|
45
|
+
* ancestor sets it. One entry per run of text, in document order; empty when the SVG has no text,
|
|
46
|
+
* only outlines, or the attribute where it is needed.
|
|
47
|
+
*/
|
|
48
|
+
export function inexactSvgText(svg: string): string[] {
|
|
49
|
+
const found: string[] = [];
|
|
50
|
+
/** Open elements: name, the text-rendering in effect, inside a <text>, character data drawn. */
|
|
51
|
+
const open: { name: string; rendering: string; inText: boolean; drawn: boolean }[] = [];
|
|
52
|
+
const data = (raw: string): void => {
|
|
53
|
+
const top = open[open.length - 1];
|
|
54
|
+
const text = raw.replace(/\s+/g, ' ').trim();
|
|
55
|
+
if (!top || !top.inText || !top.drawn || !text || top.rendering === 'geometricprecision') return;
|
|
56
|
+
found.push(text);
|
|
57
|
+
};
|
|
58
|
+
let at = 0;
|
|
59
|
+
for (const m of svg.matchAll(TOKEN)) {
|
|
60
|
+
data(svg.slice(at, m.index));
|
|
61
|
+
at = m.index + m[0].length;
|
|
62
|
+
if (m[1] !== undefined) data(m[1]);
|
|
63
|
+
else if (m[2] !== undefined) {
|
|
64
|
+
const name = local(m[2]);
|
|
65
|
+
const i = open.map((e) => e.name).lastIndexOf(name);
|
|
66
|
+
if (i >= 0) open.length = i;
|
|
67
|
+
} else if (m[3] !== undefined) {
|
|
68
|
+
const attributes = m[4] ?? '';
|
|
69
|
+
if (/\/\s*$/.test(attributes)) continue; // <path … />: no content
|
|
70
|
+
const name = local(m[3]);
|
|
71
|
+
const parent = open[open.length - 1];
|
|
72
|
+
const set = ownTextRendering(attributes);
|
|
73
|
+
open.push({
|
|
74
|
+
name,
|
|
75
|
+
rendering: set === undefined || set === 'inherit' ? (parent?.rendering ?? 'auto') : set,
|
|
76
|
+
inText: name === 'text' || (parent?.inText ?? false),
|
|
77
|
+
drawn: (parent?.drawn ?? true) && !NOT_DRAWN.has(name),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return found;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const quote = (text: string): string => (text.length > 32 ? `${text.slice(0, 31)}…` : text);
|
|
85
|
+
|
|
86
|
+
/** The warning's text for a file (its path inside the assets folder) and its inexactSvgText(). */
|
|
87
|
+
export function svgTextMessage(asset: string, texts: readonly string[]): string {
|
|
88
|
+
const more = texts.length > 1 ? ` and ${texts.length - 1} more` : '';
|
|
89
|
+
return (
|
|
90
|
+
`image "${asset}" has live text ("${quote(texts[0] ?? '')}"${more}) that can render a few pixels differently depending on the frames sought before: ` +
|
|
91
|
+
`add text-rendering="geometricPrecision" to its <svg> element, or convert the text to outlines`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The path of the first string in a video's JSON that names `asset` (as the player resolves it), or []. */
|
|
96
|
+
function firstUse(json: unknown, asset: string): (string | number)[] {
|
|
97
|
+
const same = (value: string): boolean => path.posix.normalize(value.replace(/\\/g, '/').replace(/^\.\//, '')) === asset;
|
|
98
|
+
const walk = (node: unknown, at: (string | number)[]): (string | number)[] | null => {
|
|
99
|
+
if (typeof node === 'string') return same(node) ? at : null;
|
|
100
|
+
if (Array.isArray(node)) {
|
|
101
|
+
for (let i = 0; i < node.length; i++) {
|
|
102
|
+
const hit = walk(node[i], [...at, i]);
|
|
103
|
+
if (hit) return hit;
|
|
104
|
+
}
|
|
105
|
+
} else if (node && typeof node === 'object') {
|
|
106
|
+
for (const [key, value] of Object.entries(node)) {
|
|
107
|
+
const hit = walk(value, [...at, key]);
|
|
108
|
+
if (hit) return hit;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
};
|
|
113
|
+
return walk(json, []) ?? [];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* One warning per SVG file among `files` (the asset files a check's pages loaded, absolute paths)
|
|
118
|
+
* that has inexactSvgText(), sorted by file. Its path is where the video names the file, when it does.
|
|
119
|
+
*/
|
|
120
|
+
export async function svgTextWarnings(files: Iterable<string>, assetsDir: string, json: unknown): Promise<ValidationIssue[]> {
|
|
121
|
+
const issues: ValidationIssue[] = [];
|
|
122
|
+
for (const file of [...new Set(files)].sort()) {
|
|
123
|
+
if (path.extname(file).toLowerCase() !== '.svg') continue;
|
|
124
|
+
let svg: string;
|
|
125
|
+
try {
|
|
126
|
+
svg = await fs.readFile(file, 'utf8');
|
|
127
|
+
} catch {
|
|
128
|
+
continue; // gone since it was served: nothing to say about it
|
|
129
|
+
}
|
|
130
|
+
const texts = inexactSvgText(svg);
|
|
131
|
+
if (texts.length === 0) continue;
|
|
132
|
+
const asset = path.relative(assetsDir, file).split(path.sep).join('/');
|
|
133
|
+
issues.push({ path: firstUse(json, asset), message: svgTextMessage(asset, texts), severity: 'warning', code: 'svg-text' });
|
|
134
|
+
}
|
|
135
|
+
return issues;
|
|
136
|
+
}
|
package/src/voicefx.ts
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Voice post-processing of `speak` (the 'studio' preset of VideoDoc.voiceFx): raw TTS is bright,
|
|
3
|
+
* dry and dynamic, so it sits on top of the music. The chain, applied to the trimmed clip and baked
|
|
4
|
+
* into speech/<key>.wav: high-pass → de-esser → compressor → short room reverb → peak limiter. The mix
|
|
5
|
+
* then levels each clip to -20 LUFS as before.
|
|
6
|
+
*
|
|
7
|
+
* Plain TypeScript on the sample buffer: deterministic (same input, same bytes on every machine),
|
|
8
|
+
* no native code. Levels are relative to the clip (its speech RMS, its peak), so a quiet and a loud
|
|
9
|
+
* engine output get the same treatment. Changing VOICE_FX_CHAIN changes every studio clip: bump
|
|
10
|
+
* SPEECH_FX_PROCESSING_VERSION in core (a test ties the two).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** The 'studio' chain's parameters (Hz, seconds, dB). */
|
|
14
|
+
export const VOICE_FX_CHAIN = {
|
|
15
|
+
/** 2nd-order Butterworth high-pass (12 dB/oct): rumble, plosive thumps and TTS low-end mud. */
|
|
16
|
+
highpass: { hz: 80, q: Math.SQRT1_2 },
|
|
17
|
+
/**
|
|
18
|
+
* Dynamic-EQ de-esser: a high shelf at `shelfHz` is turned down only while the band above `hz`
|
|
19
|
+
* dominates the voice (its envelope within `thresholdDb` of the full-band envelope: an "s", "sh",
|
|
20
|
+
* "t"), never on vowels, and only above `gateDb` under the clip's peak. At 0 dB the shelf is the identity.
|
|
21
|
+
*/
|
|
22
|
+
deEss: { hz: 5000, shelfHz: 4500, thresholdDb: -12, ratio: 4, maxReductionDb: 10, gateDb: -40, attack: 0.001, release: 0.04 },
|
|
23
|
+
/**
|
|
24
|
+
* Feed-forward compressor, threshold relative to the clip's speech RMS (the level of the windows
|
|
25
|
+
* the trim counts as speech), soft knee; makeup restores the speech RMS.
|
|
26
|
+
*/
|
|
27
|
+
compressor: { thresholdDb: 3, ratio: 3, kneeDb: 6, attack: 0.005, release: 0.08, lookahead: 0.005 },
|
|
28
|
+
/**
|
|
29
|
+
* Small room: four damped comb filters (Schroeder delays) and two all-passes, RT60 `rt60`; the
|
|
30
|
+
* wet signal, scaled to the dry RMS, is mixed in at `wet`. The clip grows by the tail until it
|
|
31
|
+
* falls under `tailFloorDb` below the peak (at most `rt60`).
|
|
32
|
+
*/
|
|
33
|
+
reverb: {
|
|
34
|
+
wet: 0.1,
|
|
35
|
+
rt60: 0.3,
|
|
36
|
+
predelay: 0.008,
|
|
37
|
+
combs: [0.0297, 0.0371, 0.0411, 0.0437],
|
|
38
|
+
allpasses: [0.005, 0.0017],
|
|
39
|
+
allpassGain: 0.7,
|
|
40
|
+
damping: 0.35,
|
|
41
|
+
tailFloorDb: -60,
|
|
42
|
+
fade: 0.01,
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* Lookahead peak limiter, last: no sample above the clip's speech RMS + `overSpeechDb` (and never
|
|
46
|
+
* above `peakDb` dBFS). The high-pass's phase shift raises a voice's peaks; the mix caps a render's
|
|
47
|
+
* peak with a static gain, so a voice with a lower peak-to-loudness ratio keeps the render loud.
|
|
48
|
+
*/
|
|
49
|
+
limiter: { overSpeechDb: 12, peakDb: -1, lookahead: 0.002, release: 0.05 },
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
const dbToGain = (db: number): number => 10 ** (db / 20);
|
|
53
|
+
const gainToDb = (g: number): number => 20 * Math.log10(Math.max(g, 1e-12));
|
|
54
|
+
/** One-pole smoothing coefficient for a time constant of `seconds`. */
|
|
55
|
+
const coef = (seconds: number, sampleRate: number): number => (seconds > 0 ? Math.exp(-1 / (seconds * sampleRate)) : 0);
|
|
56
|
+
|
|
57
|
+
/** RBJ cookbook biquad coefficients (normalised, a0 = 1). */
|
|
58
|
+
interface Biquad {
|
|
59
|
+
b0: number;
|
|
60
|
+
b1: number;
|
|
61
|
+
b2: number;
|
|
62
|
+
a1: number;
|
|
63
|
+
a2: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function highpassCoefs(hz: number, q: number, sampleRate: number): Biquad {
|
|
67
|
+
const w = (2 * Math.PI * Math.min(hz, sampleRate * 0.45)) / sampleRate;
|
|
68
|
+
const cos = Math.cos(w);
|
|
69
|
+
const alpha = Math.sin(w) / (2 * q);
|
|
70
|
+
const a0 = 1 + alpha;
|
|
71
|
+
return { b0: (1 + cos) / 2 / a0, b1: -(1 + cos) / a0, b2: (1 + cos) / 2 / a0, a1: (-2 * cos) / a0, a2: (1 - alpha) / a0 };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `x` through a biquad (transposed direct form II, float64 state). */
|
|
75
|
+
function biquad(x: Float32Array, c: Biquad): Float32Array {
|
|
76
|
+
const y = new Float32Array(x.length);
|
|
77
|
+
let z1 = 0;
|
|
78
|
+
let z2 = 0;
|
|
79
|
+
for (let i = 0; i < x.length; i++) {
|
|
80
|
+
const v = x[i]!;
|
|
81
|
+
const out = c.b0 * v + z1;
|
|
82
|
+
z1 = c.b1 * v - c.a1 * out + z2;
|
|
83
|
+
z2 = c.b2 * v - c.a2 * out;
|
|
84
|
+
y[i] = out;
|
|
85
|
+
}
|
|
86
|
+
return y;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 2nd-order Butterworth high-pass at `hz` (default VOICE_FX_CHAIN.highpass). */
|
|
90
|
+
export function highpass(x: Float32Array, sampleRate: number, hz: number = VOICE_FX_CHAIN.highpass.hz, q: number = VOICE_FX_CHAIN.highpass.q): Float32Array {
|
|
91
|
+
return biquad(x, highpassCoefs(hz, q, sampleRate));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Dynamic-EQ de-esser. The sidechain is the band above deEss.hz (a 2nd-order high-pass); where its
|
|
96
|
+
* envelope comes within thresholdDb of the full-band envelope, an RBJ high shelf (S = 1) at
|
|
97
|
+
* deEss.shelfHz cuts by (excess × (1 - 1/ratio)) dB, at most maxReductionDb, its coefficients
|
|
98
|
+
* following the gain sample by sample; a band quieter than gateDb under the clip's peak (noise, the
|
|
99
|
+
* first samples of an onset) never triggers it. Where nothing is sibilant the shelf is at 0 dB: the identity.
|
|
100
|
+
* A clip whose rate puts the band at or above 0.45 × the sample rate is returned unchanged.
|
|
101
|
+
*/
|
|
102
|
+
export function deEss(x: Float32Array, sampleRate: number): Float32Array {
|
|
103
|
+
const p = VOICE_FX_CHAIN.deEss;
|
|
104
|
+
if (p.hz >= sampleRate * 0.45) return x.slice();
|
|
105
|
+
const side = biquad(x, highpassCoefs(p.hz, Math.SQRT1_2, sampleRate));
|
|
106
|
+
let peak = 0;
|
|
107
|
+
for (const v of x) peak = Math.max(peak, Math.abs(v));
|
|
108
|
+
const gate = peak * dbToGain(p.gateDb);
|
|
109
|
+
const att = coef(p.attack, sampleRate);
|
|
110
|
+
const rel = coef(p.release, sampleRate);
|
|
111
|
+
const slope = 1 - 1 / p.ratio;
|
|
112
|
+
const w = (2 * Math.PI * p.shelfHz) / sampleRate;
|
|
113
|
+
const cos = Math.cos(w);
|
|
114
|
+
const sinHalf = Math.sin(w) / 2; // alpha = sin(w)/2 · sqrt((A + 1/A)(1/S - 1) + 2), S = 1: sin(w)/2 · sqrt(2)
|
|
115
|
+
const y = new Float32Array(x.length);
|
|
116
|
+
let envHigh = 0;
|
|
117
|
+
let envFull = 0;
|
|
118
|
+
let z1 = 0;
|
|
119
|
+
let z2 = 0;
|
|
120
|
+
for (let i = 0; i < x.length; i++) {
|
|
121
|
+
const v = x[i]!;
|
|
122
|
+
const h = Math.abs(side[i]!);
|
|
123
|
+
const f = Math.abs(v);
|
|
124
|
+
envHigh = h > envHigh ? att * envHigh + (1 - att) * h : rel * envHigh + (1 - rel) * h;
|
|
125
|
+
envFull = f > envFull ? att * envFull + (1 - att) * f : rel * envFull + (1 - rel) * f;
|
|
126
|
+
let cut = 0;
|
|
127
|
+
if (envHigh > gate && envFull > 1e-9) {
|
|
128
|
+
const excess = gainToDb(envHigh / envFull) - p.thresholdDb;
|
|
129
|
+
if (excess > 0) cut = Math.min(p.maxReductionDb, excess * slope);
|
|
130
|
+
}
|
|
131
|
+
// At 0 dB (A = 1) b equals a exactly: with a settled state the output is the input, sample for sample.
|
|
132
|
+
const A = cut === 0 ? 1 : 10 ** (-cut / 40);
|
|
133
|
+
const sq = 2 * Math.sqrt(A) * sinHalf * Math.SQRT2;
|
|
134
|
+
const a0 = A + 1 - (A - 1) * cos + sq;
|
|
135
|
+
const b0 = (A * (A + 1 + (A - 1) * cos + sq)) / a0;
|
|
136
|
+
const b1 = (-2 * A * (A - 1 + (A + 1) * cos)) / a0;
|
|
137
|
+
const b2 = (A * (A + 1 + (A - 1) * cos - sq)) / a0;
|
|
138
|
+
const a1 = (2 * (A - 1 - (A + 1) * cos)) / a0;
|
|
139
|
+
const a2 = (A + 1 - (A - 1) * cos - sq) / a0;
|
|
140
|
+
const out = b0 * v + z1;
|
|
141
|
+
z1 = b1 * v - a1 * out + z2;
|
|
142
|
+
z2 = b2 * v - a2 * out;
|
|
143
|
+
y[i] = out;
|
|
144
|
+
}
|
|
145
|
+
return y;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** RMS of the 10 ms windows whose RMS is within 45 dB of the clip's peak (the windows the trim counts as speech); 0 for silence. */
|
|
149
|
+
export function speechRms(x: Float32Array, sampleRate: number): number {
|
|
150
|
+
let peak = 0;
|
|
151
|
+
for (const v of x) peak = Math.max(peak, Math.abs(v));
|
|
152
|
+
if (peak === 0) return 0;
|
|
153
|
+
const floor = peak * dbToGain(-45);
|
|
154
|
+
const w = Math.max(1, Math.round(sampleRate * 0.01));
|
|
155
|
+
let sum = 0;
|
|
156
|
+
let n = 0;
|
|
157
|
+
for (let at = 0; at < x.length; at += w) {
|
|
158
|
+
const to = Math.min(x.length, at + w);
|
|
159
|
+
let s = 0;
|
|
160
|
+
for (let i = at; i < to; i++) s += x[i]! * x[i]!;
|
|
161
|
+
if (Math.sqrt(s / (to - at)) <= floor) continue;
|
|
162
|
+
sum += s;
|
|
163
|
+
n += to - at;
|
|
164
|
+
}
|
|
165
|
+
return n ? Math.sqrt(sum / n) : 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Feed-forward compressor: a peak envelope (attack/release) that looks `lookahead` ahead (no attack
|
|
170
|
+
* overshoot on onsets), a soft-knee gain computer with its
|
|
171
|
+
* threshold at the clip's speech RMS + thresholdDb, then makeup so the speech RMS is what it was.
|
|
172
|
+
*/
|
|
173
|
+
export function compress(x: Float32Array, sampleRate: number): Float32Array {
|
|
174
|
+
const p = VOICE_FX_CHAIN.compressor;
|
|
175
|
+
const rmsIn = speechRms(x, sampleRate);
|
|
176
|
+
if (rmsIn === 0) return x.slice();
|
|
177
|
+
const threshold = gainToDb(rmsIn) + p.thresholdDb;
|
|
178
|
+
const att = coef(p.attack, sampleRate);
|
|
179
|
+
const look = Math.round(p.lookahead * sampleRate);
|
|
180
|
+
const rel = coef(p.release, sampleRate);
|
|
181
|
+
const half = p.kneeDb / 2;
|
|
182
|
+
const slope = 1 - 1 / p.ratio;
|
|
183
|
+
const y = new Float32Array(x.length);
|
|
184
|
+
let env = 0;
|
|
185
|
+
for (let i = 0; i < x.length; i++) {
|
|
186
|
+
const ahead = i + look;
|
|
187
|
+
const a = ahead < x.length ? Math.abs(x[ahead]!) : 0;
|
|
188
|
+
env = a > env ? att * env + (1 - att) * a : rel * env + (1 - rel) * a;
|
|
189
|
+
const over = gainToDb(env) - threshold;
|
|
190
|
+
let reduction = 0;
|
|
191
|
+
if (over >= half) reduction = over * slope;
|
|
192
|
+
else if (over > -half) reduction = (slope * (over + half) ** 2) / (2 * p.kneeDb);
|
|
193
|
+
y[i] = x[i]! * dbToGain(-reduction);
|
|
194
|
+
}
|
|
195
|
+
const rmsOut = speechRms(y, sampleRate);
|
|
196
|
+
const makeup = rmsOut > 0 ? rmsIn / rmsOut : 1;
|
|
197
|
+
for (let i = 0; i < y.length; i++) y[i]! *= makeup;
|
|
198
|
+
return y;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* A short room: the clip plus its tail (padded by rt60), through four damped combs in parallel and
|
|
203
|
+
* two all-passes in series, the wet signal scaled to the dry RMS and mixed in at `wet`
|
|
204
|
+
* ((1 - wet) dry + wet room). The tail is cut where it falls under tailFloorDb below the peak and
|
|
205
|
+
* faded over `fade`, so the clip is at most rt60 longer.
|
|
206
|
+
*/
|
|
207
|
+
export function roomReverb(x: Float32Array, sampleRate: number): Float32Array {
|
|
208
|
+
const p = VOICE_FX_CHAIN.reverb;
|
|
209
|
+
const n = x.length + Math.round(p.rt60 * sampleRate);
|
|
210
|
+
const pre = Math.round(p.predelay * sampleRate);
|
|
211
|
+
const wet = new Float64Array(n);
|
|
212
|
+
for (const seconds of p.combs) {
|
|
213
|
+
const d = Math.max(1, Math.round(seconds * sampleRate));
|
|
214
|
+
// Feedback for a 60 dB decay over rt60: g^(rt60 / delay) = 10^-3.
|
|
215
|
+
const g = 10 ** ((-3 * d) / (sampleRate * p.rt60));
|
|
216
|
+
const buf = new Float64Array(d);
|
|
217
|
+
let lp = 0;
|
|
218
|
+
let at = 0;
|
|
219
|
+
for (let i = 0; i < n; i++) {
|
|
220
|
+
const src = i - pre;
|
|
221
|
+
const input = src >= 0 && src < x.length ? x[src]! : 0;
|
|
222
|
+
const out = buf[at]!;
|
|
223
|
+
lp = out * (1 - p.damping) + lp * p.damping;
|
|
224
|
+
buf[at] = input + lp * g;
|
|
225
|
+
wet[i]! += out;
|
|
226
|
+
at = at + 1 === d ? 0 : at + 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
for (const seconds of p.allpasses) {
|
|
230
|
+
const d = Math.max(1, Math.round(seconds * sampleRate));
|
|
231
|
+
const buf = new Float64Array(d);
|
|
232
|
+
let at = 0;
|
|
233
|
+
for (let i = 0; i < n; i++) {
|
|
234
|
+
const delayed = buf[at]!;
|
|
235
|
+
const v = wet[i]!;
|
|
236
|
+
buf[at] = v + delayed * p.allpassGain;
|
|
237
|
+
wet[i] = delayed - v * p.allpassGain;
|
|
238
|
+
at = at + 1 === d ? 0 : at + 1;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
let dry2 = 0;
|
|
242
|
+
for (const v of x) dry2 += v * v;
|
|
243
|
+
let wet2 = 0;
|
|
244
|
+
for (const v of wet) wet2 += v * v;
|
|
245
|
+
const scale = wet2 > 0 ? Math.sqrt(dry2 / wet2) : 0;
|
|
246
|
+
const y = new Float32Array(n);
|
|
247
|
+
for (let i = 0; i < n; i++) y[i] = (1 - p.wet) * (i < x.length ? x[i]! : 0) + p.wet * scale * wet[i]!;
|
|
248
|
+
// Cut the tail where it has decayed under the floor (never into the dry clip), then fade it.
|
|
249
|
+
let peak = 0;
|
|
250
|
+
for (const v of y) peak = Math.max(peak, Math.abs(v));
|
|
251
|
+
const floor = peak * dbToGain(p.tailFloorDb);
|
|
252
|
+
let end = n;
|
|
253
|
+
while (end > x.length && Math.abs(y[end - 1]!) < floor) end--;
|
|
254
|
+
const out = y.slice(0, end);
|
|
255
|
+
const fade = Math.min(end - x.length, Math.round(p.fade * sampleRate));
|
|
256
|
+
for (let i = 0; i < fade; i++) out[end - 1 - i]! *= i / fade;
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** The limiter's ceiling for `x`: its speech RMS + limiter.overSpeechDb, never above limiter.peakDb dBFS. */
|
|
261
|
+
export function limiterCeiling(x: Float32Array, sampleRate: number): number {
|
|
262
|
+
const p = VOICE_FX_CHAIN.limiter;
|
|
263
|
+
const r = speechRms(x, sampleRate);
|
|
264
|
+
return Math.min(dbToGain(p.peakDb), r > 0 ? r * dbToGain(p.overSpeechDb) : Infinity);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Lookahead peak limiter: no sample exceeds limiterCeiling. The gain each sample needs is held at
|
|
269
|
+
* its minimum over the next `lookahead`, averaged over the last `lookahead` (a ramp that is down by
|
|
270
|
+
* the time the peak arrives, never above what any sample needs), and recovers with `release`.
|
|
271
|
+
* Only the sparse transients (plosives, clicks) above the ceiling are touched.
|
|
272
|
+
*/
|
|
273
|
+
export function limit(x: Float32Array, sampleRate: number): Float32Array {
|
|
274
|
+
const p = VOICE_FX_CHAIN.limiter;
|
|
275
|
+
const ceiling = limiterCeiling(x, sampleRate);
|
|
276
|
+
const n = x.length;
|
|
277
|
+
const need = new Float64Array(n);
|
|
278
|
+
let any = false;
|
|
279
|
+
for (let i = 0; i < n; i++) {
|
|
280
|
+
const a = Math.abs(x[i]!);
|
|
281
|
+
need[i] = a > ceiling ? ceiling / a : 1;
|
|
282
|
+
if (need[i]! < 1) any = true;
|
|
283
|
+
}
|
|
284
|
+
if (!any) return x.slice();
|
|
285
|
+
const L = Math.max(1, Math.round(p.lookahead * sampleRate));
|
|
286
|
+
// held[j] = min(need[j..j+L]); gain[i] = mean(held[i-L+1..i]): every held[j] in that mean covers i.
|
|
287
|
+
const held = new Float64Array(n);
|
|
288
|
+
for (let j = 0; j < n; j++) {
|
|
289
|
+
let m = 1;
|
|
290
|
+
for (let k = j, to = Math.min(n, j + L + 1); k < to; k++) if (need[k]! < m) m = need[k]!;
|
|
291
|
+
held[j] = m;
|
|
292
|
+
}
|
|
293
|
+
const rel = coef(p.release, sampleRate);
|
|
294
|
+
const y = new Float32Array(n);
|
|
295
|
+
let sum = 0;
|
|
296
|
+
let g = 1;
|
|
297
|
+
for (let i = 0; i < n; i++) {
|
|
298
|
+
sum += held[i]!;
|
|
299
|
+
if (i >= L) sum -= held[i - L]!;
|
|
300
|
+
// Before sample L-1 the missing terms are held[0], which covers samples 0..L too.
|
|
301
|
+
const target = (sum + Math.max(0, L - 1 - i) * held[0]!) / L;
|
|
302
|
+
g = target < g ? target : rel * g + (1 - rel) * target;
|
|
303
|
+
y[i] = x[i]! * g;
|
|
304
|
+
}
|
|
305
|
+
return y;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The 'studio' chain: high-pass → de-ess → compress → room reverb → peak limiter. The result is longer by the reverb tail. */
|
|
309
|
+
export function studioVoice(samples: Float32Array, sampleRate: number): Float32Array {
|
|
310
|
+
return limit(roomReverb(compress(deEss(highpass(samples, sampleRate), sampleRate), sampleRate), sampleRate), sampleRate);
|
|
311
|
+
}
|