@biunow/agent-biutalk 0.0.0-stage → 1.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 (227) hide show
  1. package/bin/server.js +355 -0
  2. package/engine/AGENTS.md +1414 -0
  3. package/engine/CLAUDE.md +1 -0
  4. package/engine/LICENSE +21 -0
  5. package/engine/README.md +216 -0
  6. package/engine/bin/explainroo.js +19 -0
  7. package/engine/dev/audio-lab.mjs +268 -0
  8. package/engine/docs/media/contact-sheet.jpg +0 -0
  9. package/engine/docs/media/dns-poster.jpg +0 -0
  10. package/engine/docs/media/intro-poster.jpg +0 -0
  11. package/engine/docs/media/logo-dark.png +0 -0
  12. package/engine/docs/media/logo-light.png +0 -0
  13. package/engine/docs/media/looks-dark.webp +0 -0
  14. package/engine/docs/media/looks-light.webp +0 -0
  15. package/engine/docs/media/product-demo.webp +0 -0
  16. package/engine/docs/media/rebase-poster.jpg +0 -0
  17. package/engine/docs/media/sizes-dark.webp +0 -0
  18. package/engine/docs/media/sizes-light.webp +0 -0
  19. package/engine/engine/audio/dsp.js +69 -0
  20. package/engine/engine/audio/instruments.js +147 -0
  21. package/engine/engine/audio/music.js +576 -0
  22. package/engine/engine/audio/pcm.js +93 -0
  23. package/engine/engine/audio/rng.js +40 -0
  24. package/engine/engine/audio/sfx.js +287 -0
  25. package/engine/engine/audio/soundtrack.js +201 -0
  26. package/engine/engine/audio/synth.js +721 -0
  27. package/engine/engine/audio/theory.js +105 -0
  28. package/engine/engine/audio/wav.js +47 -0
  29. package/engine/engine/captions.js +131 -0
  30. package/engine/engine/clips.js +107 -0
  31. package/engine/engine/code.js +179 -0
  32. package/engine/engine/director.js +475 -0
  33. package/engine/engine/frame.html +135 -0
  34. package/engine/engine/icons/LICENSE-lucide.txt +43 -0
  35. package/engine/engine/icons/lucide-tags.json +1 -0
  36. package/engine/engine/icons/lucide.json +1 -0
  37. package/engine/engine/layout.js +69 -0
  38. package/engine/engine/pathdata.js +234 -0
  39. package/engine/engine/pen.js +240 -0
  40. package/engine/engine/player.html +67 -0
  41. package/engine/engine/player.js +219 -0
  42. package/engine/engine/readability.js +9 -0
  43. package/engine/engine/runtime.js +690 -0
  44. package/engine/engine/stage.js +2247 -0
  45. package/engine/engine/studio.html +216 -0
  46. package/engine/engine/text.js +81 -0
  47. package/engine/engine/themes.js +360 -0
  48. package/engine/engine/transitions.js +161 -0
  49. package/engine/engine/ui.js +1042 -0
  50. package/engine/engine/upload.js +32 -0
  51. package/engine/engine/util.js +204 -0
  52. package/engine/examples/callbacks-to-async-await/code-versions.js +34 -0
  53. package/engine/examples/callbacks-to-async-await/scenes.js +106 -0
  54. package/engine/examples/callbacks-to-async-await/script.md +22 -0
  55. package/engine/examples/callbacks-to-async-await/video.json +16 -0
  56. package/engine/examples/circle-area/scenes.js +228 -0
  57. package/engine/examples/circle-area/script.md +37 -0
  58. package/engine/examples/circle-area/video.json +14 -0
  59. package/engine/examples/explainroo-intro/scenes.js +133 -0
  60. package/engine/examples/explainroo-intro/script.md +27 -0
  61. package/engine/examples/explainroo-intro/video.json +8 -0
  62. package/engine/examples/how-dns-works/scenes.js +130 -0
  63. package/engine/examples/how-dns-works/script.md +28 -0
  64. package/engine/examples/how-dns-works/video.json +8 -0
  65. package/engine/examples/leap-years/scenes.js +156 -0
  66. package/engine/examples/leap-years/script.md +25 -0
  67. package/engine/examples/leap-years/video.json +16 -0
  68. package/engine/examples/leap-years-ja/scenes.js +194 -0
  69. package/engine/examples/leap-years-ja/script.md +32 -0
  70. package/engine/examples/leap-years-ja/video.json +17 -0
  71. package/engine/examples/merge-vs-rebase/scenes.js +129 -0
  72. package/engine/examples/merge-vs-rebase/script.md +22 -0
  73. package/engine/examples/merge-vs-rebase/video.json +8 -0
  74. package/engine/examples/password-storage-zh/scenes.js +185 -0
  75. package/engine/examples/password-storage-zh/script.md +25 -0
  76. package/engine/examples/password-storage-zh/video.json +19 -0
  77. package/engine/examples/sqlite-demo/README.md +19 -0
  78. package/engine/examples/sqlite-demo/scenes.js +423 -0
  79. package/engine/examples/sqlite-demo/script.md +35 -0
  80. package/engine/examples/sqlite-demo/video.json +17 -0
  81. package/engine/examples/unspar-demo/assets/fonts/Figtree-400.woff2 +0 -0
  82. package/engine/examples/unspar-demo/assets/fonts/Figtree-500.woff2 +0 -0
  83. package/engine/examples/unspar-demo/assets/fonts/Figtree-600.woff2 +0 -0
  84. package/engine/examples/unspar-demo/assets/fonts/Figtree-700.woff2 +0 -0
  85. package/engine/examples/unspar-demo/assets/fonts/Figtree-OFL.txt +93 -0
  86. package/engine/examples/unspar-demo/assets/unspar-logo.svg +23 -0
  87. package/engine/examples/unspar-demo/scenes.js +367 -0
  88. package/engine/examples/unspar-demo/script.md +25 -0
  89. package/engine/examples/unspar-demo/video.json +21 -0
  90. package/engine/examples/why-is-the-sky-blue-es/scenes.js +160 -0
  91. package/engine/examples/why-is-the-sky-blue-es/script.md +22 -0
  92. package/engine/examples/why-is-the-sky-blue-es/video.json +15 -0
  93. package/engine/fonts/ArchitectsDaughter-400-latin-ext.woff2 +0 -0
  94. package/engine/fonts/ArchitectsDaughter-400-latin.woff2 +0 -0
  95. package/engine/fonts/CabinSketch-400-latin.woff2 +0 -0
  96. package/engine/fonts/CabinSketch-700-latin.woff2 +0 -0
  97. package/engine/fonts/InstrumentSerif-400-italic-latin-ext.woff2 +0 -0
  98. package/engine/fonts/InstrumentSerif-400-italic-latin.woff2 +0 -0
  99. package/engine/fonts/InstrumentSerif-400-normal-latin-ext.woff2 +0 -0
  100. package/engine/fonts/InstrumentSerif-400-normal-latin.woff2 +0 -0
  101. package/engine/fonts/Inter-400-latin-ext.woff2 +0 -0
  102. package/engine/fonts/Inter-400-latin.woff2 +0 -0
  103. package/engine/fonts/JetBrainsMono-400-latin-ext.woff2 +0 -0
  104. package/engine/fonts/JetBrainsMono-400-latin.woff2 +0 -0
  105. package/engine/fonts/Kalam-400-latin-ext.woff2 +0 -0
  106. package/engine/fonts/Kalam-400-latin.woff2 +0 -0
  107. package/engine/fonts/Kalam-700-latin-ext.woff2 +0 -0
  108. package/engine/fonts/Kalam-700-latin.woff2 +0 -0
  109. package/engine/fonts/NotoSansDevanagari-400.ttf +0 -0
  110. package/engine/fonts/NotoSansDevanagari-700.ttf +0 -0
  111. package/engine/fonts/PatrickHand-400-latin-ext.woff2 +0 -0
  112. package/engine/fonts/PatrickHand-400-latin.woff2 +0 -0
  113. package/engine/fonts/PlusJakartaSans-600-latin-ext.woff2 +0 -0
  114. package/engine/fonts/PlusJakartaSans-600-latin.woff2 +0 -0
  115. package/engine/fonts/SpaceGrotesk-500-latin-ext.woff2 +0 -0
  116. package/engine/fonts/SpaceGrotesk-500-latin.woff2 +0 -0
  117. package/engine/fonts/fonts.json +216 -0
  118. package/engine/fonts/licenses/ArchitectsDaughter-OFL.txt +93 -0
  119. package/engine/fonts/licenses/CabinSketch-OFL.txt +93 -0
  120. package/engine/fonts/licenses/InstrumentSerif-OFL.txt +93 -0
  121. package/engine/fonts/licenses/Inter-OFL.txt +93 -0
  122. package/engine/fonts/licenses/JetBrainsMono-OFL.txt +93 -0
  123. package/engine/fonts/licenses/Kalam-OFL.txt +93 -0
  124. package/engine/fonts/licenses/NotoSansDevanagari-OFL.txt +118 -0
  125. package/engine/fonts/licenses/PatrickHand-OFL.txt +93 -0
  126. package/engine/fonts/licenses/PlusJakartaSans-OFL.txt +93 -0
  127. package/engine/fonts/licenses/SpaceGrotesk-OFL.txt +93 -0
  128. package/engine/package-lock.json +1201 -0
  129. package/engine/package.json +67 -0
  130. package/engine/scripts/build-icons.mjs +74 -0
  131. package/engine/scripts/fetch-fonts.mjs +68 -0
  132. package/engine/skills/explainroo/SKILL.md +51 -0
  133. package/engine/src/align.js +357 -0
  134. package/engine/src/browser.js +121 -0
  135. package/engine/src/capture.js +315 -0
  136. package/engine/src/chapters.js +32 -0
  137. package/engine/src/cli.js +421 -0
  138. package/engine/src/clips.js +19 -0
  139. package/engine/src/elevenlabs.js +90 -0
  140. package/engine/src/env.js +16 -0
  141. package/engine/src/export.js +187 -0
  142. package/engine/src/ffmpeg.js +103 -0
  143. package/engine/src/fonts.js +86 -0
  144. package/engine/src/g2p.py +76 -0
  145. package/engine/src/images.js +151 -0
  146. package/engine/src/licenses/misaki-Apache-2.0.txt +201 -0
  147. package/engine/src/math.js +83 -0
  148. package/engine/src/models.js +161 -0
  149. package/engine/src/phonemes.js +162 -0
  150. package/engine/src/pipeline.js +63 -0
  151. package/engine/src/project.js +331 -0
  152. package/engine/src/qa.js +186 -0
  153. package/engine/src/recordings.js +89 -0
  154. package/engine/src/render.js +156 -0
  155. package/engine/src/script.js +266 -0
  156. package/engine/src/server.js +288 -0
  157. package/engine/src/subtitles.js +78 -0
  158. package/engine/src/timeline.js +55 -0
  159. package/engine/src/voice.js +240 -0
  160. package/engine/src/wav.js +98 -0
  161. package/engine/templates/starter/scenes.js +31 -0
  162. package/engine/templates/starter/script.md +14 -0
  163. package/engine/test/align.test.js +108 -0
  164. package/engine/test/brand.test.js +41 -0
  165. package/engine/test/browser.test.js +89 -0
  166. package/engine/test/camera-browser.test.js +225 -0
  167. package/engine/test/caption-phrases.test.js +35 -0
  168. package/engine/test/capture-browser.test.js +90 -0
  169. package/engine/test/capture-fixture.js +46 -0
  170. package/engine/test/capture.test.js +62 -0
  171. package/engine/test/chapters.test.js +41 -0
  172. package/engine/test/cjk-browser.test.js +101 -0
  173. package/engine/test/cjk.test.js +232 -0
  174. package/engine/test/cli-exit.test.js +25 -0
  175. package/engine/test/cli.test.js +116 -0
  176. package/engine/test/clips-browser.test.js +125 -0
  177. package/engine/test/clips.test.js +67 -0
  178. package/engine/test/code-morph-browser.test.js +99 -0
  179. package/engine/test/code-morph.test.js +110 -0
  180. package/engine/test/contrast-browser.test.js +63 -0
  181. package/engine/test/contrast.test.js +80 -0
  182. package/engine/test/director.test.js +316 -0
  183. package/engine/test/engine-pure.test.js +29 -0
  184. package/engine/test/export-browser.test.js +224 -0
  185. package/engine/test/export.test.js +96 -0
  186. package/engine/test/fixtures/capture.html +55 -0
  187. package/engine/test/formats-pace.test.js +104 -0
  188. package/engine/test/languages-browser.test.js +78 -0
  189. package/engine/test/languages.test.js +140 -0
  190. package/engine/test/looks-browser.test.js +109 -0
  191. package/engine/test/looks-preview.test.js +57 -0
  192. package/engine/test/looks.test.js +130 -0
  193. package/engine/test/math-browser.test.js +114 -0
  194. package/engine/test/math.test.js +54 -0
  195. package/engine/test/multi-size-browser.test.js +133 -0
  196. package/engine/test/multi-size.test.js +116 -0
  197. package/engine/test/phonemes.test.js +92 -0
  198. package/engine/test/readability-browser.test.js +36 -0
  199. package/engine/test/readability.test.js +20 -0
  200. package/engine/test/render-options.test.js +52 -0
  201. package/engine/test/script.test.js +67 -0
  202. package/engine/test/server.test.js +256 -0
  203. package/engine/test/stills-browser.test.js +70 -0
  204. package/engine/test/subtitles.test.js +83 -0
  205. package/engine/test/timeline.test.js +31 -0
  206. package/engine/test/transparent-browser.test.js +123 -0
  207. package/engine/test/ui-kit.test.js +96 -0
  208. package/engine/test/upload-browser.test.js +55 -0
  209. package/engine/test/voice-providers.test.js +338 -0
  210. package/lib/host.mjs +480 -0
  211. package/manifest.json +54 -0
  212. package/package.json +8 -4
  213. package/skills/biutalk/SKILL.md +21 -0
  214. package/studio/app.js +209 -0
  215. package/studio/assert-panel-permit.mjs +67 -0
  216. package/studio/composer.css +354 -0
  217. package/studio/composer.js +169 -0
  218. package/studio/index.html +82 -0
  219. package/studio/looks/blueprint.svg +21 -0
  220. package/studio/looks/chalk.svg +15 -0
  221. package/studio/looks/clean.svg +20 -0
  222. package/studio/looks/editorial.svg +18 -0
  223. package/studio/looks/midnight.svg +17 -0
  224. package/studio/looks/paper.svg +11 -0
  225. package/studio/select-menu.js +268 -0
  226. package/studio/styles.css +4 -0
  227. package/README.md +0 -3
