@open-artifact/motion-audio 0.0.0-stage → 0.12.0
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +36 -4
- package/src/index.d.ts +46 -0
- package/src/index.js +25 -0
- package/src/keepalive.d.ts +10 -0
- package/src/keepalive.js +49 -0
- package/src/loudness.d.ts +32 -0
- package/src/loudness.js +108 -0
- package/src/midi.d.ts +16 -0
- package/src/midi.js +179 -0
- package/src/mixer.d.ts +29 -0
- package/src/mixer.js +70 -0
- package/src/narration/captions.d.ts +24 -0
- package/src/narration/captions.js +45 -0
- package/src/narration/decode.d.ts +25 -0
- package/src/narration/decode.js +72 -0
- package/src/narration/duck.d.ts +20 -0
- package/src/narration/duck.js +34 -0
- package/src/narration/limits.d.ts +23 -0
- package/src/narration/limits.js +16 -0
- package/src/narration/peaks.d.ts +14 -0
- package/src/narration/peaks.js +27 -0
- package/src/narration/plan.d.ts +12 -0
- package/src/narration/plan.js +31 -0
- package/src/narration/speech.d.ts +31 -0
- package/src/narration/speech.js +24 -0
- package/src/offline.d.ts +79 -0
- package/src/offline.js +257 -0
- package/src/playback-clock.d.ts +39 -0
- package/src/playback-clock.js +90 -0
- package/src/played-score.d.ts +28 -0
- package/src/played-score.js +31 -0
- package/src/player.d.ts +47 -0
- package/src/player.js +237 -0
- package/src/reverb.d.ts +10 -0
- package/src/reverb.js +38 -0
- package/src/scheduler.d.ts +29 -0
- package/src/scheduler.js +64 -0
- package/src/session.d.ts +24 -0
- package/src/session.js +133 -0
- package/src/synth/drums.d.ts +4 -0
- package/src/synth/drums.js +97 -0
- package/src/synth/envelope.d.ts +26 -0
- package/src/synth/envelope.js +39 -0
- package/src/synth/fx.d.ts +9 -0
- package/src/synth/fx.js +131 -0
- package/src/synth/nodes.d.ts +22 -0
- package/src/synth/nodes.js +63 -0
- package/src/synth/noise.d.ts +6 -0
- package/src/synth/noise.js +31 -0
- package/src/synth/patches.d.ts +5 -0
- package/src/synth/patches.js +119 -0
- package/src/synth/presets.d.ts +32 -0
- package/src/synth/presets.js +136 -0
- package/src/synth/schedule.d.ts +5 -0
- package/src/synth/schedule.js +25 -0
- package/src/synth/voice.d.ts +35 -0
- package/src/synth/voice.js +52 -0
- package/src/timeline.d.ts +25 -0
- package/src/timeline.js +49 -0
- package/src/wave.d.ts +17 -0
- package/src/wave.js +111 -0
- package/src/window.d.ts +32 -0
- package/src/window.js +59 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const LEAST_ATTACK = 0.0005;
|
|
2
|
+
const LEAST_DECAY = 0.001;
|
|
3
|
+
/** When a held note lets go: after its length, but never before its attack is over. */
|
|
4
|
+
export function adsrReleaseAt(at, duration, attack) {
|
|
5
|
+
return at + Math.max(duration, attack + 0.01);
|
|
6
|
+
}
|
|
7
|
+
/** The level a held note has fallen to `elapsed` seconds after its start (still in its decay phase). */
|
|
8
|
+
export function adsrLevelAt(elapsed, shape) {
|
|
9
|
+
const held = shape.peak * shape.sustain;
|
|
10
|
+
return held + (shape.peak - held) * Math.exp(-(elapsed - shape.attack) / Math.max(LEAST_DECAY, shape.decay));
|
|
11
|
+
}
|
|
12
|
+
/** When a held note is silent: its release after it lets go. */
|
|
13
|
+
export function adsrEnd(at, duration, shape) {
|
|
14
|
+
return adsrReleaseAt(at, duration, shape.attack) + shape.release;
|
|
15
|
+
}
|
|
16
|
+
export function decayEnvelope(context, destination, at, shape) {
|
|
17
|
+
const envelope = context.createGain();
|
|
18
|
+
const peakAt = at + Math.max(LEAST_ATTACK, shape.attack);
|
|
19
|
+
envelope.gain.setValueAtTime(0, at);
|
|
20
|
+
envelope.gain.linearRampToValueAtTime(shape.peak, peakAt);
|
|
21
|
+
envelope.gain.setTargetAtTime(0, peakAt, Math.max(LEAST_DECAY, shape.decay));
|
|
22
|
+
envelope.gain.setValueAtTime(0, shape.stop);
|
|
23
|
+
envelope.connect(destination);
|
|
24
|
+
return envelope;
|
|
25
|
+
}
|
|
26
|
+
export function adsrEnvelope(context, destination, at, duration, shape) {
|
|
27
|
+
const envelope = context.createGain();
|
|
28
|
+
const releaseAt = adsrReleaseAt(at, duration, shape.attack);
|
|
29
|
+
envelope.gain.setValueAtTime(0, at);
|
|
30
|
+
envelope.gain.linearRampToValueAtTime(shape.peak, at + shape.attack);
|
|
31
|
+
envelope.gain.setTargetAtTime(shape.peak * shape.sustain, at + shape.attack, Math.max(LEAST_DECAY, shape.decay));
|
|
32
|
+
// setTargetAtTime has no end, so the release starts from the level the decay would have reached by then.
|
|
33
|
+
envelope.gain.setValueAtTime(adsrLevelAt(releaseAt - at, shape), releaseAt);
|
|
34
|
+
envelope.gain.setTargetAtTime(0, releaseAt, Math.max(LEAST_DECAY, shape.release / 4));
|
|
35
|
+
const end = releaseAt + shape.release;
|
|
36
|
+
envelope.gain.setValueAtTime(0, end);
|
|
37
|
+
envelope.connect(destination);
|
|
38
|
+
return { gain: envelope, end };
|
|
39
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FxName } from '@open-artifact/motion-core';
|
|
2
|
+
import type { VoiceBus, VoiceOutput } from './nodes.ts';
|
|
3
|
+
import type { Sound } from './voice.ts';
|
|
4
|
+
/** The strikes of a drum roll: from 0.16 s apart, each 0.86 of the gap before, down to 0.035 s, over `length` seconds. */
|
|
5
|
+
export declare function rollStrikes(length: number): number[];
|
|
6
|
+
/** A boing's pitch curve: the note wobbling at `rate` per second, the wobble dying away. */
|
|
7
|
+
export declare function boingCurve(frequency: number, length: number, rate: number, points?: number): Float32Array<ArrayBuffer>;
|
|
8
|
+
/** A sound effect: some are drums or bells played in a pattern, the rest made from noise sweeps and pitch bends. */
|
|
9
|
+
export declare function synthesizeFx(bus: VoiceBus, name: FxName, sound: Sound, output: VoiceOutput): void;
|
package/src/synth/fx.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { synthesizeDrum } from "./drums.js";
|
|
2
|
+
import { decayEnvelope } from "./envelope.js";
|
|
3
|
+
import { filter, noise, oscillator } from "./nodes.js";
|
|
4
|
+
import { synthesizePatch } from "./patches.js";
|
|
5
|
+
import { midiToFrequency } from "./voice.js";
|
|
6
|
+
const BELL_INTERVALS = {
|
|
7
|
+
ding: [0, 7],
|
|
8
|
+
chime: [0, 4, 7, 12],
|
|
9
|
+
sparkle: [0, 7, 12, 16, 19, 24],
|
|
10
|
+
};
|
|
11
|
+
const BELL_STEP_SECONDS = { ding: 0.12, chime: 0.09, sparkle: 0.055 };
|
|
12
|
+
/** The strikes of a drum roll: from 0.16 s apart, each 0.86 of the gap before, down to 0.035 s, over `length` seconds. */
|
|
13
|
+
export function rollStrikes(length) {
|
|
14
|
+
const strikes = [];
|
|
15
|
+
let offset = 0;
|
|
16
|
+
let gap = 0.16;
|
|
17
|
+
while (offset < length - 0.02) {
|
|
18
|
+
strikes.push(offset);
|
|
19
|
+
offset += gap;
|
|
20
|
+
gap = Math.max(0.035, gap * 0.86);
|
|
21
|
+
}
|
|
22
|
+
return strikes;
|
|
23
|
+
}
|
|
24
|
+
/** A boing's pitch curve: the note wobbling at `rate` per second, the wobble dying away. */
|
|
25
|
+
export function boingCurve(frequency, length, rate, points = 64) {
|
|
26
|
+
const curve = new Float32Array(points);
|
|
27
|
+
for (let index = 0; index < points; index++) {
|
|
28
|
+
const progress = index / (points - 1);
|
|
29
|
+
curve[index] = frequency * (1 + 0.45 * Math.exp(-progress * 5) * Math.sin(2 * Math.PI * rate * progress * length));
|
|
30
|
+
}
|
|
31
|
+
return curve;
|
|
32
|
+
}
|
|
33
|
+
/** A sound effect: some are drums or bells played in a pattern, the rest made from noise sweeps and pitch bends. */
|
|
34
|
+
export function synthesizeFx(bus, name, sound, output) {
|
|
35
|
+
const { context } = bus;
|
|
36
|
+
const { at, node: out } = output;
|
|
37
|
+
switch (name) {
|
|
38
|
+
case 'tick': {
|
|
39
|
+
// The velocity nudges the pitch so a run of ticks is not one note repeated.
|
|
40
|
+
const pitch = 600 + (sound.vel % 7) * 14;
|
|
41
|
+
const body = decayEnvelope(context, out, at, { peak: 0.8, attack: 0.0005, decay: 0.012, stop: at + 0.12 });
|
|
42
|
+
const resonance = filter(context, 'bandpass', pitch, 5, body);
|
|
43
|
+
oscillator(bus, 'sine', pitch, resonance, at, at + 0.12);
|
|
44
|
+
oscillator(bus, 'triangle', pitch * 2.4, resonance, at, at + 0.12);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
case 'click':
|
|
48
|
+
case 'doubleclick': {
|
|
49
|
+
const clicks = name === 'click' ? 1 : 2;
|
|
50
|
+
for (let index = 0; index < clicks; index++)
|
|
51
|
+
synthesizeDrum(bus, 'sidestick', { node: out, at: at + index * 0.11 });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
case 'pop': {
|
|
55
|
+
const body = decayEnvelope(context, out, at, { peak: 0.9, attack: 0.002, decay: 0.03, stop: at + 0.2 });
|
|
56
|
+
const pitch = sound.midi > 0 ? midiToFrequency(sound.midi) : 520;
|
|
57
|
+
const bend = oscillator(bus, 'sine', pitch, body, at, at + 0.2);
|
|
58
|
+
bend.frequency.setValueAtTime(pitch * 0.7, at);
|
|
59
|
+
bend.frequency.exponentialRampToValueAtTime(pitch * 1.6, at + 0.05);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
case 'thud': {
|
|
63
|
+
const body = decayEnvelope(context, out, at, { peak: 1, attack: 0.002, decay: 0.08, stop: at + 0.4 });
|
|
64
|
+
const bend = oscillator(bus, 'sine', 90, body, at, at + 0.4);
|
|
65
|
+
bend.frequency.setValueAtTime(140, at);
|
|
66
|
+
bend.frequency.exponentialRampToValueAtTime(70, at + 0.12);
|
|
67
|
+
const impact = decayEnvelope(context, out, at, { peak: 0.3, attack: 0.001, decay: 0.01, stop: at + 0.05 });
|
|
68
|
+
noise(bus, filter(context, 'lowpass', 1200, 0.5, impact), at, at + 0.05, 1.2);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
case 'ding':
|
|
72
|
+
case 'chime':
|
|
73
|
+
case 'sparkle': {
|
|
74
|
+
const root = sound.midi > 0 ? sound.midi : name === 'sparkle' ? 96 : 88;
|
|
75
|
+
const step = BELL_STEP_SECONDS[name];
|
|
76
|
+
BELL_INTERVALS[name].forEach((interval, index) => synthesizePatch(bus, 'glock', { ...sound, midi: root + interval }, { node: out, at: at + index * step }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
case 'whoosh': {
|
|
80
|
+
const reversed = (sound.settings.into ?? 0) > 0;
|
|
81
|
+
const length = Math.max(0.15, sound.dur);
|
|
82
|
+
const swell = context.createGain();
|
|
83
|
+
swell.gain.setValueAtTime(0, at);
|
|
84
|
+
swell.gain.linearRampToValueAtTime(0.7, at + length * (reversed ? 0.8 : 0.35));
|
|
85
|
+
swell.gain.linearRampToValueAtTime(0, at + length);
|
|
86
|
+
swell.connect(out);
|
|
87
|
+
const sweep = filter(context, 'bandpass', 800, 1.1, swell);
|
|
88
|
+
sweep.frequency.setValueAtTime(reversed ? 3200 : 500, at);
|
|
89
|
+
sweep.frequency.exponentialRampToValueAtTime(reversed ? 500 : 3400, at + length);
|
|
90
|
+
noise(bus, sweep, at, at + length, 0.3);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
case 'revswell': {
|
|
94
|
+
const length = Math.max(0.3, sound.dur);
|
|
95
|
+
// Heard before its onset: when scheduled late, it starts part of the way up its swell.
|
|
96
|
+
const start = Math.min(at, Math.max(context.currentTime, at - length));
|
|
97
|
+
const progress = 1 - (at - start) / length;
|
|
98
|
+
const swell = context.createGain();
|
|
99
|
+
swell.gain.setValueAtTime(0.0001 * Math.pow(8000, progress), start);
|
|
100
|
+
swell.gain.exponentialRampToValueAtTime(0.8, at);
|
|
101
|
+
swell.gain.linearRampToValueAtTime(0, at + 0.04);
|
|
102
|
+
swell.connect(out);
|
|
103
|
+
const sweep = filter(context, 'highpass', 1800, 0.5, filter(context, 'peaking', 6000, 2, swell));
|
|
104
|
+
sweep.frequency.setValueAtTime(900 * Math.pow(4000 / 900, progress), start);
|
|
105
|
+
sweep.frequency.exponentialRampToValueAtTime(4000, at);
|
|
106
|
+
noise(bus, sweep, start, at + 0.05, 0.6);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
case 'boing': {
|
|
110
|
+
const length = Math.max(0.4, sound.dur);
|
|
111
|
+
const pitch = sound.midi > 0 ? midiToFrequency(sound.midi) : 180;
|
|
112
|
+
const body = decayEnvelope(context, out, at, { peak: 1, attack: 0.003, decay: length * 0.3, stop: at + length });
|
|
113
|
+
const spring = oscillator(bus, 'triangle', pitch, body, at, at + length);
|
|
114
|
+
spring.frequency.setValueCurveAtTime(boingCurve(pitch, length, sound.settings.rate ?? 7), at, length);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
case 'roll': {
|
|
118
|
+
const length = Math.max(0.3, sound.dur);
|
|
119
|
+
for (const offset of rollStrikes(length)) {
|
|
120
|
+
const louder = { ...sound, vel: Math.min(127, sound.vel + Math.round((offset / length) * 30)) };
|
|
121
|
+
synthesizeFx(bus, 'tick', louder, { node: out, at: at + offset });
|
|
122
|
+
}
|
|
123
|
+
synthesizeFx(bus, 'ding', sound, { node: out, at: at + length });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
default: {
|
|
127
|
+
const never = name;
|
|
128
|
+
return never;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PatchPreset } from './presets.ts';
|
|
2
|
+
import type { Sound } from './voice.ts';
|
|
3
|
+
/** Where a score's sounds go: the dry send and the reverb send of one session, and the noise they share. */
|
|
4
|
+
export interface VoiceBus {
|
|
5
|
+
readonly context: BaseAudioContext;
|
|
6
|
+
readonly dry: AudioNode;
|
|
7
|
+
readonly reverb: AudioNode;
|
|
8
|
+
readonly noise: AudioBuffer;
|
|
9
|
+
/** Keeps a source so the session can stop it early. */
|
|
10
|
+
track(source: AudioScheduledSourceNode): void;
|
|
11
|
+
}
|
|
12
|
+
/** A sound's own input (its level and pan already set) and the context time it starts at. */
|
|
13
|
+
export interface VoiceOutput {
|
|
14
|
+
readonly node: GainNode;
|
|
15
|
+
readonly at: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function connectVoiceOutput(bus: VoiceBus, sound: Sound, preset: PatchPreset, at: number): VoiceOutput;
|
|
18
|
+
export declare function oscillator(bus: VoiceBus, type: OscillatorType, frequency: number, destination: AudioNode, at: number, stop: number, detune?: number): OscillatorNode;
|
|
19
|
+
/** A low-frequency oscillator modulating `target` by ±`depth` (a tremolo or vibrato), running from `at` to `stop`. */
|
|
20
|
+
export declare function modulator(bus: VoiceBus, frequency: number, depth: number, target: AudioParam, at: number, stop: number): void;
|
|
21
|
+
export declare function noise(bus: VoiceBus, destination: AudioNode, at: number, stop: number, offset?: number): AudioBufferSourceNode;
|
|
22
|
+
export declare function filter(context: BaseAudioContext, type: BiquadFilterType, frequency: number, resonance: number, destination: AudioNode): BiquadFilterNode;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { NOISE_SECONDS } from "./noise.js";
|
|
2
|
+
import { voiceGain } from "./voice.js";
|
|
3
|
+
/** Sources run a little past their envelope's end so the envelope, not the source, ends the sound. */
|
|
4
|
+
const SOURCE_OVERRUN = 0.02;
|
|
5
|
+
export function connectVoiceOutput(bus, sound, preset, at) {
|
|
6
|
+
const { context } = bus;
|
|
7
|
+
const gain = context.createGain();
|
|
8
|
+
gain.gain.value = voiceGain(sound, preset.gain);
|
|
9
|
+
const panner = context.createStereoPanner();
|
|
10
|
+
panner.pan.value = Math.max(-1, Math.min(1, sound.settings.pan ?? preset.pan));
|
|
11
|
+
gain.connect(panner);
|
|
12
|
+
panner.connect(bus.dry);
|
|
13
|
+
const reverb = Math.max(0, Math.min(1, sound.settings.rev ?? preset.rev));
|
|
14
|
+
if (reverb > 0.001) {
|
|
15
|
+
const send = context.createGain();
|
|
16
|
+
send.gain.value = reverb;
|
|
17
|
+
panner.connect(send);
|
|
18
|
+
send.connect(bus.reverb);
|
|
19
|
+
}
|
|
20
|
+
return { node: gain, at };
|
|
21
|
+
}
|
|
22
|
+
export function oscillator(bus, type, frequency, destination, at, stop, detune = 0) {
|
|
23
|
+
const node = bus.context.createOscillator();
|
|
24
|
+
node.type = type;
|
|
25
|
+
node.frequency.value = Math.min(frequency, bus.context.sampleRate * 0.45);
|
|
26
|
+
if (detune !== 0)
|
|
27
|
+
node.detune.value = detune;
|
|
28
|
+
node.connect(destination);
|
|
29
|
+
node.start(at);
|
|
30
|
+
node.stop(stop + SOURCE_OVERRUN);
|
|
31
|
+
bus.track(node);
|
|
32
|
+
return node;
|
|
33
|
+
}
|
|
34
|
+
/** A low-frequency oscillator modulating `target` by ±`depth` (a tremolo or vibrato), running from `at` to `stop`. */
|
|
35
|
+
export function modulator(bus, frequency, depth, target, at, stop) {
|
|
36
|
+
const node = bus.context.createOscillator();
|
|
37
|
+
node.frequency.value = frequency;
|
|
38
|
+
const amount = bus.context.createGain();
|
|
39
|
+
amount.gain.value = depth;
|
|
40
|
+
node.connect(amount);
|
|
41
|
+
amount.connect(target);
|
|
42
|
+
node.start(at);
|
|
43
|
+
node.stop(stop + SOURCE_OVERRUN);
|
|
44
|
+
bus.track(node);
|
|
45
|
+
}
|
|
46
|
+
export function noise(bus, destination, at, stop, offset = 0) {
|
|
47
|
+
const source = bus.context.createBufferSource();
|
|
48
|
+
source.buffer = bus.noise;
|
|
49
|
+
source.loop = true;
|
|
50
|
+
source.connect(destination);
|
|
51
|
+
source.start(at, offset % NOISE_SECONDS);
|
|
52
|
+
source.stop(stop + SOURCE_OVERRUN);
|
|
53
|
+
bus.track(source);
|
|
54
|
+
return source;
|
|
55
|
+
}
|
|
56
|
+
export function filter(context, type, frequency, resonance, destination) {
|
|
57
|
+
const node = context.createBiquadFilter();
|
|
58
|
+
node.type = type;
|
|
59
|
+
node.frequency.value = frequency;
|
|
60
|
+
node.Q.value = resonance;
|
|
61
|
+
node.connect(destination);
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** A small deterministic generator in [0, 1), so noise and reverb sound the same on every render. */
|
|
2
|
+
export declare function seededRandom(seed: number): () => number;
|
|
3
|
+
export declare const NOISE_SECONDS = 1.5;
|
|
4
|
+
export declare function noiseSamples(length: number): Float32Array<ArrayBuffer>;
|
|
5
|
+
/** A second and a half of white noise per context, looped by every noisy sound from its own offset. */
|
|
6
|
+
export declare function noiseBuffer(context: BaseAudioContext): AudioBuffer;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A small deterministic generator in [0, 1), so noise and reverb sound the same on every render. */
|
|
2
|
+
export function seededRandom(seed) {
|
|
3
|
+
let state = seed | 0;
|
|
4
|
+
return () => {
|
|
5
|
+
state = (state + 1831565813) | 0;
|
|
6
|
+
let mixed = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
7
|
+
mixed = (mixed + Math.imul(mixed ^ (mixed >>> 7), 61 | mixed)) ^ mixed;
|
|
8
|
+
return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export const NOISE_SECONDS = 1.5;
|
|
12
|
+
const NOISE_SEED = 12345;
|
|
13
|
+
export function noiseSamples(length) {
|
|
14
|
+
const random = seededRandom(NOISE_SEED);
|
|
15
|
+
const samples = new Float32Array(length);
|
|
16
|
+
for (let index = 0; index < length; index++)
|
|
17
|
+
samples[index] = random() * 2 - 1;
|
|
18
|
+
return samples;
|
|
19
|
+
}
|
|
20
|
+
const noiseBuffers = new WeakMap();
|
|
21
|
+
/** A second and a half of white noise per context, looped by every noisy sound from its own offset. */
|
|
22
|
+
export function noiseBuffer(context) {
|
|
23
|
+
const kept = noiseBuffers.get(context);
|
|
24
|
+
if (kept !== undefined)
|
|
25
|
+
return kept;
|
|
26
|
+
const length = Math.round(context.sampleRate * NOISE_SECONDS);
|
|
27
|
+
const buffer = context.createBuffer(1, length, context.sampleRate);
|
|
28
|
+
buffer.copyToChannel(noiseSamples(length), 0);
|
|
29
|
+
noiseBuffers.set(context, buffer);
|
|
30
|
+
return buffer;
|
|
31
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PatchName } from '@open-artifact/motion-core';
|
|
2
|
+
import type { VoiceBus, VoiceOutput } from './nodes.ts';
|
|
3
|
+
import type { Sound } from './voice.ts';
|
|
4
|
+
/** A pitched note: oscillators through envelopes and filters, tuned by `detune`, its overtones scaled by `bright`. */
|
|
5
|
+
export declare function synthesizePatch(bus: VoiceBus, name: PatchName, sound: Sound, output: VoiceOutput): void;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { adsrEnvelope, decayEnvelope } from "./envelope.js";
|
|
2
|
+
import { filter, modulator, oscillator } from "./nodes.js";
|
|
3
|
+
import { midiToFrequency } from "./voice.js";
|
|
4
|
+
/** A pitched note: oscillators through envelopes and filters, tuned by `detune`, its overtones scaled by `bright`. */
|
|
5
|
+
export function synthesizePatch(bus, name, sound, output) {
|
|
6
|
+
const { context } = bus;
|
|
7
|
+
const { at, node: out } = output;
|
|
8
|
+
const frequency = midiToFrequency(sound.midi) * Math.pow(2, (sound.settings.detune ?? 0) / 1200);
|
|
9
|
+
const bright = sound.settings.bright ?? 1;
|
|
10
|
+
// Low notes ring longer than high ones.
|
|
11
|
+
const ringScale = Math.max(0.5, Math.min(2, 440 / Math.max(60, frequency)));
|
|
12
|
+
switch (name) {
|
|
13
|
+
case 'marimba': {
|
|
14
|
+
const body = decayEnvelope(context, out, at, { peak: 1, attack: 0.003, decay: 0.18 * ringScale, stop: at + 1.2 });
|
|
15
|
+
oscillator(bus, 'sine', frequency, body, at, at + 1.2);
|
|
16
|
+
const overtone = decayEnvelope(context, out, at, { peak: 0.28 * bright, attack: 0.002, decay: 0.045, stop: at + 0.3 });
|
|
17
|
+
oscillator(bus, 'sine', frequency * 3.93, overtone, at, at + 0.3);
|
|
18
|
+
const strike = decayEnvelope(context, out, at, { peak: 0.08 * bright, attack: 0.001, decay: 0.015, stop: at + 0.1 });
|
|
19
|
+
oscillator(bus, 'sine', frequency * 9.9, strike, at, at + 0.1);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
case 'ep': {
|
|
23
|
+
const { gain: body, end } = adsrEnvelope(context, out, at, sound.dur, {
|
|
24
|
+
attack: 0.006,
|
|
25
|
+
decay: 0.9,
|
|
26
|
+
sustain: 0.45,
|
|
27
|
+
release: 0.35,
|
|
28
|
+
peak: 0.8,
|
|
29
|
+
});
|
|
30
|
+
const carrier = oscillator(bus, 'sine', frequency, body, at, end);
|
|
31
|
+
// Frequency modulation by the note itself, bright at the strike and mellowing over 0.4 s.
|
|
32
|
+
const fm = context.createOscillator();
|
|
33
|
+
fm.frequency.value = frequency;
|
|
34
|
+
const fmDepth = context.createGain();
|
|
35
|
+
fmDepth.gain.setValueAtTime(frequency * 2.2 * bright, at);
|
|
36
|
+
fmDepth.gain.setTargetAtTime(frequency * 0.5, at, 0.4);
|
|
37
|
+
fm.connect(fmDepth);
|
|
38
|
+
fmDepth.connect(carrier.frequency);
|
|
39
|
+
fm.start(at);
|
|
40
|
+
fm.stop(end + 0.02);
|
|
41
|
+
bus.track(fm);
|
|
42
|
+
const tine = decayEnvelope(context, out, at, { peak: 0.12 * bright, attack: 0.002, decay: 0.08, stop: at + 0.4 });
|
|
43
|
+
oscillator(bus, 'sine', frequency * 14, tine, at, at + 0.4);
|
|
44
|
+
modulator(bus, 4.8, 0.12, body.gain, at, end);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
case 'bass': {
|
|
48
|
+
const { gain: body, end } = adsrEnvelope(context, out, at, sound.dur, {
|
|
49
|
+
attack: 0.005,
|
|
50
|
+
decay: 0.35,
|
|
51
|
+
sustain: 0.5,
|
|
52
|
+
release: 0.09,
|
|
53
|
+
peak: 1,
|
|
54
|
+
});
|
|
55
|
+
const lowpass = filter(context, 'lowpass', 380, 1.2, body);
|
|
56
|
+
lowpass.frequency.setValueAtTime(900 * bright, at);
|
|
57
|
+
lowpass.frequency.setTargetAtTime(320, at, 0.12);
|
|
58
|
+
oscillator(bus, 'sawtooth', frequency, lowpass, at, end);
|
|
59
|
+
oscillator(bus, 'triangle', frequency, body, at, end);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
case 'pad': {
|
|
63
|
+
const { gain: body, end } = adsrEnvelope(context, out, at, sound.dur, {
|
|
64
|
+
attack: 0.45,
|
|
65
|
+
decay: 0.8,
|
|
66
|
+
sustain: 0.8,
|
|
67
|
+
release: 0.9,
|
|
68
|
+
peak: 0.5,
|
|
69
|
+
});
|
|
70
|
+
const lowpass = filter(context, 'lowpass', 1400 * bright, 0.4, body);
|
|
71
|
+
oscillator(bus, 'sawtooth', frequency, lowpass, at, end, -7);
|
|
72
|
+
oscillator(bus, 'sawtooth', frequency, lowpass, at, end, 7);
|
|
73
|
+
oscillator(bus, 'sine', frequency / 2, body, at, end);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
case 'vibes': {
|
|
77
|
+
const body = decayEnvelope(context, out, at, { peak: 0.9, attack: 0.004, decay: 0.7 * ringScale, stop: at + 2.4 });
|
|
78
|
+
oscillator(bus, 'sine', frequency, body, at, at + 2.4);
|
|
79
|
+
const overtone = decayEnvelope(context, out, at, { peak: 0.18 * bright, attack: 0.003, decay: 0.25, stop: at + 1 });
|
|
80
|
+
oscillator(bus, 'sine', frequency * 4, overtone, at, at + 1);
|
|
81
|
+
modulator(bus, 5.5, 0.25, body.gain, at, at + 2.4);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
case 'glock': {
|
|
85
|
+
const body = decayEnvelope(context, out, at, { peak: 0.7, attack: 0.001, decay: 0.35, stop: at + 1.8 });
|
|
86
|
+
oscillator(bus, 'sine', frequency, body, at, at + 1.8);
|
|
87
|
+
const second = decayEnvelope(context, out, at, { peak: 0.3 * bright, attack: 0.001, decay: 0.18, stop: at + 0.9 });
|
|
88
|
+
oscillator(bus, 'sine', frequency * 2.76, second, at, at + 0.9);
|
|
89
|
+
const third = decayEnvelope(context, out, at, { peak: 0.12 * bright, attack: 0.001, decay: 0.08, stop: at + 0.4 });
|
|
90
|
+
oscillator(bus, 'sine', frequency * 5.4, third, at, at + 0.4);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
case 'pluck': {
|
|
94
|
+
const body = decayEnvelope(context, out, at, { peak: 1, attack: 0.002, decay: 0.12 * ringScale, stop: at + 0.8 });
|
|
95
|
+
const lowpass = filter(context, 'lowpass', 2500 * bright, 0.7, body);
|
|
96
|
+
lowpass.frequency.setTargetAtTime(600, at, 0.08);
|
|
97
|
+
oscillator(bus, 'triangle', frequency, lowpass, at, at + 0.8);
|
|
98
|
+
oscillator(bus, 'square', frequency, lowpass, at, at + 0.8);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
case 'keys': {
|
|
102
|
+
const { gain: body, end } = adsrEnvelope(context, out, at, sound.dur, {
|
|
103
|
+
attack: 0.02,
|
|
104
|
+
decay: 0.3,
|
|
105
|
+
sustain: 0.6,
|
|
106
|
+
release: 0.25,
|
|
107
|
+
peak: 0.6,
|
|
108
|
+
});
|
|
109
|
+
const lowpass = filter(context, 'lowpass', 1800 * bright, 0.8, body);
|
|
110
|
+
oscillator(bus, 'square', frequency, lowpass, at, end, -4);
|
|
111
|
+
oscillator(bus, 'sawtooth', frequency, lowpass, at, end, 5);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
default: {
|
|
115
|
+
const never = name;
|
|
116
|
+
return never;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DrumName, FxName, PatchName, ScoreEventKind } from '@open-artifact/motion-core';
|
|
2
|
+
/** A sound's place in the mix: its level in dB, its pan (−1…1) and how much of it goes to the reverb (0…1). */
|
|
3
|
+
export interface PatchPreset {
|
|
4
|
+
readonly gain: number;
|
|
5
|
+
readonly pan: number;
|
|
6
|
+
readonly rev: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const PATCH_PRESETS: Readonly<Record<PatchName, PatchPreset>>;
|
|
9
|
+
export declare const PATCH_ALIASES: Readonly<Record<string, PatchName>>;
|
|
10
|
+
export declare const DRUM_PRESETS: Readonly<Record<DrumName, PatchPreset>>;
|
|
11
|
+
export declare const DRUM_ALIASES: Readonly<Record<string, DrumName>>;
|
|
12
|
+
export declare const FX_PRESETS: Readonly<Record<FxName, PatchPreset>>;
|
|
13
|
+
export declare const FX_ALIASES: Readonly<Record<string, FxName>>;
|
|
14
|
+
export type ResolvedPatch = {
|
|
15
|
+
readonly kind: 'note';
|
|
16
|
+
readonly name: PatchName;
|
|
17
|
+
readonly preset: PatchPreset;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: 'drum';
|
|
20
|
+
readonly name: DrumName;
|
|
21
|
+
readonly preset: PatchPreset;
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: 'fx';
|
|
24
|
+
readonly name: FxName;
|
|
25
|
+
readonly preset: PatchPreset;
|
|
26
|
+
};
|
|
27
|
+
/** A pitched patch nobody knows plays as a marimba; an unknown drum or effect is silent (null). */
|
|
28
|
+
export declare function resolvePatch(kind: ScoreEventKind, patch: string): ResolvedPatch | null;
|
|
29
|
+
/** How long a sound rings after its written length, so a seek into its tail still hears it. */
|
|
30
|
+
export declare function ringSeconds(patch: ResolvedPatch): number;
|
|
31
|
+
/** The span a sound is heard over, given its onset and length: a reverse swell is heard before its onset. */
|
|
32
|
+
export declare function soundExtent(patch: ResolvedPatch, at: number, dur: number): [number, number];
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { DRUM_NAMES, FX_NAMES, PATCH_NAMES } from '@open-artifact/motion-core';
|
|
2
|
+
export const PATCH_PRESETS = {
|
|
3
|
+
marimba: { pan: -0.1, rev: 0.35, gain: 0 },
|
|
4
|
+
ep: { pan: 0.25, rev: 0.45, gain: -2 },
|
|
5
|
+
bass: { pan: 0, rev: 0.05, gain: -3 },
|
|
6
|
+
pad: { pan: 0, rev: 0.6, gain: -6 },
|
|
7
|
+
vibes: { pan: -0.3, rev: 0.55, gain: -2 },
|
|
8
|
+
glock: { pan: 0.4, rev: 0.55, gain: -6 },
|
|
9
|
+
pluck: { pan: -0.15, rev: 0.35, gain: -1 },
|
|
10
|
+
keys: { pan: 0.1, rev: 0.3, gain: -5 },
|
|
11
|
+
};
|
|
12
|
+
export const PATCH_ALIASES = {
|
|
13
|
+
rhodes: 'ep',
|
|
14
|
+
piano: 'ep',
|
|
15
|
+
kalimba: 'pluck',
|
|
16
|
+
harp: 'pluck',
|
|
17
|
+
sub: 'bass',
|
|
18
|
+
lead: 'keys',
|
|
19
|
+
synth: 'keys',
|
|
20
|
+
bell: 'glock',
|
|
21
|
+
bells: 'glock',
|
|
22
|
+
celesta: 'glock',
|
|
23
|
+
vibraphone: 'vibes',
|
|
24
|
+
mallet: 'marimba',
|
|
25
|
+
xylophone: 'marimba',
|
|
26
|
+
strings: 'pad',
|
|
27
|
+
};
|
|
28
|
+
export const DRUM_PRESETS = {
|
|
29
|
+
kick: { pan: 0, rev: 0.05, gain: 0 },
|
|
30
|
+
snare: { pan: 0.05, rev: 0.25, gain: -2 },
|
|
31
|
+
clap: { pan: -0.1, rev: 0.3, gain: -3 },
|
|
32
|
+
hat: { pan: 0.3, rev: 0.1, gain: -8 },
|
|
33
|
+
openhat: { pan: 0.3, rev: 0.15, gain: -8 },
|
|
34
|
+
shaker: { pan: -0.35, rev: 0.1, gain: -10 },
|
|
35
|
+
sidestick: { pan: 0.1, rev: 0.25, gain: -4 },
|
|
36
|
+
woodblock: { pan: -0.2, rev: 0.2, gain: -4 },
|
|
37
|
+
tamb: { pan: 0.4, rev: 0.2, gain: -8 },
|
|
38
|
+
tom: { pan: -0.25, rev: 0.25, gain: -2 },
|
|
39
|
+
crash: { pan: 0.2, rev: 0.4, gain: -8 },
|
|
40
|
+
ride: { pan: 0.35, rev: 0.3, gain: -9 },
|
|
41
|
+
cowbell: { pan: -0.3, rev: 0.2, gain: -8 },
|
|
42
|
+
};
|
|
43
|
+
export const DRUM_ALIASES = {
|
|
44
|
+
bd: 'kick',
|
|
45
|
+
sd: 'snare',
|
|
46
|
+
hh: 'hat',
|
|
47
|
+
hihat: 'hat',
|
|
48
|
+
ohh: 'openhat',
|
|
49
|
+
stick: 'sidestick',
|
|
50
|
+
rim: 'sidestick',
|
|
51
|
+
wb: 'woodblock',
|
|
52
|
+
block: 'woodblock',
|
|
53
|
+
tambourine: 'tamb',
|
|
54
|
+
cymbal: 'crash',
|
|
55
|
+
shake: 'shaker',
|
|
56
|
+
};
|
|
57
|
+
export const FX_PRESETS = {
|
|
58
|
+
tick: { pan: 0, rev: 0.12, gain: 2 },
|
|
59
|
+
click: { pan: 0, rev: 0.18, gain: 0 },
|
|
60
|
+
doubleclick: { pan: 0, rev: 0.18, gain: 0 },
|
|
61
|
+
pop: { pan: 0, rev: 0.25, gain: -4 },
|
|
62
|
+
ding: { pan: 0.15, rev: 0.5, gain: -5 },
|
|
63
|
+
chime: { pan: 0.15, rev: 0.55, gain: -6 },
|
|
64
|
+
whoosh: { pan: 0, rev: 0.3, gain: -8 },
|
|
65
|
+
boing: { pan: 0, rev: 0.3, gain: -4 },
|
|
66
|
+
revswell: { pan: 0, rev: 0.45, gain: -8 },
|
|
67
|
+
roll: { pan: 0, rev: 0.2, gain: -6 },
|
|
68
|
+
thud: { pan: 0, rev: 0.2, gain: -2 },
|
|
69
|
+
sparkle: { pan: 0.2, rev: 0.6, gain: -8 },
|
|
70
|
+
};
|
|
71
|
+
export const FX_ALIASES = {
|
|
72
|
+
keytick: 'tick',
|
|
73
|
+
key: 'tick',
|
|
74
|
+
tap: 'click',
|
|
75
|
+
swoosh: 'whoosh',
|
|
76
|
+
swish: 'whoosh',
|
|
77
|
+
swell: 'revswell',
|
|
78
|
+
rise: 'revswell',
|
|
79
|
+
bell: 'ding',
|
|
80
|
+
save: 'ding',
|
|
81
|
+
success: 'chime',
|
|
82
|
+
bloop: 'pop',
|
|
83
|
+
bubble: 'pop',
|
|
84
|
+
drop: 'thud',
|
|
85
|
+
land: 'thud',
|
|
86
|
+
twinkle: 'sparkle',
|
|
87
|
+
};
|
|
88
|
+
function lookUp(names, aliases, wanted) {
|
|
89
|
+
const own = names.find((name) => name === wanted);
|
|
90
|
+
if (own !== undefined)
|
|
91
|
+
return own;
|
|
92
|
+
return Object.hasOwn(aliases, wanted) ? aliases[wanted] : undefined;
|
|
93
|
+
}
|
|
94
|
+
/** A pitched patch nobody knows plays as a marimba; an unknown drum or effect is silent (null). */
|
|
95
|
+
export function resolvePatch(kind, patch) {
|
|
96
|
+
const wanted = String(patch).toLowerCase();
|
|
97
|
+
switch (kind) {
|
|
98
|
+
case 'note': {
|
|
99
|
+
const name = lookUp(PATCH_NAMES, PATCH_ALIASES, wanted) ?? 'marimba';
|
|
100
|
+
return { kind, name, preset: PATCH_PRESETS[name] };
|
|
101
|
+
}
|
|
102
|
+
case 'drum': {
|
|
103
|
+
const name = lookUp(DRUM_NAMES, DRUM_ALIASES, wanted);
|
|
104
|
+
return name === undefined ? null : { kind, name, preset: DRUM_PRESETS[name] };
|
|
105
|
+
}
|
|
106
|
+
case 'fx': {
|
|
107
|
+
const name = lookUp(FX_NAMES, FX_ALIASES, wanted);
|
|
108
|
+
return name === undefined ? null : { kind, name, preset: FX_PRESETS[name] };
|
|
109
|
+
}
|
|
110
|
+
default: {
|
|
111
|
+
const never = kind;
|
|
112
|
+
return never;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** How long a sound rings after its written length, so a seek into its tail still hears it. */
|
|
117
|
+
export function ringSeconds(patch) {
|
|
118
|
+
switch (patch.kind) {
|
|
119
|
+
case 'note':
|
|
120
|
+
return patch.name === 'pad' ? 1.2 : patch.name === 'vibes' || patch.name === 'glock' ? 1.6 : 0.6;
|
|
121
|
+
case 'fx':
|
|
122
|
+
return 1.2;
|
|
123
|
+
case 'drum':
|
|
124
|
+
return patch.name === 'crash' ? 1.8 : 0.5;
|
|
125
|
+
default: {
|
|
126
|
+
const never = patch;
|
|
127
|
+
return never;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The span a sound is heard over, given its onset and length: a reverse swell is heard before its onset. */
|
|
132
|
+
export function soundExtent(patch, at, dur) {
|
|
133
|
+
if (patch.kind === 'fx' && patch.name === 'revswell')
|
|
134
|
+
return [at - dur, at + 0.4];
|
|
135
|
+
return [at, at + dur + ringSeconds(patch)];
|
|
136
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { VoiceBus } from './nodes.ts';
|
|
2
|
+
import type { ResolvedPatch } from './presets.ts';
|
|
3
|
+
import type { Sound } from './voice.ts';
|
|
4
|
+
/** Builds a sound's nodes on the bus, starting at context time `at`. False when `at` is not a time. */
|
|
5
|
+
export declare function scheduleSound(bus: VoiceBus, patch: ResolvedPatch, sound: Sound, at: number): boolean;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { synthesizeDrum } from "./drums.js";
|
|
2
|
+
import { synthesizeFx } from "./fx.js";
|
|
3
|
+
import { connectVoiceOutput } from "./nodes.js";
|
|
4
|
+
import { synthesizePatch } from "./patches.js";
|
|
5
|
+
/** Builds a sound's nodes on the bus, starting at context time `at`. False when `at` is not a time. */
|
|
6
|
+
export function scheduleSound(bus, patch, sound, at) {
|
|
7
|
+
if (!Number.isFinite(at))
|
|
8
|
+
return false;
|
|
9
|
+
const output = connectVoiceOutput(bus, sound, patch.preset, Math.max(0, at));
|
|
10
|
+
switch (patch.kind) {
|
|
11
|
+
case 'note':
|
|
12
|
+
synthesizePatch(bus, patch.name, sound, output);
|
|
13
|
+
return true;
|
|
14
|
+
case 'drum':
|
|
15
|
+
synthesizeDrum(bus, patch.name, output);
|
|
16
|
+
return true;
|
|
17
|
+
case 'fx':
|
|
18
|
+
synthesizeFx(bus, patch.name, sound, output);
|
|
19
|
+
return true;
|
|
20
|
+
default: {
|
|
21
|
+
const never = patch;
|
|
22
|
+
return never;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|