@arraypress/effects-astro 0.0.0-stage → 0.1.1
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/AGENTS.md +85 -0
- package/COMPONENTS.md +1264 -0
- package/LICENSE +139 -0
- package/README.md +197 -2
- package/package.json +60 -4
- package/src/components/AsciiRain.astro +36 -0
- package/src/components/BorderSweep.astro +39 -0
- package/src/components/Caustics.astro +39 -0
- package/src/components/ColorPanels.astro +62 -0
- package/src/components/Cursor.astro +97 -0
- package/src/components/DotGrid.astro +34 -0
- package/src/components/DotSurface.astro +35 -0
- package/src/components/EffectCanvas.astro +152 -0
- package/src/components/FitText.astro +35 -0
- package/src/components/FlutedGlass.astro +36 -0
- package/src/components/Glass.astro +69 -0
- package/src/components/GlassGlyph.astro +38 -0
- package/src/components/GlowField.astro +177 -0
- package/src/components/Grain.astro +98 -0
- package/src/components/HaloRing.astro +38 -0
- package/src/components/HighlightSweep.astro +74 -0
- package/src/components/ImageRipple.astro +56 -0
- package/src/components/Ink.astro +37 -0
- package/src/components/InkField.astro +45 -0
- package/src/components/IsoCity.astro +54 -0
- package/src/components/LightField.astro +40 -0
- package/src/components/LiquidMetal.astro +41 -0
- package/src/components/Magnetic.astro +52 -0
- package/src/components/Marquee.astro +216 -0
- package/src/components/MosaicReveal.astro +196 -0
- package/src/components/NebulaLogo.astro +38 -0
- package/src/components/NeonSwarm.astro +38 -0
- package/src/components/Ocean.astro +40 -0
- package/src/components/OutlineWordmark.astro +222 -0
- package/src/components/PaperBackground.astro +40 -0
- package/src/components/PaperShader.astro +55 -0
- package/src/components/Parallax.astro +49 -0
- package/src/components/ParticleLogo.astro +37 -0
- package/src/components/PixelGlitchText.astro +70 -0
- package/src/components/PointCloud.astro +37 -0
- package/src/components/ProductStack.astro +298 -0
- package/src/components/PulseMark.astro +189 -0
- package/src/components/RadiantGlass.astro +38 -0
- package/src/components/RainOnGlass.astro +71 -0
- package/src/components/Reveal.astro +115 -0
- package/src/components/ScrambleText.astro +52 -0
- package/src/components/ScrollReveal.astro +38 -0
- package/src/components/ShaderBackground.astro +55 -0
- package/src/components/SignalLines.astro +41 -0
- package/src/components/SilkRibbon.astro +37 -0
- package/src/components/SpotlightText.astro +131 -0
- package/src/components/Tilt.astro +79 -0
- package/src/components/Topography.astro +38 -0
- package/src/components/Typewriter.astro +66 -0
- package/src/components/VariableType.astro +59 -0
- package/src/components/VoxelLogo.astro +46 -0
- package/src/components/WaveBars.astro +41 -0
- package/src/components/WorldRoutes.astro +41 -0
- package/src/components/style.js +19 -0
- package/src/index.d.ts +143 -0
- package/src/index.js +105 -0
- package/src/runtime/ascii-rain.d.ts +53 -0
- package/src/runtime/ascii-rain.js +355 -0
- package/src/runtime/audio-meter.d.ts +65 -0
- package/src/runtime/audio-meter.js +282 -0
- package/src/runtime/audio.d.ts +26 -0
- package/src/runtime/audio.js +453 -0
- package/src/runtime/blur.d.ts +2 -0
- package/src/runtime/blur.js +56 -0
- package/src/runtime/canvas2d.d.ts +23 -0
- package/src/runtime/canvas2d.js +105 -0
- package/src/runtime/caustics.d.ts +40 -0
- package/src/runtime/caustics.js +286 -0
- package/src/runtime/color.d.ts +33 -0
- package/src/runtime/color.js +213 -0
- package/src/runtime/curl.d.ts +5 -0
- package/src/runtime/curl.js +81 -0
- package/src/runtime/cursor.d.ts +9 -0
- package/src/runtime/cursor.js +80 -0
- package/src/runtime/device.d.ts +14 -0
- package/src/runtime/device.js +73 -0
- package/src/runtime/dot-grid.d.ts +27 -0
- package/src/runtime/dot-grid.js +245 -0
- package/src/runtime/dot-surface.d.ts +46 -0
- package/src/runtime/dot-surface.js +429 -0
- package/src/runtime/feedback.d.ts +18 -0
- package/src/runtime/feedback.js +131 -0
- package/src/runtime/fit-text.d.ts +5 -0
- package/src/runtime/fit-text.js +60 -0
- package/src/runtime/fluid.d.ts +55 -0
- package/src/runtime/fluid.js +504 -0
- package/src/runtime/fluted-glass.d.ts +27 -0
- package/src/runtime/fluted-glass.js +157 -0
- package/src/runtime/gl.d.ts +19 -0
- package/src/runtime/gl.js +104 -0
- package/src/runtime/glass-glyph.d.ts +49 -0
- package/src/runtime/glass-glyph.js +355 -0
- package/src/runtime/glass-sheen.d.ts +2 -0
- package/src/runtime/glass-sheen.js +30 -0
- package/src/runtime/glsl.d.ts +17 -0
- package/src/runtime/glsl.js +78 -0
- package/src/runtime/glyphs.d.ts +22 -0
- package/src/runtime/glyphs.js +107 -0
- package/src/runtime/halo-ring.d.ts +47 -0
- package/src/runtime/halo-ring.js +334 -0
- package/src/runtime/height-field.d.ts +50 -0
- package/src/runtime/height-field.js +176 -0
- package/src/runtime/image-ripple.d.ts +25 -0
- package/src/runtime/image-ripple.js +353 -0
- package/src/runtime/image.d.ts +11 -0
- package/src/runtime/image.js +81 -0
- package/src/runtime/in-view.d.ts +6 -0
- package/src/runtime/in-view.js +36 -0
- package/src/runtime/ink-field/shaders.js +258 -0
- package/src/runtime/ink-field/sim.js +582 -0
- package/src/runtime/ink-field.d.ts +39 -0
- package/src/runtime/ink-field.js +311 -0
- package/src/runtime/ink.d.ts +31 -0
- package/src/runtime/ink.js +109 -0
- package/src/runtime/instancing.d.ts +12 -0
- package/src/runtime/instancing.js +37 -0
- package/src/runtime/iso-camera.d.ts +35 -0
- package/src/runtime/iso-camera.js +85 -0
- package/src/runtime/iso-city/figures.d.ts +30 -0
- package/src/runtime/iso-city/figures.js +136 -0
- package/src/runtime/iso-city/look.d.ts +34 -0
- package/src/runtime/iso-city/look.js +105 -0
- package/src/runtime/iso-city/plan.d.ts +59 -0
- package/src/runtime/iso-city/plan.js +463 -0
- package/src/runtime/iso-city/roads.d.ts +64 -0
- package/src/runtime/iso-city/roads.js +320 -0
- package/src/runtime/iso-city/scene.d.ts +27 -0
- package/src/runtime/iso-city/scene.js +432 -0
- package/src/runtime/iso-city/shaders.d.ts +6 -0
- package/src/runtime/iso-city/shaders.js +233 -0
- package/src/runtime/iso-city/shadows.d.ts +13 -0
- package/src/runtime/iso-city/shadows.js +148 -0
- package/src/runtime/iso-city/traffic.d.ts +80 -0
- package/src/runtime/iso-city/traffic.js +457 -0
- package/src/runtime/iso-city.d.ts +37 -0
- package/src/runtime/iso-city.js +458 -0
- package/src/runtime/lens-map.d.ts +11 -0
- package/src/runtime/lens-map.js +146 -0
- package/src/runtime/light-field.d.ts +29 -0
- package/src/runtime/light-field.js +292 -0
- package/src/runtime/liquid-glass.d.ts +48 -0
- package/src/runtime/liquid-glass.js +423 -0
- package/src/runtime/liquid-metal/beads.js +217 -0
- package/src/runtime/liquid-metal/field.js +88 -0
- package/src/runtime/liquid-metal/look.js +64 -0
- package/src/runtime/liquid-metal/shaders.js +255 -0
- package/src/runtime/liquid-metal/wave.js +53 -0
- package/src/runtime/liquid-metal.d.ts +46 -0
- package/src/runtime/liquid-metal.js +366 -0
- package/src/runtime/loop.d.ts +7 -0
- package/src/runtime/loop.js +112 -0
- package/src/runtime/magnetic.d.ts +9 -0
- package/src/runtime/magnetic.js +46 -0
- package/src/runtime/marquee.d.ts +2 -0
- package/src/runtime/marquee.js +27 -0
- package/src/runtime/mesh.d.ts +30 -0
- package/src/runtime/mesh.js +217 -0
- package/src/runtime/mosaic-reveal.d.ts +34 -0
- package/src/runtime/mosaic-reveal.js +504 -0
- package/src/runtime/motion.d.ts +2 -0
- package/src/runtime/motion.js +21 -0
- package/src/runtime/mount.d.ts +13 -0
- package/src/runtime/mount.js +120 -0
- package/src/runtime/nebula-logo.d.ts +31 -0
- package/src/runtime/nebula-logo.js +282 -0
- package/src/runtime/neon-swarm.d.ts +31 -0
- package/src/runtime/neon-swarm.js +258 -0
- package/src/runtime/ocean/presets.d.ts +54 -0
- package/src/runtime/ocean/presets.js +184 -0
- package/src/runtime/ocean/shader.d.ts +14 -0
- package/src/runtime/ocean/shader.js +389 -0
- package/src/runtime/ocean/view.d.ts +24 -0
- package/src/runtime/ocean/view.js +98 -0
- package/src/runtime/ocean/waves.d.ts +29 -0
- package/src/runtime/ocean/waves.js +119 -0
- package/src/runtime/ocean.d.ts +31 -0
- package/src/runtime/ocean.js +173 -0
- package/src/runtime/palettes.d.ts +37 -0
- package/src/runtime/palettes.js +75 -0
- package/src/runtime/paper-look.d.ts +42 -0
- package/src/runtime/paper-look.js +91 -0
- package/src/runtime/paper-looks.d.ts +17 -0
- package/src/runtime/paper-looks.js +21 -0
- package/src/runtime/paper.d.ts +37 -0
- package/src/runtime/paper.js +303 -0
- package/src/runtime/particle-logo.d.ts +31 -0
- package/src/runtime/particle-logo.js +196 -0
- package/src/runtime/ping-pong.d.ts +43 -0
- package/src/runtime/ping-pong.js +175 -0
- package/src/runtime/pixel-glitch.d.ts +17 -0
- package/src/runtime/pixel-glitch.js +287 -0
- package/src/runtime/point-cloud.d.ts +33 -0
- package/src/runtime/point-cloud.js +287 -0
- package/src/runtime/pointer.d.ts +27 -0
- package/src/runtime/pointer.js +128 -0
- package/src/runtime/points.d.ts +25 -0
- package/src/runtime/points.js +130 -0
- package/src/runtime/pools.d.ts +2 -0
- package/src/runtime/pools.js +25 -0
- package/src/runtime/product-stack.d.ts +24 -0
- package/src/runtime/product-stack.js +210 -0
- package/src/runtime/radiant-glass.d.ts +29 -0
- package/src/runtime/radiant-glass.js +282 -0
- package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
- package/src/runtime/rain-on-glass/backdrop.js +185 -0
- package/src/runtime/rain-on-glass/shader.d.ts +2 -0
- package/src/runtime/rain-on-glass/shader.js +168 -0
- package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
- package/src/runtime/rain-on-glass/wipe.js +65 -0
- package/src/runtime/rain-on-glass.d.ts +27 -0
- package/src/runtime/rain-on-glass.js +210 -0
- package/src/runtime/ramp.d.ts +2 -0
- package/src/runtime/ramp.js +30 -0
- package/src/runtime/random.d.ts +2 -0
- package/src/runtime/random.js +22 -0
- package/src/runtime/scroll-reveal.d.ts +22 -0
- package/src/runtime/scroll-reveal.js +73 -0
- package/src/runtime/shader-background.d.ts +42 -0
- package/src/runtime/shader-background.js +261 -0
- package/src/runtime/shader-backgrounds/band-lens.js +72 -0
- package/src/runtime/shader-backgrounds/common.js +53 -0
- package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
- package/src/runtime/shader-backgrounds/grid.js +33 -0
- package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
- package/src/runtime/shader-backgrounds/silk.js +57 -0
- package/src/runtime/shader-backgrounds/waves.js +31 -0
- package/src/runtime/shape.d.ts +42 -0
- package/src/runtime/shape.js +313 -0
- package/src/runtime/silk-ribbon.d.ts +23 -0
- package/src/runtime/silk-ribbon.js +206 -0
- package/src/runtime/slideshow.d.ts +27 -0
- package/src/runtime/slideshow.js +234 -0
- package/src/runtime/spotlight.d.ts +9 -0
- package/src/runtime/spotlight.js +97 -0
- package/src/runtime/stack-poses.d.ts +53 -0
- package/src/runtime/stack-poses.js +273 -0
- package/src/runtime/stage.d.ts +47 -0
- package/src/runtime/stage.js +187 -0
- package/src/runtime/swarm.d.ts +49 -0
- package/src/runtime/swarm.js +199 -0
- package/src/runtime/text-motion.d.ts +41 -0
- package/src/runtime/text-motion.js +165 -0
- package/src/runtime/text.d.ts +11 -0
- package/src/runtime/text.js +90 -0
- package/src/runtime/tilt.d.ts +19 -0
- package/src/runtime/tilt.js +116 -0
- package/src/runtime/topography.d.ts +71 -0
- package/src/runtime/topography.js +650 -0
- package/src/runtime/trackers.d.ts +21 -0
- package/src/runtime/trackers.js +76 -0
- package/src/runtime/variable-type.d.ts +43 -0
- package/src/runtime/variable-type.js +358 -0
- package/src/runtime/voxel-logo.d.ts +31 -0
- package/src/runtime/voxel-logo.js +237 -0
- package/src/runtime/waves.d.ts +70 -0
- package/src/runtime/waves.js +799 -0
- package/src/runtime/world-labels.d.ts +29 -0
- package/src/runtime/world-labels.js +59 -0
- package/src/runtime/world-land.d.ts +8 -0
- package/src/runtime/world-land.js +105 -0
- package/src/runtime/world-map.d.ts +69 -0
- package/src/runtime/world-map.js +308 -0
- package/src/runtime/world-plan.d.ts +101 -0
- package/src/runtime/world-plan.js +333 -0
- package/src/runtime/world-routes.d.ts +75 -0
- package/src/runtime/world-routes.js +673 -0
- package/src/styles/border-sweep.css +82 -0
- package/src/styles/glass.css +104 -0
- package/src/styles/motion.css +218 -0
- package/src/styles/reveal.css +51 -0
- package/src/vendor/paper/LICENSE +201 -0
- package/src/vendor/paper/NOTICE +5 -0
- package/src/vendor/paper/README.md +8 -0
- package/src/vendor/paper/get-shader-color-from-string.js +100 -0
- package/src/vendor/paper/get-shader-noise-texture.js +17 -0
- package/src/vendor/paper/presets.js +3678 -0
- package/src/vendor/paper/shader-sizing.js +37 -0
- package/src/vendor/paper/shader-utils.js +130 -0
- package/src/vendor/paper/shaders/color-panels.js +223 -0
- package/src/vendor/paper/shaders/dithering.js +270 -0
- package/src/vendor/paper/shaders/dot-grid.js +109 -0
- package/src/vendor/paper/shaders/dot-orbit.js +122 -0
- package/src/vendor/paper/shaders/fluted-glass.js +360 -0
- package/src/vendor/paper/shaders/gem-smoke.js +572 -0
- package/src/vendor/paper/shaders/god-rays.js +132 -0
- package/src/vendor/paper/shaders/grain-gradient.js +300 -0
- package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
- package/src/vendor/paper/shaders/halftone-dots.js +320 -0
- package/src/vendor/paper/shaders/heatmap.js +383 -0
- package/src/vendor/paper/shaders/image-dithering.js +198 -0
- package/src/vendor/paper/shaders/lens-distortion.js +275 -0
- package/src/vendor/paper/shaders/liquid-metal.js +632 -0
- package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/metaballs.js +110 -0
- package/src/vendor/paper/shaders/neuro-noise.js +76 -0
- package/src/vendor/paper/shaders/paper-texture.js +526 -0
- package/src/vendor/paper/shaders/perlin-noise.js +166 -0
- package/src/vendor/paper/shaders/pulsing-border.js +238 -0
- package/src/vendor/paper/shaders/simplex-noise.js +91 -0
- package/src/vendor/paper/shaders/smoke-ring.js +132 -0
- package/src/vendor/paper/shaders/spiral.js +82 -0
- package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
- package/src/vendor/paper/shaders/swirl.js +103 -0
- package/src/vendor/paper/shaders/voronoi.js +138 -0
- package/src/vendor/paper/shaders/warp.js +129 -0
- package/src/vendor/paper/shaders/water.js +121 -0
- package/src/vendor/paper/shaders/waves.js +65 -0
- package/src/vendor/paper/vertex-shader.js +159 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/audio-meter
|
|
3
|
+
*
|
|
4
|
+
* The arithmetic of audio reactivity, kept apart from the Web Audio graph
|
|
5
|
+
* (./audio.js) so it runs and is tested under Node: an analyser's byte
|
|
6
|
+
* spectrum and waveform turned into what effects draw with.
|
|
7
|
+
*
|
|
8
|
+
* - `bandLevel`: one band of the spectrum (bass, mid, treble), 0–1.
|
|
9
|
+
* - `loudness`: a waveform's RMS on a decibel scale, 0–1.
|
|
10
|
+
* - `logSpectrum`: the spectrum resampled to a few log-spaced bins, as an
|
|
11
|
+
* ear hears it, tilted so the treble reads beside the bass.
|
|
12
|
+
* - `scope`: the waveform as an oscilloscope shows it, started at a rising
|
|
13
|
+
* zero crossing so a steady note stands still, and smoothed.
|
|
14
|
+
* - `createMeter`: all of it each frame, eased (quick to rise, slower to
|
|
15
|
+
* fall, as meters move), with the kicks counted.
|
|
16
|
+
*
|
|
17
|
+
* The analyser's bytes are 0–255: its spectrum in decibels between its
|
|
18
|
+
* `minDecibels` and `maxDecibels`, its waveform centred on 128.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** The bands, Hz: bass, mid and treble, and the low end where kicks live. */
|
|
22
|
+
export const BANDS = {
|
|
23
|
+
bass: [20, 250],
|
|
24
|
+
mid: [250, 2000],
|
|
25
|
+
treble: [2000, 12000],
|
|
26
|
+
kick: [40, 160],
|
|
27
|
+
};
|
|
28
|
+
/** Bins in a meter's `spectrum`. */
|
|
29
|
+
export const SPECTRUM = 96;
|
|
30
|
+
/** Points in a meter's `waveform`. */
|
|
31
|
+
export const WAVEFORM = 64;
|
|
32
|
+
/** The quietest RMS that registers, dB (0 on the `loudness` scale), and the loudest (1). */
|
|
33
|
+
const QUIET_DB = -50;
|
|
34
|
+
const LOUD_DB = -8;
|
|
35
|
+
/** A kick: the low end this far above its own recent average (0–1 of the band), at least this long after the last; the next waits until it has fallen back under half that. */
|
|
36
|
+
const KICK_RISE = 0.07;
|
|
37
|
+
const KICK_GAP = 0.14;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The mean of a spectrum's bins between two frequencies, 0–1.
|
|
41
|
+
*
|
|
42
|
+
* @param {ArrayLike<number>} freq The spectrum, bytes 0–255, bin 0 at 0 Hz.
|
|
43
|
+
* @param {number} binHz Hz per bin (the sample rate over the FFT size).
|
|
44
|
+
* @param {number} lo The band's low edge, Hz.
|
|
45
|
+
* @param {number} hi Its high edge, Hz.
|
|
46
|
+
* @returns {number} Its level, 0 for silence or a band outside the spectrum.
|
|
47
|
+
*/
|
|
48
|
+
export function bandLevel(freq, binHz, lo, hi) {
|
|
49
|
+
const from = Math.max(0, Math.ceil(lo / binHz));
|
|
50
|
+
const to = Math.min(freq.length - 1, Math.floor(hi / binHz));
|
|
51
|
+
if (from >= freq.length) return 0;
|
|
52
|
+
// A band narrower than a bin reads the bin it falls in.
|
|
53
|
+
if (to < from) return freq[Math.min(freq.length - 1, Math.round(lo / binHz))] / 255;
|
|
54
|
+
let sum = 0;
|
|
55
|
+
for (let i = from; i <= to; i++) sum += freq[i];
|
|
56
|
+
return sum / ((to - from + 1) * 255);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* How loud a waveform is: its RMS in decibels, mapped from QUIET_DB (0) to
|
|
61
|
+
* LOUD_DB (1), so a quiet passage still moves and a loud one does not pin.
|
|
62
|
+
*
|
|
63
|
+
* @param {ArrayLike<number>} time The waveform, bytes centred on 128.
|
|
64
|
+
* @returns {number} Its loudness, 0–1.
|
|
65
|
+
*/
|
|
66
|
+
export function loudness(time) {
|
|
67
|
+
if (!time.length) return 0;
|
|
68
|
+
let sum = 0;
|
|
69
|
+
for (let i = 0; i < time.length; i++) {
|
|
70
|
+
const v = (time[i] - 128) / 128;
|
|
71
|
+
sum += v * v;
|
|
72
|
+
}
|
|
73
|
+
const rms = Math.sqrt(sum / time.length);
|
|
74
|
+
if (rms <= 0) return 0;
|
|
75
|
+
const db = 20 * Math.log10(rms);
|
|
76
|
+
return Math.min(1, Math.max(0, (db - QUIET_DB) / (LOUD_DB - QUIET_DB)));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Resamples a spectrum into log-spaced bins between two frequencies, each
|
|
81
|
+
* the loudest of the analyser bins it covers (interpolated where a bin is
|
|
82
|
+
* narrower than one), weighted up a little each octave so the treble,
|
|
83
|
+
* which carries less energy, reads beside the bass. Silence stays silent.
|
|
84
|
+
*
|
|
85
|
+
* @param {ArrayLike<number>} freq The spectrum, bytes 0–255, bin 0 at 0 Hz.
|
|
86
|
+
* @param {number} binHz Hz per bin.
|
|
87
|
+
* @param {Float32Array} out Filled with the bins, 0–1, lowest first.
|
|
88
|
+
* @param {number} [lo=40] The lowest frequency, Hz.
|
|
89
|
+
* @param {number} [hi=16000] The highest, Hz.
|
|
90
|
+
* @returns {Float32Array} `out`.
|
|
91
|
+
*/
|
|
92
|
+
export function logSpectrum(freq, binHz, out, lo = 40, hi = 16000) {
|
|
93
|
+
const n = out.length;
|
|
94
|
+
const ratio = hi / lo;
|
|
95
|
+
const last = freq.length - 1;
|
|
96
|
+
for (let i = 0; i < n; i++) {
|
|
97
|
+
const f0 = lo * ratio ** (i / n);
|
|
98
|
+
const f1 = lo * ratio ** ((i + 1) / n);
|
|
99
|
+
const a = f0 / binHz;
|
|
100
|
+
const b = f1 / binHz;
|
|
101
|
+
let v = 0;
|
|
102
|
+
if (b - a < 1) {
|
|
103
|
+
v = sampleAt(freq, (a + b) / 2 / last);
|
|
104
|
+
} else {
|
|
105
|
+
for (let j = Math.floor(a); j <= Math.min(last, Math.ceil(b)); j++) v = Math.max(v, freq[j]);
|
|
106
|
+
}
|
|
107
|
+
const tilt = Math.max(0.9, 1 + 0.07 * Math.log2(Math.sqrt(f0 * f1) / 250));
|
|
108
|
+
out[i] = Math.min(1, (v / 255) * tilt);
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The waveform as an oscilloscope draws it: from the first rising zero
|
|
115
|
+
* crossing in its first half (so a steady note holds still), averaged down
|
|
116
|
+
* to `out`'s length over the second half's span, -1–1.
|
|
117
|
+
*
|
|
118
|
+
* @param {ArrayLike<number>} time The waveform, bytes centred on 128.
|
|
119
|
+
* @param {Float32Array} out Filled with the trace.
|
|
120
|
+
* @returns {Float32Array} `out`.
|
|
121
|
+
*/
|
|
122
|
+
export function scope(time, out) {
|
|
123
|
+
const span = Math.floor(time.length / 2);
|
|
124
|
+
let start = 0;
|
|
125
|
+
for (let i = 1; i < span; i++) {
|
|
126
|
+
if (time[i - 1] < 128 && time[i] >= 128) {
|
|
127
|
+
start = i;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const per = span / out.length;
|
|
132
|
+
for (let i = 0; i < out.length; i++) {
|
|
133
|
+
const from = start + Math.floor(i * per);
|
|
134
|
+
const to = Math.max(from + 1, start + Math.floor((i + 1) * per));
|
|
135
|
+
let sum = 0;
|
|
136
|
+
for (let j = from; j < to; j++) sum += time[j];
|
|
137
|
+
out[i] = (sum / (to - from) - 128) / 128;
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A value read between an array's entries, as a texture reads: `u` 0 is the
|
|
144
|
+
* first, 1 the last, linearly blended between.
|
|
145
|
+
*
|
|
146
|
+
* @param {ArrayLike<number>} values The values, at least one.
|
|
147
|
+
* @param {number} u Where, 0–1 (clamped).
|
|
148
|
+
* @returns {number} The value there.
|
|
149
|
+
*/
|
|
150
|
+
export function sampleAt(values, u) {
|
|
151
|
+
const at = Math.min(1, Math.max(0, u)) * (values.length - 1);
|
|
152
|
+
const i = Math.floor(at);
|
|
153
|
+
const j = Math.min(values.length - 1, i + 1);
|
|
154
|
+
return values[i] + (values[j] - values[i]) * (at - i);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Resamples values onto a finer array along a smooth curve through them
|
|
159
|
+
* (Catmull-Rom), so a trace drawn from it has no corners.
|
|
160
|
+
*
|
|
161
|
+
* @param {ArrayLike<number>} values The values, at least two.
|
|
162
|
+
* @param {Float32Array} out Filled with the curve, its ends on the first and last value.
|
|
163
|
+
* @returns {Float32Array} `out`.
|
|
164
|
+
*/
|
|
165
|
+
export function smoothInto(values, out) {
|
|
166
|
+
const last = values.length - 1;
|
|
167
|
+
for (let i = 0; i < out.length; i++) {
|
|
168
|
+
const at = (i / Math.max(1, out.length - 1)) * last;
|
|
169
|
+
const j = Math.min(last - 1, Math.floor(at));
|
|
170
|
+
const t = at - j;
|
|
171
|
+
const p0 = values[Math.max(0, j - 1)];
|
|
172
|
+
const p1 = values[j];
|
|
173
|
+
const p2 = values[j + 1];
|
|
174
|
+
const p3 = values[Math.min(last, j + 2)];
|
|
175
|
+
out[i] = p1 + 0.5 * t * (p2 - p0 + t * (2 * p0 - 5 * p1 + 4 * p2 - p3 + t * (3 * (p1 - p2) + p3 - p0)));
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Eases a value towards a target over time: quick on the way up, slower on
|
|
182
|
+
* the way down, as a meter's needle moves.
|
|
183
|
+
*
|
|
184
|
+
* @param {number} value Now.
|
|
185
|
+
* @param {number} target Where it is headed.
|
|
186
|
+
* @param {number} dt Seconds since the last step.
|
|
187
|
+
* @param {number} rise The time constant going up, seconds.
|
|
188
|
+
* @param {number} fall The time constant going down, seconds.
|
|
189
|
+
* @returns {number} The value after the step.
|
|
190
|
+
*/
|
|
191
|
+
export function follow(value, target, dt, rise, fall) {
|
|
192
|
+
return value + (target - value) * (1 - Math.exp(-dt / (target > value ? rise : fall)));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* What a meter reports each frame. The arrays are the meter's own, filled
|
|
197
|
+
* again each frame.
|
|
198
|
+
*
|
|
199
|
+
* @typedef {object} Reading
|
|
200
|
+
* @property {number} level How loud, 0–1 (`loudness`, eased).
|
|
201
|
+
* @property {number} bass The bass band, 0–1, eased.
|
|
202
|
+
* @property {number} mid The mid band, 0–1, eased.
|
|
203
|
+
* @property {number} treble The treble band, 0–1, eased.
|
|
204
|
+
* @property {number} kick A kick's light: 1 (or its strength) on a kick in the low end, fading over a fifth of a second.
|
|
205
|
+
* @property {number} beats Kicks heard so far: an effect does something once each time it goes up.
|
|
206
|
+
* @property {Float32Array} spectrum SPECTRUM log-spaced bins, 0–1, lowest first, eased.
|
|
207
|
+
* @property {Float32Array} waveform WAVEFORM points of the trace (`scope`), -1–1, eased and scaled to its own recent peak.
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @typedef {object} Meter
|
|
212
|
+
* @property {Reading} reading The latest reading.
|
|
213
|
+
* @property {(freq: ArrayLike<number>, time: ArrayLike<number>, binHz: number, dt: number) => Reading} update
|
|
214
|
+
* Reads a frame of the analyser's spectrum and waveform `dt` seconds after the last.
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* A meter: the analyser's bytes each frame in, an eased reading out.
|
|
219
|
+
*
|
|
220
|
+
* @returns {Meter} The meter, reading silence.
|
|
221
|
+
*/
|
|
222
|
+
export function createMeter() {
|
|
223
|
+
/** @type {Reading} */
|
|
224
|
+
const reading = { level: 0, bass: 0, mid: 0, treble: 0, kick: 0, beats: 0, spectrum: new Float32Array(SPECTRUM), waveform: new Float32Array(WAVEFORM) };
|
|
225
|
+
const bins = new Float32Array(SPECTRUM);
|
|
226
|
+
const trace = new Float32Array(WAVEFORM);
|
|
227
|
+
/** The low end's recent average, the trace's recent peak, time since the last kick, and whether the next may come. */
|
|
228
|
+
let low = 0;
|
|
229
|
+
let peak = 0.2;
|
|
230
|
+
let since = 1;
|
|
231
|
+
let armed = true;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Eases the reading towards a frame's values.
|
|
235
|
+
*
|
|
236
|
+
* @param {{ level: number, bass: number, mid: number, treble: number }} raw This frame's levels.
|
|
237
|
+
* @param {number} dt Seconds since the last frame.
|
|
238
|
+
*/
|
|
239
|
+
const ease = (raw, dt) => {
|
|
240
|
+
reading.level = follow(reading.level, raw.level, dt, 0.04, 0.2);
|
|
241
|
+
reading.bass = follow(reading.bass, raw.bass, dt, 0.03, 0.16);
|
|
242
|
+
reading.mid = follow(reading.mid, raw.mid, dt, 0.03, 0.14);
|
|
243
|
+
reading.treble = follow(reading.treble, raw.treble, dt, 0.02, 0.12);
|
|
244
|
+
reading.kick *= Math.exp(-dt / 0.2);
|
|
245
|
+
for (let i = 0; i < SPECTRUM; i++) reading.spectrum[i] = follow(reading.spectrum[i], bins[i], dt, 0.02, 0.12);
|
|
246
|
+
const k = 1 - Math.exp(-dt / 0.05);
|
|
247
|
+
for (let i = 0; i < WAVEFORM; i++) reading.waveform[i] += (trace[i] / peak - reading.waveform[i]) * k;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
reading,
|
|
252
|
+
update(freq, time, binHz, dt) {
|
|
253
|
+
logSpectrum(freq, binHz, bins);
|
|
254
|
+
scope(time, trace);
|
|
255
|
+
let top = 0;
|
|
256
|
+
for (let i = 0; i < WAVEFORM; i++) top = Math.max(top, Math.abs(trace[i]));
|
|
257
|
+
peak = Math.max(0.08, top, peak * Math.exp(-dt / 1.5));
|
|
258
|
+
// A kick: the low end jumping above where it has been lately.
|
|
259
|
+
const kick = bandLevel(freq, binHz, BANDS.kick[0], BANDS.kick[1]);
|
|
260
|
+
since += dt;
|
|
261
|
+
if (kick - low > KICK_RISE && since > KICK_GAP && armed) {
|
|
262
|
+
reading.kick = Math.min(1, 0.4 + (kick - low) * 5);
|
|
263
|
+
reading.beats++;
|
|
264
|
+
since = 0;
|
|
265
|
+
armed = false;
|
|
266
|
+
} else if (kick - low < KICK_RISE / 2) {
|
|
267
|
+
armed = true;
|
|
268
|
+
}
|
|
269
|
+
low = follow(low, kick, dt, 0.25, 0.25);
|
|
270
|
+
ease(
|
|
271
|
+
{
|
|
272
|
+
level: loudness(time),
|
|
273
|
+
bass: bandLevel(freq, binHz, BANDS.bass[0], BANDS.bass[1]),
|
|
274
|
+
mid: bandLevel(freq, binHz, BANDS.mid[0], BANDS.mid[1]),
|
|
275
|
+
treble: bandLevel(freq, binHz, BANDS.treble[0], BANDS.treble[1]),
|
|
276
|
+
},
|
|
277
|
+
dt,
|
|
278
|
+
);
|
|
279
|
+
return reading;
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Reading } from './audio-meter.js';
|
|
2
|
+
|
|
3
|
+
/** What an effect reads each frame while sound plays. */
|
|
4
|
+
export interface AudioReading extends Reading {
|
|
5
|
+
/** How much to follow the sound, 0–1: eases in as playback starts and out after it stops. */
|
|
6
|
+
on: number;
|
|
7
|
+
/** How far through the track, 0–1, or -1 when its length is unknown (a stream). */
|
|
8
|
+
progress: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The sound an effect follows. */
|
|
12
|
+
export interface AudioSignal {
|
|
13
|
+
/** The sound this frame, or null when nothing it follows has played lately (the effect runs as without sound, at no cost). */
|
|
14
|
+
read(now: number, dt: number): AudioReading | null;
|
|
15
|
+
/** Stops listening. */
|
|
16
|
+
stop(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Where an effect's sound comes from: `'page'` for whatever plays, or a CSS selector for an `<audio>`/`<video>` or a player's container (a WaveformPlayer). */
|
|
20
|
+
export type AudioSource = 'page' | (string & {});
|
|
21
|
+
|
|
22
|
+
/** Whether audio from `src` can be read by Web Audio: same origin, a blob or data URL, or fetched with CORS (`crossOrigin` set). */
|
|
23
|
+
export function readable(src: string, crossOrigin: string | null, origin: string): boolean;
|
|
24
|
+
|
|
25
|
+
/** Starts listening to the sound an effect follows. Read it each frame; stop it with the effect. */
|
|
26
|
+
export function audioSignal(source: AudioSource): AudioSignal;
|