@@ -0,0 +1,721 @@
1
+ // Deterministic sample-level synthesis core. Everything is plain JavaScript:
2
+ // voices are summed in scheduling order, so the same schedule always produces
3
+ // the same samples (Web Audio sums node inputs in an unspecified order).
4
+ //
5
+ // A Synth owns lanes (mono instrument buses with filter, pump, pan and a
6
+ // reverb send, or stereo buses for sound effects) and voices (short-lived
7
+ // oscillator/noise stacks with their own envelopes and optional filter).
8
+ // It renders block by block into caller-provided stereo buffers.
9
+
10
+ export const BLOCK = 256;
11
+ const TWO_PI = Math.PI * 2;
12
+
13
+ // --- sine table ----------------------------------------------------------------
14
+
15
+ const TABLE = 4096;
16
+ const SINE = new Float64Array(TABLE + 1);
17
+ for (let i = 0; i <= TABLE; i++) SINE[i] = Math.sin((TWO_PI * i) / TABLE);
18
+
19
+ // --- envelopes (AudioParam-style automation, evaluated per sample) --------------
20
+
21
+ const HOLD = 0;
22
+ const LIN = 1;
23
+ const EXP = 2;
24
+ const TARGET = 3;
25
+
26
+ export class Env {
27
+ constructor(initial = 0) {
28
+ this.initial = initial;
29
+ this.events = [];
30
+ }
31
+ set(v, t) {
32
+ this.events.push([HOLD, t, v, 0]);
33
+ return this;
34
+ }
35
+ lin(v, t) {
36
+ this.events.push([LIN, t, v, 0]);
37
+ return this;
38
+ }
39
+ exp(v, t) {
40
+ this.events.push([EXP, t, v, 0]);
41
+ return this;
42
+ }
43
+ target(v, t, tau) {
44
+ this.events.push([TARGET, t, v, tau]);
45
+ return this;
46
+ }
47
+
48
+ // Prepare for sequential evaluation starting at absolute sample `from`.
49
+ begin(sr, from) {
50
+ this.ev = this.events
51
+ .map((e, i) => ({ k: e[0], s: Math.max(0, Math.round(e[1] * sr)), v: e[2], c: e[0] === TARGET ? Math.exp(-1 / (Math.max(1e-5, e[3]) * sr)) : 0, i }))
52
+ .sort((a, b) => a.s - b.s || a.i - b.i);
53
+ this.idx = 0;
54
+ this.n = from;
55
+ this.v = this.initial;
56
+ this.mode = HOLD;
57
+ this.dv = 0;
58
+ this.ratio = 1;
59
+ this.tv = 0;
60
+ this.tc = 0;
61
+ this.targeting = false;
62
+ this._process();
63
+ }
64
+
65
+ _process() {
66
+ const ev = this.ev;
67
+ while (this.idx < ev.length && ev[this.idx].s <= this.n) {
68
+ const e = ev[this.idx++];
69
+ if (e.k === TARGET) {
70
+ this.targeting = true;
71
+ this.tv = e.v;
72
+ this.tc = e.c;
73
+ } else {
74
+ this.v = e.v;
75
+ this.targeting = false;
76
+ }
77
+ }
78
+ const next = ev[this.idx];
79
+ this.nextS = next ? next.s : Infinity;
80
+ this.mode = this.targeting ? TARGET : HOLD;
81
+ if (next && (next.k === LIN || next.k === EXP)) {
82
+ const len = next.s - this.n;
83
+ if (next.k === EXP && this.v > 0 && next.v > 0) {
84
+ this.mode = EXP;
85
+ this.ratio = Math.pow(next.v / this.v, 1 / len);
86
+ } else {
87
+ this.mode = LIN;
88
+ this.dv = (next.v - this.v) / len;
89
+ }
90
+ }
91
+ }
92
+
93
+ fill(out, count) {
94
+ let v = this.v;
95
+ let n = this.n;
96
+ let mode = this.mode;
97
+ for (let k = 0; k < count; k++) {
98
+ if (n >= this.nextS) {
99
+ this.v = v;
100
+ this.n = n;
101
+ this._process();
102
+ v = this.v;
103
+ mode = this.mode;
104
+ }
105
+ out[k] = v;
106
+ if (mode === LIN) v += this.dv;
107
+ else if (mode === EXP) v *= this.ratio;
108
+ else if (mode === TARGET) v = this.tv + (v - this.tv) * this.tc;
109
+ n++;
110
+ }
111
+ this.v = v;
112
+ this.n = n;
113
+ }
114
+ }
115
+
116
+ // --- oscillator helpers -----------------------------------------------------------
117
+
118
+ function polyblep(t, dt) {
119
+ if (t < dt) {
120
+ t /= dt;
121
+ return t + t - t * t - 1;
122
+ }
123
+ if (t > 1 - dt) {
124
+ t = (t - 1) / dt;
125
+ return t * t + t + t + 1;
126
+ }
127
+ return 0;
128
+ }
129
+
130
+ export const WAVES = { sine: 0, saw: 1, tri: 2, square: 3, noise: 4, pink: 5 };
131
+
132
+ // --- biquad (RBJ cookbook) ------------------------------------------------------------
133
+
134
+ export function biquadCoefs(type, freq, q, sr, c) {
135
+ const f = Math.min(Math.max(freq, 10), sr * 0.45);
136
+ const w0 = (TWO_PI * f) / sr;
137
+ const cs = Math.cos(w0);
138
+ const alpha = Math.sin(w0) / (2 * Math.max(0.05, q));
139
+ let b0;
140
+ let b1;
141
+ let b2;
142
+ if (type === 'highpass') {
143
+ b0 = (1 + cs) / 2;
144
+ b1 = -(1 + cs);
145
+ b2 = (1 + cs) / 2;
146
+ } else if (type === 'bandpass') {
147
+ b0 = alpha;
148
+ b1 = 0;
149
+ b2 = -alpha;
150
+ } else {
151
+ b0 = (1 - cs) / 2;
152
+ b1 = 1 - cs;
153
+ b2 = (1 - cs) / 2;
154
+ }
155
+ const a0 = 1 + alpha;
156
+ c.b0 = b0 / a0;
157
+ c.b1 = b1 / a0;
158
+ c.b2 = b2 / a0;
159
+ c.a1 = (-2 * cs) / a0;
160
+ c.a2 = (1 - alpha) / a0;
161
+ }
162
+
163
+ // Transposed direct form II, in place over buf[0..count).
164
+ function biquadRun(c, buf, from, to) {
165
+ const { b0, b1, b2, a1, a2 } = c;
166
+ let z1 = c.z1;
167
+ let z2 = c.z2;
168
+ for (let k = from; k < to; k++) {
169
+ const x = buf[k];
170
+ const y = b0 * x + z1;
171
+ z1 = b1 * x - a1 * y + z2;
172
+ z2 = b2 * x - a2 * y;
173
+ buf[k] = y;
174
+ }
175
+ c.z1 = z1;
176
+ c.z2 = z2;
177
+ }
178
+
179
+ export function makeFilter(type, freq, q = 0.707) {
180
+ return { type, freq, q, b0: 1, b1: 0, b2: 0, a1: 0, a2: 0, z1: 0, z2: 0, lastF: -1 };
181
+ }
182
+
183
+ // --- voices ---------------------------------------------------------------------------
184
+
185
+ // A voice is a stack of layers summed, optionally filtered, then written to its
186
+ // lane (mono) or panned into it (stereo lanes).
187
+ // layer: { wave, freq: number, freqEnv?: Env (Hz, overrides freq), detune (cents),
188
+ // amp: Env, fm?: { ratio, depth: Env (Hz) }, seed }
189
+ // filter: { type, freq, freqEnv?: Env, q }
190
+ export class Voice {
191
+ constructor(t0, t1) {
192
+ this.t0 = t0;
193
+ this.t1 = t1;
194
+ this.layers = [];
195
+ this.filter = null;
196
+ this.pan = 0;
197
+ this.panEnv = null;
198
+ }
199
+ layer(spec) {
200
+ this.layers.push(spec);
201
+ return this;
202
+ }
203
+ }
204
+
205
+ // --- reverb (Freeverb topology, block processed) ---------------------------------------------
206
+
207
+ const COMBS = [1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617];
208
+ const ALLPASSES = [556, 441, 341, 225];
209
+ const SPREAD = 23;
210
+
211
+ export class Reverb {
212
+ constructor(sr, { room = 0.8, damp = 0.35, preDelay = 0.02, lowCut = 200 } = {}) {
213
+ const scale = sr / 44100;
214
+ const line = (d) => ({ buf: new Float32Array(Math.max(1, Math.round(d * scale))), i: 0, fs: 0 });
215
+ this.combs = COMBS.map(line);
216
+ this.aps = [ALLPASSES.map(line), ALLPASSES.map((d) => line(d + SPREAD))];
217
+ this.fb = room * 0.28 + 0.7;
218
+ this.damp = damp * 0.4;
219
+ this.pre = new Float32Array(Math.max(1, Math.round(preDelay * sr)));
220
+ this.preI = 0;
221
+ this.hpA = Math.exp((-TWO_PI * lowCut) / sr);
222
+ this.hpX = 0;
223
+ this.hpY = 0;
224
+ this.inBuf = new Float32Array(BLOCK);
225
+ this.wet = new Float32Array(BLOCK);
226
+ this.chBuf = new Float32Array(BLOCK);
227
+ // after this many silent input samples the tail is below -100 dB: skip work
228
+ this.tailSamples = Math.round(sr * (0.4 + 12 * (1 / (1 - this.fb)) * (1116 * scale) / sr));
229
+ this.silent = this.tailSamples;
230
+ }
231
+
232
+ // Adds the wet signal (times `level`) for mono `input[0..n)` into outL/outR.
233
+ process(input, n, outL, outR, level) {
234
+ let loud = false;
235
+ for (let k = 0; k < n; k++) {
236
+ if (input[k] !== 0) {
237
+ loud = true;
238
+ break;
239
+ }
240
+ }
241
+ if (loud) this.silent = 0;
242
+ else if (this.silent >= this.tailSamples) return;
243
+ else this.silent += n;
244
+
245
+ const x = this.inBuf;
246
+ const pre = this.pre;
247
+ let pi = this.preI;
248
+ let hx = this.hpX;
249
+ let hy = this.hpY;
250
+ const a = this.hpA;
251
+ for (let k = 0; k < n; k++) {
252
+ const d = pre[pi];
253
+ pre[pi] = input[k];
254
+ pi = pi + 1 === pre.length ? 0 : pi + 1;
255
+ hy = a * (hy + d - hx);
256
+ hx = d;
257
+ x[k] = hy * 0.015;
258
+ }
259
+ this.preI = pi;
260
+ this.hpX = hx;
261
+ this.hpY = hy;
262
+
263
+ const fb = this.fb;
264
+ const damp = this.damp;
265
+ const damp1 = 1 - damp;
266
+ const wet = this.wet;
267
+ wet.fill(0, 0, n);
268
+ for (const c of this.combs) {
269
+ const buf = c.buf;
270
+ const len = buf.length;
271
+ let i = c.i;
272
+ let fs = c.fs;
273
+ for (let k = 0; k < n; k++) {
274
+ const o = buf[i];
275
+ fs = o * damp1 + fs * damp;
276
+ buf[i] = x[k] + fs * fb;
277
+ if (++i === len) i = 0;
278
+ wet[k] += o;
279
+ }
280
+ c.i = i;
281
+ c.fs = fs;
282
+ }
283
+ const outs = [outL, outR];
284
+ const g = level * 3;
285
+ for (let ch = 0; ch < 2; ch++) {
286
+ const y = this.chBuf;
287
+ y.set(wet.subarray(0, n));
288
+ for (const ap of this.aps[ch]) {
289
+ const buf = ap.buf;
290
+ const len = buf.length;
291
+ let i = ap.i;
292
+ for (let k = 0; k < n; k++) {
293
+ const bo = buf[i];
294
+ const inp = y[k];
295
+ buf[i] = inp + bo * 0.5;
296
+ y[k] = bo - inp;
297
+ if (++i === len) i = 0;
298
+ }
299
+ ap.i = i;
300
+ }
301
+ const out = outs[ch];
302
+ for (let k = 0; k < n; k++) out[k] += y[k] * g;
303
+ }
304
+ }
305
+ }
306
+
307
+ // --- glue compressor (stereo linked, feed-forward, soft knee) ---------------------------------
308
+
309
+ export class Compressor {
310
+ constructor(sr, { threshold = -20, ratio = 2, knee = 10, attack = 0.02, release = 0.3, makeupDb = 0 } = {}) {
311
+ this.t = threshold;
312
+ this.r = ratio;
313
+ this.k = knee;
314
+ this.att = Math.exp(-1 / (attack * sr));
315
+ this.rel = Math.exp(-1 / (release * sr));
316
+ this.env = 0;
317
+ this.makeup = Math.pow(10, makeupDb / 20);
318
+ this.gain = 1;
319
+ }
320
+ process(L, R, n) {
321
+ let env = this.env;
322
+ let g = this.gain;
323
+ for (let k = 0; k < n; k++) {
324
+ const p = Math.max(Math.abs(L[k]), Math.abs(R[k]));
325
+ env = p > env ? p + (env - p) * this.att : p + (env - p) * this.rel;
326
+ if ((k & 7) === 0) {
327
+ const lvl = 20 * Math.log10(env + 1e-9);
328
+ const over = lvl - this.t;
329
+ let gr = 0;
330
+ if (over > this.k / 2) gr = (1 / this.r - 1) * over;
331
+ else if (over > -this.k / 2) {
332
+ const x = over + this.k / 2;
333
+ gr = ((1 / this.r - 1) * x * x) / (2 * this.k);
334
+ }
335
+ g = Math.pow(10, gr / 20) * this.makeup;
336
+ }
337
+ L[k] *= g;
338
+ R[k] *= g;
339
+ }
340
+ this.env = env;
341
+ this.gain = g;
342
+ }
343
+ }
344
+
345
+ // --- lanes ---------------------------------------------------------------------------------
346
+
347
+ export class Lane {
348
+ constructor(synth, spec = {}) {
349
+ this.synth = synth;
350
+ this.stereo = !!spec.stereo;
351
+ this.gain = spec.gain ?? 1;
352
+ this.panBase = spec.pan ?? 0;
353
+ this.panLfo = spec.panLfo || null;
354
+ this.send = spec.reverb || 0;
355
+ this.filter = spec.filter ? makeFilter(spec.filter.type, spec.filter.freq, spec.filter.q ?? 0.707) : null;
356
+ this.filterLfo = spec.filter && spec.filter.lfo ? spec.filter.lfo : null;
357
+ this.pumpTimes = spec.pump ? [] : null;
358
+ this.pumpI = 0;
359
+ this.pumpD = 0;
360
+ this.mono = new Float32Array(BLOCK);
361
+ this.L = this.stereo ? new Float32Array(BLOCK) : null;
362
+ this.R = this.stereo ? new Float32Array(BLOCK) : null;
363
+ }
364
+
365
+ // Ducks the lane at time t (tech-style pumping); decays back to 1.
366
+ pump(t, depth = 0.55) {
367
+ if (this.pumpTimes) this.pumpTimes.push([Math.round(t * this.synth.sr), depth]);
368
+ }
369
+
370
+ clear(n) {
371
+ this.dirty = false;
372
+ if (this.stereo) {
373
+ this.L.fill(0, 0, n);
374
+ this.R.fill(0, 0, n);
375
+ } else {
376
+ this.mono.fill(0, 0, n);
377
+ }
378
+ }
379
+
380
+ // Mixes the block into the synth's dry buffers and the reverb input.
381
+ mix(i0, n, dryL, dryR, send) {
382
+ if (!this.dirty && !this.pumpTimes && (!this.filter || (Math.abs(this.filter.z1) < 1e-12 && Math.abs(this.filter.z2) < 1e-12))) return;
383
+ const sr = this.synth.sr;
384
+ const tMid = (i0 + n / 2) / sr;
385
+ const g = this.gain;
386
+ if (this.stereo) {
387
+ const L = this.L;
388
+ const R = this.R;
389
+ const s = this.send;
390
+ for (let k = 0; k < n; k++) {
391
+ const l = L[k] * g;
392
+ const r = R[k] * g;
393
+ dryL[k] += l;
394
+ dryR[k] += r;
395
+ if (s) send[k] += (l + r) * 0.5 * s;
396
+ }
397
+ return;
398
+ }
399
+ const m = this.mono;
400
+ if (this.filter) {
401
+ const f = this.filter;
402
+ const freq = this.filterLfo ? f.freq + this.filterLfo.depth * Math.sin(TWO_PI * this.filterLfo.rate * tMid) : f.freq;
403
+ if (freq !== f.lastF) {
404
+ biquadCoefs(f.type, freq, f.q, sr, f);
405
+ f.lastF = freq;
406
+ }
407
+ biquadRun(f, m, 0, n);
408
+ }
409
+ if (this.pumpTimes) {
410
+ const times = this.pumpTimes;
411
+ const c = Math.exp(-1 / (0.07 * sr));
412
+ let d = this.pumpD;
413
+ let pi = this.pumpI;
414
+ for (let k = 0; k < n; k++) {
415
+ while (pi < times.length && times[pi][0] <= i0 + k) d = times[pi++][1];
416
+ m[k] *= 1 - d;
417
+ d *= c;
418
+ }
419
+ this.pumpD = d;
420
+ this.pumpI = pi;
421
+ }
422
+ let pan = this.panBase;
423
+ if (this.panLfo) pan += this.panLfo.depth * Math.sin(TWO_PI * this.panLfo.rate * tMid);
424
+ pan = Math.max(-1, Math.min(1, pan));
425
+ const x = ((pan + 1) / 2) * (Math.PI / 2);
426
+ const gl = Math.cos(x) * g * Math.SQRT2;
427
+ const gr = Math.sin(x) * g * Math.SQRT2;
428
+ const s = this.send * g;
429
+ for (let k = 0; k < n; k++) {
430
+ const v = m[k];
431
+ dryL[k] += v * gl;
432
+ dryR[k] += v * gr;
433
+ if (s) send[k] += v * s;
434
+ }
435
+ }
436
+ }
437
+
438
+ // --- the synth ---------------------------------------------------------------------------------
439
+
440
+ export class Synth {
441
+ constructor(sampleRate, length, { reverb = null, reverbLevel = 0, gain = 1, highpass = 0 } = {}) {
442
+ this.sr = sampleRate;
443
+ this.length = length;
444
+ this.lanes = [];
445
+ this.pending = [];
446
+ this.sorted = null;
447
+ this.active = [];
448
+ this.next = 0;
449
+ this.reverb = reverb ? new Reverb(sampleRate, reverb) : null;
450
+ this.reverbLevel = reverbLevel;
451
+ this.gain = gain;
452
+ this.hp = null;
453
+ if (highpass > 0) {
454
+ this.hp = [makeFilter('highpass', highpass, 0.707), makeFilter('highpass', highpass, 0.707)];
455
+ for (const f of this.hp) biquadCoefs('highpass', highpass, 0.707, sampleRate, f);
456
+ }
457
+ this.send = new Float32Array(BLOCK);
458
+ this.acc = new Float32Array(BLOCK);
459
+ this.tmp = new Float32Array(BLOCK);
460
+ this.amp = new Float32Array(BLOCK);
461
+ this.fq = new Float32Array(BLOCK);
462
+ this.dep = new Float32Array(BLOCK);
463
+ this.pan = new Float32Array(BLOCK);
464
+ }
465
+
466
+ lane(spec) {
467
+ const lane = new Lane(this, spec);
468
+ this.lanes.push(lane);
469
+ return lane;
470
+ }
471
+
472
+ add(lane, voice) {
473
+ voice.lane = lane;
474
+ voice.start = Math.max(0, Math.round(voice.t0 * this.sr));
475
+ voice.end = Math.min(this.length, Math.round(voice.t1 * this.sr));
476
+ voice.order = this.pending.length;
477
+ if (voice.end > voice.start) this.pending.push(voice);
478
+ return voice;
479
+ }
480
+
481
+ _begin(v) {
482
+ const sr = this.sr;
483
+ for (const L of v.layers) {
484
+ L.amp.begin(sr, v.start);
485
+ if (L.freqEnv) L.freqEnv.begin(sr, v.start);
486
+ if (L.fm) L.fm.depth.begin(sr, v.start);
487
+ L.ph = 0;
488
+ L.mph = 0;
489
+ L.rs = (L.seed >>> 0) || 0x9e3779b9;
490
+ L.b0 = 0;
491
+ L.b1 = 0;
492
+ L.b2 = 0;
493
+ L.mul = L.detune ? Math.pow(2, L.detune / 1200) : 1;
494
+ }
495
+ if (v.filter) {
496
+ const f = v.filter;
497
+ Object.assign(f, { b0: 1, b1: 0, b2: 0, a1: 0, a2: 0, z1: 0, z2: 0, lastF: -1 });
498
+ if (f.freqEnv) f.freqEnv.begin(sr, v.start);
499
+ }
500
+ if (v.panEnv) v.panEnv.begin(sr, v.start);
501
+ }
502
+
503
+ _renderVoice(v, i0, n) {
504
+ const a = Math.max(v.start, i0);
505
+ const b = Math.min(v.end, i0 + n);
506
+ const cnt = b - a;
507
+ if (cnt <= 0) return;
508
+ const sr = this.sr;
509
+ const acc = this.acc;
510
+ const amp = this.amp;
511
+ const fq = this.fq;
512
+ acc.fill(0, 0, cnt);
513
+ for (const L of v.layers) {
514
+ L.amp.fill(amp, cnt);
515
+ if (L.freqEnv) L.freqEnv.fill(fq, cnt);
516
+ const mul = L.mul;
517
+ const fixed = L.freq * mul;
518
+ let ph = L.ph;
519
+ switch (L.wave) {
520
+ case 0: { // sine, optional FM
521
+ if (L.fm) {
522
+ const dep = this.dep;
523
+ L.fm.depth.fill(dep, cnt);
524
+ let mph = L.mph;
525
+ const ratio = L.fm.ratio;
526
+ for (let k = 0; k < cnt; k++) {
527
+ const f = L.freqEnv ? fq[k] * mul : fixed;
528
+ let x = mph * TABLE;
529
+ let i = x | 0;
530
+ const mod = SINE[i] + (SINE[i + 1] - SINE[i]) * (x - i);
531
+ mph += (f * ratio) / sr;
532
+ mph -= Math.floor(mph);
533
+ x = ph * TABLE;
534
+ i = x | 0;
535
+ acc[k] += (SINE[i] + (SINE[i + 1] - SINE[i]) * (x - i)) * amp[k];
536
+ ph += (f + dep[k] * mod) / sr;
537
+ ph -= Math.floor(ph);
538
+ }
539
+ L.mph = mph;
540
+ } else if (L.freqEnv) {
541
+ for (let k = 0; k < cnt; k++) {
542
+ const x = ph * TABLE;
543
+ const i = x | 0;
544
+ acc[k] += (SINE[i] + (SINE[i + 1] - SINE[i]) * (x - i)) * amp[k];
545
+ ph += (fq[k] * mul) / sr;
546
+ ph -= Math.floor(ph);
547
+ }
548
+ } else {
549
+ const inc = fixed / sr;
550
+ for (let k = 0; k < cnt; k++) {
551
+ const x = ph * TABLE;
552
+ const i = x | 0;
553
+ acc[k] += (SINE[i] + (SINE[i + 1] - SINE[i]) * (x - i)) * amp[k];
554
+ ph += inc;
555
+ if (ph >= 1) ph -= 1;
556
+ }
557
+ }
558
+ break;
559
+ }
560
+ case 1: { // saw (PolyBLEP)
561
+ const env = L.freqEnv;
562
+ let dt = fixed / sr;
563
+ for (let k = 0; k < cnt; k++) {
564
+ if (env) dt = (fq[k] * mul) / sr;
565
+ let v = 2 * ph - 1;
566
+ if (ph < dt) {
567
+ const u = ph / dt;
568
+ v -= u + u - u * u - 1;
569
+ } else if (ph > 1 - dt) {
570
+ const u = (ph - 1) / dt;
571
+ v -= u * u + u + u + 1;
572
+ }
573
+ acc[k] += v * amp[k];
574
+ ph += dt;
575
+ if (ph >= 1) ph -= 1;
576
+ }
577
+ break;
578
+ }
579
+ case 3: { // square: difference of two band-limited saws
580
+ const env = L.freqEnv;
581
+ let dt = fixed / sr;
582
+ for (let k = 0; k < cnt; k++) {
583
+ if (env) dt = (fq[k] * mul) / sr;
584
+ let p2 = ph + 0.5;
585
+ if (p2 >= 1) p2 -= 1;
586
+ const s = (2 * ph - 1 - polyblep(ph, dt) - (2 * p2 - 1 - polyblep(p2, dt))) * 0.5;
587
+ acc[k] += s * amp[k];
588
+ ph += dt;
589
+ if (ph >= 1) ph -= 1;
590
+ }
591
+ break;
592
+ }
593
+ case 2: { // triangle
594
+ const env = L.freqEnv;
595
+ let dt = fixed / sr;
596
+ for (let k = 0; k < cnt; k++) {
597
+ if (env) dt = (fq[k] * mul) / sr;
598
+ acc[k] += (4 * Math.abs(ph - 0.5) - 1) * amp[k];
599
+ ph += dt;
600
+ if (ph >= 1) ph -= 1;
601
+ }
602
+ break;
603
+ }
604
+ case 4: { // white noise
605
+ let s = L.rs;
606
+ for (let k = 0; k < cnt; k++) {
607
+ s ^= s << 13;
608
+ s ^= s >>> 17;
609
+ s ^= s << 5;
610
+ acc[k] += ((s >>> 0) / 2147483648 - 1) * amp[k];
611
+ }
612
+ L.rs = s;
613
+ break;
614
+ }
615
+ case 5: { // pink noise (Paul Kellet economy filter)
616
+ let s = L.rs;
617
+ let b0 = L.b0;
618
+ let b1 = L.b1;
619
+ let b2 = L.b2;
620
+ for (let k = 0; k < cnt; k++) {
621
+ s ^= s << 13;
622
+ s ^= s >>> 17;
623
+ s ^= s << 5;
624
+ const w = (s >>> 0) / 2147483648 - 1;
625
+ b0 = 0.99765 * b0 + w * 0.099046;
626
+ b1 = 0.963 * b1 + w * 0.2965164;
627
+ b2 = 0.57 * b2 + w * 1.0526913;
628
+ acc[k] += (b0 + b1 + b2 + w * 0.1848) * 0.22 * amp[k];
629
+ }
630
+ L.rs = s;
631
+ L.b0 = b0;
632
+ L.b1 = b1;
633
+ L.b2 = b2;
634
+ break;
635
+ }
636
+ default:
637
+ break;
638
+ }
639
+ L.ph = ph;
640
+ }
641
+
642
+ if (v.filter) {
643
+ const f = v.filter;
644
+ if (f.freqEnv) {
645
+ f.freqEnv.fill(fq, cnt);
646
+ for (let k = 0; k < cnt; k += 16) {
647
+ const e = Math.min(cnt, k + 16);
648
+ if (fq[k] !== f.lastF) {
649
+ const { z1, z2 } = f;
650
+ biquadCoefs(f.type, fq[k], f.q, sr, f);
651
+ f.z1 = z1;
652
+ f.z2 = z2;
653
+ f.lastF = fq[k];
654
+ }
655
+ biquadRun(f, acc, k, e);
656
+ }
657
+ } else {
658
+ if (f.lastF !== f.freq) {
659
+ biquadCoefs(f.type, f.freq, f.q, sr, f);
660
+ f.lastF = f.freq;
661
+ }
662
+ biquadRun(f, acc, 0, cnt);
663
+ }
664
+ }
665
+
666
+ const lane = v.lane;
667
+ lane.dirty = true;
668
+ const off = a - i0;
669
+ if (lane.stereo) {
670
+ const pan = this.pan;
671
+ if (v.panEnv) v.panEnv.fill(pan, cnt);
672
+ const L = lane.L;
673
+ const R = lane.R;
674
+ for (let k = 0; k < cnt; k++) {
675
+ const p = v.panEnv ? pan[k] : v.pan;
676
+ const x = ((Math.max(-1, Math.min(1, p)) + 1) / 2) * (Math.PI / 2);
677
+ L[off + k] += acc[k] * Math.cos(x) * Math.SQRT2;
678
+ R[off + k] += acc[k] * Math.sin(x) * Math.SQRT2;
679
+ }
680
+ } else {
681
+ const m = lane.mono;
682
+ for (let k = 0; k < cnt; k++) m[off + k] += acc[k];
683
+ }
684
+ }
685
+
686
+ // Renders samples [i0, i0 + n) (n <= BLOCK) and ADDS them into outL/outR.
687
+ renderBlock(i0, n, outL, outR) {
688
+ if (!this.sorted) {
689
+ this.sorted = this.pending.slice().sort((x, y) => x.start - y.start || x.order - y.order);
690
+ this.next = 0;
691
+ }
692
+ const end = i0 + n;
693
+ const sorted = this.sorted;
694
+ while (this.next < sorted.length && sorted[this.next].start < end) {
695
+ const v = sorted[this.next++];
696
+ this._begin(v);
697
+ this.active.push(v);
698
+ }
699
+ for (const lane of this.lanes) lane.clear(n);
700
+ for (const v of this.active) this._renderVoice(v, i0, n);
701
+ if (this.active.length) this.active = this.active.filter((v) => v.end > end);
702
+
703
+ const send = this.send;
704
+ send.fill(0, 0, n);
705
+ const dryL = this.tmp;
706
+ const dryR = this.acc;
707
+ dryL.fill(0, 0, n);
708
+ dryR.fill(0, 0, n);
709
+ for (const lane of this.lanes) lane.mix(i0, n, dryL, dryR, send);
710
+ if (this.reverb) this.reverb.process(send, n, dryL, dryR, this.reverbLevel);
711
+ if (this.hp) {
712
+ biquadRun(this.hp[0], dryL, 0, n);
713
+ biquadRun(this.hp[1], dryR, 0, n);
714
+ }
715
+ const g = this.gain;
716
+ for (let k = 0; k < n; k++) {
717
+ outL[k] += dryL[k] * g;
718
+ outR[k] += dryR[k] * g;
719
+ }
720
+ }
721
+ }