@open-artifact/motion-audio 0.0.0-stage → 0.12.0-rc.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 +38 -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
package/src/player.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { filmSeconds, playSeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import { createAudioKeepalive } from "./keepalive.js";
|
|
3
|
+
import { createMasterBus } from "./mixer.js";
|
|
4
|
+
import { filmSound, knownScoreGain, measureScoreGain } from "./offline.js";
|
|
5
|
+
import { playScore } from "./played-score.js";
|
|
6
|
+
import { audioClockSource, createVisibleClock, wallClockSource } from "./playback-clock.js";
|
|
7
|
+
import { startScorePass } from "./scheduler.js";
|
|
8
|
+
import { identityTimeline } from "./timeline.js";
|
|
9
|
+
const VOLUME_SMOOTHING_SECONDS = 0.015;
|
|
10
|
+
const NORMALIZATION_SMOOTHING_SECONDS = 0.15;
|
|
11
|
+
const LEAST_RATE = 0.1;
|
|
12
|
+
const MOST_RATE = 16;
|
|
13
|
+
function pageAudioContext() {
|
|
14
|
+
if (typeof AudioContext === 'undefined')
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
return new AudioContext({ latencyHint: 'interactive', sampleRate: 48000 });
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
try {
|
|
21
|
+
return new AudioContext();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The film's sound in the editor: the score synthesized live and the narration over it, in step with the picture.
|
|
30
|
+
* Starts muted. The page reads `now()` every frame while playing, so the picture follows the sound.
|
|
31
|
+
*/
|
|
32
|
+
export class FilmSoundPlayer {
|
|
33
|
+
#createContext;
|
|
34
|
+
#timers;
|
|
35
|
+
#clock;
|
|
36
|
+
#listeners = new Set();
|
|
37
|
+
#context = null;
|
|
38
|
+
#master = null;
|
|
39
|
+
#keepalive = null;
|
|
40
|
+
#sound = { score: playScore(null, identityTimeline(filmSeconds(0))), speech: null };
|
|
41
|
+
#pass = null;
|
|
42
|
+
#source = null;
|
|
43
|
+
#position = playSeconds(0);
|
|
44
|
+
#pendingGains = new WeakSet();
|
|
45
|
+
#state = { available: false, playing: false, muted: true, volume: 1, rate: 1, interrupted: false };
|
|
46
|
+
constructor(options = {}) {
|
|
47
|
+
this.#createContext = options.createContext ?? pageAudioContext;
|
|
48
|
+
this.#timers = options.timers;
|
|
49
|
+
this.#clock = options.clock ?? createVisibleClock();
|
|
50
|
+
}
|
|
51
|
+
get state() {
|
|
52
|
+
return this.#state;
|
|
53
|
+
}
|
|
54
|
+
/** The film's sound as last loaded, in play time: what an export renders. */
|
|
55
|
+
get sound() {
|
|
56
|
+
return this.#sound;
|
|
57
|
+
}
|
|
58
|
+
subscribe(listener) {
|
|
59
|
+
this.#listeners.add(listener);
|
|
60
|
+
return () => this.#listeners.delete(listener);
|
|
61
|
+
}
|
|
62
|
+
/** Takes a new score, narration or timeline; playing on from where the playhead is. */
|
|
63
|
+
load(input) {
|
|
64
|
+
this.#sound = filmSound(input);
|
|
65
|
+
this.#prepareGain(this.#sound.score);
|
|
66
|
+
this.#applyGain(false);
|
|
67
|
+
if (this.#state.playing)
|
|
68
|
+
this.#restart(this.now());
|
|
69
|
+
this.#update({});
|
|
70
|
+
}
|
|
71
|
+
/** Makes or wakes the audio context; call from a click or key press, which browsers require for sound. */
|
|
72
|
+
prime() {
|
|
73
|
+
const hadContext = this.#context !== null;
|
|
74
|
+
const context = this.#ensureContext();
|
|
75
|
+
if (context === null)
|
|
76
|
+
return;
|
|
77
|
+
this.#resume(context);
|
|
78
|
+
// Playing on the visible clock until now: the sound joins where the picture is.
|
|
79
|
+
if (!hadContext && this.#state.playing)
|
|
80
|
+
this.#restart(this.now());
|
|
81
|
+
}
|
|
82
|
+
play(from) {
|
|
83
|
+
this.#restart(from);
|
|
84
|
+
this.#update({ playing: true, interrupted: false });
|
|
85
|
+
}
|
|
86
|
+
pause() {
|
|
87
|
+
this.#position = this.now();
|
|
88
|
+
this.#stopPass();
|
|
89
|
+
this.#update({ playing: false });
|
|
90
|
+
}
|
|
91
|
+
seek(to) {
|
|
92
|
+
this.#position = to;
|
|
93
|
+
if (this.#state.playing)
|
|
94
|
+
this.#restart(to);
|
|
95
|
+
}
|
|
96
|
+
setRate(rate) {
|
|
97
|
+
if (!Number.isFinite(rate))
|
|
98
|
+
return;
|
|
99
|
+
const bounded = Math.max(LEAST_RATE, Math.min(MOST_RATE, rate));
|
|
100
|
+
if (bounded === this.#state.rate)
|
|
101
|
+
return;
|
|
102
|
+
const at = this.now();
|
|
103
|
+
this.#update({ rate: bounded });
|
|
104
|
+
if (this.#state.playing)
|
|
105
|
+
this.#restart(at);
|
|
106
|
+
}
|
|
107
|
+
setMuted(muted) {
|
|
108
|
+
this.#update({ muted });
|
|
109
|
+
if (!muted)
|
|
110
|
+
this.prime();
|
|
111
|
+
this.#applyVolume();
|
|
112
|
+
}
|
|
113
|
+
setVolume(volume) {
|
|
114
|
+
this.#update({ volume: Math.max(0, Math.min(1, Number.isFinite(volume) ? volume : 1)) });
|
|
115
|
+
this.#applyVolume();
|
|
116
|
+
}
|
|
117
|
+
/** Where the playhead is: from the audio context while it moves, else from the visible clock. */
|
|
118
|
+
now() {
|
|
119
|
+
if (!this.#state.playing || this.#source === null)
|
|
120
|
+
return this.#position;
|
|
121
|
+
const at = this.#source.now();
|
|
122
|
+
if (this.#source.interrupted() && !this.#state.interrupted) {
|
|
123
|
+
this.#position = at;
|
|
124
|
+
this.#stopPass();
|
|
125
|
+
this.#update({ playing: false, interrupted: true });
|
|
126
|
+
}
|
|
127
|
+
return at;
|
|
128
|
+
}
|
|
129
|
+
dispose() {
|
|
130
|
+
this.#stopPass();
|
|
131
|
+
this.#keepalive?.dispose();
|
|
132
|
+
this.#keepalive = null;
|
|
133
|
+
this.#clock.dispose();
|
|
134
|
+
this.#context?.close().catch(() => undefined);
|
|
135
|
+
this.#context = null;
|
|
136
|
+
this.#master = null;
|
|
137
|
+
this.#listeners.clear();
|
|
138
|
+
}
|
|
139
|
+
#update(change) {
|
|
140
|
+
const available = this.#context !== null && this.#master !== null && this.#context.state !== 'closed';
|
|
141
|
+
const next = { ...this.#state, ...change, available };
|
|
142
|
+
const changed = Object.keys(next).some((key) => next[key] !== this.#state[key]);
|
|
143
|
+
this.#state = next;
|
|
144
|
+
this.#keepalive?.set(this.#audible());
|
|
145
|
+
if (changed)
|
|
146
|
+
for (const listener of this.#listeners)
|
|
147
|
+
listener(next);
|
|
148
|
+
}
|
|
149
|
+
#audible() {
|
|
150
|
+
const { playing, muted, volume } = this.#state;
|
|
151
|
+
return playing && !muted && volume > 0 && (this.#sound.score.sounds.length > 0 || this.#sound.speech !== null);
|
|
152
|
+
}
|
|
153
|
+
#ensureContext() {
|
|
154
|
+
if (this.#context !== null)
|
|
155
|
+
return this.#context;
|
|
156
|
+
const context = this.#createContext();
|
|
157
|
+
if (context === null)
|
|
158
|
+
return null;
|
|
159
|
+
this.#context = context;
|
|
160
|
+
this.#master = createMasterBus(context);
|
|
161
|
+
this.#master.output.gain.value = this.#state.muted ? 0 : this.#state.volume;
|
|
162
|
+
this.#keepalive = createAudioKeepalive(context);
|
|
163
|
+
this.#applyGain(false);
|
|
164
|
+
this.#update({});
|
|
165
|
+
return context;
|
|
166
|
+
}
|
|
167
|
+
#resume(context) {
|
|
168
|
+
if (context.state !== 'running' && context.state !== 'closed')
|
|
169
|
+
context.resume().catch(() => undefined);
|
|
170
|
+
}
|
|
171
|
+
#applyVolume() {
|
|
172
|
+
if (this.#master === null || this.#context === null)
|
|
173
|
+
return;
|
|
174
|
+
const { muted, volume } = this.#state;
|
|
175
|
+
this.#master.output.gain.setTargetAtTime(muted ? 0 : volume, this.#context.currentTime, VOLUME_SMOOTHING_SECONDS);
|
|
176
|
+
}
|
|
177
|
+
#prepareGain(score) {
|
|
178
|
+
if (knownScoreGain(score) !== undefined || this.#pendingGains.has(score) || typeof OfflineAudioContext === 'undefined')
|
|
179
|
+
return;
|
|
180
|
+
this.#pendingGains.add(score);
|
|
181
|
+
measureScoreGain(score)
|
|
182
|
+
.catch(() => 1)
|
|
183
|
+
.then(() => {
|
|
184
|
+
this.#pendingGains.delete(score);
|
|
185
|
+
if (this.#sound.score === score)
|
|
186
|
+
this.#applyGain(true);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
#applyGain(smoothly) {
|
|
190
|
+
if (this.#master === null || this.#context === null)
|
|
191
|
+
return;
|
|
192
|
+
const gain = knownScoreGain(this.#sound.score) ?? 1;
|
|
193
|
+
if (smoothly)
|
|
194
|
+
this.#master.normalization.gain.setTargetAtTime(gain, this.#context.currentTime, NORMALIZATION_SMOOTHING_SECONDS);
|
|
195
|
+
else
|
|
196
|
+
this.#master.normalization.gain.value = gain;
|
|
197
|
+
}
|
|
198
|
+
#restart(from) {
|
|
199
|
+
this.#stopPass();
|
|
200
|
+
this.#position = from;
|
|
201
|
+
const { rate } = this.#state;
|
|
202
|
+
const context = this.#context;
|
|
203
|
+
const master = this.#master;
|
|
204
|
+
if (context === null || master === null || context.state === 'closed') {
|
|
205
|
+
this.#source = wallClockSource(from, rate, this.#clock);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
this.#resume(context);
|
|
209
|
+
this.#applyGain(false);
|
|
210
|
+
const pass = this.#startPass(master, from);
|
|
211
|
+
this.#source = audioClockSource({
|
|
212
|
+
context,
|
|
213
|
+
clock: this.#clock,
|
|
214
|
+
from,
|
|
215
|
+
rate,
|
|
216
|
+
origin: () => this.#pass?.session.origin ?? pass.session.origin,
|
|
217
|
+
audible: () => this.#audible(),
|
|
218
|
+
onStall: () => this.#pass?.stop(),
|
|
219
|
+
onResume: (at) => {
|
|
220
|
+
if (this.#pass === null || this.#state.rate !== rate)
|
|
221
|
+
return;
|
|
222
|
+
this.#pass.stop();
|
|
223
|
+
this.#pass = this.#startPass(master, at);
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
#startPass(master, from) {
|
|
228
|
+
const pass = startScorePass({ master, sound: this.#sound, from, rate: this.#state.rate, timers: this.#timers });
|
|
229
|
+
this.#pass = pass;
|
|
230
|
+
return pass;
|
|
231
|
+
}
|
|
232
|
+
#stopPass() {
|
|
233
|
+
this.#pass?.stop();
|
|
234
|
+
this.#pass = null;
|
|
235
|
+
this.#source = null;
|
|
236
|
+
}
|
|
237
|
+
}
|
package/src/reverb.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const REVERB_SECONDS = 1.9;
|
|
2
|
+
/** The reverb return's level into the mix. */
|
|
3
|
+
export declare const REVERB_RETURN = 0.5;
|
|
4
|
+
/** A room's impulse: seeded noise decaying on a 2.6 power curve, one channel after the other from the same generator. */
|
|
5
|
+
export declare function reverbImpulseSamples(sampleRate: number, seconds?: number): [Float32Array<ArrayBuffer>, Float32Array<ArrayBuffer>];
|
|
6
|
+
/** The reverb send: a convolver on the impulse above, at the return level. Returns its input and its output. */
|
|
7
|
+
export declare function createReverb(context: BaseAudioContext): {
|
|
8
|
+
input: GainNode;
|
|
9
|
+
output: GainNode;
|
|
10
|
+
};
|
package/src/reverb.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { seededRandom } from "./synth/noise.js";
|
|
2
|
+
export const REVERB_SECONDS = 1.9;
|
|
3
|
+
/** The reverb return's level into the mix. */
|
|
4
|
+
export const REVERB_RETURN = 0.5;
|
|
5
|
+
const REVERB_SEED = 777;
|
|
6
|
+
/** Samples over which the impulse fades in, so it starts without a click. */
|
|
7
|
+
const FADE_IN_SAMPLES = 200;
|
|
8
|
+
/** A room's impulse: seeded noise decaying on a 2.6 power curve, one channel after the other from the same generator. */
|
|
9
|
+
export function reverbImpulseSamples(sampleRate, seconds = REVERB_SECONDS) {
|
|
10
|
+
const random = seededRandom(REVERB_SEED);
|
|
11
|
+
const length = Math.round(sampleRate * seconds);
|
|
12
|
+
const channel = () => {
|
|
13
|
+
const samples = new Float32Array(length);
|
|
14
|
+
for (let index = 0; index < length; index++) {
|
|
15
|
+
const progress = index / length;
|
|
16
|
+
samples[index] = (random() * 2 - 1) * Math.pow(1 - progress, 2.6) * (index < FADE_IN_SAMPLES ? index / FADE_IN_SAMPLES : 1);
|
|
17
|
+
}
|
|
18
|
+
return samples;
|
|
19
|
+
};
|
|
20
|
+
const left = channel();
|
|
21
|
+
return [left, channel()];
|
|
22
|
+
}
|
|
23
|
+
/** The reverb send: a convolver on the impulse above, at the return level. Returns its input and its output. */
|
|
24
|
+
export function createReverb(context) {
|
|
25
|
+
const input = context.createGain();
|
|
26
|
+
const convolver = context.createConvolver();
|
|
27
|
+
convolver.normalize = true;
|
|
28
|
+
const [left, right] = reverbImpulseSamples(context.sampleRate);
|
|
29
|
+
const impulse = context.createBuffer(2, left.length, context.sampleRate);
|
|
30
|
+
impulse.copyToChannel(left, 0);
|
|
31
|
+
impulse.copyToChannel(right, 1);
|
|
32
|
+
convolver.buffer = impulse;
|
|
33
|
+
const output = context.createGain();
|
|
34
|
+
output.gain.value = REVERB_RETURN;
|
|
35
|
+
input.connect(convolver);
|
|
36
|
+
convolver.connect(output);
|
|
37
|
+
return { input, output };
|
|
38
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PlaySeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import type { MasterBus } from './mixer.ts';
|
|
3
|
+
import type { FilmSound } from './offline.ts';
|
|
4
|
+
import type { ScoreSession } from './session.ts';
|
|
5
|
+
/** The timer the scheduler looks ahead on; the page's own by default. */
|
|
6
|
+
export interface SchedulerTimers {
|
|
7
|
+
setInterval(callback: () => void, ms: number): number;
|
|
8
|
+
clearInterval(id: number): void;
|
|
9
|
+
}
|
|
10
|
+
export interface ScorePassOptions {
|
|
11
|
+
readonly master: MasterBus;
|
|
12
|
+
readonly sound: FilmSound;
|
|
13
|
+
readonly from: PlaySeconds;
|
|
14
|
+
readonly rate: number;
|
|
15
|
+
readonly timers?: SchedulerTimers;
|
|
16
|
+
}
|
|
17
|
+
/** One stretch of live playback from `from`: sounds are handed to the context a little ahead of the playhead. */
|
|
18
|
+
export interface ScorePass {
|
|
19
|
+
readonly session: ScoreSession;
|
|
20
|
+
readonly from: PlaySeconds;
|
|
21
|
+
/** Every sound has been handed over. */
|
|
22
|
+
done(): boolean;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Starts playback from `from`: the narration and the duck at once, the score's sounds a lookahead at a time, every
|
|
27
|
+
* scheduler interval. A seek, a pause, a new rate or a new timeline stops the pass and starts another.
|
|
28
|
+
*/
|
|
29
|
+
export declare function startScorePass(options: ScorePassOptions): ScorePass;
|
package/src/scheduler.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createScoreSession } from "./session.js";
|
|
2
|
+
import { scheduleSound } from "./synth/schedule.js";
|
|
3
|
+
import { SCHEDULER_INTERVAL_MS, dueSounds, firstHeard, lookaheadHorizon, passOrigin } from "./window.js";
|
|
4
|
+
const pageTimers = {
|
|
5
|
+
setInterval: (callback, ms) => Number(globalThis.setInterval(callback, ms)),
|
|
6
|
+
clearInterval: (id) => globalThis.clearInterval(id),
|
|
7
|
+
};
|
|
8
|
+
/** Sounds hold their length in play time; at another rate they are heard over that length divided by the rate. */
|
|
9
|
+
function atRate(heard, rate) {
|
|
10
|
+
return rate === 1 ? heard.sound : { ...heard.sound, dur: heard.sound.dur / rate };
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Starts playback from `from`: the narration and the duck at once, the score's sounds a lookahead at a time, every
|
|
14
|
+
* scheduler interval. A seek, a pause, a new rate or a new timeline stops the pass and starts another.
|
|
15
|
+
*/
|
|
16
|
+
export function startScorePass(options) {
|
|
17
|
+
const { master, sound, from, rate } = options;
|
|
18
|
+
const timers = options.timers ?? pageTimers;
|
|
19
|
+
const { context } = master;
|
|
20
|
+
const session = createScoreSession(master, {
|
|
21
|
+
master: sound.score.master,
|
|
22
|
+
speech: sound.speech,
|
|
23
|
+
duration: sound.score.duration,
|
|
24
|
+
origin: passOrigin(context.currentTime, from, rate),
|
|
25
|
+
rate,
|
|
26
|
+
});
|
|
27
|
+
session.speak(from);
|
|
28
|
+
const { sounds } = sound.score;
|
|
29
|
+
let index = firstHeard(sounds, from);
|
|
30
|
+
let timer = null;
|
|
31
|
+
let stopped = false;
|
|
32
|
+
const look = () => {
|
|
33
|
+
if (stopped)
|
|
34
|
+
return;
|
|
35
|
+
const horizon = lookaheadHorizon(context.currentTime, session.origin, rate);
|
|
36
|
+
const { due, next } = dueSounds(sounds, index, from, horizon);
|
|
37
|
+
for (const at of due) {
|
|
38
|
+
const heard = sounds[at];
|
|
39
|
+
scheduleSound(session.bus, heard.patch, atRate(heard, rate), session.contextTime(heard.at));
|
|
40
|
+
}
|
|
41
|
+
index = next;
|
|
42
|
+
if (index >= sounds.length && timer !== null) {
|
|
43
|
+
timers.clearInterval(timer);
|
|
44
|
+
timer = null;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
look();
|
|
48
|
+
if (index < sounds.length)
|
|
49
|
+
timer = timers.setInterval(look, SCHEDULER_INTERVAL_MS);
|
|
50
|
+
return {
|
|
51
|
+
session,
|
|
52
|
+
from,
|
|
53
|
+
done: () => index >= sounds.length,
|
|
54
|
+
stop() {
|
|
55
|
+
if (stopped)
|
|
56
|
+
return;
|
|
57
|
+
stopped = true;
|
|
58
|
+
if (timer !== null)
|
|
59
|
+
timers.clearInterval(timer);
|
|
60
|
+
timer = null;
|
|
61
|
+
session.stop();
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
package/src/session.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PlaySeconds, ScoreMaster } from '@open-artifact/motion-core';
|
|
2
|
+
import type { MasterBus } from './mixer.ts';
|
|
3
|
+
import type { Speech } from './narration/speech.ts';
|
|
4
|
+
import type { VoiceBus } from './synth/nodes.ts';
|
|
5
|
+
/** One pass of playback on a master bus: the score's sends and the narration, from one origin at one rate. */
|
|
6
|
+
export interface ScoreSession {
|
|
7
|
+
readonly bus: VoiceBus;
|
|
8
|
+
/** The context time play time 0 stands at. */
|
|
9
|
+
readonly origin: number;
|
|
10
|
+
readonly rate: number;
|
|
11
|
+
contextTime(play: number): number;
|
|
12
|
+
/** Starts the narration's takes and the score's duck from play time `from`. */
|
|
13
|
+
speak(from: PlaySeconds): void;
|
|
14
|
+
/** Fades everything out over a few milliseconds and stops every source. */
|
|
15
|
+
stop(): void;
|
|
16
|
+
}
|
|
17
|
+
export interface SessionOptions {
|
|
18
|
+
readonly master: ScoreMaster;
|
|
19
|
+
readonly speech: Speech | null;
|
|
20
|
+
readonly duration: PlaySeconds;
|
|
21
|
+
readonly origin: number;
|
|
22
|
+
readonly rate: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function createScoreSession(masterBus: MasterBus, options: SessionOptions): ScoreSession;
|
package/src/session.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { fadeLevel, masterGain } from "./mixer.js";
|
|
2
|
+
import { sampleEnvelope } from "./narration/duck.js";
|
|
3
|
+
import { noiseBuffer } from "./synth/noise.js";
|
|
4
|
+
import { contextTimeOf, playTimeAt } from "./window.js";
|
|
5
|
+
const SPEECH_FADE_SECONDS = 0.02;
|
|
6
|
+
const SPEECH_TAIL_SECONDS = 0.03;
|
|
7
|
+
const STOP_FADE_SECONDS = 0.03;
|
|
8
|
+
const DUCK_RECOVERY_SECONDS = 0.5;
|
|
9
|
+
const DISCONNECT_DELAY_MS = 120;
|
|
10
|
+
export function createScoreSession(masterBus, options) {
|
|
11
|
+
const { context } = masterBus;
|
|
12
|
+
const { origin, rate, duration, speech } = options;
|
|
13
|
+
const contextTime = (play) => contextTimeOf(play, origin, rate);
|
|
14
|
+
const level = masterGain(options.master.gain);
|
|
15
|
+
const fadeOut = Math.max(0, Math.min(duration, options.master.fadeOut));
|
|
16
|
+
const endTime = contextTime(duration);
|
|
17
|
+
const dry = context.createGain();
|
|
18
|
+
dry.gain.value = level;
|
|
19
|
+
if (duration > 0 && fadeOut > 0) {
|
|
20
|
+
const now = playTimeAt(context.currentTime, origin, rate);
|
|
21
|
+
const fadeStart = duration - fadeOut;
|
|
22
|
+
if (now <= fadeStart) {
|
|
23
|
+
dry.gain.setValueAtTime(level, contextTime(fadeStart));
|
|
24
|
+
dry.gain.linearRampToValueAtTime(0, endTime);
|
|
25
|
+
}
|
|
26
|
+
else if (now < duration) {
|
|
27
|
+
dry.gain.setValueAtTime(fadeLevel(level, fadeOut, duration, now), context.currentTime);
|
|
28
|
+
dry.gain.linearRampToValueAtTime(0, endTime);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
dry.gain.value = 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
dry.connect(masterBus.input);
|
|
35
|
+
const reverb = context.createGain();
|
|
36
|
+
reverb.gain.value = level;
|
|
37
|
+
reverb.connect(masterBus.reverbInput);
|
|
38
|
+
if (duration > 0 && fadeOut > 0) {
|
|
39
|
+
reverb.gain.setValueAtTime(level, Math.max(context.currentTime, contextTime(duration - fadeOut)));
|
|
40
|
+
reverb.gain.linearRampToValueAtTime(0, Math.max(context.currentTime, endTime));
|
|
41
|
+
}
|
|
42
|
+
const sources = new Set();
|
|
43
|
+
const bus = {
|
|
44
|
+
context,
|
|
45
|
+
dry,
|
|
46
|
+
reverb,
|
|
47
|
+
noise: noiseBuffer(context),
|
|
48
|
+
track(source) {
|
|
49
|
+
sources.add(source);
|
|
50
|
+
source.onended = () => sources.delete(source);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const speechGain = speech === null ? null : context.createGain();
|
|
54
|
+
if (speechGain !== null) {
|
|
55
|
+
speechGain.gain.value = 0;
|
|
56
|
+
speechGain.connect(masterBus.speech);
|
|
57
|
+
}
|
|
58
|
+
const duck = masterBus.duck.gain;
|
|
59
|
+
return {
|
|
60
|
+
bus,
|
|
61
|
+
origin,
|
|
62
|
+
rate,
|
|
63
|
+
contextTime,
|
|
64
|
+
speak(from) {
|
|
65
|
+
const now = context.currentTime;
|
|
66
|
+
if (speech === null || speechGain === null) {
|
|
67
|
+
duck.cancelScheduledValues(now);
|
|
68
|
+
duck.setValueAtTime(1, now);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const start = Math.max(now, contextTime(from));
|
|
72
|
+
speechGain.gain.setValueAtTime(0, start);
|
|
73
|
+
speechGain.gain.linearRampToValueAtTime(speech.gain, start + SPEECH_FADE_SECONDS);
|
|
74
|
+
if (endTime - SPEECH_TAIL_SECONDS > start + SPEECH_FADE_SECONDS) {
|
|
75
|
+
speechGain.gain.setValueAtTime(speech.gain, endTime - SPEECH_TAIL_SECONDS);
|
|
76
|
+
speechGain.gain.linearRampToValueAtTime(0, endTime);
|
|
77
|
+
}
|
|
78
|
+
for (const clip of speech.clips) {
|
|
79
|
+
// At a rate other than 1 a take plays faster or slower with the picture, its pitch moving with it.
|
|
80
|
+
const offset = Math.max(0, from - clip.at);
|
|
81
|
+
if (offset >= clip.buffer.duration)
|
|
82
|
+
continue;
|
|
83
|
+
const source = context.createBufferSource();
|
|
84
|
+
source.buffer = clip.buffer;
|
|
85
|
+
source.playbackRate.value = rate;
|
|
86
|
+
source.connect(speechGain);
|
|
87
|
+
bus.track(source);
|
|
88
|
+
source.start(Math.max(now, contextTime(clip.at + offset)), offset);
|
|
89
|
+
}
|
|
90
|
+
const level = sampleEnvelope(speech.duck, from);
|
|
91
|
+
duck.cancelScheduledValues(now);
|
|
92
|
+
duck.setValueAtTime(level, now);
|
|
93
|
+
duck.setValueAtTime(level, start);
|
|
94
|
+
for (const point of speech.duck)
|
|
95
|
+
if (point.t > from)
|
|
96
|
+
duck.linearRampToValueAtTime(point.v, contextTime(point.t));
|
|
97
|
+
},
|
|
98
|
+
stop() {
|
|
99
|
+
const now = context.currentTime;
|
|
100
|
+
try {
|
|
101
|
+
if (speechGain !== null && speech !== null) {
|
|
102
|
+
speechGain.gain.cancelScheduledValues(now);
|
|
103
|
+
speechGain.gain.setValueAtTime(speech.gain * Math.max(0, Math.min(1, (endTime - now) / SPEECH_TAIL_SECONDS)), now);
|
|
104
|
+
speechGain.gain.linearRampToValueAtTime(0, now + STOP_FADE_SECONDS);
|
|
105
|
+
duck.cancelScheduledValues(now);
|
|
106
|
+
duck.setValueAtTime(sampleEnvelope(speech.duck, playTimeAt(now, origin, rate)), now);
|
|
107
|
+
duck.linearRampToValueAtTime(1, now + DUCK_RECOVERY_SECONDS);
|
|
108
|
+
}
|
|
109
|
+
dry.gain.cancelScheduledValues(now);
|
|
110
|
+
dry.gain.setValueAtTime(dry.gain.value, now);
|
|
111
|
+
dry.gain.linearRampToValueAtTime(0, now + STOP_FADE_SECONDS);
|
|
112
|
+
reverb.gain.cancelScheduledValues(now);
|
|
113
|
+
reverb.gain.setValueAtTime(0, now);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// A closed context refuses automation; its sources are stopped below all the same.
|
|
117
|
+
}
|
|
118
|
+
for (const source of sources) {
|
|
119
|
+
try {
|
|
120
|
+
source.stop(now + 0.05);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// A source that never started cannot be stopped.
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
dry.disconnect();
|
|
128
|
+
reverb.disconnect();
|
|
129
|
+
speechGain?.disconnect();
|
|
130
|
+
}, DISCONNECT_DELAY_MS);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DrumName } from '@open-artifact/motion-core';
|
|
2
|
+
import type { VoiceBus, VoiceOutput } from './nodes.ts';
|
|
3
|
+
/** A drum hit: filtered noise and pitched sines, each on its own decay. Drums have no pitch or length of their own. */
|
|
4
|
+
export declare function synthesizeDrum(bus: VoiceBus, name: DrumName, output: VoiceOutput): void;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { decayEnvelope } from "./envelope.js";
|
|
2
|
+
import { filter, noise, oscillator } from "./nodes.js";
|
|
3
|
+
/** A drum hit: filtered noise and pitched sines, each on its own decay. Drums have no pitch or length of their own. */
|
|
4
|
+
export function synthesizeDrum(bus, name, output) {
|
|
5
|
+
const { context } = bus;
|
|
6
|
+
const { at, node: out } = output;
|
|
7
|
+
switch (name) {
|
|
8
|
+
case 'kick': {
|
|
9
|
+
const body = decayEnvelope(context, out, at, { peak: 1.1, attack: 0.002, decay: 0.09, stop: at + 0.5 });
|
|
10
|
+
const sweep = oscillator(bus, 'sine', 50, body, at, at + 0.5);
|
|
11
|
+
sweep.frequency.setValueAtTime(160, at);
|
|
12
|
+
sweep.frequency.exponentialRampToValueAtTime(46, at + 0.1);
|
|
13
|
+
const beater = decayEnvelope(context, out, at, { peak: 0.25, attack: 0.0005, decay: 0.004, stop: at + 0.03 });
|
|
14
|
+
noise(bus, filter(context, 'lowpass', 2500, 0.5, beater), at, at + 0.03, 0.1);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
case 'snare': {
|
|
18
|
+
const wires = decayEnvelope(context, out, at, { peak: 0.8, attack: 0.001, decay: 0.06, stop: at + 0.3 });
|
|
19
|
+
noise(bus, filter(context, 'bandpass', 1900, 0.8, wires), at, at + 0.3, 0.3);
|
|
20
|
+
const shell = decayEnvelope(context, out, at, { peak: 0.5, attack: 0.001, decay: 0.04, stop: at + 0.2 });
|
|
21
|
+
oscillator(bus, 'triangle', 185, shell, at, at + 0.2).frequency.setTargetAtTime(150, at, 0.03);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
case 'clap': {
|
|
25
|
+
for (let hand = 0; hand < 3; hand++) {
|
|
26
|
+
const start = at + hand * 0.011;
|
|
27
|
+
const slap = decayEnvelope(context, out, start, { peak: 0.6, attack: 0.0005, decay: 0.008, stop: at + 0.06 });
|
|
28
|
+
noise(bus, filter(context, 'bandpass', 1300, 1.2, slap), start, at + 0.06, 0.2 + hand * 0.1);
|
|
29
|
+
}
|
|
30
|
+
const room = decayEnvelope(context, out, at + 0.033, { peak: 0.55, attack: 0.001, decay: 0.07, stop: at + 0.4 });
|
|
31
|
+
noise(bus, filter(context, 'bandpass', 1500, 0.9, room), at + 0.033, at + 0.4, 0.5);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
case 'hat':
|
|
35
|
+
case 'openhat': {
|
|
36
|
+
const decay = name === 'hat' ? 0.022 : 0.16;
|
|
37
|
+
const body = decayEnvelope(context, out, at, { peak: 0.5, attack: 0.0008, decay, stop: at + decay * 8 });
|
|
38
|
+
noise(bus, filter(context, 'highpass', 7200, 0.6, body), at, at + decay * 8, 0.7);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
case 'shaker': {
|
|
42
|
+
const body = decayEnvelope(context, out, at, { peak: 0.45, attack: 0.012, decay: 0.025, stop: at + 0.2 });
|
|
43
|
+
noise(bus, filter(context, 'bandpass', 6500, 1.5, body), at, at + 0.2, 0.9);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
case 'sidestick': {
|
|
47
|
+
const body = decayEnvelope(context, out, at, { peak: 0.7, attack: 0.0005, decay: 0.012, stop: at + 0.1 });
|
|
48
|
+
oscillator(bus, 'sine', 1750, body, at, at + 0.1);
|
|
49
|
+
oscillator(bus, 'sine', 2600, body, at, at + 0.1);
|
|
50
|
+
const rim = decayEnvelope(context, out, at, { peak: 0.5, attack: 0.0003, decay: 0.006, stop: at + 0.05 });
|
|
51
|
+
noise(bus, filter(context, 'highpass', 2500, 0.5, rim), at, at + 0.05, 0.4);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
case 'woodblock':
|
|
55
|
+
case 'cowbell': {
|
|
56
|
+
const wood = name === 'woodblock';
|
|
57
|
+
const pitch = wood ? 820 : 560;
|
|
58
|
+
const body = decayEnvelope(context, out, at, { peak: 0.9, attack: 0.0005, decay: wood ? 0.018 : 0.06, stop: at + 0.3 });
|
|
59
|
+
const resonance = filter(context, 'bandpass', pitch, 6, body);
|
|
60
|
+
oscillator(bus, wood ? 'sine' : 'square', pitch, resonance, at, at + 0.3);
|
|
61
|
+
oscillator(bus, 'sine', pitch * 1.51, resonance, at, at + 0.3);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
case 'tamb': {
|
|
65
|
+
for (let jingle = 0; jingle < 3; jingle++) {
|
|
66
|
+
const start = at + jingle * 0.018;
|
|
67
|
+
const body = decayEnvelope(context, out, start, { peak: 0.35, attack: 0.001, decay: 0.03, stop: at + 0.25 });
|
|
68
|
+
noise(bus, filter(context, 'bandpass', 8000 + jingle * 900, 2.5, body), start, at + 0.25, 0.3 * jingle);
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
case 'tom': {
|
|
73
|
+
const body = decayEnvelope(context, out, at, { peak: 0.9, attack: 0.002, decay: 0.12, stop: at + 0.5 });
|
|
74
|
+
const sweep = oscillator(bus, 'sine', 130, body, at, at + 0.5);
|
|
75
|
+
sweep.frequency.setValueAtTime(210, at);
|
|
76
|
+
sweep.frequency.exponentialRampToValueAtTime(120, at + 0.15);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
case 'crash':
|
|
80
|
+
case 'ride': {
|
|
81
|
+
const crash = name === 'crash';
|
|
82
|
+
const stop = at + (crash ? 2.5 : 1.2);
|
|
83
|
+
const body = decayEnvelope(context, out, at, { peak: crash ? 0.5 : 0.35, attack: 0.002, decay: crash ? 0.6 : 0.25, stop });
|
|
84
|
+
const shimmer = filter(context, 'peaking', crash ? 5200 : 3100, 4, body);
|
|
85
|
+
noise(bus, filter(context, 'highpass', crash ? 3500 : 6000, 0.4, shimmer), at, stop, 1.1);
|
|
86
|
+
if (!crash) {
|
|
87
|
+
const bell = decayEnvelope(context, out, at, { peak: 0.12, attack: 0.001, decay: 0.2, stop: at + 0.8 });
|
|
88
|
+
oscillator(bus, 'sine', 3120, bell, at, at + 0.8);
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
default: {
|
|
93
|
+
const never = name;
|
|
94
|
+
return never;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** A struck sound: up to `peak` over `attack`, then an exponential fall with time constant `decay`, silent at `stop`. */
|
|
2
|
+
export interface DecayShape {
|
|
3
|
+
readonly peak: number;
|
|
4
|
+
readonly attack: number;
|
|
5
|
+
readonly decay: number;
|
|
6
|
+
readonly stop: number;
|
|
7
|
+
}
|
|
8
|
+
/** A held sound: attack to `peak`, decay towards `peak × sustain`, held for the note's length, then released. */
|
|
9
|
+
export interface AdsrShape {
|
|
10
|
+
readonly attack: number;
|
|
11
|
+
readonly decay: number;
|
|
12
|
+
readonly sustain: number;
|
|
13
|
+
readonly release: number;
|
|
14
|
+
readonly peak: number;
|
|
15
|
+
}
|
|
16
|
+
/** When a held note lets go: after its length, but never before its attack is over. */
|
|
17
|
+
export declare function adsrReleaseAt(at: number, duration: number, attack: number): number;
|
|
18
|
+
/** The level a held note has fallen to `elapsed` seconds after its start (still in its decay phase). */
|
|
19
|
+
export declare function adsrLevelAt(elapsed: number, shape: AdsrShape): number;
|
|
20
|
+
/** When a held note is silent: its release after it lets go. */
|
|
21
|
+
export declare function adsrEnd(at: number, duration: number, shape: AdsrShape): number;
|
|
22
|
+
export declare function decayEnvelope(context: BaseAudioContext, destination: AudioNode, at: number, shape: DecayShape): GainNode;
|
|
23
|
+
export declare function adsrEnvelope(context: BaseAudioContext, destination: AudioNode, at: number, duration: number, shape: AdsrShape): {
|
|
24
|
+
gain: GainNode;
|
|
25
|
+
end: number;
|
|
26
|
+
};
|