Under the hood

This document covers how Horror Engine makes its sound, how the 3D machine is put together and how the page is built, along with the design rules the code follows.

Design principles

Files

index.html            snapshot of the inlined app (no longer updated in git; CI builds it for Releases)
build.py              `python3 build.py` writes the inlined index.html; `--hosted DEST` writes the Pages site
src/
  engine.js           the DSP: voices, effects, macros, moments, looper, recorder
  io.js               presets, performance files and their schemas, WAV and zip writers, offline render
  ui.js               page UI: voice rack, knobs, Surface pad, keys, MIDI, recording, presets
  machine2.js         the 3D Machine (built behind the Surface at load): scene, parts, hit zones, gestures, camera
  altar.js            the Altar skin (?skin=altar): same zones and handlers, different parts
  sculpt.js           its sculpting kit: distance fields to meshes, swept tubes, the blood/rust shader
  look.js             the Machine's look: post chain, mesh merging, contact shadows
  body.html, style.css
assets/textures/      CC0 photo-scanned texture sets as WebP (see CREDITS.md)
assets/crypt_assets.js  Altar crypt textures and scanned skulls, packed (see CREDITS.md)
vendor/three.min.js   three.js r160 (MIT)
test/                 Node DSP tests (no browser or GPU): node test/engine.js
.github/workflows/    test CI, Pages deploy, and the Release zip

Signal flow

gestures (mouse, pen, keys, MIDI, moments, loop layers, Hold)
   -> nine voices  -> channel strip (level, pan, Grit, ear guard, Distance)
                         |-> hall send ------> reverb (8-line FDN) -----------|
                         |-> spring send ----> spring tank ---------------------|
                         |-> harmonizer send -> pitch shifter (+feedback) ------|
                         |-> body send ------> wooden body + piezo ------------|
                         |-> aura capture ---> granular pad (voice VIII) ------|
                         '-> dry --------------------------------------------- +--> per-stem master
                                                                                   (tilt, volume, low cut)
                                                                                 -> look-ahead limiter
                                                                                    (about -1 dBFS) -> out

The recorder takes the mix after the limiter and each stem before it, all on the same frame.

The audio engine

src/engine.js defines HE_SOURCES, the nine voice ids, and HE_PARAMS, the one registry of every parameter with its range, default and unit. The UI, presets and file schemas are all built from that registry.

HECoreFactory() returns the engine as one self-contained function. ui.js turns its source text into an AudioWorklet module, so the DSP runs off the main thread at the device's sample rate. Chrome refuses Blob-URL worklets on file://, so the page tries a data: URL first there and a Blob URL over http(s), and each falls back to the other. If AudioWorklet is missing, the same engine runs on the main thread ("compatibility mode").

The engine renders in 128-frame blocks. Per block it does the following:

  1. Moments and looper. Scripted gestures and loop layers write into contact slots, the same shape as a live touch.
  2. Contacts. Each voice takes its contacts in priority order: live touch, then moments, then loop layers (oldest first), then Hold. Voices with several instances keep each contact on its own instance.
  3. Macros. Dread, Distance, Material and Pitch are smoothed into shared values: detune, instability, roughness, sub level, material decay and brightness, and the pitch multiplier.
  4. Voices, each with its own model:
Voice Model Instances
E-bow string waveguide loop with a self-oscillating magnetic driver 2
Hurdy-gurdy three bowed waveguide strings on a rosined wheel, six body modes 1
Bowed metal modal bank driven through a velocity-weakening friction contact 3
Glass modal bank with a stick-slip contact, wet-finger beating 3
Struck metal modal bank per hit 12 voices
Spring tank three springs, each a chain of dispersive allpasses in a feedback loop 1
Junk tray 2D particle simulation (up to 80 pieces) driving 24 tiny resonators 1
Aura granular cloud over the last ~10 s of the dry bus 32 grains
Superball stick-slip pulse train into a modal surface, with mode lock 1
  1. Strip. Each voice passes through its channel strip: level, pan, Grit, the glass ear guard and the Distance stage. The strip sends to the hall, spring, harmonizer and body, and writes the stem buffer.
  2. Shared effects: - the body (twelve wooden modes and the piezo filter); - the harmonizer (two pairs of 70 ms read heads, with feedback); - the hall (an eight-line Householder feedback delay network with slowly modulated lines); - the reverse swell used by the Stinger.
  3. Master. Each stem gets tilt, volume and a low cut. The sum then goes through a look-ahead limiter.

Every message from the page carries a sequence number. The engine reports the frame it applied each message on, and that log becomes the .horror.json performance file. io.js can replay a performance offline (renderJob) through the same engine, which is how Render… works.

Telemetry comes back to the page every 1024 frames: peaks per voice, limiter gain, loop positions, moment states and the junk positions for drawing.

Speed. In a measurement on a desktop CPU, a busy patch (eight voices held, Heartbeat and In the walls running, struck hits, harmonizer and body sends) rendered at about 7× real time on one core. That is about 0.36 ms of each 2.67 ms block at 48 kHz.

The macros, in the engine

The Machine

machine2.js builds the cabinet in three.js from code; there are no model files. Every playable part has an invisible zone with a kind (play, bowtap, knock, puck, knob, tray, wand and so on) and a handler in the HD table. A handler turns pointer movement into the same {x, y, pressure} gestures the Surface sends, and sets any parameters the part implies. For example, the slate sets Glass to Chalkboard and Chalk, and a ruler tap sets Struck to Ruler. The hint text for each part lives in HINT.

The textures are CC0 photo scans. The inlined build (python3 build.py) embeds them as base64 WebP, because WebGL will not load image files from file://. The hosted build (python3 build.py --hosted DEST) leaves them as files and fetches only the sets for the skin you are on: Workshop never downloads the Altar crypt textures or scanned skulls, and Altar skips the workshop-only sets (walnut, brick, and so on). Shared sets such as oak, floor, steel, iron and crinkle are used by both. Web Workers decode them and build mip chains off the main thread. Half-size sets load first and the full-size sets stream in after.

High detail renders into an MSAA HDR buffer, which look.js finishes with bloom, ACES tone mapping, a vignette and dither (this needs WebGL 2). Low detail (?quality=low) turns off shadows and the post chain, keeps the half-size textures and renders at a pixel ratio of 1. The sound is identical in both.

The page exposes window.__he for scripting: state, parameters and a ready() check.

File formats

Both JSON formats are checked against the JSON Schemas in io.js on load.

Building

python3 build.py            # Python 3 and Pillow (pip install pillow)

Edit the files in src/, run the build and reload index.html. The build inlines src/* and the textures. vendor/three.min.js stays a separate file loaded by a script tag, so keep it next to index.html.