Download web/js/audio/engine.js from paralaif/tiny-scene-three: direct link, hf CLI and curl.
- Browser
- Download file 13.1 kB
-
https://huggingface.co/paralaif/tiny-scene-three/resolve/main/web/js/audio/engine.js
- Command line
-
hf download hf://paralaif/tiny-scene-three/web/js/audio/engine.js
-
curl -L -o engine.js https://huggingface.co/paralaif/tiny-scene-three/resolve/main/web/js/audio/engine.js
13.1 kB
| // Plays the loops from theory.js with Tone.js (loaded as window.Tone), plus weather/place ambience, | |
| // vehicle engines and video recording. Everything is synthesised; there are no audio files. | |
| import { STEPS, buildSong, resolveMusic } from './theory.js'; | |
| const hz = (m) => 440 * 2 ** ((m - 69) / 12); | |
| // [oscillator, [attack, decay, sustain, release], volume dB] | |
| const PATCH = { | |
| chiptune: { chords: ['square', [0.005, 0.08, 0.25, 0.05], -21], bass: ['triangle', [0.005, 0.1, 0.6, 0.08], -8], lead: ['square', [0.01, 0.1, 0.5, 0.1], -15], lp: 9000, wet: 0.06, kick: -8, snare: -17, hat: -26 }, | |
| electro: { chords: ['fatsawtooth', [0.005, 0.15, 0.2, 0.1], -22], bass: ['sawtooth', [0.005, 0.12, 0.5, 0.08], -13], lead: ['fatsawtooth', [0.02, 0.1, 0.6, 0.2], -19], lp: 6000, wet: 0.18, kick: -5, snare: -15, hat: -24 }, | |
| ambient: { chords: ['fattriangle', [1.2, 0.5, 0.8, 2.5], -12], bass: ['sine', [0.4, 0.3, 0.9, 1.5], -7], lead: ['sine', [0.05, 0.4, 0.3, 1.5], -10], lp: 3500, wet: 0.6 }, | |
| rock: { chords: ['fatsawtooth', [0.005, 0.1, 0.7, 0.1], -25, 0.55], bass: ['sawtooth', [0.005, 0.1, 0.7, 0.1], -12], lead: ['square', [0.01, 0.1, 0.7, 0.15], -17], lp: 6500, wet: 0.12, kick: -5, snare: -12, hat: -25 }, | |
| jazz: { chords: ['fmsine', [0.01, 0.5, 0.3, 0.6], -15], bass: ['triangle', [0.01, 0.2, 0.5, 0.15], -6], lead: ['triangle', [0.02, 0.2, 0.6, 0.3], -12], lp: 5000, wet: 0.2, kick: -17, snare: -25, hat: -25 }, | |
| orchestral: { chords: ['fatsawtooth', [0.5, 0.3, 0.8, 1.2], -21], bass: ['fatsawtooth', [0.3, 0.3, 0.8, 1.0], -14], lead: ['triangle', [0.08, 0.2, 0.8, 0.5], -8], lp: 3600, wet: 0.45, kick: -7 }, | |
| lofi: { chords: ['triangle', [0.01, 0.6, 0.4, 0.8], -13], bass: ['sine', [0.01, 0.2, 0.7, 0.2], -6], lead: ['sine', [0.02, 0.3, 0.4, 0.5], -13], lp: 2200, wet: 0.25, kick: -7, snare: -19, hat: -30 }, | |
| }; | |
| // engine hum per vehicle: [oscillator, frequency, tremolo Hz (0 = none), volume dB] | |
| const ENGINE = { car: ['sawtooth', 72, 0, -20], truck: ['sawtooth', 50, 0, -17], bus: ['sawtooth', 56, 0, -18], train: ['square', 44, 5, -19], plane: ['sawtooth', 110, 0, -19], helicopter: ['sawtooth', 38, 13, -14], rocket: ['sawtooth', 60, 30, -14], ufo: ['sine', 230, 6, -22], boat: ['triangle', 62, 0, -20] }; | |
| const MOVING = new Set(['drive', 'walk', 'run', 'fly', 'roll', 'circle', 'swim']); | |
| export class Music { | |
| constructor() { | |
| this.T = globalThis.Tone ?? null; | |
| this.ready = false; this.muted = false; this.paused = false; | |
| this.bpm = 90; this.p = null; this.song = null; this.step = 0; this.band = null; this.genre = null; | |
| this.ambKey = null; this.ambNodes = []; this.engines = []; this.lastC = 0; | |
| } | |
| /** Audio can only start from a user gesture; call this from a click or key press. */ | |
| unlock() { | |
| if (!this.T || this.ready) return Promise.resolve(); | |
| return (this._unlocking ??= this.T.start().then(() => { | |
| this._init(); | |
| this.ready = true; | |
| if (this.scene) this.apply(this.scene, this.insts); | |
| })); | |
| } | |
| _init() { | |
| const T = this.T; | |
| this.out = new T.Volume(-4); | |
| this.limiter = new T.Limiter(-1); | |
| this.meter = new T.Meter({ smoothing: 0 }); // raw level: Tone only applies smoothing when it is read | |
| this.out.chain(this.limiter, T.getDestination()); | |
| this.limiter.connect(this.meter); | |
| this.musicVol = new T.Volume(0); | |
| this.filter = new T.Filter(8000, 'lowpass'); | |
| this.reverb = new T.Reverb({ decay: 3.2, wet: 0.2 }); | |
| this.bandBus = new T.Gain(1); | |
| this.bandBus.chain(this.filter, this.reverb, this.musicVol, this.out); | |
| this.ambBus = new T.Volume(4); | |
| this.ambBus.connect(this.out); | |
| this.thunder = new T.NoiseSynth({ noise: { type: 'brown' }, envelope: { attack: 0.03, decay: 2.4, sustain: 0 }, volume: -4 }); | |
| this.thunder.chain(new T.Filter(260, 'lowpass'), this.ambBus); | |
| T.getTransport().scheduleRepeat((time) => this._tick(time), '16n'); | |
| } | |
| _synth(Kind, [osc, [attack, decay, sustain, release], volume]) { | |
| const T = this.T, opts = { oscillator: { type: osc }, envelope: { attack, decay, sustain, release }, volume }; | |
| return Kind === 'poly' ? new T.PolySynth(T.Synth, opts) : new T.Synth(opts); | |
| } | |
| _makeBand(genre) { | |
| const T = this.T, P = PATCH[genre], old = this.band; | |
| const b = { nodes: [] }; | |
| b.chords = this._synth('poly', P.chords); | |
| b.bass = this._synth('mono', P.bass); | |
| b.lead = this._synth('mono', P.lead); | |
| if (P.chords[3]) { const d = new T.Distortion(P.chords[3]); b.chords.chain(d, this.bandBus); b.nodes.push(d); } else b.chords.connect(this.bandBus); | |
| b.bass.connect(this.bandBus); | |
| b.lead.connect(this.bandBus); | |
| if (P.kick != null) { | |
| b.kick = new T.MembraneSynth({ pitchDecay: 0.04, octaves: genre === 'orchestral' ? 2 : 6, envelope: { attack: 0.001, decay: genre === 'orchestral' ? 0.9 : 0.3, sustain: 0 }, volume: P.kick }).connect(this.bandBus); | |
| } | |
| if (P.snare != null) { | |
| const hp = new T.Filter(1400, 'highpass'); | |
| b.snare = new T.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.14, sustain: 0 }, volume: P.snare }).chain(hp, this.bandBus); | |
| b.nodes.push(hp); | |
| } | |
| if (P.hat != null) { | |
| const hp = new T.Filter(7000, 'highpass'); | |
| b.hat = new T.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.04, sustain: 0 }, volume: P.hat }).chain(hp, this.bandBus); | |
| b.nodes.push(hp); | |
| } | |
| this.band = b; | |
| this.genre = genre; | |
| this.filter.frequency.rampTo(P.lp, 0.3); | |
| this.reverb.wet.rampTo(P.wet, 0.3); | |
| if (old) setTimeout(() => { for (const n of [old.chords, old.bass, old.lead, old.kick, old.snare, old.hat, ...old.nodes]) n?.dispose(); }, 2500); | |
| } | |
| /** Update everything from the scene description. `insts` are the built objects (for engine sounds). */ | |
| apply(scene, insts = []) { | |
| this.scene = scene; this.insts = insts; | |
| const p = this.p = resolveMusic(scene); | |
| this.bpm = p.bpm; | |
| if (!this.ready) return p; | |
| const T = this.T, tr = T.getTransport(); | |
| if (p.key !== this.songKey) { this.song = buildSong(p); this.songKey = p.key; } | |
| if (p.genre !== this.genre) this._makeBand(p.genre); | |
| tr.bpm.rampTo(p.bpm, 0.3); | |
| tr.swing = this.song.swing; | |
| tr.swingSubdivision = '8n'; | |
| this.musicVol.volume.rampTo(p.on ? p.vol * 4 : -80, 0.25); | |
| this._ambience(scene, insts); | |
| this._run(); | |
| return p; | |
| } | |
| _run() { | |
| if (!this.ready) return; | |
| const tr = this.T.getTransport(); | |
| this.T.getDestination().mute = this.muted; | |
| if (this.paused) { if (tr.state === 'started') tr.pause(); } else if (tr.state !== 'started') tr.start(); | |
| } | |
| setMuted(m) { this.muted = m; this._run(); } | |
| setPaused(p) { this.paused = p; this._run(); } | |
| /** Musical time in beats while the transport is running, else null (the caller keeps its own clock). */ | |
| beat() { | |
| if (!this.ready || this.paused) return null; | |
| const tr = this.T.getTransport(); | |
| return tr.state === 'started' && this.T.getContext().state === 'running' ? tr.ticks / tr.PPQ : null; | |
| } | |
| /** Output level in dB (for tests and meters). */ | |
| level() { return this.ready ? this.meter.getValue() : -Infinity; } | |
| _tick(time) { | |
| const s = this.song, b = this.band, p = this.p; | |
| if (!s || !b || !p?.on) { this.step++; return; } | |
| const sec = 60 / this.T.getTransport().bpm.value / 4; | |
| for (const e of s.events[this.step++ % STEPS]) { | |
| const d = Math.max(0.03, e.dur * sec); | |
| if (e.layer === 'kick') { if (p.drums && b.kick) b.kick.triggerAttackRelease(this.genre === 'orchestral' ? hz(e.notes[0]) : 52, d, time, e.vel); } | |
| else if (e.layer === 'snare') { if (p.drums && b.snare) b.snare.triggerAttackRelease(0.14, time, e.vel); } | |
| else if (e.layer === 'hat') { if (p.drums && b.hat) b.hat.triggerAttackRelease(0.04, time, e.vel); } | |
| else if (e.layer === 'bass') { if (p.bass) b.bass.triggerAttackRelease(hz(e.notes[0]), d, time, e.vel); } | |
| else if (e.layer === 'lead') { if (p.melody) b.lead.triggerAttackRelease(hz(e.notes[0]), d, time, e.vel); } | |
| else if (e.strum) e.notes.forEach((n, i) => b.chords.triggerAttackRelease(hz(n), d, time + i * e.strum, e.vel)); | |
| else b.chords.triggerAttackRelease(e.notes.map(hz), d, time, e.vel); | |
| } | |
| } | |
| // ---------------------------------------------------------------- ambience | |
| _ambience(scene, insts) { | |
| const T = this.T, { loc, time, weather } = scene; | |
| const movers = insts.filter((i) => ENGINE[i.ent.type] && MOVING.has(i.act) && !i.mount).slice(0, 3); | |
| const key = [loc, time, weather, ...movers.map((i) => i.ent.id + i.ent.type + i.act)].join('|'); | |
| if (key === this.ambKey) { movers.forEach((i, k) => { if (this.engines[k]) this.engines[k].inst = i; }); return; } | |
| this.ambKey = key; | |
| for (const n of this.ambNodes) n.dispose(); | |
| const N = this.ambNodes = [], keep = (...nodes) => { N.push(...nodes); return nodes[0]; }; | |
| const noise = (type, filterType, freq, db, q = 1) => { | |
| const src = keep(new T.Noise(type).start()), f = keep(new T.Filter({ type: filterType, frequency: freq, Q: q })), v = keep(new T.Volume(db)); | |
| src.chain(f, v, this.ambBus); | |
| return { f, v }; | |
| }; | |
| const outdoor = loc !== 'room' && loc !== 'space' && loc !== 'moon'; | |
| if (outdoor && (weather === 'rain' || weather === 'storm')) { | |
| noise('white', 'highpass', 2500, weather === 'storm' ? -27 : -31); | |
| noise('pink', 'bandpass', 900, weather === 'storm' ? -22 : -27); | |
| } | |
| if (outdoor && (weather === 'storm' || weather === 'snow' || weather === 'fog' || ['desert', 'mountain', 'snow'].includes(loc))) { | |
| const w = noise('brown', 'bandpass', 420, weather === 'storm' ? -14 : -22, 2); | |
| keep(new T.LFO({ frequency: 0.13, min: 220, max: 800 }).start()).connect(w.f.frequency); | |
| } | |
| if (loc === 'ocean' || loc === 'beach') { | |
| const w = noise('pink', 'lowpass', 750, -18), g = keep(new T.Gain(0.6)); | |
| w.v.disconnect(); w.v.chain(g, this.ambBus); | |
| keep(new T.LFO({ frequency: 0.11, min: 0.25, max: 1 }).start()).connect(g.gain); | |
| } | |
| if (time === 'night' && weather === 'clear' && ['field', 'forest', 'farm', 'park', 'mountain', 'road', 'desert', 'beach'].includes(loc)) { | |
| const o = keep(new T.Oscillator(4300, 'sine').start()), chirp = keep(new T.Gain(0)), burst = keep(new T.Gain(0)), v = keep(new T.Volume(-34)); | |
| o.chain(chirp, burst, v, this.ambBus); | |
| keep(new T.LFO({ frequency: 27, min: 0, max: 1, type: 'square' }).start()).connect(chirp.gain); | |
| keep(new T.LFO({ frequency: 0.8, min: -0.6, max: 1 }).start()).connect(burst.gain); | |
| } | |
| this.engines = movers.map((inst) => { | |
| const [type, freq, trem, db] = ENGINE[inst.ent.type]; | |
| const o = keep(new T.Oscillator(freq, type).start()), f = keep(new T.Filter(type === 'sine' ? 2000 : 420, 'lowpass')), g = keep(new T.Gain(1)); | |
| const pan = keep(new T.Panner3D({ panningModel: 'equalpower', distanceModel: 'inverse', refDistance: 6, rolloffFactor: 1.2 })), v = keep(new T.Volume(db)); | |
| o.chain(f, g, pan, v, this.ambBus); | |
| if (trem) keep(new T.LFO({ frequency: trem, min: 0.25, max: 1 }).start()).connect(g.gain); | |
| return { inst, pan }; | |
| }); | |
| } | |
| /** Once per rendered frame: thunder follows the lightning, engine sounds follow their vehicles. */ | |
| frame(t, camera, sceneWeather) { | |
| if (!this.ready || this.paused) return; | |
| if (sceneWeather === 'storm') { | |
| const c = t % 5.3; | |
| if (c < this.lastC) this.thunder.triggerAttackRelease(2.4, this.T.now() + 0.35, 0.6 + Math.random() * 0.4); | |
| this.lastC = c; | |
| } | |
| if (!this.engines.length) return; | |
| const L = this.T.getListener(), e = camera.matrixWorld.elements; | |
| L.positionX.value = e[12]; L.positionY.value = e[13]; L.positionZ.value = e[14]; | |
| L.forwardX.value = -e[8]; L.forwardY.value = -e[9]; L.forwardZ.value = -e[10]; | |
| L.upX.value = e[4]; L.upY.value = e[5]; L.upZ.value = e[6]; | |
| for (const { inst, pan } of this.engines) { | |
| const m = inst.obj.matrixWorld.elements; | |
| pan.positionX.value = m[12]; pan.positionY.value = m[13]; pan.positionZ.value = m[14]; | |
| } | |
| } | |
| // ---------------------------------------------------------------- video | |
| /** Start recording the canvas (and the audio, if it is running). Returns { stop(): Promise<Blob> }. */ | |
| record(canvas, fps = 30) { | |
| const stream = canvas.captureStream(fps); | |
| if (this.ready) { | |
| if (!this.recDest) { this.recDest = this.T.getContext().createMediaStreamDestination(); this.limiter.connect(this.recDest); } | |
| for (const tr of this.recDest.stream.getAudioTracks()) stream.addTrack(tr); | |
| } | |
| const mime = ['video/webm;codecs=vp9,opus', 'video/webm;codecs=vp8,opus', 'video/webm'].find((m) => MediaRecorder.isTypeSupported(m)); | |
| const rec = new MediaRecorder(stream, { mimeType: mime, videoBitsPerSecond: 6_000_000 }), chunks = []; | |
| rec.ondataavailable = (ev) => { if (ev.data.size) chunks.push(ev.data); }; | |
| rec.start(250); | |
| return { | |
| stop: () => new Promise((resolve) => { | |
| rec.onstop = () => { for (const tr of stream.getVideoTracks()) tr.stop(); resolve(new Blob(chunks, { type: 'video/webm' })); }; | |
| rec.stop(); | |
| }), | |
| }; | |
| } | |
| } | |