File size: 13,141 Bytes
4a0e872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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();
      }),
    };
  }
}