Repository navigation
Expand file tree
/
Copy pathaudio.js
More file actions
387 lines (368 loc) · 16.7 KB
/
Copy pathaudio.js
File metadata and controls
387 lines (368 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* audio — full bundle with all built-in ops, stats, and methods.
*
* import audio from 'audio'
* let a = await audio('file.mp3')
* a.gain(-3).trim().normalize()
* await a.save('out.wav')
*/
export { default } from './core.js'
export { parseTime, bitDepth } from './core.js'
export { render, wholeOf } from './plan.js'
export { keysOf, recall, keep, onto, prefix, rootOf, idOf } from './checkpoint.js'
import audio, { LOAD, loadOps, positional, blame, keeping } from './core.js'
import { opMethods, widening, builtin } from './plan.js'
// The package's ops, registered by the modules imported below (an import runs before this line): their edits' values
// are judged at the call (plan.js judge); an op registered later, the user's or a registry processor, is none of them
for (let d of Object.values(audio.op())) builtin.add(d)
// ── Plugin registry: name → package, loaded by dynamic import on first use (autowired:
// a.compressor() works without audio.use; the atom loads at the first render).
// Contract plugins from the @audio scope; grows with the published set. A family's processors by its umbrella's entry
// (@audio/<family>/audio: its members' manifests together), the one a name asks for registered from it (core.js use).
const OPS = {
compressor: '@audio/dynamics/audio',
limiter: '@audio/dynamics/audio',
gate: '@audio/dynamics/audio',
deesser: '@audio/dynamics/audio',
softclip: '@audio/dynamics/audio',
expander: '@audio/dynamics/audio',
compand: '@audio/dynamics/audio',
leveler: '@audio/dynamics/audio',
'transient-shaper': '@audio/dynamics/audio',
unlimit: '@audio/dynamics/audio',
auto: '@audio/chain/audio',
ducker: '@audio/dynamics/audio',
dehum: '@audio/denoise/audio',
specsub: '@audio/denoise/audio',
wiener: '@audio/denoise/audio',
omlsa: '@audio/denoise/audio',
deplosive: '@audio/denoise/audio',
dewind: '@audio/denoise/audio',
dewow: '@audio/denoise/audio',
debleed: '@audio/denoise/audio',
declip: '@audio/denoise/audio',
decrackle: '@audio/denoise/audio',
debreath: '@audio/denoise/audio',
desqueak: '@audio/denoise/audio',
// neural lane, optional package: RNNoise with its weights inside (deepfilter, its DeepFilterNet3, is fn/deepfilter.js)
rnnoise: '@audio/neural-denoise/audio',
// denoise-gate/-deesser merged into the dynamics atoms above (2026-07 near-dupe merge:
// gate gained hysteresis + look-ahead; deesser gained mode 'band' dynamic peaking EQ)
freeverb: '@audio/reverb/audio',
delay: '@audio/effect/audio',
chorus: '@audio/effect/audio',
flanger: '@audio/effect/audio',
phaser: '@audio/effect/audio',
tremolo: '@audio/effect/audio',
vibrato: '@audio/effect/audio',
autowah: '@audio/effect/audio',
wah: '@audio/effect/audio',
bitcrusher: '@audio/effect/audio',
distortion: '@audio/effect/audio',
exciter: '@audio/effect/audio',
ringmod: '@audio/effect/audio',
freqshift: '@audio/effect/audio',
multitap: '@audio/effect/audio',
pingpong: '@audio/effect/audio',
slew: '@audio/effect/audio',
noiseshaper: '@audio/effect/audio',
lofi: '@audio/effect/audio',
graindelay: '@audio/effect/audio',
stutter: '@audio/effect/audio',
subbass: '@audio/effect/audio',
sbr: '@audio/effect/audio',
rotary: '@audio/effect/audio',
tapestop: '@audio/effect/audio',
tube: '@audio/saturate/audio',
osc: '@audio/synth/audio',
fm: '@audio/synth/audio',
modal: '@audio/synth/audio',
isolate: '@audio/vocals/audio',
widener: '@audio/spatial/audio',
haas: '@audio/spatial/audio',
panner: '@audio/spatial/audio',
autopan: '@audio/spatial/audio',
midside: '@audio/spatial/audio',
microshift: '@audio/spatial/audio',
binaural: '@audio/spatial/audio',
surround: '@audio/spatial/audio',
'ambisonic-panner': '@audio/spatial/audio',
'ambisonic-decoder': '@audio/spatial/audio',
'ambisonic-rotate': '@audio/spatial/audio',
vocoder: '@audio/shift/audio',
'formant-shift': '@audio/shift/audio',
paulstretch: '@audio/shift/audio',
'pitch-shift': '@audio/shift/audio',
// each of pitch-shift's methods under a name of its own, so pitch({ method }) registers that one (fn/pitch.js)
'pitch-ola': '@audio/shift/audio',
'pitch-transient': '@audio/shift/audio',
'pitch-psola': '@audio/shift/audio',
'pitch-wsola': '@audio/shift/audio',
'pitch-granular': '@audio/shift/audio',
'pitch-sms': '@audio/shift/audio',
'pitch-hpss': '@audio/shift/audio',
'pitch-sample': '@audio/shift/audio',
'pitch-hybrid': '@audio/shift/audio',
'pitch-delay': '@audio/shift/audio',
'pitch-lpc': '@audio/shift/audio',
tune: '@audio/tune/audio',
// @audio/tune-midi exists too — direct-import only (guide-note list isn't a scalar param)
schroeder: '@audio/reverb/audio',
plate: '@audio/reverb/audio',
fdn: '@audio/reverb/audio',
spring: '@audio/reverb/audio',
shimmer: '@audio/reverb/audio',
// @audio/reverb-convolution — a.convolve(ir) (fn/convolve.js: the IR is a file, an instance or samples)
fet: '@audio/dynamics/audio',
opto: '@audio/dynamics/audio',
varimu: '@audio/dynamics/audio',
vca: '@audio/dynamics/audio',
multiband: '@audio/dynamics/audio',
moog: '@audio/filter/audio',
korg35: '@audio/filter/audio',
diode: '@audio/filter/audio',
oberheim: '@audio/filter/audio',
resonator: '@audio/filter/audio',
'spectral-tilt': '@audio/filter/audio',
variable: '@audio/filter/audio',
comb: '@audio/filter/audio',
dcblocker: '@audio/filter/audio',
emphasis: '@audio/filter/audio',
deemphasis: '@audio/filter/audio',
derivative: '@audio/filter/audio',
integral: '@audio/filter/audio',
'a-weighting': '@audio/weighting/audio',
'b-weighting': '@audio/weighting/audio',
'c-weighting': '@audio/weighting/audio',
'd-weighting': '@audio/weighting/audio',
'k-weighting': '@audio/weighting/audio',
itu468: '@audio/weighting/audio',
riaa: '@audio/weighting/audio',
geq: '@audio/eq/audio',
tilt: '@audio/eq/audio',
baxandall: '@audio/eq/audio',
dyneq: '@audio/eq/audio',
// @audio/eq-fir / eq-crossover — direct-import only (response curves / SOS designers, not processors)
tape: '@audio/saturate/audio',
transistor: '@audio/saturate/audio',
waveshaper: '@audio/saturate/audio',
multisat: '@audio/saturate/audio',
amp: '@audio/amp/audio',
cabinet: '@audio/amp/audio',
defeedback: '@audio/defeedback/audio',
noise: '@audio/synth/audio',
chirp: '@audio/synth/audio',
pluck: '@audio/synth/audio',
risset: '@audio/synth/audio',
rhythm: '@audio/synth/audio',
sfx: '@audio/synth/audio',
kick: '@audio/synth/audio',
cymbal: '@audio/synth/audio',
snare: '@audio/synth/audio',
adsr: '@audio/synth/audio',
voice: '@audio/synth/audio',
poly: '@audio/synth/audio',
tonewheel: '@audio/synth/audio',
// ── Time-stretch (whole-render + frames hook — structural output length) ──
'stretch-pvoc-lock': '@audio/stretch/audio',
'stretch-pvoc': '@audio/stretch/audio',
'stretch-pvsola': '@audio/stretch/audio',
'stretch-pghi': '@audio/stretch/audio',
'stretch-wsola': '@audio/stretch/audio',
'stretch-psola': '@audio/stretch/audio',
'stretch-sms': '@audio/stretch/audio',
'stretch-transient': '@audio/stretch/audio',
'stretch-hybrid': '@audio/stretch/audio',
'stretch-paul': '@audio/stretch/audio',
// ↑ note-event instruments: pass notes — a.voice({ notes: [{ time, midi|freq, duration, velocity }] })
// @audio/synth-dtmf (digit string) / synth-wavetable (table arrays) — direct-import only
// Codec plugins ({ codec, test?, decode?, encode? }) register the same way — none published yet
}
// ── Stat plugins ({ stat, compute }, registered as a.stat(name)) ──────────
const STATS = {
// auto()'s plan alone: analyze and plan, nothing rendered (the manifest that hosts auto)
chain: '@audio/chain/audio',
truepeak: '@audio/loudness/audio',
lra: '@audio/loudness/audio',
replaygain: '@audio/loudness/audio',
dr: '@audio/loudness/audio',
'speech-contrast': '@audio/loudness/audio',
sounds: '@audio/loudness/audio',
rolloff: '@audio/spectral/audio',
spread: '@audio/spectral/audio',
slope: '@audio/spectral/audio',
flux: '@audio/spectral/audio',
contrast: '@audio/spectral/audio',
ltas: '@audio/spectral/audio',
zcr: '@audio/spectral/audio',
structure: '@audio/mir/audio',
tempogram: '@audio/mir/audio',
melody: '@audio/mir/audio',
downbeat: '@audio/mir/audio',
fingerprint: '@audio/mir/audio',
drums: '@audio/mir/audio',
multif0: '@audio/mir/audio',
transcribe: '@audio/mir/audio',
similarity: '@audio/mir/audio',
coversong: '@audio/mir/audio',
chroma: '@audio/mir/audio',
tonnetz: '@audio/mir/audio',
// speech quality with no reference: DNSMOS P.835 { sig, bak, ovrl, segments }, each 1 to 5, over the whole range. The
// optional @audio/neural-quality: its model (1.1 MB) is fetched on first use, onnxruntime installed beside it
dnsmos: '@audio/neural-quality/audio',
}
audio.plugins = { ...OPS, ...STATS }
audio.atoms = audio.plugins // deprecated ≤2.5 name — same object, mutations visible through both
// Registry ops are methods from the start (a dashed name under its camelCase too); positional args wait as `args`
// until the op loads
for (let name in OPS) opMethods(name)
// The registry ops that can change a sound's width, as audio hosts their processors (core.js hostOp: a count on the
// output, or layouts other than the input's), each with the width it makes: a stereo processor, of the width it is
// given (two channels of a mono sound, any other width kept); one of a count of its own, that count; one with layouts
// of its own, a width no one knows before it loads (a `layouts` option picks): 0. So a width asked before one of them
// has loaded is the width it will make, and an error naming the op only where that is not known (plan.js
// deriveFormat). test/plugin-ops.js holds the list to the manifests installed
for (let name of ['debleed', 'pingpong', 'isolate', 'widener', 'haas', 'panner', 'autopan', 'midside', 'microshift',
'binaural']) widening.set(name, n => n > 1 ? n : 2)
for (let [name, k] of [['rotary', 2], ['osc', 1], ['surround', 6]]) widening.set(name, k)
for (let name of ['ambisonic-panner', 'ambisonic-decoder']) widening.set(name, 0)
// The registry ops whose package's keywords say they keep a take's length: a repair (restoration), a pitch shift
// (pitch-shift); and a time stretch (time-stretch), whose length is the factor's, round(n · factor), each by its name. The ring they declare
// past that end renders only where an edit asks for it (`tail: true`: core.js keeping). test/plugin-ops.js holds the
// list to the packages installed
for (let name of ['dehum', 'specsub', 'wiener', 'omlsa', 'deplosive', 'dewind', 'dewow', 'debleed', 'declip', 'decrackle',
'debreath', 'desqueak', 'rnnoise', 'deesser', 'vocoder', 'formant-shift', 'paulstretch', 'pitch-shift', 'pitch-ola', 'pitch-transient',
'pitch-psola', 'pitch-wsola', 'pitch-granular', 'pitch-sms', 'pitch-hpss', 'pitch-sample', 'pitch-hybrid', 'pitch-delay',
'pitch-lpc', 'stretch-pvoc-lock', 'stretch-pvoc', 'stretch-pvsola', 'stretch-pghi', 'stretch-wsola', 'stretch-psola',
'stretch-sms', 'stretch-transient', 'stretch-hybrid', 'stretch-paul']) keeping.add(name)
/** Load every registry op an instance's edits name (dynamic import), then map the
* positional args recorded before it loaded onto its params, and settle its options as a call to it loaded would
* (plan.js normalizeEdit); so for the sources its edits take (mix, insert, a key),
* which its plan reads synchronously: an insert's length, a normalize measuring a mix. */
async function autowire(a, seen = new Set()) {
if (seen.has(a)) return
seen.add(a)
// a failure is said of its edit (core.js blame); one of a sound an edit takes, of that edit
for (let [j, e] of a.edits.entries()) try {
let [type, o] = e, d = audio.op(type)
if (!d && audio.plugins[type]) { await audio.use(type); d = audio.op(type) }
if (o?.args && d?.params) {
let { args, ...rest } = o, p = positional(d, args)
if (args.length > p.length) throw new TypeError(`${type}: expected at most ${p.length} arguments (${p.join(', ')}), got ${args.length}`)
p.forEach((k, i) => { if (i < args.length) rest[k] = args[i] })
e[1] = rest
}
if (e[1]) d?.settle?.(e[1])
} catch (err) { throw blame(err, a, j) }
for (let [j, [, o]] of a.edits.entries()) for (let k in o) if (o[k]?.edits?.length) try { await autowire(o[k], seen); await loadOps(o[k]) } catch (err) { throw blame(err, a, j) }
}
let load = audio.fn[LOAD], stream = audio.fn.stream
audio.fn[LOAD] = async function() { if (this.edits?.length) await autowire(this); return load.call(this) }
// a live (still decoding) stream skips LOAD: wire before its first plan too; and so does a file encoded or saved as it
// decodes, which plans from its length before it streams
audio.fn.stream = audio.fn[Symbol.asyncIterator] = async function*(opts) {
if (this.edits?.length) { await autowire(this); await loadOps(this) }
yield* stream.call(this, opts)
}
for (let name of ['encode', 'save']) {
let f = audio.fn[name]
audio.fn[name] = async function(...args) {
if (this.edits?.length) { await autowire(this); await loadOps(this) }
return f.apply(this, args)
}
}
// ── Infrastructure (self-register on import) ────────────────────────────
import './store.js'
import './cache.js'
import './stats.js'
import './plan.js'
// ── Methods ─────────────────────────────────────────────────────────────
import './fn/clip.js'
import './fn/split.js'
import './fn/play.js'
import './fn/meter.js'
import './fn/meta.js'
import './fn/save.js'
import './fn/check.js'
import './fn/cuts.js'
// compare (what changed from a sound before) reads two sounds whole: its module loads on first use, as the registry's
// plugins do, out of a page's bundle (.esbuild.js leaves it beside dist/); it reads the sounds through their methods
audio.fn.compare = async function(ref) { return (await import('./fn/compare.js')).default.call(this, ref) }
// deslop (a generated track remastered by what it carries) and its plan, stat('slop'): one module, loaded on the first
// use of either, as compare's is; the op renders ahead (its prepare), so the module is there before a block is played
let slop = null
const deslop = async () => slop ??= await import('./fn/deslop.js')
audio.op('deslop', {
params: ['intensity'],
prepare: async (a, index) => (await deslop()).prepare(a, index),
process: (input, output, ctx) => { if (!slop) throw new Error('deslop: runs ahead of rendering, through read(), stream() or save()'); slop.process(input, output, ctx) },
})
builtin.add(audio.op('deslop')) // one of the package's own, registered here past the line that gathers them
audio.stat('slop', {})
audio.fn.slop = async function(opts) { return (await deslop()).plan.call(this, opts) }
// ── Ops ─────────────────────────────────────────────────────────────────
import './fn/crop.js'
import './fn/remove.js'
import './fn/insert.js'
import './fn/copy.js'
import './fn/cut.js'
import './fn/paste.js'
import './fn/move.js'
import './fn/repeat.js'
import './fn/gain.js'
import './fn/fade.js'
import './fn/reverse.js'
import './fn/mix.js'
import './fn/write.js'
import './fn/remix.js'
import './fn/trim.js'
import './fn/shrink.js'
import './fn/normalize.js'
import './fn/filter.js'
import './fn/pan.js'
import './fn/pad.js'
import './fn/plugin.js'
import './fn/convolve.js'
import './fn/speed.js'
import './fn/stretch.js'
import './fn/warp.js'
import './fn/pitch.js'
import './fn/intonation.js'
import './fn/formant.js'
import './fn/transform.js'
import './fn/crossfade.js'
import './fn/crossover.js'
import './fn/match.js'
import './fn/align.js'
import './fn/spectral.js'
import './fn/vocals.js'
import './fn/rebalance.js'
import './fn/deepfilter.js'
import './fn/dereverb.js'
import './fn/vocos.js'
import './fn/restore.js'
import './fn/auto.js'
import './fn/declip.js'
import './fn/recover.js'
import './fn/derustle.js'
import './fn/denoise.js'
import './fn/declick.js'
import './fn/dither.js'
import './fn/crossfeed.js'
import './fn/roomtone.js'
import './fn/phase.js'
import './fn/azimuth.js'
import './fn/codec.js'
import './fn/deconstruct.js'
import './fn/resample.js'
// ── Stats ───────────────────────────────────────────────────────────────
import './fn/stat.js'
import './fn/loudness.js'
import './fn/spectrum.js'
import './fn/cepstrum.js'
import './fn/silence.js'
import './fn/hits.js'
import './fn/similar.js'
import './fn/beat.js'
import './fn/pitch-detect.js'
import './fn/periodicity.js'